fix(codegen): Fixes various code gen issues. Adds better embedded serialization support (#688)
* Adds save flag support (see `ElvenGlasses` for an example) * Updates AOSAttributes so they are code genned * Fixes embedded object support by adding an `IRawSerializable` * Fixes various inconsistencies in serializing with codegen
This commit is contained in:
parent
69af652a18
commit
2c8097f707
54 changed files with 787 additions and 507 deletions
|
|
@ -13,6 +13,7 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.IO;
|
||||
|
|
@ -113,6 +114,8 @@ namespace SerializationGenerator
|
|||
compilation.GetTypeByMetadataName(SymbolMetadata.SERIALIZABLE_INTERFACE);
|
||||
var parentSerializableAttribute =
|
||||
compilation.GetTypeByMetadataName(SymbolMetadata.SERIALIZABLE_PARENT_ATTRIBUTE);
|
||||
var serializableFieldSaveFlagAttribute =
|
||||
compilation.GetTypeByMetadataName(SymbolMetadata.SERIALIZABLE_FIELD_SAVE_FLAG_ATTRIBUTE);
|
||||
|
||||
// If we have a parent that is or derives from ISerializable, then we are in override
|
||||
var isOverride = classSymbol.BaseType.ContainsInterface(serializableInterface);
|
||||
|
|
@ -122,9 +125,28 @@ namespace SerializationGenerator
|
|||
return null;
|
||||
}
|
||||
|
||||
var isRawSerializable = classSymbol.HasRawSerializableInterface(compilation, ImmutableArray<INamedTypeSymbol>.Empty);
|
||||
|
||||
var version = (int)serializableAttr.ConstructorArguments[0].Value!;
|
||||
var encodedVersion = (bool)serializableAttr.ConstructorArguments[1].Value!;
|
||||
|
||||
// Let's find out if we need to do serialization flags
|
||||
var serializablePropertyFlagGettersSet = new SortedSet<(IMethodSymbol, int)>(new SerializableFieldFlagComparer());
|
||||
foreach (var m in classSymbol.GetMembers().OfType<IMethodSymbol>())
|
||||
{
|
||||
var getSaveFlagAttribute = m.GetAttribute(serializableFieldSaveFlagAttribute);
|
||||
if (getSaveFlagAttribute == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var attrCtorArgs = getSaveFlagAttribute.ConstructorArguments;
|
||||
var order = (int)attrCtorArgs[0].Value!;
|
||||
|
||||
serializablePropertyFlagGettersSet.Add((m, order));
|
||||
}
|
||||
var serializablePropertyFlagGetters = serializablePropertyFlagGettersSet.ToImmutableArray();
|
||||
|
||||
var namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
|
||||
var className = classSymbol.Name;
|
||||
|
||||
|
|
@ -133,7 +155,11 @@ namespace SerializationGenerator
|
|||
source.AppendLine("#pragma warning disable\n");
|
||||
source.GenerateNamespaceStart(namespaceName);
|
||||
|
||||
source.GenerateClassStart(className, ImmutableArray<ITypeSymbol>.Empty);
|
||||
var interfaces = !embedded || isRawSerializable
|
||||
? Array.Empty<ITypeSymbol>()
|
||||
: new ITypeSymbol[] { compilation.GetTypeByMetadataName(SymbolMetadata.RAW_SERIALIZABLE_INTERFACE) };
|
||||
|
||||
source.GenerateClassStart(className, " ", interfaces.ToImmutableArray());
|
||||
|
||||
const string indent = " ";
|
||||
|
||||
|
|
@ -224,7 +250,8 @@ namespace SerializationGenerator
|
|||
allAttributes,
|
||||
serializableTypes,
|
||||
embeddedSerializableTypes,
|
||||
classSymbol
|
||||
classSymbol,
|
||||
serializablePropertyFlagGetters.FirstOrDefault(m => m.Item2 == order).Item1
|
||||
);
|
||||
|
||||
serializablePropertySet.Add(serializableProperty);
|
||||
|
|
@ -271,7 +298,7 @@ namespace SerializationGenerator
|
|||
var migration = migrations[i];
|
||||
if (migration.Version < version)
|
||||
{
|
||||
source.GenerateMigrationContentStruct(migration);
|
||||
source.GenerateMigrationContentStruct(migration, classSymbol);
|
||||
source.AppendLine();
|
||||
}
|
||||
}
|
||||
|
|
@ -282,7 +309,8 @@ namespace SerializationGenerator
|
|||
compilation,
|
||||
isOverride,
|
||||
encodedVersion,
|
||||
serializableProperties
|
||||
serializableProperties,
|
||||
serializablePropertyFlagGetters
|
||||
);
|
||||
source.AppendLine();
|
||||
|
||||
|
|
@ -295,10 +323,32 @@ namespace SerializationGenerator
|
|||
encodedVersion,
|
||||
migrations,
|
||||
serializableProperties,
|
||||
parentFieldOrProperty
|
||||
parentFieldOrProperty,
|
||||
serializablePropertyFlagGetters
|
||||
);
|
||||
|
||||
source.GenerateClassEnd();
|
||||
// Serialize SaveFlag enum class
|
||||
if (serializablePropertyFlagGetters.Length > 0)
|
||||
{
|
||||
source.AppendLine();
|
||||
source.GenerateEnumStart(
|
||||
"SaveFlag",
|
||||
" ",
|
||||
true,
|
||||
Accessibility.Private
|
||||
);
|
||||
|
||||
int index = 0;
|
||||
source.GenerateEnumValue(" ", true, "None", index++);
|
||||
foreach (var (_, order) in serializablePropertyFlagGetters)
|
||||
{
|
||||
source.GenerateEnumValue(" ", true, serializableProperties[order].Name, index++);
|
||||
}
|
||||
|
||||
source.GenerateEnumEnd(" ");
|
||||
}
|
||||
|
||||
source.GenerateClassEnd(" ");
|
||||
source.GenerateNamespaceEnd();
|
||||
|
||||
if (migrationPath != null)
|
||||
|
|
|
|||
|
|
@ -32,7 +32,8 @@ namespace SerializationGenerator
|
|||
bool encodedVersion,
|
||||
ImmutableArray<SerializableMetadata> migrations,
|
||||
ImmutableArray<SerializableProperty> properties,
|
||||
ISymbol parentFieldOrProperty
|
||||
ISymbol parentFieldOrProperty,
|
||||
ImmutableArray<(IMethodSymbol, int)> propertyFlagGetters
|
||||
)
|
||||
{
|
||||
var genericReaderInterface = compilation.GetTypeByMetadataName(SymbolMetadata.GENERIC_READER_INTERFACE);
|
||||
|
|
@ -47,6 +48,7 @@ namespace SerializationGenerator
|
|||
);
|
||||
|
||||
const string indent = " ";
|
||||
const string innerIndent = $"{indent} ";
|
||||
|
||||
if (isOverride)
|
||||
{
|
||||
|
|
@ -89,7 +91,7 @@ namespace SerializationGenerator
|
|||
source.AppendLine();
|
||||
source.AppendLine($"{indent}if (version == {migrationVersion})");
|
||||
source.AppendLine($"{indent}{{");
|
||||
source.AppendLine($"{indent} MigrateFrom(new V{migrationVersion}Content(reader));");
|
||||
source.AppendLine($"{indent} MigrateFrom(new V{migrationVersion}Content(reader, this));");
|
||||
source.AppendLine($"{indent} {parent}.MarkDirty();");
|
||||
if (afterDeserialization != null)
|
||||
{
|
||||
|
|
@ -115,17 +117,41 @@ namespace SerializationGenerator
|
|||
}
|
||||
}
|
||||
|
||||
foreach (var property in properties)
|
||||
if (propertyFlagGetters.Length > 0)
|
||||
{
|
||||
source.AppendLine();
|
||||
var rule = SerializableMigrationRulesEngine.Rules[property.Rule];
|
||||
rule.GenerateDeserializationMethod(
|
||||
source,
|
||||
indent,
|
||||
property
|
||||
);
|
||||
source.AppendLine($"{indent}var saveFlags = reader.ReadEnum<SaveFlag>();");
|
||||
}
|
||||
|
||||
(rule as IPostDeserializeMethod)?.PostDeserializeMethod(source, indent, property, compilation, classSymbol);
|
||||
foreach (var property in properties)
|
||||
{
|
||||
var usesSaveFlag = propertyFlagGetters.Any(m => m.Item2 == property.Order);
|
||||
var rule = SerializableMigrationRulesEngine.Rules[property.Rule];
|
||||
|
||||
if (usesSaveFlag)
|
||||
{
|
||||
source.AppendLine($"\n{indent}if ((saveFlags & SaveFlag.{property.Name}) != 0)\n{indent}{{");
|
||||
rule.GenerateDeserializationMethod(
|
||||
source,
|
||||
innerIndent,
|
||||
property,
|
||||
"this"
|
||||
);
|
||||
(rule as IPostDeserializeMethod)?.PostDeserializeMethod(source, innerIndent, property, compilation, classSymbol);
|
||||
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
else
|
||||
{
|
||||
source.AppendLine();
|
||||
rule.GenerateDeserializationMethod(
|
||||
source,
|
||||
indent,
|
||||
property,
|
||||
"this"
|
||||
);
|
||||
(rule as IPostDeserializeMethod)?.PostDeserializeMethod(source, indent, property, compilation, classSymbol);
|
||||
}
|
||||
}
|
||||
|
||||
if (afterDeserialization != null)
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
|
@ -28,7 +29,7 @@ namespace SerializationGenerator
|
|||
Accessibility getter,
|
||||
Accessibility? setter,
|
||||
bool isVirtual,
|
||||
ISymbol? parentFieldOrProperty = null
|
||||
ISymbol? parentFieldOrProperty
|
||||
)
|
||||
{
|
||||
var fieldName = fieldSymbol.Name;
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@
|
|||
*************************************************************************/
|
||||
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SerializableMigration;
|
||||
|
|
@ -27,7 +28,8 @@ namespace SerializationGenerator
|
|||
Compilation compilation,
|
||||
bool isOverride,
|
||||
bool encodedVersion,
|
||||
ImmutableArray<SerializableProperty> properties
|
||||
ImmutableArray<SerializableProperty> properties,
|
||||
ImmutableArray<(IMethodSymbol, int)> propertyFlagGetters
|
||||
)
|
||||
{
|
||||
var genericWriterInterface = compilation.GetTypeByMetadataName(SymbolMetadata.GENERIC_WRITER_INTERFACE);
|
||||
|
|
@ -42,6 +44,7 @@ namespace SerializationGenerator
|
|||
);
|
||||
|
||||
const string indent = " ";
|
||||
const string innerIndent = $"{indent} ";
|
||||
|
||||
if (isOverride)
|
||||
{
|
||||
|
|
@ -52,14 +55,47 @@ namespace SerializationGenerator
|
|||
// Version
|
||||
source.AppendLine($"{indent}writer.{(encodedVersion ? "WriteEncodedInt" : "Write")}(_version);");
|
||||
|
||||
// Let's collect the flags
|
||||
if (propertyFlagGetters.Length > 0)
|
||||
{
|
||||
source.AppendLine($"\n{indent}var saveFlags = SaveFlag.None;");
|
||||
|
||||
foreach (var (m, order) in propertyFlagGetters)
|
||||
{
|
||||
source.AppendLine($"{indent}if ({m.Name}())\n{indent}{{");
|
||||
|
||||
var propertyName = properties[order].Name;
|
||||
source.AppendLine($"{innerIndent}saveFlags |= SaveFlag.{propertyName};");
|
||||
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
|
||||
source.AppendLine($"{indent}writer.WriteEnum(saveFlags);");
|
||||
}
|
||||
|
||||
foreach (var property in properties)
|
||||
{
|
||||
source.AppendLine();
|
||||
SerializableMigrationRulesEngine.Rules[property.Rule].GenerateSerializationMethod(
|
||||
source,
|
||||
indent,
|
||||
property
|
||||
);
|
||||
var usesSaveFlag = propertyFlagGetters.Any(m => m.Item2 == property.Order);
|
||||
|
||||
if (usesSaveFlag)
|
||||
{
|
||||
source.AppendLine($"\n{indent}if ((saveFlags & SaveFlag.{property.Name}) != 0)\n{indent}{{");
|
||||
SerializableMigrationRulesEngine.Rules[property.Rule].GenerateSerializationMethod(
|
||||
source,
|
||||
innerIndent,
|
||||
property
|
||||
);
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
else
|
||||
{
|
||||
source.AppendLine();
|
||||
SerializableMigrationRulesEngine.Rules[property.Rule].GenerateSerializationMethod(
|
||||
source,
|
||||
indent,
|
||||
property
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
source.GenerateMethodEnd(" ");
|
||||
|
|
|
|||
|
|
@ -13,7 +13,9 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SerializableMigration;
|
||||
|
||||
namespace SerializationGenerator
|
||||
|
|
@ -22,7 +24,8 @@ namespace SerializationGenerator
|
|||
{
|
||||
public static void GenerateMigrationContentStruct(
|
||||
this StringBuilder source,
|
||||
SerializableMetadata migration
|
||||
SerializableMetadata migration,
|
||||
INamedTypeSymbol classSymbol
|
||||
)
|
||||
{
|
||||
const string indent = " ";
|
||||
|
|
@ -34,16 +37,69 @@ namespace SerializationGenerator
|
|||
source.AppendLine($"{indent} internal readonly {serializableProperty.Type} {serializableProperty.Name};");
|
||||
}
|
||||
|
||||
source.AppendLine($"{indent} internal V{migration.Version}Content(IGenericReader reader)");
|
||||
var innerIndent = $"{indent} ";
|
||||
|
||||
var usesSaveFlags = migration.Properties.Any(p => p.UsesSaveFlag == true);
|
||||
|
||||
if (usesSaveFlags)
|
||||
{
|
||||
source.AppendLine();
|
||||
source.GenerateEnumStart(
|
||||
$"V{migration.Version}SaveFlag",
|
||||
$"{indent} ",
|
||||
true,
|
||||
Accessibility.Private
|
||||
);
|
||||
|
||||
int index = 0;
|
||||
source.GenerateEnumValue(innerIndent, true, "None", index++);
|
||||
foreach (var property in migration.Properties)
|
||||
{
|
||||
if (property.UsesSaveFlag == true)
|
||||
{
|
||||
source.GenerateEnumValue(innerIndent, true, property.Name, index++);
|
||||
}
|
||||
}
|
||||
|
||||
source.GenerateEnumEnd($"{indent} ");
|
||||
}
|
||||
|
||||
source.AppendLine($"{indent} internal V{migration.Version}Content(IGenericReader reader, {classSymbol.ToDisplayString()} entity)");
|
||||
source.AppendLine($"{indent} {{");
|
||||
|
||||
foreach (var serializableProperty in migration.Properties)
|
||||
if (usesSaveFlags)
|
||||
{
|
||||
SerializableMigrationRulesEngine.Rules[serializableProperty.Rule].GenerateDeserializationMethod(
|
||||
source,
|
||||
$"{indent} ",
|
||||
serializableProperty
|
||||
);
|
||||
source.AppendLine($"{innerIndent}var saveFlags = reader.ReadEnum<V{migration.Version}SaveFlag>();");
|
||||
}
|
||||
|
||||
if (migration.Properties.Length > 0)
|
||||
{
|
||||
source.AppendLine();
|
||||
foreach (var property in migration.Properties)
|
||||
{
|
||||
if (property.UsesSaveFlag == true)
|
||||
{
|
||||
source.AppendLine($"\n{innerIndent}if ((saveFlags & V{migration.Version}SaveFlag.{property.Name}) != 0)\n{innerIndent}{{");
|
||||
SerializableMigrationRulesEngine.Rules[property.Rule].GenerateDeserializationMethod(
|
||||
source,
|
||||
$"{innerIndent} ",
|
||||
property,
|
||||
"entity"
|
||||
);
|
||||
source.AppendLine($"{innerIndent}}}\n{innerIndent}else\n{innerIndent}{{");
|
||||
source.AppendLine($"{innerIndent} {property.Name} = default;");
|
||||
source.AppendLine($"{innerIndent}}}");
|
||||
}
|
||||
else
|
||||
{
|
||||
SerializableMigrationRulesEngine.Rules[property.Rule].GenerateDeserializationMethod(
|
||||
source,
|
||||
innerIndent,
|
||||
property,
|
||||
"entity"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
source.AppendLine($"{indent} }}");
|
||||
|
|
|
|||
|
|
@ -36,7 +36,8 @@ namespace SerializableMigration
|
|||
void GenerateDeserializationMethod(
|
||||
StringBuilder source,
|
||||
string indent,
|
||||
SerializableProperty property
|
||||
SerializableProperty property,
|
||||
string? parentReference
|
||||
);
|
||||
|
||||
void GenerateSerializationMethod(
|
||||
|
|
|
|||
|
|
@ -48,7 +48,8 @@ namespace SerializableMigration
|
|||
attributes,
|
||||
serializableTypes,
|
||||
embeddedSerializableTypes,
|
||||
parentSymbol
|
||||
parentSymbol,
|
||||
null
|
||||
);
|
||||
|
||||
var length = serializableArrayType.RuleArguments.Length;
|
||||
|
|
@ -60,7 +61,7 @@ namespace SerializableMigration
|
|||
return true;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property, string? parentReference)
|
||||
{
|
||||
var expectedRule = RuleName;
|
||||
var ruleName = property.Rule;
|
||||
|
|
@ -74,7 +75,7 @@ namespace SerializableMigration
|
|||
Array.Copy(ruleArguments, 2, arrayElementRuleArguments, 0, ruleArguments.Length - 2);
|
||||
|
||||
var propertyIndex = $"{property.Name}Index";
|
||||
source.AppendLine($"{indent}{property.Name} = new {ruleArguments[0]}[reader.ReadInt()];");
|
||||
source.AppendLine($"{indent}{property.Name} = new {ruleArguments[0]}[reader.ReadEncodedInt()];");
|
||||
source.AppendLine($"{indent}for (var {propertyIndex} = 0; {propertyIndex} < {property.Name}.Length; {propertyIndex}++)");
|
||||
source.AppendLine($"{indent}{{");
|
||||
|
||||
|
|
@ -86,7 +87,7 @@ namespace SerializableMigration
|
|||
RuleArguments = arrayElementRuleArguments
|
||||
};
|
||||
|
||||
arrayElementRule.GenerateDeserializationMethod(source, $"{indent} ", serializableArrayElement);
|
||||
arrayElementRule.GenerateDeserializationMethod(source, $"{indent} ", serializableArrayElement, parentReference);
|
||||
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
|
|
@ -110,7 +111,7 @@ namespace SerializableMigration
|
|||
var propertyIndex = $"{propertyVarPrefix}Index";
|
||||
var propertyLength = $"{propertyVarPrefix}Length";
|
||||
source.AppendLine($"{indent}var {propertyLength} = {property.Name}?.Length ?? 0;");
|
||||
source.AppendLine($"{indent}writer.Write({propertyLength});");
|
||||
source.AppendLine($"{indent}writer.WriteEncodedInt({propertyLength});");
|
||||
source.AppendLine($"{indent}for (var {propertyIndex} = 0; {propertyIndex} < {propertyLength}; {propertyIndex}++)");
|
||||
source.AppendLine($"{indent}{{");
|
||||
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ namespace SerializableMigration
|
|||
return true;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property, string? parentReference)
|
||||
{
|
||||
var expectedRule = RuleName;
|
||||
var ruleName = property.Rule;
|
||||
|
|
|
|||
|
|
@ -52,7 +52,8 @@ namespace SerializableMigration
|
|||
attributes,
|
||||
serializableTypes,
|
||||
embeddedSerializableTypes,
|
||||
parentSymbol
|
||||
parentSymbol,
|
||||
null
|
||||
);
|
||||
|
||||
var extraOptions = "";
|
||||
|
|
@ -71,7 +72,7 @@ namespace SerializableMigration
|
|||
return true;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property, string? parentReference)
|
||||
{
|
||||
var expectedRule = RuleName;
|
||||
var ruleName = property.Rule;
|
||||
|
|
@ -95,7 +96,7 @@ namespace SerializableMigration
|
|||
var propertyCount = $"{propertyVarPrefix}Count";
|
||||
|
||||
source.AppendLine($"{indent}{ruleArguments[argumentsOffset]} {propertyEntry};");
|
||||
source.AppendLine($"{indent}var {propertyCount} = reader.ReadInt();");
|
||||
source.AppendLine($"{indent}var {propertyCount} = reader.ReadEncodedInt();");
|
||||
source.AppendLine($"{indent}{property.Name} = new System.Collections.Generic.HashSet<{ruleArguments[argumentsOffset]}>({propertyCount});");
|
||||
source.AppendLine($"{indent}for (var {propertyIndex} = 0; i < {propertyCount}; {propertyIndex}++)");
|
||||
source.AppendLine($"{indent}{{");
|
||||
|
|
@ -108,7 +109,7 @@ namespace SerializableMigration
|
|||
RuleArguments = setElementRuleArguments
|
||||
};
|
||||
|
||||
setElementRule.GenerateDeserializationMethod(source, $"{indent} ", serializableSetElement);
|
||||
setElementRule.GenerateDeserializationMethod(source, $"{indent} ", serializableSetElement, parentReference);
|
||||
source.AppendLine($"{indent} {property.Name}.Add({propertyEntry});");
|
||||
|
||||
source.AppendLine($"{indent}}}");
|
||||
|
|
@ -142,7 +143,7 @@ namespace SerializableMigration
|
|||
source.AppendLine($"{indent}{property.Name}?.Tidy();");
|
||||
}
|
||||
source.AppendLine($"{indent}var {propertyCount} = {property.Name}?.Count ?? 0;");
|
||||
source.AppendLine($"{indent}writer.Write({propertyCount});");
|
||||
source.AppendLine($"{indent}writer.WriteEncodedInt({propertyCount});");
|
||||
source.AppendLine($"{indent}if ({propertyCount} > 0)");
|
||||
source.AppendLine($"{indent}{{");
|
||||
source.AppendLine($"{indent} foreach (var {propertyEntry} in {property.Name}!)");
|
||||
|
|
|
|||
|
|
@ -51,7 +51,8 @@ namespace SerializableMigration
|
|||
attributes,
|
||||
serializableTypes,
|
||||
embeddedSerializableTypes,
|
||||
parentSymbol
|
||||
parentSymbol,
|
||||
null
|
||||
);
|
||||
|
||||
var valueSerializedProperty = SerializableMigrationRulesEngine.GenerateSerializableProperty(
|
||||
|
|
@ -62,7 +63,8 @@ namespace SerializableMigration
|
|||
attributes,
|
||||
serializableTypes,
|
||||
embeddedSerializableTypes,
|
||||
parentSymbol
|
||||
parentSymbol,
|
||||
null
|
||||
);
|
||||
|
||||
// Key
|
||||
|
|
@ -81,7 +83,7 @@ namespace SerializableMigration
|
|||
return true;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property, string? parentReference)
|
||||
{
|
||||
var expectedRule = RuleName;
|
||||
var ruleName = property.Rule;
|
||||
|
|
@ -107,7 +109,8 @@ namespace SerializableMigration
|
|||
keyRule.GenerateDeserializationMethod(
|
||||
source,
|
||||
indent,
|
||||
serializableKeyProperty
|
||||
serializableKeyProperty,
|
||||
parentReference
|
||||
);
|
||||
|
||||
var valueIndex = 3 + keyRuleArguments.Length;
|
||||
|
|
@ -127,7 +130,8 @@ namespace SerializableMigration
|
|||
keyRule.GenerateDeserializationMethod(
|
||||
source,
|
||||
indent,
|
||||
serializableValueProperty
|
||||
serializableValueProperty,
|
||||
parentReference
|
||||
);
|
||||
|
||||
source.AppendLine(
|
||||
|
|
|
|||
|
|
@ -52,7 +52,8 @@ namespace SerializableMigration
|
|||
attributes,
|
||||
serializableTypes,
|
||||
embeddedSerializableTypes,
|
||||
parentSymbol
|
||||
parentSymbol,
|
||||
null
|
||||
);
|
||||
|
||||
var extraOptions = "";
|
||||
|
|
@ -71,7 +72,7 @@ namespace SerializableMigration
|
|||
return true;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property, string? parentReference)
|
||||
{
|
||||
var expectedRule = RuleName;
|
||||
var ruleName = property.Rule;
|
||||
|
|
@ -96,7 +97,7 @@ namespace SerializableMigration
|
|||
var propertyCount = $"{propertyVarPrefix}Count";
|
||||
|
||||
source.AppendLine($"{indent}{ruleArguments[argumentsOffset]} {propertyEntry};");
|
||||
source.AppendLine($"{indent}var {propertyCount} = reader.ReadInt();");
|
||||
source.AppendLine($"{indent}var {propertyCount} = reader.ReadEncodedInt();");
|
||||
source.AppendLine($"{indent}{propertyName} = new System.Collections.Generic.List<{ruleArguments[argumentsOffset]}>({propertyCount});");
|
||||
source.AppendLine($"{indent}for (var {propertyIndex} = 0; {propertyIndex} < {propertyCount}; {propertyIndex}++)");
|
||||
source.AppendLine($"{indent}{{");
|
||||
|
|
@ -109,7 +110,7 @@ namespace SerializableMigration
|
|||
RuleArguments = listElementRuleArguments
|
||||
};
|
||||
|
||||
listElementRule.GenerateDeserializationMethod(source, $"{indent} ", serializableListElement);
|
||||
listElementRule.GenerateDeserializationMethod(source, $"{indent} ", serializableListElement, parentReference);
|
||||
source.AppendLine($"{indent} {propertyName}.Add({propertyEntry});");
|
||||
|
||||
source.AppendLine($"{indent}}}");
|
||||
|
|
@ -143,7 +144,7 @@ namespace SerializableMigration
|
|||
source.AppendLine($"{indent}{property.Name}?.Tidy();");
|
||||
}
|
||||
source.AppendLine($"{indent}var {propertyCount} = {property.Name}?.Count ?? 0;");
|
||||
source.AppendLine($"{indent}writer.Write({propertyCount});");
|
||||
source.AppendLine($"{indent}writer.WriteEncodedInt({propertyCount});");
|
||||
source.AppendLine($"{indent}if ({propertyCount} > 0)");
|
||||
source.AppendLine($"{indent}{{");
|
||||
source.AppendLine($"{indent} foreach (var {propertyEntry} in {property.Name}!)");
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ namespace SerializableMigration
|
|||
return true;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property, string? parentReference)
|
||||
{
|
||||
var expectedRule = RuleName;
|
||||
var ruleName = property.Rule;
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ namespace SerializableMigration
|
|||
return ruleArguments != null;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property, string? parentReference)
|
||||
{
|
||||
var expectedRule = RuleName;
|
||||
var ruleName = property.Rule;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: EmbeddedSerializableMigrationRule.cs *
|
||||
* File: RawSerializableMigrationRule.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
|
|
@ -21,9 +21,9 @@ using SerializationGenerator;
|
|||
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public class EmbeddedSerializableMigrationRule : ISerializableMigrationRule
|
||||
public class RawSerializableMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
public string RuleName => nameof(EmbeddedSerializableMigrationRule);
|
||||
public string RuleName => nameof(RawSerializableMigrationRule);
|
||||
|
||||
public bool GenerateRuleState(
|
||||
Compilation compilation,
|
||||
|
|
@ -35,13 +35,13 @@ namespace SerializableMigration
|
|||
out string[] ruleArguments
|
||||
)
|
||||
{
|
||||
if (symbol is not INamedTypeSymbol namedTypeSymbol)
|
||||
if (symbol is not ITypeSymbol typeSymbol)
|
||||
{
|
||||
ruleArguments = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!embeddedSerializableTypes.Contains(namedTypeSymbol))
|
||||
if (!typeSymbol.HasRawSerializableInterface(compilation, embeddedSerializableTypes))
|
||||
{
|
||||
ruleArguments = null;
|
||||
return false;
|
||||
|
|
@ -51,7 +51,7 @@ namespace SerializableMigration
|
|||
return true;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property, string? parentReference)
|
||||
{
|
||||
var expectedRule = RuleName;
|
||||
var ruleName = property.Rule;
|
||||
|
|
@ -61,7 +61,7 @@ namespace SerializableMigration
|
|||
}
|
||||
|
||||
var propertyName = property.Name;
|
||||
source.AppendLine($"{indent}{propertyName} = new {property.Type}(this);");
|
||||
source.AppendLine($"{indent}{propertyName} = new {property.Type}({parentReference ?? "this"});");
|
||||
source.AppendLine($"{indent}{propertyName}.Deserialize(reader);");
|
||||
}
|
||||
|
||||
|
|
@ -45,7 +45,7 @@ namespace SerializableMigration
|
|||
return false;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property, string? parentReference)
|
||||
{
|
||||
var expectedRule = RuleName;
|
||||
var ruleName = property.Rule;
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ namespace SerializableMigration
|
|||
return true;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property, string? parentReference)
|
||||
{
|
||||
var expectedRule = RuleName;
|
||||
var ruleName = property.Rule;
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ namespace SerializableMigration
|
|||
return true;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property, string? parentReference)
|
||||
{
|
||||
var expectedRule = RuleName;
|
||||
var ruleName = property.Rule;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
using System.Collections.Generic;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public class SerializableFieldFlagComparer : IComparer<(IMethodSymbol, int)>
|
||||
{
|
||||
public int Compare((IMethodSymbol, int) x, (IMethodSymbol, int) y)
|
||||
{
|
||||
var (methodSymbolX, orderX) = x;
|
||||
var (methodSymbolY, orderY) = y;
|
||||
|
||||
if (ReferenceEquals(methodSymbolX, methodSymbolY))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(null, methodSymbolY))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(null, methodSymbolX))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return orderX.CompareTo(orderY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -38,7 +38,7 @@ namespace SerializableMigration
|
|||
new PrimitiveUOTypeMigrationRule(),
|
||||
new SerializableInterfaceMigrationRule(),
|
||||
new SerializationMethodSignatureMigrationRule(),
|
||||
new EmbeddedSerializableMigrationRule(),
|
||||
new RawSerializableMigrationRule(),
|
||||
new TimerMigrationRule()
|
||||
};
|
||||
|
||||
|
|
@ -55,7 +55,8 @@ namespace SerializableMigration
|
|||
ImmutableArray<AttributeData> attributes,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes,
|
||||
ImmutableArray<INamedTypeSymbol> embeddedSerializableTypes,
|
||||
ISymbol? parentSymbol = default
|
||||
ISymbol? parentSymbol,
|
||||
IMethodSymbol? serializablePropertyFlagGetter
|
||||
)
|
||||
{
|
||||
string propertyName;
|
||||
|
|
@ -84,7 +85,8 @@ namespace SerializableMigration
|
|||
attributes,
|
||||
serializableTypes,
|
||||
embeddedSerializableTypes,
|
||||
parentSymbol
|
||||
parentSymbol,
|
||||
serializablePropertyFlagGetter
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -96,7 +98,8 @@ namespace SerializableMigration
|
|||
ImmutableArray<AttributeData> attributes,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes,
|
||||
ImmutableArray<INamedTypeSymbol> embeddedSerializableTypes,
|
||||
ISymbol? parentSymbol = default
|
||||
ISymbol? parentSymbol,
|
||||
IMethodSymbol? serializablePropertyFlagGetter
|
||||
)
|
||||
{
|
||||
foreach (var rule in Rules.Values)
|
||||
|
|
@ -116,6 +119,7 @@ namespace SerializableMigration
|
|||
Name = propertyName,
|
||||
Type = propertyType.ToDisplayString(),
|
||||
Order = order,
|
||||
UsesSaveFlag = serializablePropertyFlagGetter != null ? true : null,
|
||||
Rule = rule.RuleName,
|
||||
RuleArguments = ruleArguments
|
||||
};
|
||||
|
|
|
|||
|
|
@ -25,6 +25,9 @@ namespace SerializableMigration
|
|||
[JsonPropertyName("type")]
|
||||
public string Type { get; init; }
|
||||
|
||||
[JsonPropertyName("usesSaveFlag")]
|
||||
public bool? UsesSaveFlag { get; init; }
|
||||
|
||||
[JsonPropertyName("rule")]
|
||||
public string Rule { get; init; }
|
||||
|
||||
|
|
|
|||
|
|
@ -21,9 +21,16 @@ namespace SerializationGenerator
|
|||
{
|
||||
public static partial class SourceGeneration
|
||||
{
|
||||
public static void GenerateClassStart(this StringBuilder source, string className, ImmutableArray<ITypeSymbol> interfaces)
|
||||
public static void GenerateClassStart(
|
||||
this StringBuilder source,
|
||||
string className,
|
||||
string indent,
|
||||
ImmutableArray<ITypeSymbol> interfaces,
|
||||
Accessibility accessor = Accessibility.Public,
|
||||
bool isPartial = true
|
||||
)
|
||||
{
|
||||
source.Append($" public partial class {className}");
|
||||
source.Append($"{indent}{accessor.ToFriendlyString()} {(isPartial ? "partial " : "")}class {className}");
|
||||
if (!interfaces.IsEmpty)
|
||||
{
|
||||
source.Append(" : ");
|
||||
|
|
@ -37,13 +44,12 @@ namespace SerializationGenerator
|
|||
}
|
||||
}
|
||||
|
||||
source.AppendLine(@"
|
||||
{");
|
||||
source.AppendLine($"\n{indent}{{");
|
||||
}
|
||||
|
||||
public static void GenerateClassEnd(this StringBuilder source)
|
||||
public static void GenerateClassEnd(this StringBuilder source, string indent)
|
||||
{
|
||||
source.AppendLine(" }");
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
|
||||
// TODO: Generalize this to any field using dynamic indentation
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SourceGeneration.Enum.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static partial class SourceGeneration
|
||||
{
|
||||
public static void GenerateEnumStart(
|
||||
this StringBuilder source,
|
||||
string enumName,
|
||||
string indent,
|
||||
bool useFlags,
|
||||
Accessibility accessor = Accessibility.Public
|
||||
)
|
||||
{
|
||||
if (useFlags)
|
||||
{
|
||||
source.AppendLine($"{indent}[System.Flags]");
|
||||
}
|
||||
source.AppendLine($"{indent}{accessor.ToFriendlyString()} enum {enumName}\n{indent}{{");
|
||||
}
|
||||
|
||||
public static void GenerateEnumValue(this StringBuilder source, string indent, bool isFlag, string name, int value)
|
||||
{
|
||||
var valueStr = isFlag ? $"0x{1 << value:X8}" : value.ToString();
|
||||
source.AppendLine($"{indent}{name} = {valueStr},");
|
||||
}
|
||||
|
||||
public static void GenerateEnumEnd(this StringBuilder source, string indent)
|
||||
{
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -44,6 +44,8 @@ namespace SerializationGenerator
|
|||
public const string TIMER_CLASS = "Server.Timer";
|
||||
public const string TIMER_DRIFT_ATTRIBUTE = "Server.TimerDriftAttribute";
|
||||
public const string DESERIALIZE_TIMER_FIELD_ATTRIBUTE = "Server.DeserializeTimerFieldAttribute";
|
||||
public const string SERIALIZABLE_FIELD_SAVE_FLAG_ATTRIBUTE = "Server.SerializableFieldSaveFlagAttribute";
|
||||
public const string RAW_SERIALIZABLE_INTERFACE = "Server.IRawSerializable";
|
||||
|
||||
public static bool IsTimerDrift(this AttributeData attr, Compilation compilation) =>
|
||||
attr?.IsAttribute(compilation.GetTypeByMetadataName(TIMER_DRIFT_ATTRIBUTE)) == true;
|
||||
|
|
@ -77,9 +79,17 @@ namespace SerializationGenerator
|
|||
symbol.ContainsInterface(compilation.GetTypeByMetadataName(SERIALIZABLE_INTERFACE)) ||
|
||||
serializableTypes.Contains(symbol);
|
||||
|
||||
public static bool Contains(this ImmutableArray<INamedTypeSymbol> symbols, ITypeSymbol symbol) =>
|
||||
public static bool HasRawSerializableInterface(
|
||||
this ITypeSymbol symbol,
|
||||
Compilation compilation,
|
||||
ImmutableArray<INamedTypeSymbol> embeddedSerializableTypes
|
||||
) =>
|
||||
symbol.ContainsInterface(compilation.GetTypeByMetadataName(RAW_SERIALIZABLE_INTERFACE)) ||
|
||||
embeddedSerializableTypes.Contains(symbol);
|
||||
|
||||
public static bool Contains(this ImmutableArray<INamedTypeSymbol> symbols, ITypeSymbol? symbol) =>
|
||||
symbol is INamedTypeSymbol namedSymbol &&
|
||||
symbols.Contains(namedSymbol, SymbolEqualityComparer.Default);
|
||||
symbols.Contains(namedSymbol, SymbolEqualityComparer.Default) || symbols.Contains(symbol?.BaseType);
|
||||
|
||||
public static bool HasGenericReaderCtor(
|
||||
this INamedTypeSymbol symbol,
|
||||
|
|
@ -106,11 +116,6 @@ namespace SerializationGenerator
|
|||
ImmutableArray<INamedTypeSymbol> serializableTypes
|
||||
)
|
||||
{
|
||||
if (symbol.HasSerializableInterface(compilation, serializableTypes))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var genericWriterInterface = compilation.GetTypeByMetadataName(GENERIC_WRITER_INTERFACE);
|
||||
|
||||
return symbol.GetAllMethods("Serialize")
|
||||
|
|
@ -129,11 +134,6 @@ namespace SerializationGenerator
|
|||
ImmutableArray<INamedTypeSymbol> serializableTypes
|
||||
)
|
||||
{
|
||||
if (symbol.HasSerializableInterface(compilation, serializableTypes))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var genericReaderInterface = compilation.GetTypeByMetadataName(GENERIC_READER_INTERFACE);
|
||||
|
||||
return symbol.GetAllMethods("Deserialize")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue