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:
parent
cb66bef0e5
commit
9afa4e4cab
61 changed files with 3203 additions and 142 deletions
|
|
@ -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; }
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue