fix(codegen): Adds string intern, encoded int, enum and legacy version support (#613)
- [X] Adds Enum migration rule - [X] Adds legacy version (writing full int for version field) - [X] Adds encoded int attribute - [X] Adds intern string attribute - [X] Fixes missing base deserialize/serialize
This commit is contained in:
parent
7084c6c24f
commit
ca5a06e9a2
13 changed files with 182 additions and 43 deletions
|
|
@ -1,12 +0,0 @@
|
|||
{
|
||||
"type": "Server.Items.TestItem",
|
||||
"version": 1,
|
||||
"properties": [
|
||||
{
|
||||
"name": "SomeProperty",
|
||||
"type": "Server.Item",
|
||||
"rule": "SerializableInterfaceMigrationRule",
|
||||
"ruleArguments": ["Server.Items.Item"]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -83,15 +83,13 @@ namespace SerializationGenerator
|
|||
return null;
|
||||
}
|
||||
|
||||
var version = classSymbol.GetAttributes()
|
||||
var serializableAttribute = classSymbol.GetAttributes()
|
||||
.FirstOrDefault(
|
||||
attr => attr.AttributeClass?.Equals(serializableEntityAttribute, SymbolEqualityComparer.Default) ?? false
|
||||
)?.ConstructorArguments.FirstOrDefault().Value?.ToString();
|
||||
);
|
||||
|
||||
if (version == null)
|
||||
{
|
||||
return null; // We don't have the attribute
|
||||
}
|
||||
var version = (int)serializableAttribute?.ConstructorArguments[0].Value!;
|
||||
var encodedVersion = (bool)serializableAttribute?.ConstructorArguments[1].Value!;
|
||||
|
||||
var namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
|
||||
var className = classSymbol.Name;
|
||||
|
|
@ -112,7 +110,7 @@ namespace SerializationGenerator
|
|||
InstanceModifier.Const,
|
||||
"int",
|
||||
"_version",
|
||||
version,
|
||||
version.ToString(),
|
||||
true
|
||||
);
|
||||
source.AppendLine();
|
||||
|
|
@ -200,22 +198,21 @@ namespace SerializationGenerator
|
|||
source.GenerateSerialCtor(context, className, isOverride);
|
||||
source.AppendLine();
|
||||
|
||||
var versionValue = int.Parse(version);
|
||||
List<SerializableMetadata> migrations;
|
||||
|
||||
if (versionValue > 0)
|
||||
if (version > 0)
|
||||
{
|
||||
migrations = SerializableMigration.GetMigrations(
|
||||
migrationPath,
|
||||
classSymbol,
|
||||
versionValue,
|
||||
version,
|
||||
jsonSerializerOptions
|
||||
);
|
||||
|
||||
for (var i = 0; i < migrations.Count; i++)
|
||||
{
|
||||
var migration = migrations[i];
|
||||
if (migration.Version < versionValue)
|
||||
if (migration.Version < version)
|
||||
{
|
||||
source.GenerateMigrationContentStruct(migration);
|
||||
source.AppendLine();
|
||||
|
|
@ -231,6 +228,7 @@ namespace SerializationGenerator
|
|||
source.GenerateSerializeMethod(
|
||||
compilation,
|
||||
isOverride,
|
||||
encodedVersion,
|
||||
serializableProperties
|
||||
);
|
||||
source.AppendLine();
|
||||
|
|
@ -239,7 +237,8 @@ namespace SerializationGenerator
|
|||
source.GenerateDeserializeMethod(
|
||||
compilation,
|
||||
isOverride,
|
||||
versionValue,
|
||||
version,
|
||||
encodedVersion,
|
||||
migrations,
|
||||
serializableProperties
|
||||
);
|
||||
|
|
@ -250,7 +249,7 @@ namespace SerializationGenerator
|
|||
// Write the migration file
|
||||
var newMigration = new SerializableMetadata
|
||||
{
|
||||
Version = versionValue,
|
||||
Version = version,
|
||||
Type = classSymbol.ToDisplayString(),
|
||||
Properties = serializableProperties
|
||||
};
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ namespace SerializationGenerator
|
|||
Compilation compilation,
|
||||
bool isOverride,
|
||||
int version,
|
||||
bool encodedVersion,
|
||||
List<SerializableMetadata> migrations,
|
||||
List<SerializableProperty> properties
|
||||
)
|
||||
|
|
@ -43,8 +44,13 @@ namespace SerializationGenerator
|
|||
|
||||
const string indent = " ";
|
||||
|
||||
if (isOverride)
|
||||
{
|
||||
source.AppendLine($"{indent}base.Deserialize(reader);");
|
||||
}
|
||||
|
||||
// Version
|
||||
source.AppendLine($"{indent}var version = reader.ReadEncodedInt();");
|
||||
source.AppendLine($"{indent}var version = reader.{(encodedVersion ? "ReadEncodedInt" : "ReadInt")}();");
|
||||
|
||||
if (version > 0)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -33,6 +33,8 @@ namespace SerializationGenerator
|
|||
public const string GENERIC_WRITER_INTERFACE = "Server.IGenericWriter";
|
||||
public const string GENERIC_READER_INTERFACE = "Server.IGenericReader";
|
||||
public const string DELTA_DATE_TIME_ATTRIBUTE = "Server.DeltaDateTimeAttribute";
|
||||
public const string INTERN_STRING_ATTRIBUTE = "Server.InternStringAttribute";
|
||||
public const string ENCODED_INT_ATTRIBUTE = "Server.EncodedIntAttribute";
|
||||
public const string POINT2D_STRUCT = "Server.Point2D";
|
||||
public const string POINT3D_STRUCT = "Server.Point3D";
|
||||
public const string RECTANGLE2D_STRUCT = "Server.Rectangle2D";
|
||||
|
|
@ -40,9 +42,15 @@ namespace SerializationGenerator
|
|||
public const string RACE_CLASS = "Server.Race";
|
||||
public const string MAP_CLASS = "Server.Map";
|
||||
|
||||
public static bool IsEncodedInt(this AttributeData attr, Compilation compilation) =>
|
||||
attr?.IsAttribute(compilation.GetTypeByMetadataName(ENCODED_INT_ATTRIBUTE)) == true;
|
||||
|
||||
public static bool IsDeltaDateTime(this AttributeData attr, Compilation compilation) =>
|
||||
attr?.IsAttribute(compilation.GetTypeByMetadataName(DELTA_DATE_TIME_ATTRIBUTE)) == true;
|
||||
|
||||
public static bool IsInternString(this AttributeData attr, Compilation compilation) =>
|
||||
attr?.IsAttribute(compilation.GetTypeByMetadataName(INTERN_STRING_ATTRIBUTE)) == true;
|
||||
|
||||
public static bool IsAttribute(this AttributeData attr, ISymbol symbol) =>
|
||||
attr?.AttributeClass?.Equals(symbol, SymbolEqualityComparer.Default) == true;
|
||||
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ namespace SerializationGenerator
|
|||
this StringBuilder source,
|
||||
Compilation compilation,
|
||||
bool isOverride,
|
||||
bool encodedVersion,
|
||||
List<SerializableProperty> properties
|
||||
)
|
||||
{
|
||||
|
|
@ -48,9 +49,15 @@ namespace SerializationGenerator
|
|||
{indent} return;
|
||||
{indent}}}");
|
||||
|
||||
if (isOverride)
|
||||
{
|
||||
source.AppendLine();
|
||||
source.AppendLine($"{indent}base.Serialize(writer);");
|
||||
}
|
||||
|
||||
// Version
|
||||
source.AppendLine();
|
||||
source.AppendLine($"{indent}writer.WriteEncodedInt(_version);");
|
||||
source.AppendLine($"{indent}writer.{(encodedVersion ? "WriteEncodedInt" : "Write")}(_version);");
|
||||
|
||||
foreach (var property in properties)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,69 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: EnumMigrationRule.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;
|
||||
using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class EnumMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
public string RuleName => nameof(EnumMigrationRule);
|
||||
|
||||
public bool GenerateRuleState(
|
||||
Compilation compilation,
|
||||
ISymbol symbol,
|
||||
ImmutableArray<AttributeData> attributes,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes,
|
||||
out string[] ruleArguments
|
||||
)
|
||||
{
|
||||
if (symbol is not ITypeSymbol typeSymbol || !typeSymbol.IsEnum())
|
||||
{
|
||||
ruleArguments = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
ruleArguments = Array.Empty<string>();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(EnumMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
source.AppendLine($"{indent}{property.Name} = reader.ReadEnum<{property.Type}>();");
|
||||
}
|
||||
|
||||
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(EnumMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
source.AppendLine($"{indent}{property.Name} = reader.WriteEnum<{property.Type}>({property.Name});");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -63,14 +63,16 @@ namespace SerializationGenerator
|
|||
return false;
|
||||
}
|
||||
|
||||
if (typeSymbol.SpecialType == SpecialType.System_DateTime && attributes.Any(a => a.IsDeltaDateTime(compilation)))
|
||||
ruleArguments = typeSymbol.SpecialType switch
|
||||
{
|
||||
ruleArguments = new[] { "DeltaTime" };
|
||||
}
|
||||
else
|
||||
{
|
||||
ruleArguments = Array.Empty<string>();
|
||||
}
|
||||
SpecialType.System_Int32 when attributes.Any(a => a.IsEncodedInt(compilation)) =>
|
||||
new[] { "EncodedInt" },
|
||||
SpecialType.System_DateTime when attributes.Any(a => a.IsDeltaDateTime(compilation)) =>
|
||||
new[] { "DeltaTime" },
|
||||
SpecialType.System_String when attributes.Any(a => a.IsInternString(compilation)) =>
|
||||
new[] { "InternString" },
|
||||
_ => Array.Empty<string>()
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
@ -85,11 +87,10 @@ namespace SerializationGenerator
|
|||
}
|
||||
|
||||
var propertyName = property.Name;
|
||||
string readMethod;
|
||||
|
||||
const string ipAddress = SerializableEntityGeneration.IPADDRESS_CLASS;
|
||||
|
||||
readMethod = property.Type switch
|
||||
var readMethod = property.Type switch
|
||||
{
|
||||
"bool" => "ReadBool",
|
||||
"sbyte" => "ReadSByte",
|
||||
|
|
@ -100,7 +101,7 @@ namespace SerializationGenerator
|
|||
"ushort" => "ReadUShort",
|
||||
"uint" => "ReadUInt",
|
||||
"ulong" => "ReadULong",
|
||||
"float" => "ReadFloat",
|
||||
"float" => "ReadFloat",
|
||||
"double" => "ReadDouble",
|
||||
"string" => "ReadString",
|
||||
"decimal" => "ReadDecimal",
|
||||
|
|
@ -111,7 +112,11 @@ namespace SerializationGenerator
|
|||
"ReadDateTime"
|
||||
};
|
||||
|
||||
source.AppendLine($"{indent}{propertyName} = reader.{readMethod}();");
|
||||
var readArgument = readMethod == "ReadString" &&
|
||||
property.RuleArguments.Length >= 1 &&
|
||||
property.RuleArguments[0] == "InternString" ? "true" : "";
|
||||
|
||||
source.AppendLine($"{indent}{propertyName} = reader.{readMethod}({readArgument});");
|
||||
}
|
||||
|
||||
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
|
|
|
|||
|
|
@ -28,14 +28,15 @@ namespace SerializationGenerator
|
|||
{
|
||||
var rules = new ISerializableMigrationRule[]
|
||||
{
|
||||
new EnumMigrationRule(),
|
||||
new ListMigrationRule(),
|
||||
new ArrayMigrationRule(),
|
||||
new HashSetMigrationRule(),
|
||||
new KeyValuePairMigrationRule(),
|
||||
new ListMigrationRule(),
|
||||
new PrimitiveTypeMigrationRule(),
|
||||
new PrimitiveUOTypeMigrationRule(),
|
||||
new SerializableInterfaceMigrationRule(),
|
||||
new SerializationMethodSignatureMigrationRule()
|
||||
new SerializationMethodSignatureMigrationRule(),
|
||||
};
|
||||
|
||||
foreach (var rule in rules)
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ namespace SerializationGenerator
|
|||
return;
|
||||
}
|
||||
|
||||
if (!ClassAndFields.ContainsKey(classSymbol))
|
||||
if (classSymbol.GetAttributes().Any(ad => AttributeTypes.Contains(ad.AttributeClass?.ToDisplayString()) && !ClassAndFields.ContainsKey(classSymbol)))
|
||||
{
|
||||
ClassAndFields.Add(classSymbol, new List<IFieldSymbol>());
|
||||
}
|
||||
|
|
|
|||
27
Projects/Server/Serialization/EncodedIntAttribute.cs
Normal file
27
Projects/Server/Serialization/EncodedIntAttribute.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: EncodedIntAttribute.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;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
/// <summary>
|
||||
/// Hints to the source generator that a serializable int field or property should be encoded
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
||||
public class EncodedIntAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -116,7 +116,7 @@ namespace Server
|
|||
void Write(Map value) => Write((byte)(value?.MapIndex ?? 0xFF));
|
||||
void Write(Race value) => Write((byte)(value?.RaceIndex ?? 0xFF));
|
||||
void Write(ReadOnlySpan<byte> bytes);
|
||||
unsafe void Write<T>(T value) where T : unmanaged, Enum
|
||||
unsafe void WriteEnum<T>(T value) where T : unmanaged, Enum
|
||||
{
|
||||
var size = sizeof(T);
|
||||
|
||||
|
|
@ -149,6 +149,7 @@ namespace Server
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
long Seek(long offset, SeekOrigin origin);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
27
Projects/Server/Serialization/InternStringAttribute.cs
Normal file
27
Projects/Server/Serialization/InternStringAttribute.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: InternalizeString.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;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
/// <summary>
|
||||
/// Hints to the source generator that a serializable string field or property should be internalized on deserialization
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
||||
public class InternStringAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -21,7 +21,8 @@ namespace Server
|
|||
public sealed class SerializableAttribute : Attribute
|
||||
{
|
||||
public int Version { get; }
|
||||
public bool EncodedVersion { get; }
|
||||
|
||||
public SerializableAttribute(int version) => Version = version;
|
||||
public SerializableAttribute(int version, bool encodedVersion = true) => Version = version;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue