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

@ -26,7 +26,7 @@ jobs:
- name: Setup .NET 5
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.202
dotnet-version: 5.0.203
- name: Build
run: ./publish.cmd
- name: Test

1
.gitignore vendored
View file

@ -18,6 +18,7 @@
/Projects/*/obj
/Projects/*/bin
/Projects/*/Generated
*.log
*.user

View file

@ -7,7 +7,7 @@
<TargetFramework>net5.0</TargetFramework>
<Platforms>x64</Platforms>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>9</LangVersion>
<LangVersion>preview</LangVersion>
<PublicRelease>true</PublicRelease>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<NoWarn>NU1603</NoWarn>

View file

@ -12,6 +12,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UOContent.Tests", "Projects
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Benchmarks", "Projects\Benchmarks\Benchmarks.csproj", "{38B7E4A1-FDDD-486C-98EE-BA7B13712528}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerializationGenerator", "Projects\SerializationGenerator\SerializationGenerator.csproj", "{07DDB8CF-F926-44F4-A584-FF2997D2C9D0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerializationGenerator.Tests", "Projects\SerializationGenerator.Tests\SerializationGenerator.Tests.csproj", "{2BC92375-BB66-4CA5-B39C-36215749FE64}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Analyze|x64 = Analyze|x64
@ -49,6 +53,18 @@ Global
{38B7E4A1-FDDD-486C-98EE-BA7B13712528}.Debug|x64.Build.0 = Debug|x64
{38B7E4A1-FDDD-486C-98EE-BA7B13712528}.Release|x64.ActiveCfg = Release|x64
{38B7E4A1-FDDD-486C-98EE-BA7B13712528}.Release|x64.Build.0 = Release|x64
{07DDB8CF-F926-44F4-A584-FF2997D2C9D0}.Analyze|x64.ActiveCfg = Analyze|x64
{07DDB8CF-F926-44F4-A584-FF2997D2C9D0}.Analyze|x64.Build.0 = Analyze|x64
{07DDB8CF-F926-44F4-A584-FF2997D2C9D0}.Debug|x64.ActiveCfg = Debug|x64
{07DDB8CF-F926-44F4-A584-FF2997D2C9D0}.Debug|x64.Build.0 = Debug|x64
{07DDB8CF-F926-44F4-A584-FF2997D2C9D0}.Release|x64.ActiveCfg = Release|x64
{07DDB8CF-F926-44F4-A584-FF2997D2C9D0}.Release|x64.Build.0 = Release|x64
{2BC92375-BB66-4CA5-B39C-36215749FE64}.Analyze|x64.ActiveCfg = Analyze|x64
{2BC92375-BB66-4CA5-B39C-36215749FE64}.Analyze|x64.Build.0 = Analyze|x64
{2BC92375-BB66-4CA5-B39C-36215749FE64}.Debug|x64.ActiveCfg = Debug|x64
{2BC92375-BB66-4CA5-B39C-36215749FE64}.Debug|x64.Build.0 = Debug|x64
{2BC92375-BB66-4CA5-B39C-36215749FE64}.Release|x64.ActiveCfg = Release|x64
{2BC92375-BB66-4CA5-B39C-36215749FE64}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View file

@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SerializationGenerator\SerializationGenerator.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,23 @@
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
using SerializationGenerator;
using Xunit;
namespace SerializationGeneratorTests
{
public class GenerateClassTests
{
[Fact]
public void Test1()
{
var source = new StringBuilder();
source.GenerateClassStart(
"TestClass",
ImmutableArray<ITypeSymbol>.Empty
);
Assert.NotEmpty(source.ToString());
}
}
}

View file

@ -0,0 +1,81 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: EntityJsonGenerator.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.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
namespace SerializationGenerator
{
[Generator]
public class EntitySerializationGenerator : ISourceGenerator
{
public void Initialize(GeneratorInitializationContext context)
{
#if DEBUG
if (!Debugger.IsAttached)
{
Debugger.Launch();
}
#endif
context.RegisterForPostInitialization(i =>
{
SerializerSyntaxReceiver.AttributeTypes.Add("Server.SerializableAttribute");
SerializerSyntaxReceiver.AttributeTypes.Add("Server.SerializableFieldAttribute");
});
context.RegisterForSyntaxNotifications(() => new SerializerSyntaxReceiver());
}
public void Execute(GeneratorExecutionContext context)
{
if (context.SyntaxContextReceiver is not SerializerSyntaxReceiver receiver)
{
return;
}
var migrationPath = SerializableMigration.GetMigrationPath(context);
var jsonOptions = SerializableMigration.GetJsonSerializerOptions(context.Compilation);
// List of types that _will_ become ISerializable
var serializableList = receiver
.Fields
.GroupBy(f => f.ContainingType, SymbolEqualityComparer.Default)
.Select(g => g.Key as INamedTypeSymbol)
.Where(t => t.WillBeSerializable(context))
.ToImmutableArray();
foreach (IGrouping<ISymbol, IFieldSymbol> group in receiver.Fields.GroupBy(f => f.ContainingType, SymbolEqualityComparer.Default))
{
string classSource = SerializableEntityGeneration.GenerateSerializationPartialClass(
group.Key as INamedTypeSymbol,
group.ToList(),
context,
migrationPath,
jsonOptions,
serializableList
);
if (classSource != null)
{
context.AddSource($"{group.Key.Name}.Serialization.cs", SourceText.From(classSource, Encoding.UTF8));
}
}
}
}
}

View file

@ -0,0 +1,12 @@
{
"type": "Server.Items.TestItem",
"version": 1,
"properties": [
{
"name": "SomeProperty",
"type": "Server.Item",
"rule": "SerializableInterfaceMigrationRule",
"ruleArguments": ["Server.Items.Item"]
}
]
}

View file

@ -0,0 +1,4 @@
namespace System.Runtime.CompilerServices
{
internal static class IsExternalInit {}
}

View file

@ -0,0 +1,262 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableEntityGeneration.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.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Text.Json;
using Microsoft.CodeAnalysis;
namespace SerializationGenerator
{
public static partial class SerializableEntityGeneration
{
public static bool WillBeSerializable(this INamedTypeSymbol classSymbol, GeneratorExecutionContext context)
{
var compilation = context.Compilation;
var serializableEntityAttribute =
compilation.GetTypeByMetadataName(SERIALIZABLE_ATTRIBUTE);
var serializableInterface = compilation.GetTypeByMetadataName(SERIALIZABLE_INTERFACE);
if (!classSymbol.ContainingSymbol.Equals(classSymbol.ContainingNamespace, SymbolEqualityComparer.Default))
{
return false;
}
if (!classSymbol.ContainsInterface(serializableInterface))
{
return false;
}
var versionValue = classSymbol.GetAttributes()
.FirstOrDefault(
attr => attr.AttributeClass?.Equals(serializableEntityAttribute, SymbolEqualityComparer.Default) ?? false
)?.ConstructorArguments.FirstOrDefault().Value;
return versionValue != null;
}
public static string GenerateSerializationPartialClass(
INamedTypeSymbol classSymbol,
IList<IFieldSymbol> fields,
GeneratorExecutionContext context,
string migrationPath,
JsonSerializerOptions jsonSerializerOptions,
ImmutableArray<INamedTypeSymbol> serializableTypes
)
{
var compilation = context.Compilation;
var serializableEntityAttribute =
compilation.GetTypeByMetadataName(SERIALIZABLE_ATTRIBUTE);
var serializableFieldAttribute =
compilation.GetTypeByMetadataName(SERIALIZABLE_FIELD_ATTRIBUTE);
var serializableFieldAttrAttribute =
compilation.GetTypeByMetadataName(SERIALIZABLE_FIELD_ATTR_ATTRIBUTE);
var serializableInterface = compilation.GetTypeByMetadataName(SERIALIZABLE_INTERFACE);
// This is a class symbol if the containing symbol is the namespace
if (!classSymbol.ContainingSymbol.Equals(classSymbol.ContainingNamespace, SymbolEqualityComparer.Default))
{
return null;
}
// If we have a parent that is or derives from ISerializable, then we are in override
var isOverride = classSymbol.BaseType.ContainsInterface(serializableInterface);
if (!isOverride && !classSymbol.ContainsInterface(serializableInterface))
{
return null;
}
var version = 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 namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
var className = classSymbol.Name;
StringBuilder source = new StringBuilder();
source.GenerateNamespaceStart(namespaceName);
source.GenerateClassStart(
className,
isOverride ?
ImmutableArray<ITypeSymbol>.Empty :
ImmutableArray.Create<ITypeSymbol>(serializableInterface)
);
source.GenerateClassField(
AccessModifier.Private,
InstanceModifier.Const,
"int",
"_version",
version,
true
);
source.AppendLine();
var serializableProperties = new List<SerializableProperty>();
foreach (IFieldSymbol fieldSymbol in fields)
{
var allAttributes = fieldSymbol.GetAttributes();
var hasAttribute = allAttributes
.Any(
attr =>
SymbolEqualityComparer.Default.Equals(attr.AttributeClass, serializableFieldAttribute)
);
if (hasAttribute)
{
foreach (var attr in allAttributes)
{
if (!SymbolEqualityComparer.Default.Equals(attr.AttributeClass, serializableFieldAttrAttribute))
{
continue;
}
if (attr.AttributeClass == null)
{
continue;
}
var ctorArgs = attr.ConstructorArguments;
var attrTypeArg = ctorArgs[0];
if (attrTypeArg.Kind == TypedConstantKind.Primitive && attrTypeArg.Value is string attrStr)
{
source.AppendLine($" {attrStr}");
}
else
{
var attrType = (ITypeSymbol)attrTypeArg.Value;
source.GenerateAttribute(attrType.Name, ctorArgs[1].Values);
}
}
source.GenerateSerializableProperty(fieldSymbol);
source.AppendLine();
var serializableProperty = SerializableMigrationRulesEngine.GenerateSerializableProperty(
compilation,
fieldSymbol.GetPropertyName(),
fieldSymbol.Type,
allAttributes,
serializableTypes
);
serializableProperties.Add(serializableProperty);
}
}
// If we are not inheriting ISerializable, then we need to define some stuff
if (!isOverride)
{
// long ISerializable.SavePosition { get; set; }
source.GenerateAutoProperty(
AccessModifier.None,
"long",
"ISerializable.SavePosition",
AccessModifier.None,
AccessModifier.None
);
source.AppendLine();
// BufferWriter ISerializable.SaveBuffer { get; set; }
source.GenerateAutoProperty(
AccessModifier.None,
"BufferWriter",
"ISerializable.SaveBuffer",
AccessModifier.None,
AccessModifier.None
);
source.AppendLine();
}
// Serial constructor
source.GenerateSerialCtor(context, className, isOverride);
source.AppendLine();
var versionValue = int.Parse(version);
List<SerializableMetadata> migrations;
if (versionValue > 0)
{
migrations = SerializableMigration.GetMigrations(
migrationPath,
classSymbol,
versionValue,
jsonSerializerOptions
);
for (var i = 0; i < migrations.Count; i++)
{
var migration = migrations[i];
if (migration.Version < versionValue)
{
source.GenerateMigrationContentStruct(migration);
source.AppendLine();
}
}
}
else
{
migrations = new List<SerializableMetadata>();
}
// Serialize Method
source.GenerateSerializeMethod(
compilation,
isOverride,
serializableProperties
);
source.AppendLine();
// Deserialize Method
source.GenerateDeserializeMethod(
compilation,
isOverride,
versionValue,
migrations,
serializableProperties
);
source.GenerateClassEnd();
source.GenerateNamespaceEnd();
// Write the migration file
var newMigration = new SerializableMetadata
{
Version = versionValue,
Type = classSymbol.ToDisplayString(),
Properties = serializableProperties
};
SerializableMigration.WriteMigration(migrationPath, newMigration, jsonSerializerOptions);
return source.ToString();
}
}
}

View file

@ -0,0 +1,95 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableEntityGeneration.DeserializeMethod.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 SerializableEntityGeneration
{
public static void GenerateDeserializeMethod(
this StringBuilder source,
Compilation compilation,
bool isOverride,
int version,
List<SerializableMetadata> migrations,
List<SerializableProperty> properties
)
{
var genericReaderInterface = compilation.GetTypeByMetadataName(GENERIC_READER_INTERFACE);
source.GenerateMethodStart(
"Deserialize",
AccessModifier.Public,
isOverride,
"void",
ImmutableArray.Create<(ITypeSymbol, string)>((genericReaderInterface, "reader"))
);
const string indent = " ";
// Version
source.AppendLine($"{indent}var version = reader.ReadEncodedInt();");
if (version > 0)
{
var nextVersion = 0;
for (var i = 0; i < migrations.Count; i++)
{
var migrationVersion = migrations[i].Version;
if (migrationVersion == nextVersion)
{
nextVersion++;
}
source.AppendLine();
source.AppendLine($"{indent}if (version == {migrationVersion})");
source.AppendLine($"{indent}{{");
source.AppendLine($"{indent} MigrateFrom(new V{migrationVersion}Content(reader));");
source.AppendLine($"{indent} ((Server.ISerializable)this).MarkDirty();");
source.AppendLine($"{indent} return;");
source.AppendLine($"{indent}}}");
}
if (nextVersion < version)
{
source.AppendLine();
source.AppendLine($"{indent}if (version < _version)");
source.AppendLine($"{indent}{{");
source.AppendLine($"{indent} Deserialize(reader, version);");
source.AppendLine($"{indent} ((Server.ISerializable)this).MarkDirty();");
source.AppendLine($"{indent} return;");
source.AppendLine($"{indent}}}");
}
}
foreach (var property in properties)
{
source.AppendLine();
SerializableMigrationRulesEngine.Rules[property.Rule].GenerateDeserializationMethod(
source,
indent,
property
);
}
source.GenerateMethodEnd();
}
}
}

View file

@ -0,0 +1,161 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableEntityGeneration.MetadataTypes.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.Linq;
using Microsoft.CodeAnalysis;
namespace SerializationGenerator
{
public static partial class SerializableEntityGeneration
{
public const string LIST_CLASS = "System.Collections.Generic.List`1";
public const string HASHSET_CLASS = "System.Collections.Generic.HashSet`1";
public const string IP_CLASS = "System.Net.IPAddress";
public const string KEYVALUEPAIR_STRUCT = "System.Collections.Generic.KeyValuePair";
public const string SERIALIZABLE_ATTRIBUTE = "Server.SerializableAttribute";
public const string SERIALIZABLE_FIELD_ATTRIBUTE = "Server.SerializableFieldAttribute";
public const string SERIALIZABLE_FIELD_ATTR_ATTRIBUTE = "Server.SerializableFieldAttrAttribute";
public const string SERIALIZABLE_INTERFACE = "Server.ISerializable";
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 POINT2D_STRUCT = "Server.Point2D";
public const string POINT3D_STRUCT = "Server.Point3D";
public const string RECTANGLE2D_STRUCT = "Server.Rectangle2D";
public const string RECTANGLE3D_STRUCT = "Server.Rectangle3D";
public const string RACE_CLASS = "Server.Race";
public const string MAP_CLASS = "Server.Map";
public static bool IsDeltaDateTime(this AttributeData attr, Compilation compilation) =>
attr?.IsAttribute(compilation.GetTypeByMetadataName(DELTA_DATE_TIME_ATTRIBUTE)) == true;
public static bool IsAttribute(this AttributeData attr, ISymbol symbol) =>
attr?.AttributeClass?.Equals(symbol, SymbolEqualityComparer.Default) == true;
public static bool IsEnum(this ITypeSymbol symbol) =>
symbol.SpecialType == SpecialType.System_Enum || symbol.TypeKind == TypeKind.Enum;
public static bool HasSerializableInterface(
this ITypeSymbol symbol,
Compilation compilation,
ImmutableArray<INamedTypeSymbol> serializableTypes
) =>
symbol.ContainsInterface(compilation.GetTypeByMetadataName(SERIALIZABLE_INTERFACE)) ||
serializableTypes.Contains(symbol);
public static bool Contains(this ImmutableArray<INamedTypeSymbol> symbols, ITypeSymbol symbol) =>
symbol is INamedTypeSymbol namedSymbol &&
symbols.Contains(namedSymbol, SymbolEqualityComparer.Default);
public static bool HasGenericReaderCtor(this INamedTypeSymbol symbol, Compilation compilation, out bool requiresParent)
{
var genericReaderInterface = compilation.GetTypeByMetadataName(GENERIC_READER_INTERFACE);
var genericCtor = symbol.Constructors.FirstOrDefault(
m => !m.IsStatic &&
m.MethodKind == MethodKind.Constructor &&
m.Parameters.Length <= 2 &&
m.Parameters[0].Equals(genericReaderInterface, SymbolEqualityComparer.Default)
);
requiresParent = genericCtor?.Parameters.Length == 2 && genericCtor.Parameters[1].Equals(symbol, SymbolEqualityComparer.Default);
return genericCtor != null;
}
public static bool HasPublicSerializeMethod(
this ITypeSymbol symbol,
Compilation compilation,
ImmutableArray<INamedTypeSymbol> serializableTypes
)
{
if (symbol.HasSerializableInterface(compilation, serializableTypes))
{
return true;
}
var genericWriterInterface = compilation.GetTypeByMetadataName(GENERIC_WRITER_INTERFACE);
return symbol.GetAllMethods("Serialize")
.Any(
m => !m.IsStatic &&
m.ReturnsVoid &&
m.Parameters.Length == 1 &&
m.Parameters[0].Equals(genericWriterInterface, SymbolEqualityComparer.Default) &&
m.DeclaredAccessibility == Accessibility.Public
);
}
public static bool IsPoint2D(this ISymbol symbol, Compilation compilation) =>
symbol.Equals(
compilation.GetTypeByMetadataName(POINT2D_STRUCT),
SymbolEqualityComparer.Default
);
public static bool IsPoint3D(this ISymbol symbol, Compilation compilation) =>
symbol.Equals(
compilation.GetTypeByMetadataName(POINT3D_STRUCT),
SymbolEqualityComparer.Default
);
public static bool IsRectangle2D(this ISymbol symbol, Compilation compilation) =>
symbol.Equals(
compilation.GetTypeByMetadataName(RECTANGLE2D_STRUCT),
SymbolEqualityComparer.Default
);
public static bool IsRectangle3D(this ISymbol symbol, Compilation compilation) =>
symbol.Equals(
compilation.GetTypeByMetadataName(RECTANGLE3D_STRUCT),
SymbolEqualityComparer.Default
);
public static bool IsIpAddress(this ISymbol symbol, Compilation compilation) =>
symbol.Equals(
compilation.GetTypeByMetadataName(IP_CLASS),
SymbolEqualityComparer.Default
);
public static bool IsRace(this ISymbol symbol, Compilation compilation) =>
symbol.Equals(
compilation.GetTypeByMetadataName(RACE_CLASS),
SymbolEqualityComparer.Default
);
public static bool IsMap(this ISymbol symbol, Compilation compilation) =>
symbol.Equals(
compilation.GetTypeByMetadataName(MAP_CLASS),
SymbolEqualityComparer.Default
);
public static bool IsKeyValuePair(this ISymbol symbol, Compilation compilation) =>
(symbol as INamedTypeSymbol)?.ConstructedFrom.Equals(
compilation.GetTypeByMetadataName(KEYVALUEPAIR_STRUCT),
SymbolEqualityComparer.Default
) == true;
public static bool IsList(this ISymbol symbol, Compilation compilation) =>
(symbol as INamedTypeSymbol)?.ConstructedFrom.Equals(
compilation.GetTypeByMetadataName(LIST_CLASS),
SymbolEqualityComparer.Default
) == true;
public static bool IsHashSet(this ISymbol symbol, Compilation compilation) =>
(symbol as INamedTypeSymbol)?.ConstructedFrom.Equals(
compilation.GetTypeByMetadataName(HASHSET_CLASS),
SymbolEqualityComparer.Default
) == true;
}
}

View file

@ -0,0 +1,49 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableEntityGeneration.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.Text;
using Microsoft.CodeAnalysis;
namespace SerializationGenerator
{
public static partial class SerializableEntityGeneration
{
public static void GenerateSerializableProperty(
this StringBuilder source,
IFieldSymbol fieldSymbol
)
{
var fieldName = fieldSymbol.Name;
source.GeneratePropertyStart(AccessModifier.Public, fieldSymbol);
// Getter
source.GeneratePropertyGetterReturnsField(fieldSymbol);
// Setter
source.GeneratePropertySetterStart(false);
source.AppendLine(
$@" if (value != {fieldName})
{{
((ISerializable)this).MarkDirty();
{fieldName} = value;
}}"
);
source.GeneratePropertyGetSetEnd(false);
source.GeneratePropertyEnd();
}
}
}

View file

@ -0,0 +1,50 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableEntityGeneration.SerialCtor.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 SerializableEntityGeneration
{
private static readonly ImmutableArray<string> _baseParameters = new[] { "serial" }.ToImmutableArray();
public static void GenerateSerialCtor(
this StringBuilder source,
GeneratorExecutionContext context,
string className,
bool isOverride
)
{
var serialType = (ITypeSymbol)context.Compilation.GetTypeByMetadataName("Server.Serial");
source.GenerateConstructorStart(
className,
AccessModifier.Public,
new []{ (serialType, "serial") }.ToImmutableArray(),
isOverride ? _baseParameters : ImmutableArray<string>.Empty
);
if (!isOverride)
{
source.Append(@$" Serial = serial;
SetTypeRef(typeof({className}));");
}
source.GenerateMethodEnd();
}
}
}

View file

@ -0,0 +1,68 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableEntityGeneration.SerializeMethod.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 SerializableEntityGeneration
{
public static void GenerateSerializeMethod(
this StringBuilder source,
Compilation compilation,
bool isOverride,
List<SerializableProperty> properties
)
{
var genericWriterInterface = compilation.GetTypeByMetadataName(GENERIC_WRITER_INTERFACE);
source.GenerateMethodStart(
"Serialize",
AccessModifier.Public,
isOverride,
"void",
ImmutableArray.Create<(ITypeSymbol, string)>((genericWriterInterface, "writer"))
);
const string indent = " ";
source.AppendLine($"{indent}var savePosition = ((Server.ISerializable)this).SavePosition;");
source.AppendLine(@$"{indent}if (savePosition > -1)
{indent}{{
{indent} writer.Seek(savePosition, System.IO.SeekOrigin.Begin);
{indent} return;
{indent}}}");
// Version
source.AppendLine();
source.AppendLine($"{indent}writer.WriteEncodedInt(_version);");
foreach (var property in properties)
{
source.AppendLine();
SerializableMigrationRulesEngine.Rules[property.Rule].GenerateSerializationMethod(
source,
indent,
property
);
}
source.GenerateMethodEnd();
}
}
}

View file

@ -0,0 +1,46 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableMigrationRule.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 interface ISerializableMigrationRule
{
string RuleName { get; }
bool GenerateRuleState(
Compilation compilation,
ISymbol symbol,
ImmutableArray<AttributeData> attributes,
ImmutableArray<INamedTypeSymbol> serializableTypes,
out string[] ruleArguments
);
void GenerateDeserializationMethod(
StringBuilder source,
string indent,
SerializableProperty property
);
void GenerateSerializationMethod(
StringBuilder source,
string indent,
SerializableProperty property
);
}
}

View file

@ -0,0 +1,121 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: ArrayMigrationRule.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 ArrayMigrationRule : ISerializableMigrationRule
{
public string RuleName => nameof(ArrayMigrationRule);
public bool GenerateRuleState(
Compilation compilation,
ISymbol symbol,
ImmutableArray<AttributeData> attributes,
ImmutableArray<INamedTypeSymbol> serializableTypes,
out string[] ruleArguments
)
{
if (symbol is not IArrayTypeSymbol arrayTypeSymbol)
{
ruleArguments = null;
return false;
}
var serializableArrayType = SerializableMigrationRulesEngine.GenerateSerializableProperty(
compilation,
"ArrayEntry",
arrayTypeSymbol.ElementType,
attributes,
serializableTypes
);
var length = serializableArrayType.RuleArguments.Length;
ruleArguments = new string[length + 2];
ruleArguments[0] = arrayTypeSymbol.ElementType.ToDisplayString();
ruleArguments[1] = serializableArrayType.Rule;
Array.Copy(serializableArrayType.RuleArguments, 0, ruleArguments, 2, length);
return true;
}
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(ArrayMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var ruleArguments = property.RuleArguments;
var arrayElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
var arrayElementRuleArguments = new string[ruleArguments.Length - 2];
Array.Copy(ruleArguments, 2, arrayElementRuleArguments, 0, ruleArguments.Length - 2);
var propertyIndex = $"{property.Name}Index";
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}{{");
var serializableArrayElement = new SerializableProperty
{
Name = $"{property.Name}[{propertyIndex}]",
Type = ruleArguments[0],
Rule = arrayElementRule.RuleName,
RuleArguments = arrayElementRuleArguments
};
arrayElementRule.GenerateDeserializationMethod(source, $"{indent} ", serializableArrayElement);
source.AppendLine($"{indent}}}");
}
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(ArrayMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var ruleArguments = property.RuleArguments;
var arrayElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
var arrayElementRuleArguments = new string[ruleArguments.Length - 2];
Array.Copy(ruleArguments, 2, arrayElementRuleArguments, 0, ruleArguments.Length - 2);
var propertyIndex = $"{property.Name}Index";
source.AppendLine($"{indent}writer.WriteEncodedInt({property.Name}.Length);");
source.AppendLine($"{indent}for (var {propertyIndex} = 0; {propertyIndex} < {property.Name}.Length; {propertyIndex}++)");
source.AppendLine($"{indent}{{");
var serializableArrayElement = new SerializableProperty
{
Name = $"{property.Name}[{propertyIndex}]",
Type = ruleArguments[0],
Rule = arrayElementRule.RuleName,
RuleArguments = arrayElementRuleArguments
};
arrayElementRule.GenerateSerializationMethod(source, $"{indent} ", serializableArrayElement);
source.AppendLine($"{indent}}}");
}
}
}

View file

@ -0,0 +1,134 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: HashSetMigrationRule.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 HashSetMigrationRule : ISerializableMigrationRule
{
public string RuleName => nameof(HashSetMigrationRule);
public bool GenerateRuleState(
Compilation compilation,
ISymbol symbol,
ImmutableArray<AttributeData> attributes,
ImmutableArray<INamedTypeSymbol> serializableTypes,
out string[] ruleArguments
)
{
if (symbol is not INamedTypeSymbol namedTypeSymbol || !symbol.IsHashSet(compilation))
{
ruleArguments = null;
return false;
}
var setTypeSymbol = namedTypeSymbol.TypeArguments[0];
var serializableSetType = SerializableMigrationRulesEngine.GenerateSerializableProperty(
compilation,
"SetEntry",
setTypeSymbol,
attributes,
serializableTypes
);
var length = serializableSetType.RuleArguments.Length;
ruleArguments = new string[length + 2];
ruleArguments[0] = setTypeSymbol.ToDisplayString();
ruleArguments[1] = serializableSetType.Rule;
Array.Copy(serializableSetType.RuleArguments, 0, ruleArguments, 2, length);
return true;
}
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(HashSetMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var ruleArguments = property.RuleArguments;
var setElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
var setElementRuleArguments = new string[ruleArguments.Length - 2];
Array.Copy(ruleArguments, 2, setElementRuleArguments, 0, ruleArguments.Length - 2);
var propertyName = property.Name;
var propertyVarPrefix = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1, propertyName.Length - 1)}";
var propertyIndex = $"{propertyVarPrefix}Index";
var propertyEntry = $"{propertyVarPrefix}Entry";
var propertyCount = $"{propertyVarPrefix}Count";
source.AppendLine($"{indent}{ruleArguments[0]} {propertyEntry};");
source.AppendLine($"{indent}var {propertyCount} = reader.ReadEncodedInt();");
source.AppendLine($"{indent}{property.Name} = new System.Collections.Generic.HashSet<{ruleArguments[0]}>({propertyCount});");
source.AppendLine($"{indent}for (var {propertyIndex} = 0; i < {propertyCount}; {propertyIndex}++)");
source.AppendLine($"{indent}{{");
var serializableSetElement = new SerializableProperty
{
Name = propertyEntry,
Type = ruleArguments[0],
Rule = setElementRule.RuleName,
RuleArguments = setElementRuleArguments
};
setElementRule.GenerateDeserializationMethod(source, $"{indent} ", serializableSetElement);
source.AppendLine($"{indent} {property.Name}.Add({propertyEntry});");
source.AppendLine($"{indent}}}");
}
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(HashSetMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var ruleArguments = property.RuleArguments;
var setElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
var setElementRuleArguments = new string[ruleArguments.Length - 2];
Array.Copy(ruleArguments, 2, setElementRuleArguments, 0, ruleArguments.Length - 2);
var propertyName = property.Name;
var propertyVarPrefix = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1, propertyName.Length - 1)}";
var propertyEntry = $"{propertyVarPrefix}Entry";
source.AppendLine($"{indent}writer.WriteEncodedInt({property.Name}.Count);");
source.AppendLine($"{indent}foreach (var {propertyEntry} in {property.Name});");
source.AppendLine($"{indent}{{");
var serializableSetElement = new SerializableProperty
{
Name = propertyEntry,
Type = ruleArguments[0],
Rule = setElementRule.RuleName,
RuleArguments = setElementRuleArguments
};
setElementRule.GenerateSerializationMethod(source, $"{indent} ", serializableSetElement);
source.AppendLine($"{indent}}}");
}
}
}

View file

@ -0,0 +1,179 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: KeyValuePairMigrationRule.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 KeyValuePairMigrationRule : ISerializableMigrationRule
{
public string RuleName => nameof(KeyValuePairMigrationRule);
public bool GenerateRuleState(
Compilation compilation,
ISymbol symbol,
ImmutableArray<AttributeData> attributes,
ImmutableArray<INamedTypeSymbol> serializableTypes,
out string[] ruleArguments
)
{
if (symbol is not INamedTypeSymbol namedTypeSymbol || !symbol.IsKeyValuePair(compilation))
{
ruleArguments = null;
return false;
}
var typeArguments = namedTypeSymbol.TypeArguments;
var keySerializedProperty = SerializableMigrationRulesEngine.GenerateSerializableProperty(
compilation,
"key",
typeArguments[0],
attributes,
serializableTypes
);
var valueSerializedProperty = SerializableMigrationRulesEngine.GenerateSerializableProperty(
compilation,
"value",
typeArguments[1],
attributes,
serializableTypes
);
// Key
ruleArguments = new string[5 + keySerializedProperty.RuleArguments.Length + valueSerializedProperty.RuleArguments.Length];
ruleArguments[0] = typeArguments[0].ToDisplayString();
ruleArguments[1] = keySerializedProperty.Rule;
ruleArguments[2] = keySerializedProperty.RuleArguments.Length.ToString();
Array.Copy(keySerializedProperty.RuleArguments, 0, ruleArguments, 2, keySerializedProperty.RuleArguments.Length);
// Value
var valueIndex = 3 + keySerializedProperty.RuleArguments.Length;
ruleArguments[valueIndex++] = typeArguments[1].ToDisplayString();
ruleArguments[valueIndex++] = valueSerializedProperty.Rule;
Array.Copy(valueSerializedProperty.RuleArguments, 0, ruleArguments, valueIndex, valueSerializedProperty.RuleArguments.Length);
return true;
}
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(KeyValuePairMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var ruleArguments = property.RuleArguments;
var keyType = ruleArguments[0];
var keyRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
var keyRuleArguments = new string[int.Parse(ruleArguments[2])];
Array.Copy(ruleArguments, 3, keyRuleArguments, 0, keyRuleArguments.Length);
var serializableKeyProperty = new SerializableProperty
{
Name = "key",
Type = keyType,
Rule = keyRule.RuleName,
RuleArguments = keyRuleArguments
};
keyRule.GenerateDeserializationMethod(
source,
indent,
serializableKeyProperty
);
var valueIndex = 3 + keyRuleArguments.Length;
var valueType = ruleArguments[valueIndex++];
var valueRule = SerializableMigrationRulesEngine.Rules[ruleArguments[valueIndex++]];
var valueRuleArguments = new string[ruleArguments.Length - valueIndex];
Array.Copy(ruleArguments, valueIndex, valueRuleArguments, 0, valueRuleArguments.Length);
var serializableValueProperty = new SerializableProperty
{
Name = "value",
Type = valueType,
Rule = valueRule.RuleName,
RuleArguments = valueRuleArguments
};
keyRule.GenerateDeserializationMethod(
source,
indent,
serializableValueProperty
);
source.AppendLine(
$"{indent}{property.Name} = new {SerializableEntityGeneration.KEYVALUEPAIR_STRUCT}<{keyType}, {valueType}>(key, value);"
);
}
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(KeyValuePairMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var ruleArguments = property.RuleArguments;
var keyType = ruleArguments[0];
var keyRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
var keyRuleArguments = new string[int.Parse(ruleArguments[2])];
Array.Copy(ruleArguments, 3, keyRuleArguments, 0, keyRuleArguments.Length);
var serializableKeyProperty = new SerializableProperty
{
Name = $"{property.Name}.Key",
Type = keyType,
Rule = keyRule.RuleName,
RuleArguments = keyRuleArguments
};
keyRule.GenerateSerializationMethod(
source,
indent,
serializableKeyProperty
);
var valueIndex = 3 + keyRuleArguments.Length;
var valueType = ruleArguments[valueIndex++];
var valueRule = SerializableMigrationRulesEngine.Rules[ruleArguments[valueIndex++]];
var valueRuleArguments = new string[ruleArguments.Length - valueIndex];
Array.Copy(ruleArguments, valueIndex, valueRuleArguments, 0, valueRuleArguments.Length);
var serializableValueProperty = new SerializableProperty
{
Name = $"{property.Name}.Value",
Type = valueType,
Rule = valueRule.RuleName,
RuleArguments = valueRuleArguments
};
keyRule.GenerateSerializationMethod(
source,
indent,
serializableValueProperty
);
}
}
}

View file

@ -0,0 +1,133 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: ListMigrationRule.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 ListMigrationRule : ISerializableMigrationRule
{
public string RuleName => nameof(ListMigrationRule);
public bool GenerateRuleState(
Compilation compilation,
ISymbol symbol,
ImmutableArray<AttributeData> attributes,
ImmutableArray<INamedTypeSymbol> serializableTypes,
out string[] ruleArguments
)
{
if (symbol is not INamedTypeSymbol namedTypeSymbol || !symbol.IsList(compilation))
{
ruleArguments = null;
return false;
}
var listTypeSymbol = namedTypeSymbol.TypeArguments[0];
var serializableListType = SerializableMigrationRulesEngine.GenerateSerializableProperty(
compilation,
"ListEntry",
listTypeSymbol,
attributes,
serializableTypes
);
var length = serializableListType.RuleArguments.Length;
ruleArguments = new string[length + 2];
ruleArguments[0] = listTypeSymbol.ToDisplayString();
ruleArguments[1] = serializableListType.Rule;
Array.Copy(serializableListType.RuleArguments, 0, ruleArguments, 2, length);
return true;
}
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(ListMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var ruleArguments = property.RuleArguments;
var listElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
var listElementRuleArguments = new string[ruleArguments.Length - 2];
Array.Copy(ruleArguments, 2, listElementRuleArguments, 0, ruleArguments.Length - 2);
var propertyName = property.Name;
var propertyVarPrefix = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1, propertyName.Length - 1)}";
var propertyIndex = $"{propertyVarPrefix}Index";
var propertyEntry = $"{propertyVarPrefix}Entry";
var propertyCount = $"{propertyVarPrefix}Count";
source.AppendLine($"{indent}{ruleArguments[0]} {propertyEntry};");
source.AppendLine($"{indent}var {propertyCount} = reader.ReadEncodedInt();");
source.AppendLine($"{indent}{propertyName} = new System.Collections.Generic.List<{ruleArguments[0]}>({propertyCount});");
source.AppendLine($"{indent}for (var {propertyIndex} = 0; {propertyIndex} < {propertyCount}; {propertyIndex}++)");
source.AppendLine($"{indent}{{");
var serializableListElement = new SerializableProperty
{
Name = propertyEntry,
Type = ruleArguments[0],
Rule = listElementRule.RuleName,
RuleArguments = listElementRuleArguments
};
listElementRule.GenerateDeserializationMethod(source, $"{indent} ", serializableListElement);
source.AppendLine($"{indent} {propertyName}.Add({propertyEntry});");
source.AppendLine($"{indent}}}");
}
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(ListMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var ruleArguments = property.RuleArguments;
var listElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
var listElementRuleArguments = new string[ruleArguments.Length - 2];
Array.Copy(ruleArguments, 2, listElementRuleArguments, 0, ruleArguments.Length - 2);
var propertyName = property.Name;
var propertyEntry = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1, propertyName.Length - 1)}Entry";
source.AppendLine($"{indent}writer.WriteEncodedInt({propertyName}.Count);");
source.AppendLine($"{indent}foreach (var {propertyEntry} in {propertyName})");
source.AppendLine($"{indent}{{");
var serializableListElement = new SerializableProperty
{
Name = propertyEntry,
Type = ruleArguments[0],
Rule = listElementRule.RuleName,
RuleArguments = listElementRuleArguments
};
listElementRule.GenerateSerializationMethod(source, $"{indent} ", serializableListElement);
source.AppendLine($"{indent}}}");
}
}
}

View file

@ -0,0 +1,159 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: PrimitiveTypeMigrationRule.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 class PrimitiveTypeMigrationRule : ISerializableMigrationRule
{
public string RuleName => nameof(PrimitiveTypeMigrationRule);
public bool GenerateRuleState(
Compilation compilation,
ISymbol symbol,
ImmutableArray<AttributeData> attributes,
ImmutableArray<INamedTypeSymbol> serializableTypes,
out string[] ruleArguments
)
{
if (symbol.IsIpAddress(compilation))
{
ruleArguments = new[] { "IPAddress" };
return true;
}
if (symbol is not ITypeSymbol typeSymbol)
{
ruleArguments = null;
return false;
}
if (
typeSymbol.SpecialType is
SpecialType.System_Boolean or
SpecialType.System_SByte or
SpecialType.System_Int16 or
SpecialType.System_Int32 or
SpecialType.System_Int64 or
SpecialType.System_Byte or
SpecialType.System_UInt16 or
SpecialType.System_UInt32 or
SpecialType.System_UInt64 or
SpecialType.System_Single or
SpecialType.System_Double or
SpecialType.System_String or
SpecialType.System_Decimal or
SpecialType.System_DateTime
)
{
ruleArguments = new[] { typeSymbol.SpecialType.ToString() };
return true;
}
if (typeSymbol.SpecialType == SpecialType.System_DateTime)
{
ruleArguments = attributes.Any(a => a.IsDeltaDateTime(compilation))
? new[] { typeSymbol.SpecialType.ToString(), "DeltaTime" }
: new[] { typeSymbol.SpecialType.ToString() };
return true;
}
ruleArguments = null;
return false;
}
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(PrimitiveTypeMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var propertyName = property.Name;
var ruleType = property.RuleArguments[0];
string readMethod;
if (ruleType == "IPAddress")
{
readMethod = "ReadIPAddress";
}
else
{
if (!Enum.TryParse<SpecialType>(ruleType, out var specialType))
{
throw new ArgumentException($"Invalid rule state for property {propertyName} ({ruleType})");
}
readMethod = specialType switch
{
SpecialType.System_Boolean => "ReadBool",
SpecialType.System_SByte => "ReadSByte",
SpecialType.System_Int16 => "ReadShort",
SpecialType.System_Int32 => "ReadInt",
SpecialType.System_Int64 => "ReadLong",
SpecialType.System_Byte => "ReadByte",
SpecialType.System_UInt16 => "ReadUShort",
SpecialType.System_UInt32 => "ReadUInt",
SpecialType.System_UInt64 => "ReadULong",
SpecialType.System_Single => "ReadFloat",
SpecialType.System_Double => "ReadDouble",
SpecialType.System_String => "ReadString",
SpecialType.System_Decimal => "ReadDecimal",
SpecialType.System_DateTime => property.RuleArguments.Length >= 2 &&
property.RuleArguments[1] == "DeltaTime" ?
"ReadDeltaTime" :
"ReadDateTime"
};
}
source.AppendLine($"{indent}{propertyName} = reader.{readMethod}()");
}
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(PrimitiveTypeMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var propertyName = property.Name;
var ruleType = property.RuleArguments[0];
if (!Enum.TryParse<SpecialType>(ruleType, out var specialType))
{
throw new ArgumentException($"Invalid rule state for property {propertyName} ({ruleType})");
}
if (specialType == SpecialType.System_DateTime && property.RuleArguments[1] == "DeltaTime")
{
source.AppendLine($"{indent}writer.WriteDeltaTime({propertyName});");
}
else
{
source.AppendLine($"{indent}writer.Write({propertyName});");
}
}
}
}

View file

@ -0,0 +1,75 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: PrimitiveUOTypeMigrationRule.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 PrimitiveUOTypeMigrationRule : ISerializableMigrationRule
{
public string RuleName => nameof(PrimitiveUOTypeMigrationRule);
public bool GenerateRuleState(
Compilation compilation,
ISymbol symbol,
ImmutableArray<AttributeData> attributes,
ImmutableArray<INamedTypeSymbol> serializableTypes,
out string[] ruleArguments
)
{
ruleArguments = symbol switch
{
_ when symbol.IsPoint2D(compilation) => new[] { "Point2D" },
_ when symbol.IsPoint3D(compilation) => new[] { "Point3D" },
_ when symbol.IsRectangle2D(compilation) => new[] { "Rect2D" },
_ when symbol.IsRectangle3D(compilation) => new[] { "Rect3D" },
_ when symbol.IsRace(compilation) => new[] { "Race" },
_ when symbol.IsMap(compilation) => new[] { "Map" },
_ => null
};
return ruleArguments != null;
}
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(PrimitiveUOTypeMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var propertyName = property.Name;
source.AppendLine($"{indent}{propertyName} = reader.Read{property.RuleArguments[0]}()");
}
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(PrimitiveUOTypeMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var propertyName = property.Name;
source.AppendLine($"{indent}writer.Write({propertyName});");
}
}
}

View file

@ -0,0 +1,71 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableInterfaceMigrationRule.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 SerializableInterfaceMigrationRule : ISerializableMigrationRule
{
public string RuleName => nameof(SerializableInterfaceMigrationRule);
public bool GenerateRuleState(
Compilation compilation,
ISymbol symbol,
ImmutableArray<AttributeData> attributes,
ImmutableArray<INamedTypeSymbol> serializableTypes,
out string[] ruleArguments
)
{
if (symbol is ITypeSymbol typeSymbol && typeSymbol.HasSerializableInterface(compilation, serializableTypes))
{
ruleArguments = Array.Empty<string>();
return true;
}
ruleArguments = null;
return false;
}
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(SerializableInterfaceMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var propertyName = property.Name;
source.AppendLine($"{indent}{propertyName} = reader.ReadEntity<{property.Type}>();");
}
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(SerializableInterfaceMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var propertyName = property.Name;
source.AppendLine($"{indent}writer.Write({propertyName});");
}
}
}

View file

@ -0,0 +1,81 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializationMethodSignatureMigrationRule.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 SerializationMethodSignatureMigrationRule : ISerializableMigrationRule
{
public string RuleName => nameof(SerializationMethodSignatureMigrationRule);
public bool GenerateRuleState(
Compilation compilation,
ISymbol symbol,
ImmutableArray<AttributeData> attributes,
ImmutableArray<INamedTypeSymbol> serializableTypes,
out string[] ruleArguments
)
{
if ((symbol as ITypeSymbol)?.HasPublicSerializeMethod(compilation, serializableTypes) != true)
{
ruleArguments = null;
return false;
}
if (symbol is not INamedTypeSymbol namedTypeSymbol ||
!namedTypeSymbol.HasGenericReaderCtor(compilation, out var requiresParent))
{
ruleArguments = null;
return false;
}
ruleArguments = requiresParent ? new[] { "DeserializationRequiresParent" } : Array.Empty<string>();
return true;
}
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(SerializationMethodSignatureMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var propertyName = property.Name;
var argument = property.RuleArguments.Length >= 1 &&
property.RuleArguments[0] == "DeserializationRequiresParent" ? ", this" : "";
source.AppendLine($"{indent}{propertyName} = new {property.Type}(reader{argument})");
}
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
{
const string expectedRule = nameof(SerializationMethodSignatureMigrationRule);
var ruleName = property.Rule;
if (expectedRule != ruleName)
{
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
}
var propertyName = property.Name;
source.AppendLine($"{indent}{propertyName}.Serialize(writer);");
}
}
}

View file

@ -0,0 +1,32 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableMigration.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.Text.Json.Serialization;
namespace SerializationGenerator
{
public class SerializableMetadata
{
[JsonPropertyName("version")]
public int Version { get; set; }
[JsonPropertyName("type")]
public string Type { get; set; }
[JsonPropertyName("properties")]
public List<SerializableProperty> Properties { get; set; }
}
}

View file

@ -0,0 +1,27 @@
using System.Collections.Generic;
namespace SerializationGenerator
{
public class SerializableMetadataComparer : IComparer<SerializableMetadata>
{
public int Compare(SerializableMetadata x, SerializableMetadata y)
{
if (ReferenceEquals(x, y))
{
return 0;
}
if (ReferenceEquals(null, y))
{
return 1;
}
if (ReferenceEquals(null, x))
{
return -1;
}
return x.Version.CompareTo(y.Version);
}
}
}

View file

@ -0,0 +1,53 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableMigration.ContentStruct.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;
namespace SerializationGenerator
{
public static partial class SerializableMigration
{
public static void GenerateMigrationContentStruct(
this StringBuilder source,
SerializableMetadata migration
)
{
const string indent = " ";
source.AppendLine($"{indent}ref struct V{migration.Version}Content");
source.AppendLine($"{indent}{{");
foreach (var serializableProperty in migration.Properties)
{
source.AppendLine($"{indent} internal readonly {serializableProperty.Type} {serializableProperty.Name};");
}
source.AppendLine($"{indent} internal V{migration.Version}Content(IGenericReader reader)");
source.AppendLine($"{indent} {{");
foreach (var serializableProperty in migration.Properties)
{
SerializableMigrationRulesEngine.Rules[serializableProperty.Rule].GenerateDeserializationMethod(
source,
$"{indent} ",
serializableProperty
);
}
source.AppendLine($"{indent} }}");
source.AppendLine($"{indent}}}");
}
}
}

View file

@ -0,0 +1,78 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableMigration.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.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using Microsoft.CodeAnalysis;
namespace SerializationGenerator
{
public static partial class SerializableMigration
{
public static JsonSerializerOptions GetJsonSerializerOptions(Compilation compilation) =>
new()
{
WriteIndented = true,
AllowTrailingCommas = true,
IgnoreNullValues = true,
ReadCommentHandling = JsonCommentHandling.Skip
};
public static string GetMigrationPath(GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(
"build_property.SerializableMigrationPath",
out var migrationPath
);
return migrationPath;
}
public static List<SerializableMetadata> GetMigrations(
string migrationPath,
INamedTypeSymbol typeSymbol,
int version,
JsonSerializerOptions options
)
{
var typeName = typeSymbol.ToDisplayString();
var migrations = new SortedSet<SerializableMetadata>(new SerializableMetadataComparer());
var migrationFiles = Directory.GetFiles(migrationPath, $"{typeName}.v*.json");
foreach (var migrationFile in migrationFiles)
{
var text = File.ReadAllText(migrationFile, Encoding.UTF8);
var migration = JsonSerializer.Deserialize<SerializableMetadata>(text, options);
if (typeName == migration!.Type && version > migration.Version)
{
migrations.Add(migration);
}
}
return migrations.ToList();
}
public static void WriteMigration(string migrationPath, SerializableMetadata metadata, JsonSerializerOptions options)
{
Directory.CreateDirectory(migrationPath);
var filePath = Path.Combine(migrationPath, $"{metadata.Type}.v{metadata.Version}.json");
File.WriteAllText(filePath, JsonSerializer.Serialize(metadata, options));
}
}
}

View file

@ -0,0 +1,78 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableMigrationRulesEngine.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.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
namespace SerializationGenerator
{
public static class SerializableMigrationRulesEngine
{
public static readonly Dictionary<string, ISerializableMigrationRule> Rules = new();
static SerializableMigrationRulesEngine()
{
var rules = new ISerializableMigrationRule[]
{
new ArrayMigrationRule(),
new HashSetMigrationRule(),
new KeyValuePairMigrationRule(),
new ListMigrationRule(),
new PrimitiveTypeMigrationRule(),
new PrimitiveUOTypeMigrationRule(),
new SerializableInterfaceMigrationRule(),
new SerializationMethodSignatureMigrationRule()
};
foreach (var rule in rules)
{
Rules.Add(rule.RuleName, rule);
}
}
public static SerializableProperty GenerateSerializableProperty(
Compilation compilation,
string propertyName,
ISymbol propertyType,
ImmutableArray<AttributeData> attributes,
ImmutableArray<INamedTypeSymbol> serializableTypes
)
{
foreach (var rule in Rules.Values)
{
if (rule.GenerateRuleState(
compilation,
propertyType,
attributes,
serializableTypes,
out var ruleArguments
))
{
return new SerializableProperty
{
Name = propertyName,
Type = propertyType.ToDisplayString(),
Rule = rule.RuleName,
RuleArguments = ruleArguments
};
}
}
throw new Exception($"No rule found for property {propertyName} of type {propertyType} ({Rules.Count})");
}
}
}

View file

@ -0,0 +1,34 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableProperty.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.Json.Serialization;
namespace SerializationGenerator
{
public class SerializableProperty
{
[JsonPropertyName("name")]
public string Name { get; init; }
[JsonPropertyName("type")]
public string Type { get; init; }
[JsonPropertyName("rule")]
public string Rule { get; init; }
[JsonPropertyName("ruleArguments")]
public string[] RuleArguments { get; init; }
}
}

View file

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>preview</LangVersion>
<BuildOutputTargetFolder>analyzers</BuildOutputTargetFolder>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.9.0" />
<PackageReference Include="Humanizer.Core" Version="2.10.1" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="System.Text.Json" Version="4.7.2" GeneratePathProperty="true" PrivateAssets="all" />
</ItemGroup>
<PropertyGroup>
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
</PropertyGroup>
<Target Name="GetDependencyTargetPaths">
<ItemGroup>
<TargetPathWithTargetPlatformMoniker Include="$(PKGHumanizer_Core)\lib\netstandard2.0\*.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_Text_Json)\lib\netstandard2.0\*.dll" IncludeRuntimeDependency="false" />
</ItemGroup>
</Target>
</Project>

View file

@ -0,0 +1,48 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SyntaxReceiver.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.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace SerializationGenerator
{
public class SerializerSyntaxReceiver : ISyntaxContextReceiver
{
public List<IFieldSymbol> Fields { get; } = new();
public static HashSet<string> AttributeTypes { get; } = new();
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
{
if (context.Node is FieldDeclarationSyntax { AttributeLists: { Count: > 0 } } fieldDeclarationSyntax)
{
foreach (VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables)
{
if (context.SemanticModel.GetDeclaredSymbol(variable) is not IFieldSymbol fieldSymbol)
{
return;
}
if (fieldSymbol.GetAttributes().Any(ad => AttributeTypes.Contains(ad.AttributeClass?.ToDisplayString())))
{
Fields.Add(fieldSymbol);
}
}
}
}
}
}

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 {")}");
}
}

View file

@ -0,0 +1,22 @@
using System;
using Xunit;
namespace Server.Tests
{
public class EnumConversionTests
{
[Fact]
public void TestToEnum()
{
var e = ReadEnum<TileFlag>();
Assert.Equal(TileFlag.Container, e);
}
private unsafe T ReadEnum<T>() where T : unmanaged, Enum
{
var num = (long)TileFlag.Container;
return *(T*)&num;
}
}
}

View file

@ -1,3 +1,19 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: Guild.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.Generic;
namespace Server.Guilds
@ -11,31 +27,27 @@ namespace Server.Guilds
public abstract class BaseGuild : ISerializable
{
protected BaseGuild(Serial serial)
{
Serial = serial;
var ourType = GetType();
TypeRef = World.GuildTypes.IndexOf(ourType);
if (TypeRef == -1)
{
World.GuildTypes.Add(ourType);
TypeRef = World.GuildTypes.Count - 1;
}
}
protected BaseGuild()
{
Serial = World.NewGuild;
World.AddGuild(this);
var ourType = GetType();
TypeRef = World.GuildTypes.IndexOf(ourType);
SetTypeRef(GetType());
}
protected BaseGuild(Serial serial)
{
Serial = serial;
SetTypeRef(GetType());
}
public void SetTypeRef(Type type)
{
TypeRef = World.GuildTypes.IndexOf(type);
if (TypeRef == -1)
{
World.GuildTypes.Add(ourType);
World.GuildTypes.Add(type);
TypeRef = World.GuildTypes.Count - 1;
}
}
@ -51,9 +63,11 @@ namespace Server.Guilds
[CommandProperty(AccessLevel.Counselor)]
public Serial Serial { get; }
long ISerializable.SavePosition { get; set; }
BufferWriter ISerializable.SaveBuffer { get; set; }
public int TypeRef { get; }
public int TypeRef { get; private set; }
public abstract void Serialize(IGenericWriter writer);
public abstract void Deserialize(IGenericReader reader);

View file

@ -13,15 +13,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
namespace Server
{
public interface IEntity : IPoint3D, ISerializable
{
// Serial Serial { get; }
Point3D Location { get; }
Map Map { get; }
bool Deleted { get; }
void Delete();
void MoveToWorld(Point3D location, Map map);
void ProcessDelta();
@ -45,6 +44,12 @@ namespace Server
Deleted = false;
}
public void SetTypeRef(Type type)
{
}
long ISerializable.SavePosition { get; set; }
BufferWriter ISerializable.SaveBuffer { get; set; }
public int TypeRef { get; } = -1;
@ -61,7 +66,7 @@ namespace Server
public Map Map { get; private set; }
public virtual void MoveToWorld(Point3D newLocation, Map map)
public void MoveToWorld(Point3D newLocation, Map map)
{
Location = newLocation;
Map = map;

View file

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using Server.ContextMenus;
using Server.Items;
@ -216,9 +215,6 @@ namespace Server
private ObjectPropertyList m_PropertyList;
// Position in the save buffer where serialization ends. -1 if dirty
private int _savePosition = -1;
[Constructible]
public Item(int itemID = 0)
{
@ -234,27 +230,22 @@ namespace Server
SetLastMoved();
World.AddEntity(this);
var ourType = GetType();
TypeRef = World.ItemTypes.IndexOf(ourType);
if (TypeRef == -1)
{
World.ItemTypes.Add(ourType);
TypeRef = World.ItemTypes.Count - 1;
}
SetTypeRef(GetType());
}
public Item(Serial serial)
{
Serial = serial;
SetTypeRef(GetType());
}
var ourType = GetType();
TypeRef = World.ItemTypes.IndexOf(ourType);
public void SetTypeRef(Type type)
{
TypeRef = World.ItemTypes.IndexOf(type);
if (TypeRef == -1)
{
World.ItemTypes.Add(ourType);
World.ItemTypes.Add(type);
TypeRef = World.ItemTypes.Count - 1;
}
}
@ -792,22 +783,17 @@ namespace Server
AddNameProperties(list);
}
long ISerializable.SavePosition { get; set; }
BufferWriter ISerializable.SaveBuffer { get; set; }
[CommandProperty(AccessLevel.Counselor)]
public Serial Serial { get; }
public int TypeRef { get; }
public int TypeRef { get; private set; }
public virtual void Serialize(IGenericWriter writer)
{
// The item is clean, so let's skip
if (_savePosition > -1)
{
writer.Seek(_savePosition, SeekOrigin.Begin);
return;
}
writer.Write(9); // version
var flags = SaveFlag.None;

View file

@ -35,7 +35,7 @@ namespace Server.Json
Console.WriteLine("Invalid type {0} deserialized", typeName);
}
return AssemblyHandler.FindTypeByName(reader.GetString());
return type;
}
public override void Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options) =>

View file

@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using Microsoft.Toolkit.HighPerformance;
using Server.Accounting;
using Server.Buffers;
@ -379,22 +377,6 @@ namespace Server
Cured
}
[Serializable]
public class MobileNotConnectedException : Exception
{
public MobileNotConnectedException(Mobile source, string message)
: base(message) =>
Source = source.ToString();
public MobileNotConnectedException(Mobile source, string message, Exception innerException)
: base(message, innerException) =>
Source = source.ToString();
protected MobileNotConnectedException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
public delegate bool SkillCheckTargetHandler(
Mobile from, SkillName skill, object target, double minSkill,
double maxSkill
@ -413,8 +395,7 @@ namespace Server
public delegate bool AllowHarmfulHandler(Mobile from, Mobile target);
public delegate Container CreateCorpseHandler(
Mobile from, HairInfo hair, FacialHairInfo facialhair,
List<Item> initialContent, List<Item> equippedItems
Mobile from, HairInfo hair, FacialHairInfo facialhair, List<Item> initialContent, List<Item> equippedItems
);
public delegate int AOSStatusHandler(Mobile from, int index);
@ -570,8 +551,17 @@ namespace Server
private bool m_YellowHealthbar;
// Position in the save buffer where serialization ends. -1 if dirty
private int _savePosition = -1;
public Mobile()
{
m_Region = Map.Internal.DefaultRegion;
Serial = World.NewMobile;
DefaultMobileInit();
World.AddEntity(this);
SetTypeRef(GetType());
}
public Mobile(Serial serial)
{
@ -582,31 +572,16 @@ namespace Server
NextSkillTime = Core.TickCount;
DamageEntries = new List<DamageEntry>();
var ourType = GetType();
TypeRef = World.MobileTypes.IndexOf(ourType);
SetTypeRef(GetType());
}
public void SetTypeRef(Type type)
{
TypeRef = World.MobileTypes.IndexOf(type);
if (TypeRef == -1)
{
World.MobileTypes.Add(ourType);
TypeRef = World.MobileTypes.Count - 1;
}
}
public Mobile()
{
m_Region = Map.Internal.DefaultRegion;
Serial = World.NewMobile;
DefaultMobileInit();
World.AddEntity(this);
var ourType = GetType();
TypeRef = World.MobileTypes.IndexOf(ourType);
if (TypeRef == -1)
{
World.MobileTypes.Add(ourType);
World.MobileTypes.Add(type);
TypeRef = World.MobileTypes.Count - 1;
}
}
@ -2547,22 +2522,17 @@ namespace Server
AddNameProperties(list);
}
long ISerializable.SavePosition { get; set; }
BufferWriter ISerializable.SaveBuffer { get; set; }
[CommandProperty(AccessLevel.Counselor)]
public Serial Serial { get; }
public int TypeRef { get; }
public int TypeRef { get; private set; }
public virtual void Serialize(IGenericWriter writer)
{
// The item is clean, so let's skip
if (_savePosition > -1)
{
writer.Seek(_savePosition, SeekOrigin.Begin);
return;
}
writer.Write(32); // version
writer.WriteDeltaTime(LastStrGain);
@ -8373,7 +8343,7 @@ namespace Server
public void Yell(int number, string args = "") =>
PublicOverheadMessage(MessageType.Yell, YellHue, number, args);
public bool SendHuePicker(HuePicker p, bool throwOnOffline = false)
public bool SendHuePicker(HuePicker p)
{
if (m_NetState != null)
{
@ -8381,11 +8351,6 @@ namespace Server
return true;
}
if (throwOnOffline)
{
throw new MobileNotConnectedException(this, "Hue picker could not be sent.");
}
return false;
}

View file

@ -16,7 +16,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
using Server.Text;

View file

@ -0,0 +1,27 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: DeltaDateTimeAttribute.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 DateTime field or property is for delta time (duration)
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class DeltaDateTimeAttribute : Attribute
{
}
}

View file

@ -53,6 +53,7 @@ namespace Server
void Deserialize(string savePath)
{
var path = Path.Combine(savePath, name);
AssemblyHandler.EnsureDirectory(path);
string binPath = Path.Combine(path, $"{name}.bin");

View file

@ -13,12 +13,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.IO;
namespace Server
{
public interface ISerializable
{
long SavePosition { get; protected set; }
BufferWriter SaveBuffer { get; protected internal set; }
int TypeRef { get; }
Serial Serial { get; }
@ -27,6 +29,13 @@ namespace Server
void Delete();
bool Deleted { get; }
void MarkDirty()
{
SavePosition = -1;
}
void SetTypeRef(Type type);
public void InitializeSaveBuffer(byte[] buffer)
{
SaveBuffer = new BufferWriter(buffer, true);
@ -35,8 +44,21 @@ namespace Server
public void Serialize()
{
SaveBuffer ??= new BufferWriter(true);
// Clean, don't bother serializing
if (SavePosition > -1)
{
SaveBuffer.Seek(SavePosition, SeekOrigin.Begin);
return;
}
SaveBuffer.Seek(0, SeekOrigin.Begin);
Serialize(SaveBuffer);
if (World.DirtyTrackingEnabled)
{
SavePosition = SaveBuffer.Position;
}
}
}
}

View file

@ -0,0 +1,27 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableEntityAttribute.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
{
[AttributeUsage(AttributeTargets.Class)]
public sealed class SerializableAttribute : Attribute
{
public int Version { get; }
public SerializableAttribute(int version) => Version = version;
}
}

View file

@ -0,0 +1,27 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableFieldAttribute.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
{
[AttributeUsage(AttributeTargets.Field)]
public sealed class SerializableFieldAttribute : Attribute
{
public int Order { get; }
public SerializableFieldAttribute(int order) => Order = order;
}
}

View file

@ -0,0 +1,40 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializableFieldAttributeAttribute.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
{
[AttributeUsage(AttributeTargets.Field)]
public sealed class SerializableFieldAttrAttribute : Attribute
{
public string AttributeString { get; }
public Type AttributeType { get; }
public object[] Arguments { get; }
public SerializableFieldAttrAttribute(string attrString) => AttributeString = attrString;
public SerializableFieldAttrAttribute(Type type, params object[] args)
{
if (typeof(Attribute).IsAssignableFrom(type))
{
throw new ArgumentException($"Argument {nameof(type)} must be an attribute.");
}
AttributeType = type;
Arguments = args;
}
}
}

View file

@ -0,0 +1,30 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SerializablePropertyAttribute.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>
/// Marks a property as serializable. Requires a call to ISerializable.MarkDirty()
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class SerializablePropertyAttribute : Attribute
{
public int Order { get; }
public SerializablePropertyAttribute(int order) => Order = order;
}
}

31
Projects/Server/Server.csproj Normal file → Executable file
View file

@ -10,6 +10,8 @@
<PublishDir>..\..\Distribution</PublishDir>
<OutDir>..\..\Distribution</OutDir>
<Version>0.0.0</Version>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<Target Name="CleanPub" AfterTargets="Clean">
<Message Text="Removing distribution files..." />
@ -30,6 +32,7 @@
<Delete Files="..\..\Distribution\$(AssemblyName).pdb" ContinueOnError="true" />
<Delete Files="..\..\Distribution\$(AssemblyName).runtimeconfig.dev.json" ContinueOnError="true" />
<Delete Files="..\..\Distribution\$(AssemblyName).runtimeconfig.json" ContinueOnError="true" />
<RemoveDir Directories="Generated" />
</Target>
<ItemGroup>
<PackageReference Include="Microsoft.Toolkit.HighPerformance" Version="7.0.2" />
@ -38,4 +41,32 @@
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Zlib.Bindings" Version="1.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SerializationGenerator\SerializationGenerator.csproj">
<SetTargetFramework>TargetFramework=netstandard2.0</SetTargetFramework>
<OutputItemType>Analyzer</OutputItemType>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<PrivateAssets>all</PrivateAssets>
</ProjectReference>
</ItemGroup>
<Target Name="AddSourceGeneratedFiles" AfterTargets="CoreCompile">
<ItemGroup>
<Compile Include="Generated\**" />
</ItemGroup>
</Target>
<Target Name="RemoveSourceGeneratedFiles" BeforeTargets="CoreCompile">
<ItemGroup>
<Compile Remove="Generated\**" />
</ItemGroup>
<RemoveDir Directories="Generated" />
</Target>
<ItemGroup>
<CompilerVisibleProperty Include="SerializableMigrationPath" />
</ItemGroup>
<ItemGroup>
<Folder Include="Migrations" />
</ItemGroup>
<PropertyGroup>
<SerializableMigrationPath>.\Migrations\</SerializableMigrationPath>
</PropertyGroup>
</Project>

View file

@ -48,6 +48,7 @@ namespace Server
private static string _tempSavePath; // Path to the temporary folder for the save
private static string _savePath; // Path to "Saves" folder
public const bool DirtyTrackingEnabled = false;
public const uint ItemOffset = 0x40000000;
public const uint MaxItemSerial = 0x7FFFFFFF;
public const uint MaxMobileSerial = ItemOffset - 1;

View file

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime.CompilerServices;
using System.Xml;
using Server.Accounting.Security;
using Server.Misc;
@ -10,7 +12,7 @@ using Server.Network;
namespace Server.Accounting
{
public class Account : IAccount, IComparable<Account>
public partial class Account : IAccount, IComparable<Account>
{
public static readonly TimeSpan YoungDuration = TimeSpan.FromHours(40.0);
public static readonly TimeSpan InactiveDuration = TimeSpan.FromDays(180.0);
@ -23,7 +25,6 @@ namespace Server.Accounting
private List<AccountTag> m_Tags;
private TimeSpan m_TotalGameTime;
private Timer m_YoungTimer;
private BufferWriter _saveBuffer;
public Account(string username, string password) : this(Accounts.NewAccount)
{
@ -48,28 +49,13 @@ namespace Server.Accounting
{
Serial = serial;
var ourType = GetType();
TypeRef = Accounts.Types.IndexOf(ourType);
if (TypeRef == -1)
{
Accounts.Types.Add(ourType);
TypeRef = Accounts.Types.Count - 1;
}
SetTypeRef(GetType());
}
public Account(XmlElement node)
{
Serial = Accounts.NewAccount;
var ourType = GetType();
TypeRef = Accounts.Types.IndexOf(ourType);
if (TypeRef == -1)
{
Accounts.Types.Add(ourType);
TypeRef = Accounts.Types.Count - 1;
}
SetTypeRef(GetType());
Username = Utility.GetText(node["username"], "empty");
@ -141,6 +127,17 @@ namespace Server.Accounting
Accounts.Add(this);
}
public void SetTypeRef(Type type)
{
TypeRef = Accounts.Types.IndexOf(type);
if (TypeRef == -1)
{
Accounts.Types.Add(type);
TypeRef = Accounts.Types.Count - 1;
}
}
/// <summary>
/// Object detailing information about the hardware of the last person to log into this account
/// </summary>
@ -270,11 +267,9 @@ namespace Server.Accounting
}
}
BufferWriter ISerializable.SaveBuffer
{
get => _saveBuffer;
set => _saveBuffer = value;
}
long ISerializable.SavePosition { get; set; }
BufferWriter ISerializable.SaveBuffer { get; set; }
public int TypeRef { get; private set; }

View file

@ -275,7 +275,6 @@ namespace Server.Factions
if (Faction.Election != this)
{
m_Timer?.Stop();
m_Timer = null;
return;
@ -447,14 +446,7 @@ namespace Server.Factions
gameTime = mobile.GameTime;
}
var kp = 0;
var pl = PlayerState.Find(From);
if (pl != null)
{
kp = pl.KillPoints;
}
var kp = PlayerState.Find(From)?.KillPoints ?? 0;
var sk = From.Skills.Total;

View file

@ -6,6 +6,8 @@
<Product>ModernUO Content</Product>
<PublishDir>..\..\Distribution\Assemblies</PublishDir>
<OutDir>..\..\Distribution\Assemblies</OutDir>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<Target Name="CleanPub" AfterTargets="Clean">
<Message Text="Removing distribution assemblies..." />
@ -26,6 +28,7 @@
<Delete Files="..\..\Distribution\Assemblies\libz.dylib" ContinueOnError="true" />
<Delete Files="..\..\Distribution\Assemblies\libz.so" ContinueOnError="true" />
<Delete Files="..\..\Distribution\Assemblies\ZLib.Bindings.dll" ContinueOnError="true" />
<RemoveDir Directories="Generated" />
</Target>
<ItemGroup>
<ProjectReference Include="..\Server\Server.csproj" Private="false" PrivateAssets="All" IncludeAssets="None">
@ -36,4 +39,24 @@
<PackageReference Include="Zlib.Bindings" Version="1.5.0" />
<PackageReference Include="Argon2.Bindings" Version="1.9.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SerializationGenerator\SerializationGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<Target Name="AddSourceGeneratedFiles" AfterTargets="CoreCompile">
<ItemGroup>
<Compile Include="Generated\**" />
</ItemGroup>
</Target>
<Target Name="RemoveSourceGeneratedFiles" BeforeTargets="CoreCompile">
<ItemGroup>
<Compile Remove="Generated\**" />
</ItemGroup>
<RemoveDir Directories="Generated" />
</Target>
<ItemGroup>
<CompilerVisibleProperty Include="SerializableMigrationPath" />
</ItemGroup>
<PropertyGroup>
<SerializableMigrationPath>.\Migrations\</SerializableMigrationPath>
</PropertyGroup>
</Project>

View file

@ -18,7 +18,7 @@ jobs:
displayName: 'Install .NET 5'
inputs:
packageType: sdk
version: 5.0.202
version: 5.0.203
- task: NuGetAuthenticate@0
- script: ./publish.cmd Release win
displayName: 'Build'
@ -62,7 +62,7 @@ jobs:
displayName: 'Install .NET 5'
inputs:
packageType: sdk
version: 5.0.202
version: 5.0.203
- task: NuGetAuthenticate@0
- script: ./publish.cmd Release $(os)
displayName: 'Build'