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
|
|
@ -33,7 +33,6 @@ namespace Server.Guilds
|
|||
World.AddGuild(this);
|
||||
|
||||
SetTypeRef(GetType());
|
||||
((ISerializable)this).MarkDirty();
|
||||
}
|
||||
|
||||
protected BaseGuild(Serial serial)
|
||||
|
|
@ -64,7 +63,9 @@ namespace Server.Guilds
|
|||
[CommandProperty(AccessLevel.Counselor)]
|
||||
public Serial Serial { get; }
|
||||
|
||||
long ISerializable.SavePosition { get; set; }
|
||||
bool ISerializable.UseDirtyChecking => false;
|
||||
|
||||
long ISerializable.SavePosition { get; set; } = -1;
|
||||
|
||||
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; }
|
||||
|
||||
|
|
|
|||
|
|
@ -231,7 +231,6 @@ namespace Server
|
|||
|
||||
World.AddEntity(this);
|
||||
SetTypeRef(GetType());
|
||||
((ISerializable)this).MarkDirty();
|
||||
}
|
||||
|
||||
public Item(Serial serial)
|
||||
|
|
@ -784,7 +783,9 @@ namespace Server
|
|||
AddNameProperties(list);
|
||||
}
|
||||
|
||||
long ISerializable.SavePosition { get; set; }
|
||||
public virtual bool UseDirtyChecking => false;
|
||||
|
||||
long ISerializable.SavePosition { get; set; } = -1;
|
||||
|
||||
BufferWriter ISerializable.SaveBuffer { get; set; }
|
||||
|
||||
|
|
|
|||
|
|
@ -560,7 +560,6 @@ namespace Server
|
|||
|
||||
World.AddEntity(this);
|
||||
SetTypeRef(GetType());
|
||||
((ISerializable)this).MarkDirty();
|
||||
}
|
||||
|
||||
public Mobile(Serial serial)
|
||||
|
|
@ -2522,7 +2521,9 @@ namespace Server
|
|||
AddNameProperties(list);
|
||||
}
|
||||
|
||||
long ISerializable.SavePosition { get; set; }
|
||||
public virtual bool UseDirtyChecking => false;
|
||||
|
||||
long ISerializable.SavePosition { get; set; } = -1;
|
||||
|
||||
BufferWriter ISerializable.SaveBuffer { get; set; }
|
||||
|
||||
|
|
|
|||
|
|
@ -20,6 +20,10 @@ namespace Server
|
|||
{
|
||||
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; }
|
||||
BufferWriter SaveBuffer { get; protected internal set; }
|
||||
int TypeRef { get; }
|
||||
|
|
@ -39,7 +43,7 @@ namespace Server
|
|||
public void InitializeSaveBuffer(byte[] buffer)
|
||||
{
|
||||
SaveBuffer = new BufferWriter(buffer, true);
|
||||
if (World.DirtyTrackingEnabled)
|
||||
if (UseDirtyChecking)
|
||||
{
|
||||
SavePosition = SaveBuffer.Position;
|
||||
}
|
||||
|
|
@ -63,7 +67,7 @@ namespace Server
|
|||
SaveBuffer.Seek(0, SeekOrigin.Begin);
|
||||
Serialize(SaveBuffer);
|
||||
|
||||
if (World.DirtyTrackingEnabled)
|
||||
if (UseDirtyChecking)
|
||||
{
|
||||
SavePosition = SaveBuffer.Position;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,7 +48,6 @@ 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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue