feat(codegen): Adds AfterDeserialization support (#615)
Added the ability to execute arbitrary code after deserialization.
Example, let's say you want to delete an item after you deserialize it:
```cs
[AfterDeserialization]
private void OnAfterDeserialization()
{
Delete();
}
```
Generates this:
```cs
public override void Deserialize(IGenericReader reader)
{
base.Deserialize(reader);
var version = reader.ReadEncodedInt();
Timer.DelayCall(OnAfterDeserialization);
}
```
This commit is contained in:
parent
c3f414f04f
commit
b20f3df595
4 changed files with 56 additions and 0 deletions
|
|
@ -236,6 +236,7 @@ namespace SerializationGenerator
|
|||
// Deserialize Method
|
||||
source.GenerateDeserializeMethod(
|
||||
compilation,
|
||||
classSymbol,
|
||||
isOverride,
|
||||
version,
|
||||
encodedVersion,
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
|
|
@ -25,6 +26,7 @@ namespace SerializationGenerator
|
|||
public static void GenerateDeserializeMethod(
|
||||
this StringBuilder source,
|
||||
Compilation compilation,
|
||||
INamedTypeSymbol classSymbol,
|
||||
bool isOverride,
|
||||
int version,
|
||||
bool encodedVersion,
|
||||
|
|
@ -47,6 +49,7 @@ namespace SerializationGenerator
|
|||
if (isOverride)
|
||||
{
|
||||
source.AppendLine($"{indent}base.Deserialize(reader);");
|
||||
source.AppendLine();
|
||||
}
|
||||
|
||||
// Version
|
||||
|
|
@ -95,6 +98,29 @@ namespace SerializationGenerator
|
|||
);
|
||||
}
|
||||
|
||||
var afterDeserialization = classSymbol
|
||||
.GetMembers()
|
||||
.OfType<IMethodSymbol>()
|
||||
.FirstOrDefault(
|
||||
m =>
|
||||
m.ReturnsVoid &&
|
||||
m.Parameters.Length == 0 &&
|
||||
m.GetAttributes()
|
||||
.OfType<AttributeData>()
|
||||
.Any(
|
||||
attr => attr.AttributeClass?.Equals(
|
||||
compilation.GetTypeByMetadataName(AFTERDESERIALIZATION_ATTRIBUTE),
|
||||
SymbolEqualityComparer.Default
|
||||
) ?? false
|
||||
)
|
||||
);
|
||||
|
||||
if (afterDeserialization != null)
|
||||
{
|
||||
source.AppendLine();
|
||||
source.AppendLine($"{indent}Timer.DelayCall({afterDeserialization.Name});");
|
||||
}
|
||||
|
||||
source.GenerateMethodEnd();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ namespace SerializationGenerator
|
|||
public const string IPADDRESS_CLASS = "System.Net.IPAddress";
|
||||
public const string KEYVALUEPAIR_STRUCT = "System.Collections.Generic.KeyValuePair";
|
||||
|
||||
public const string AFTERDESERIALIZATION_ATTRIBUTE = "Server.AfterDeserializationAttribute";
|
||||
public const string SERIALIZABLE_ATTRIBUTE = "Server.SerializableAttribute";
|
||||
public const string SERIALIZABLE_FIELD_ATTRIBUTE = "Server.SerializableFieldAttribute";
|
||||
public const string SERIALIZABLE_FIELD_ATTR_ATTRIBUTE = "Server.SerializableFieldAttrAttribute";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue