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

@ -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);
if (TypeRef == -1)
{
World.MobileTypes.Add(ourType);
TypeRef = World.MobileTypes.Count - 1;
}
SetTypeRef(GetType());
}
public Mobile()
public void SetTypeRef(Type type)
{
m_Region = Map.Internal.DefaultRegion;
Serial = World.NewMobile;
DefaultMobileInit();
World.AddEntity(this);
var ourType = GetType();
TypeRef = World.MobileTypes.IndexOf(ourType);
TypeRef = World.MobileTypes.IndexOf(type);
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;