fix(core): Fixes encoded int. Adds dirty checking opt-out (#619)
### Additions
- Automatically opts-out `Item/Mobile/Guild/Accounts` from dirty checking with a new property `UseDirtyChecking`
- Codegen now enables `UseDirtyChecking` via getter. This requires that the property is `virtual` for derived types.
### Fixes
- Fixes `EncodedInt` being broken
- Fixes issues with new custom serializable types that are not derived from Item/Mobile/etc.
- Removes double dirty checking.
### Example of a brand new serializable type that isn't an Item/Mobile/etc.
User created code:
```cs
using System;
namespace Server.Items
{
[Serializable(0)]
public partial class NewTestEntityObject : ISerializable
{
[EncodedInt]
[SerializableField(0)]
private int _someProperty;
public NewTestEntityObject()
{
SetTypeRef(GetType());
// Add to serial tracking like World.Item
/*
Serial = World.NewEntity;
World.AddEntity(this);
*/
}
[AfterDeserialization]
private void AfterDeserialization()
{
Console.WriteLine("This ran!");
}
public int TypeRef { get; }
public Serial Serial { get; }
public void Delete()
{
}
public bool Deleted { get; set; }
public void SetTypeRef(Type type)
{
// Type tracking for persistence goes here
/*
TypeRef = World.NewEntityTypes.IndexOf(type);
if (TypeRef == -1)
{
World.NewEntityTypes.Add(type);
TypeRef = World.NewEntityTypes.Count - 1;
}
*/
}
}
}
```
Generated code:
```cs
namespace Server.Items
{
public partial class NewTestEntityObject
{
#pragma warning disable 0414
private const int _version = 0;
#pragma warning restore 0414
public int SomeProperty
{
get => _someProperty;
set
{
if (value != _someProperty)
{
_someProperty = value;
((ISerializable)this).MarkDirty();
}
}
}
long ISerializable.SavePosition { get; set; } = -1;
BufferWriter ISerializable.SaveBuffer { get; set; }
bool ISerializable.UseDirtyChecking => true;
public NewTestEntityObject(Serial serial)
{
Serial = serial;
SetTypeRef(typeof(NewTestEntityObject));
}
public void Serialize(IGenericWriter writer)
{
writer.WriteEncodedInt(_version);
writer.WriteEncodedInt(SomeProperty);
}
public void Deserialize(IGenericReader reader)
{
var version = reader.ReadEncodedInt();
SomeProperty = reader.ReadEncodedInt();
Timer.DelayCall(AfterDeserialization);
}
}
}
```
This commit is contained in:
parent
1f499b00c6
commit
312e3873f1
13 changed files with 99 additions and 58 deletions
|
|
@ -101,11 +101,11 @@ namespace SerializationGenerator
|
||||||
|
|
||||||
source.GenerateClassStart(
|
source.GenerateClassStart(
|
||||||
className,
|
className,
|
||||||
isOverride ?
|
ImmutableArray<ITypeSymbol>.Empty
|
||||||
ImmutableArray<ITypeSymbol>.Empty :
|
|
||||||
ImmutableArray.Create<ITypeSymbol>(serializableInterface)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const string indent = " ";
|
||||||
|
|
||||||
source.GenerateClassField(
|
source.GenerateClassField(
|
||||||
AccessModifier.Private,
|
AccessModifier.Private,
|
||||||
InstanceModifier.Const,
|
InstanceModifier.Const,
|
||||||
|
|
@ -199,15 +199,16 @@ namespace SerializationGenerator
|
||||||
// If we are not inheriting ISerializable, then we need to define some stuff
|
// If we are not inheriting ISerializable, then we need to define some stuff
|
||||||
if (!isOverride)
|
if (!isOverride)
|
||||||
{
|
{
|
||||||
// long ISerializable.SavePosition { get; set; }
|
// long ISerializable.SavePosition { get; set; } = -1;
|
||||||
source.GenerateAutoProperty(
|
source.GenerateAutoProperty(
|
||||||
AccessModifier.None,
|
AccessModifier.None,
|
||||||
"long",
|
"long",
|
||||||
"ISerializable.SavePosition",
|
"ISerializable.SavePosition",
|
||||||
AccessModifier.None,
|
AccessModifier.None,
|
||||||
AccessModifier.None
|
AccessModifier.None,
|
||||||
|
indent,
|
||||||
|
defaultValue: "-1"
|
||||||
);
|
);
|
||||||
source.AppendLine();
|
|
||||||
|
|
||||||
// BufferWriter ISerializable.SaveBuffer { get; set; }
|
// BufferWriter ISerializable.SaveBuffer { get; set; }
|
||||||
source.GenerateAutoProperty(
|
source.GenerateAutoProperty(
|
||||||
|
|
@ -215,7 +216,35 @@ namespace SerializationGenerator
|
||||||
"BufferWriter",
|
"BufferWriter",
|
||||||
"ISerializable.SaveBuffer",
|
"ISerializable.SaveBuffer",
|
||||||
AccessModifier.None,
|
AccessModifier.None,
|
||||||
AccessModifier.None
|
AccessModifier.None,
|
||||||
|
indent
|
||||||
|
);
|
||||||
|
|
||||||
|
// bool ISerializable.UseDirtyChecking { get; } = true;
|
||||||
|
source.GenerateAutoProperty(
|
||||||
|
AccessModifier.None,
|
||||||
|
"bool",
|
||||||
|
"ISerializable.UseDirtyChecking",
|
||||||
|
AccessModifier.None,
|
||||||
|
null,
|
||||||
|
indent,
|
||||||
|
defaultValue: "true"
|
||||||
|
);
|
||||||
|
source.AppendLine();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// If this type does not *directly* inherit `ISerializable`, then we assume it has an overridable `UseDirtyChecking`
|
||||||
|
// public override bool ISerializable.UseDirtyChecking { get; } = true;
|
||||||
|
source.GenerateAutoProperty(
|
||||||
|
AccessModifier.Public,
|
||||||
|
"bool",
|
||||||
|
"UseDirtyChecking",
|
||||||
|
AccessModifier.None,
|
||||||
|
null,
|
||||||
|
indent,
|
||||||
|
defaultValue: "true",
|
||||||
|
isOverride: true
|
||||||
);
|
);
|
||||||
source.AppendLine();
|
source.AppendLine();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ namespace SerializationGenerator
|
||||||
// Setter
|
// Setter
|
||||||
source.GeneratePropertySetterStart(false);
|
source.GeneratePropertySetterStart(false);
|
||||||
const string indent = " ";
|
const string indent = " ";
|
||||||
source.AppendLine($"{indent}if(value != {fieldName})");
|
source.AppendLine($"{indent}if (value != {fieldName})");
|
||||||
source.AppendLine($"{indent}{{");
|
source.AppendLine($"{indent}{{");
|
||||||
source.AppendLine($"{indent} {fieldName} = value;");
|
source.AppendLine($"{indent} {fieldName} = value;");
|
||||||
source.AppendLine($"{indent} ((ISerializable)this).MarkDirty();");
|
source.AppendLine($"{indent} ((ISerializable)this).MarkDirty();");
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ namespace SerializationGenerator
|
||||||
|
|
||||||
if (!isOverride)
|
if (!isOverride)
|
||||||
{
|
{
|
||||||
source.Append(@$" Serial = serial;
|
source.AppendLine(@$" Serial = serial;
|
||||||
SetTypeRef(typeof({className}));");
|
SetTypeRef(typeof({className}));");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,13 +41,6 @@ namespace SerializationGenerator
|
||||||
|
|
||||||
const string indent = " ";
|
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}}}");
|
|
||||||
|
|
||||||
if (isOverride)
|
if (isOverride)
|
||||||
{
|
{
|
||||||
source.AppendLine();
|
source.AppendLine();
|
||||||
|
|
|
||||||
|
|
@ -87,34 +87,33 @@ namespace SerializationGenerator
|
||||||
}
|
}
|
||||||
|
|
||||||
var propertyName = property.Name;
|
var propertyName = property.Name;
|
||||||
|
var argument = property.RuleArguments.Length >= 1 ? property.RuleArguments[0] : null;
|
||||||
|
|
||||||
const string ipAddress = SerializableEntityGeneration.IPADDRESS_CLASS;
|
const string ipAddress = SerializableEntityGeneration.IPADDRESS_CLASS;
|
||||||
|
const string date = "System.DateTime";
|
||||||
|
|
||||||
var readMethod = property.Type switch
|
var readMethod = property.Type switch
|
||||||
{
|
{
|
||||||
"bool" => "ReadBool",
|
"bool" => "ReadBool",
|
||||||
"sbyte" => "ReadSByte",
|
"sbyte" => "ReadSByte",
|
||||||
"short" => "ReadShort",
|
"short" => "ReadShort",
|
||||||
"int" => "ReadInt",
|
"int" when argument == "EncodedInt" => "ReadEncodedInt",
|
||||||
"long" => "ReadLong",
|
"int" => "ReadInt",
|
||||||
"byte" => "ReadByte",
|
"long" => "ReadLong",
|
||||||
"ushort" => "ReadUShort",
|
"byte" => "ReadByte",
|
||||||
"uint" => "ReadUInt",
|
"ushort" => "ReadUShort",
|
||||||
"ulong" => "ReadULong",
|
"uint" => "ReadUInt",
|
||||||
"float" => "ReadFloat",
|
"ulong" => "ReadULong",
|
||||||
"double" => "ReadDouble",
|
"float" => "ReadFloat",
|
||||||
"string" => "ReadString",
|
"double" => "ReadDouble",
|
||||||
"decimal" => "ReadDecimal",
|
"string" => "ReadString",
|
||||||
ipAddress => "ReadIPAddress",
|
"decimal" => "ReadDecimal",
|
||||||
"System.DateTime" => property.RuleArguments.Length >= 1 &&
|
date when argument == "DeltaTime" => "ReadDeltaTime",
|
||||||
property.RuleArguments[0] == "DeltaTime" ?
|
date => "ReadDateTime",
|
||||||
"ReadDeltaTime" :
|
ipAddress => "ReadIPAddress"
|
||||||
"ReadDateTime"
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var readArgument = readMethod == "ReadString" &&
|
var readArgument = readMethod == "ReadString" && argument == "InternString" ? "true" : "";
|
||||||
property.RuleArguments.Length >= 1 &&
|
|
||||||
property.RuleArguments[0] == "InternString" ? "true" : "";
|
|
||||||
|
|
||||||
source.AppendLine($"{indent}{propertyName} = reader.{readMethod}({readArgument});");
|
source.AppendLine($"{indent}{propertyName} = reader.{readMethod}({readArgument});");
|
||||||
}
|
}
|
||||||
|
|
@ -129,15 +128,16 @@ namespace SerializationGenerator
|
||||||
}
|
}
|
||||||
|
|
||||||
var propertyName = property.Name;
|
var propertyName = property.Name;
|
||||||
|
var argument = property.RuleArguments.Length >= 1 ? property.RuleArguments[0] : null;
|
||||||
|
|
||||||
if (property.Type == "System.DateTime" && property.RuleArguments.Length >= 1 && property.RuleArguments[0] == "DeltaTime")
|
var writeMethod = property.Type switch
|
||||||
{
|
{
|
||||||
source.AppendLine($"{indent}writer.WriteDeltaTime({propertyName});");
|
"System.DateTime" when argument == "DeltaTime" => "WriteDeltaTime",
|
||||||
}
|
"int" when argument == "EncodedInt" => "WriteEncodedInt",
|
||||||
else
|
_ => "Write"
|
||||||
{
|
};
|
||||||
source.AppendLine($"{indent}writer.Write({propertyName});");
|
|
||||||
}
|
source.AppendLine($"{indent}writer.{writeMethod}({propertyName});");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,10 @@ namespace SerializationGenerator
|
||||||
string propertyName,
|
string propertyName,
|
||||||
AccessModifier? getAccessor,
|
AccessModifier? getAccessor,
|
||||||
AccessModifier? setAccessor,
|
AccessModifier? setAccessor,
|
||||||
bool useInit = false
|
string indent,
|
||||||
|
bool useInit = false,
|
||||||
|
string defaultValue = null,
|
||||||
|
bool isOverride = false
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
if (getAccessor == null && setAccessor == null)
|
if (getAccessor == null && setAccessor == null)
|
||||||
|
|
@ -74,12 +77,18 @@ namespace SerializationGenerator
|
||||||
var getterSpace = getAccessor != null ? " " : "";
|
var getterSpace = getAccessor != null ? " " : "";
|
||||||
var setOrInit = useInit ? "init;" : "set;";
|
var setOrInit = useInit ? "init;" : "set;";
|
||||||
|
|
||||||
var setterAccessor = setAccessor != AccessModifier.None ? $"{setAccessor?.ToFriendlyString() ?? ""} " : "";
|
var setterAccessor = setAccessor is null or AccessModifier.None
|
||||||
var setter = setterAccessor == "" ? "" : $"{getterSpace}{setterAccessor}{setOrInit}";
|
? ""
|
||||||
|
: $"{setAccessor.Value.ToFriendlyString() ?? ""} ";
|
||||||
|
|
||||||
|
var setter = setAccessor == null ? "" : $"{getterSpace}{setterAccessor}{setOrInit}";
|
||||||
|
|
||||||
var propertyAccessor = accessors == AccessModifier.None ? "" : $"{accessors.ToFriendlyString()} ";
|
var propertyAccessor = accessors == AccessModifier.None ? "" : $"{accessors.ToFriendlyString()} ";
|
||||||
|
var printOverride = isOverride ? "override " : "";
|
||||||
|
var printDefaultValue = defaultValue != null ? $"{(setAccessor != null ? " =" : "")} {defaultValue};" : "";
|
||||||
|
var printGetterSetter = setAccessor == null ? "=>" : $"{{ {getter}{setter} }}";
|
||||||
|
|
||||||
source.AppendLine($"{propertyAccessor}{type} {propertyName} {{ {getter}{setter} }}");
|
source.AppendLine($"{indent}{propertyAccessor}{printOverride}{type} {propertyName} {printGetterSetter}{printDefaultValue}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void GeneratePropertyEnd(this StringBuilder source) => source.AppendLine(" }");
|
public static void GeneratePropertyEnd(this StringBuilder source) => source.AppendLine(" }");
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,6 @@ namespace Server.Guilds
|
||||||
World.AddGuild(this);
|
World.AddGuild(this);
|
||||||
|
|
||||||
SetTypeRef(GetType());
|
SetTypeRef(GetType());
|
||||||
((ISerializable)this).MarkDirty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected BaseGuild(Serial serial)
|
protected BaseGuild(Serial serial)
|
||||||
|
|
@ -64,7 +63,9 @@ namespace Server.Guilds
|
||||||
[CommandProperty(AccessLevel.Counselor)]
|
[CommandProperty(AccessLevel.Counselor)]
|
||||||
public Serial Serial { get; }
|
public Serial Serial { get; }
|
||||||
|
|
||||||
long ISerializable.SavePosition { get; set; }
|
bool ISerializable.UseDirtyChecking => false;
|
||||||
|
|
||||||
|
long ISerializable.SavePosition { get; set; } = -1;
|
||||||
|
|
||||||
BufferWriter ISerializable.SaveBuffer { get; set; }
|
BufferWriter ISerializable.SaveBuffer { get; set; }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,9 @@ namespace Server
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
long ISerializable.SavePosition { get; set; }
|
bool ISerializable.UseDirtyChecking => false;
|
||||||
|
|
||||||
|
long ISerializable.SavePosition { get; set; } = -1;
|
||||||
|
|
||||||
BufferWriter ISerializable.SaveBuffer { get; set; }
|
BufferWriter ISerializable.SaveBuffer { get; set; }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -231,7 +231,6 @@ namespace Server
|
||||||
|
|
||||||
World.AddEntity(this);
|
World.AddEntity(this);
|
||||||
SetTypeRef(GetType());
|
SetTypeRef(GetType());
|
||||||
((ISerializable)this).MarkDirty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Item(Serial serial)
|
public Item(Serial serial)
|
||||||
|
|
@ -784,7 +783,9 @@ namespace Server
|
||||||
AddNameProperties(list);
|
AddNameProperties(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
long ISerializable.SavePosition { get; set; }
|
public virtual bool UseDirtyChecking => false;
|
||||||
|
|
||||||
|
long ISerializable.SavePosition { get; set; } = -1;
|
||||||
|
|
||||||
BufferWriter ISerializable.SaveBuffer { get; set; }
|
BufferWriter ISerializable.SaveBuffer { get; set; }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -560,7 +560,6 @@ namespace Server
|
||||||
|
|
||||||
World.AddEntity(this);
|
World.AddEntity(this);
|
||||||
SetTypeRef(GetType());
|
SetTypeRef(GetType());
|
||||||
((ISerializable)this).MarkDirty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Mobile(Serial serial)
|
public Mobile(Serial serial)
|
||||||
|
|
@ -2522,7 +2521,9 @@ namespace Server
|
||||||
AddNameProperties(list);
|
AddNameProperties(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
long ISerializable.SavePosition { get; set; }
|
public virtual bool UseDirtyChecking => false;
|
||||||
|
|
||||||
|
long ISerializable.SavePosition { get; set; } = -1;
|
||||||
|
|
||||||
BufferWriter ISerializable.SaveBuffer { get; set; }
|
BufferWriter ISerializable.SaveBuffer { get; set; }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,10 @@ namespace Server
|
||||||
{
|
{
|
||||||
public interface ISerializable
|
public interface ISerializable
|
||||||
{
|
{
|
||||||
|
// Make sure all properties that will be serialized are calling `MarkDirty()` when they get modified.
|
||||||
|
// This should be done manually or via code gen through SerializedField attribute.
|
||||||
|
// This attribute should be virtual for any base serializalbe type (Item, Mobile, etc) that can be opt-in per type.
|
||||||
|
bool UseDirtyChecking { get; }
|
||||||
long SavePosition { get; protected set; }
|
long SavePosition { get; protected set; }
|
||||||
BufferWriter SaveBuffer { get; protected internal set; }
|
BufferWriter SaveBuffer { get; protected internal set; }
|
||||||
int TypeRef { get; }
|
int TypeRef { get; }
|
||||||
|
|
@ -39,7 +43,7 @@ namespace Server
|
||||||
public void InitializeSaveBuffer(byte[] buffer)
|
public void InitializeSaveBuffer(byte[] buffer)
|
||||||
{
|
{
|
||||||
SaveBuffer = new BufferWriter(buffer, true);
|
SaveBuffer = new BufferWriter(buffer, true);
|
||||||
if (World.DirtyTrackingEnabled)
|
if (UseDirtyChecking)
|
||||||
{
|
{
|
||||||
SavePosition = SaveBuffer.Position;
|
SavePosition = SaveBuffer.Position;
|
||||||
}
|
}
|
||||||
|
|
@ -63,7 +67,7 @@ namespace Server
|
||||||
SaveBuffer.Seek(0, SeekOrigin.Begin);
|
SaveBuffer.Seek(0, SeekOrigin.Begin);
|
||||||
Serialize(SaveBuffer);
|
Serialize(SaveBuffer);
|
||||||
|
|
||||||
if (World.DirtyTrackingEnabled)
|
if (UseDirtyChecking)
|
||||||
{
|
{
|
||||||
SavePosition = SaveBuffer.Position;
|
SavePosition = SaveBuffer.Position;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,6 @@ namespace Server
|
||||||
private static string _tempSavePath; // Path to the temporary folder for the save
|
private static string _tempSavePath; // Path to the temporary folder for the save
|
||||||
private static string _savePath; // Path to "Saves" folder
|
private static string _savePath; // Path to "Saves" folder
|
||||||
|
|
||||||
public const bool DirtyTrackingEnabled = false;
|
|
||||||
public const uint ItemOffset = 0x40000000;
|
public const uint ItemOffset = 0x40000000;
|
||||||
public const uint MaxItemSerial = 0x7FFFFFFF;
|
public const uint MaxItemSerial = 0x7FFFFFFF;
|
||||||
public const uint MaxMobileSerial = ItemOffset - 1;
|
public const uint MaxMobileSerial = ItemOffset - 1;
|
||||||
|
|
|
||||||
|
|
@ -265,6 +265,8 @@ namespace Server.Accounting
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ISerializable.UseDirtyChecking => false;
|
||||||
|
|
||||||
long ISerializable.SavePosition { get; set; }
|
long ISerializable.SavePosition { get; set; }
|
||||||
|
|
||||||
BufferWriter ISerializable.SaveBuffer { get; set; }
|
BufferWriter ISerializable.SaveBuffer { get; set; }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue