feat: Source generated Serialization/Deserialization (#550)

### Features
* Fully abstracts serialization by using compile-time attributes.
* Supports serializing the following:
  - Primitives (integers, strings, etc)
  - IP Addresses
  - BigDecimal
  - DateTime, Delta DateTimes
  - TimeSpan
  - Server.Race
  - Server.Map
  - Point2D, Point3D, Rect2D, Rect3D
  - Existing/New `ISerializable` references
  - Lists/Sets of serializable types
  - Type with a `Serialize` method and constructor that takes an `IGenericReader`
* Supports forward-only migration
* Supports existing RunUO deserialization for older versions by changing to the following signature:
  - `public void OldDeserialize(IGenericReader reader, int version)`
  - Must remove deserializing the version since this is already done
* Supports serializing from private fields or custom made properties.
* Types do not require inheriting Item/Mobile. Code gen will fully create `ISerializable` information.
  - This is not recommended yet, since it requires wiring to `Persistence` which will cause lots of unresolved symbol errors until code gen is built.

### Example
```cs
using System.Collections.Generic;

namespace Server.Items
{
    [Serializable(1)]
    public partial class TestItem1 : Item
    {
        [SerializableField(1)]
        [SerializableFieldAttr("[CommandProperty(AccessLevel.Administrator)]")]
        private List<Item> _someProperty;

        private void Deserialize(IGenericReader reader, int version)
        {
        }
    }
}
```

Generates this:
```cs
namespace Server.Items
{
    public partial class TestItem1
    {
#pragma warning disable 0414
        private const int _version = 1;
#pragma warning restore 0414

        [CommandProperty(AccessLevel.Administrator)]
        public System.Collections.Generic.List<Server.Item> SomeProperty
        {
            get => _someProperty;
            set
            {
                if (value != _someProperty)
                {
                    ((ISerializable)this).MarkDirty();
                    _someProperty = value;
                }
            }
        }

        public TestItem1(Serial serial) : base(serial)
        {
        }

        public override void Serialize(IGenericWriter writer)
        {
            var savePosition = ((Server.ISerializable)this).SavePosition;
            if (savePosition > -1)
            {
                writer.Seek(savePosition, System.IO.SeekOrigin.Begin);
                return;
            }
            writer.WriteEncodedInt(_version);
            writer.Write(_someProperty);
        }

        public override void Deserialize(IGenericReader reader)
        {
            var version = reader.ReadEncodedInt();
            if (version < 1)
            {
                OldDeserialize(reader, version);
                ((Server.ISerializable)this).MarkDirty();
                return;
            }
            SomeProperty = reader.ReadEntityList<Server.Item>();
        }
    }
}
```

And this:
```json
{
  "version": 1,
  "type": "TestItem1",
  "properties": [
    {
      "name": "SomeProperty",
      "type": "System.Collections.Generic.List\u003CServer.Item\u003E",
      "rule": "ListMigrationRule",
      "ruleArguments": [
        "Server.Item",
        "SerializableInterfaceMigrationRule"
      ]
    }
  ]
}
```
This commit is contained in:
Kamron Batman 2021-05-23 21:06:23 -07:00 committed by GitHub
parent cb66bef0e5
commit 9afa4e4cab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
61 changed files with 3203 additions and 142 deletions

View file

@ -0,0 +1,44 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: Helpers.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.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
namespace SerializationGenerator
{
public static class Helpers
{
public static bool ContainsInterface(this ITypeSymbol symbol, ISymbol interfaceSymbol) =>
symbol.Interfaces.Any(i => i.ConstructedFrom.Equals(interfaceSymbol, SymbolEqualityComparer.Default)) ||
symbol.AllInterfaces.Any(i => i.ConstructedFrom.Equals(interfaceSymbol, SymbolEqualityComparer.Default));
public static ImmutableArray<IMethodSymbol> GetAllMethods(this ITypeSymbol symbol, string name)
{
var methods = symbol.GetMembers(name).OfType<IMethodSymbol>().ToImmutableArray();
if (symbol.ContainingSymbol is not ITypeSymbol typeSymbol)
{
return methods;
}
var list = new List<IMethodSymbol>();
list.AddRange(methods.ToList());
list.AddRange(GetAllMethods(typeSymbol, name).ToList());
return list.ToImmutableArray();
}
}
}

View file

@ -0,0 +1,43 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SourceGeneration.AccessModifier.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/>. *
*************************************************************************/
namespace SerializationGenerator
{
public enum AccessModifier
{
None,
Public,
Private,
Protected,
Internal,
ProtectedInternal,
PrivateProtected
}
public static partial class SourceGeneration
{
public static string ToFriendlyString(this AccessModifier modifier) =>
modifier switch
{
AccessModifier.Public => "public",
AccessModifier.Private => "private",
AccessModifier.Protected => "protected",
AccessModifier.Internal => "internal",
AccessModifier.ProtectedInternal => "protected internal",
AccessModifier.PrivateProtected => "private protected",
_ => ""
};
}
}

View file

@ -0,0 +1,125 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SourceGeneration.Arguments.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.Collections.Generic;
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
namespace SerializationGenerator
{
public static partial class SourceGeneration
{
public static void GetTypesFromTypedConstant(TypedConstant arg, List<ITypeSymbol> list)
{
if (arg.Kind == TypedConstantKind.Type)
{
list.Add((ITypeSymbol)arg.Value);
}
else if (arg.Kind == TypedConstantKind.Array)
{
for (var i = 0; i < arg.Values.Length; i++)
{
GetTypesFromTypedConstant(arg.Values[i], list);
}
}
}
public static void GenerateSignatureArguments(this StringBuilder source, ImmutableArray<(ITypeSymbol, string)> parameters)
{
for (var i = 0; i < parameters.Length; i++)
{
var (t, v) = parameters[i];
source.AppendFormat("{0} {1}", t.Name, v);
if (i < parameters.Length - 1)
{
source.Append(", ");
}
}
}
public static void GenerateNamedArgument(this StringBuilder source, KeyValuePair<string, TypedConstant> namedArg)
{
source.AppendFormat("{0} = ", namedArg.Key);
source.GenerateTypedConstant(namedArg.Value);
}
public static void GenerateTypedConstants(this StringBuilder source, ImmutableArray<TypedConstant> args)
{
source.Append("new []{");
for (var i = 0; i < args.Length; i++)
{
source.GenerateTypedConstant(args[i]);
if (i < args.Length - 1)
{
source.Append(", ");
}
}
source.Append('}');
}
public static void GenerateTypedConstant(this StringBuilder source, TypedConstant arg)
{
if (arg.IsNull)
{
source.Append("null");
return;
}
switch (arg.Kind)
{
default:
{
return;
}
case TypedConstantKind.Primitive:
{
if (arg.Value is string str)
{
source.AppendFormat("\"{0}\"", str);
}
else
{
source.Append(arg.Value);
}
break;
}
case TypedConstantKind.Enum:
{
if (arg.Type == null || arg.Value == null)
{
source.Append("null");
}
else
{
source.AppendFormat("({0}){1}", arg.Type.ToDisplayString(), arg.Value);
}
break;
}
case TypedConstantKind.Type:
{
source.AppendFormat("typeof({0})", ((ITypeSymbol)arg.Value)?.Name);
break;
}
case TypedConstantKind.Array:
{
source.GenerateTypedConstants(arg.Values);
break;
}
}
}
}
}

View file

@ -0,0 +1,92 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SourceGeneration.Attribute.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.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
namespace SerializationGenerator
{
public static partial class SourceGeneration
{
public static void GenerateAttribute(this StringBuilder source, string attrClassName, ImmutableArray<TypedConstant> args)
{
source.Append($" [{attrClassName}");
var hasArgs = args.Length > 0;
if (hasArgs)
{
source.Append("(");
}
for (var i = 0; i < args.Length; i++)
{
var arg = args[i];
source.GenerateTypedConstant(arg);
if (i < args.Length - 1)
{
source.Append(", ");
}
}
if (hasArgs)
{
source.Append(")");
}
source.AppendLine("]");
}
public static void GenerateAttribute(this StringBuilder source, AttributeData attr)
{
source.Append($" [{attr.AttributeClass?.Name}");
var ctorArgs = attr.ConstructorArguments;
var namedArgs = attr.NamedArguments;
var hasArgs = ctorArgs.Length + namedArgs.Length > 0;
if (hasArgs)
{
source.Append("(");
}
for (var i = 0; i < ctorArgs.Length; i++)
{
var arg = ctorArgs[i];
source.GenerateTypedConstant(arg);
if (i < ctorArgs.Length - 1)
{
source.Append(", ");
}
}
for (var i = 0; i < namedArgs.Length; i++)
{
var arg = namedArgs[i];
source.GenerateNamedArgument(arg);
if (i < namedArgs.Length - 1)
{
source.Append(", ");
}
}
if (hasArgs)
{
source.Append(")");
}
source.AppendLine("]");
}
}
}

View file

@ -0,0 +1,76 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SourceGeneration.Class.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.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
namespace SerializationGenerator
{
public static partial class SourceGeneration
{
public static void GenerateClassStart(this StringBuilder source, string className, ImmutableArray<ITypeSymbol> interfaces)
{
source.Append($" public partial class {className}");
if (!interfaces.IsEmpty)
{
source.Append(" : ");
for (var i = 0; i < interfaces.Length; i++)
{
source.Append(interfaces[i].ToDisplayString());
if (i < interfaces.Length - 1)
{
source.Append(", ");
}
}
}
source.AppendLine(@"
{");
}
public static void GenerateClassEnd(this StringBuilder source)
{
source.AppendLine(" }");
}
// TODO: Generalize this to any field using dynamic indentation
public static void GenerateClassField(
this StringBuilder source,
AccessModifier accessors,
InstanceModifier instance,
string type,
string variableName,
string value,
bool unusedPragma = false
)
{
if (unusedPragma)
{
source.AppendLine("#pragma warning disable 0414"); // assigned, but never used
}
var instanceStr = instance == InstanceModifier.None ? "" : $"{instance.ToFriendlyString()} ";
var accessorStr = accessors == AccessModifier.None ? "" : $"{accessors.ToFriendlyString()} ";
var valueStr = value == null ? "" : $" = {value}";
source.AppendLine($" {accessorStr}{instanceStr}{type} {variableName}{valueStr};");
if (unusedPragma)
{
source.AppendLine("#pragma warning restore 0414");
}
}
}
}

View file

@ -0,0 +1,39 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SourceGeneration.InstanceModifier.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/>. *
*************************************************************************/
namespace SerializationGenerator
{
public enum InstanceModifier
{
None,
Const,
ReadOnly,
Static,
StaticReadOnly
}
public static partial class SourceGeneration
{
public static string ToFriendlyString(this InstanceModifier modifier) =>
modifier switch
{
InstanceModifier.Const => "const",
InstanceModifier.ReadOnly => "readonly",
InstanceModifier.Static => "static",
InstanceModifier.StaticReadOnly => "static readonly",
_ => ""
};
}
}

View file

@ -0,0 +1,60 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SourceGeneration.Method.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.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
namespace SerializationGenerator
{
public static partial class SourceGeneration
{
public static void GenerateMethodStart(this StringBuilder source, string methodName, AccessModifier accessors, bool isOverride, string returnType, ImmutableArray<(ITypeSymbol, string)> parameters)
{
source.Append($" {accessors.ToFriendlyString()}{(isOverride ? " override" : "")} {returnType} {methodName}(");
source.GenerateSignatureArguments(parameters);
source.AppendLine(@")
{");
}
public static void GenerateMethodEnd(this StringBuilder source) => source.AppendLine(@" }");
public static void GenerateConstructorStart(
this StringBuilder source, string className, AccessModifier accessors, ImmutableArray<(ITypeSymbol, string)> parameters,
ImmutableArray<string> baseParameters, bool isOverload = false
)
{
source.Append($" {accessors.ToFriendlyString()} {className}(");
source.GenerateSignatureArguments(parameters);
source.Append(')');
bool hasBaseParams = baseParameters.Length > 0;
if (hasBaseParams)
{
source.AppendFormat(" : {0}(", isOverload ? "this" : "base");
for (int i = 0; i < baseParameters.Length; i++)
{
source.Append(baseParameters[i]);
if (i < baseParameters.Length - 1)
{
source.Append(',');
}
}
source.Append(')');
}
source.AppendLine("\n {");
}
}
}

View file

@ -0,0 +1,50 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SourceGeneration.Namespace.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.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
namespace SerializationGenerator
{
public static partial class SourceGeneration
{
public static void GenerateUsings(this StringBuilder source, IImmutableList<ITypeSymbol> typesUsed)
{
var enumerable = typesUsed
.Select(t => t.ContainingNamespace.Name)
.Distinct()
.OrderByDescending(t => t);
foreach (var t in enumerable)
{
source.Insert(0, $"using {t}{Environment.NewLine}");
}
}
public static void GenerateNamespaceStart(this StringBuilder source, string namespaceName)
{
source.AppendLine($@"namespace {namespaceName}
{{");
}
public static void GenerateNamespaceEnd(this StringBuilder source)
{
source.AppendLine("}");
}
}
}

View file

@ -0,0 +1,107 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SourceGeneration.Property.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.Text;
using Humanizer;
using Microsoft.CodeAnalysis;
namespace SerializationGenerator
{
public static partial class SourceGeneration
{
public static string GetPropertyName(this IFieldSymbol fieldSymbol)
{
var fieldName = fieldSymbol.Name;
var propertyName = fieldName;
if (propertyName.StartsWith("m_", StringComparison.OrdinalIgnoreCase))
{
propertyName = propertyName.Substring(2);
}
else if (propertyName.StartsWith("_", StringComparison.OrdinalIgnoreCase))
{
propertyName = propertyName.Substring(1);
}
return propertyName.Dehumanize();
}
public static void GeneratePropertyStart(
this StringBuilder source,
AccessModifier accessors,
IFieldSymbol fieldSymbol
)
{
var propertyName = fieldSymbol.GetPropertyName();
source.AppendLine($@" {accessors.ToFriendlyString()} {fieldSymbol.Type} {propertyName}
{{");
}
public static void GenerateAutoProperty(
this StringBuilder source,
AccessModifier accessors,
string type,
string propertyName,
AccessModifier? getAccessor,
AccessModifier? setAccessor,
bool useInit = false
)
{
if (getAccessor == null && setAccessor == null)
{
throw new ArgumentNullException($"Must specify a {nameof(getAccessor)} or {nameof(setAccessor)} parameter");
}
var getter = getAccessor == null ?
"" :
$"{(getAccessor != AccessModifier.None ? $"{getAccessor.Value.ToFriendlyString()} " : "")}get;";
var getterSpace = getAccessor != null ? " " : "";
var setOrInit = useInit ? "init;" : "set;";
var setterAccessor = setAccessor != AccessModifier.None ? $"{setAccessor?.ToFriendlyString() ?? ""} " : "";
var setter = setterAccessor == "" ? "" : $"{getterSpace}{setterAccessor}{setOrInit}";
var propertyAccessor = accessors == AccessModifier.None ? "" : $"{accessors.ToFriendlyString()} ";
source.AppendLine($"{propertyAccessor}{type} {propertyName} {{ {getter}{setter} }}");
}
public static void GeneratePropertyEnd(this StringBuilder source) => source.AppendLine(" }");
public static void GeneratePropertyGetterReturnsField(this StringBuilder source, IFieldSymbol fieldSymbol) =>
source.AppendLine($" get => {fieldSymbol.Name};");
public static void GeneratePropertyGetterStart(this StringBuilder source, bool useExpression) =>
source.AppendLine($" get{(useExpression ? " => " : "\n {")}");
public static void GeneratePropertyGetSetEnd(this StringBuilder source, bool useExpression)
{
if (!useExpression)
{
source.AppendLine(" }");
}
}
public static void GeneratePropertySetterSetsValue(this StringBuilder source, IFieldSymbol fieldSymbol) =>
source.AppendLine($" set => {fieldSymbol.Name} = value;");
public static void GeneratePropertySetterStart(this StringBuilder source, bool useExpression, bool useInit = false) =>
source.AppendLine($" {(useInit ? "init" : "set")}{(useExpression ? " => " : "\n {")}");
}
}