### 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);
}
}
}
```
80 lines
2.8 KiB
C#
80 lines
2.8 KiB
C#
/*************************************************************************
|
|
* ModernUO *
|
|
* Copyright 2019-2020 - ModernUO Development Team *
|
|
* Email: hi@modernuo.com *
|
|
* File: ISerializable.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.IO;
|
|
|
|
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; }
|
|
Serial Serial { get; }
|
|
void Deserialize(IGenericReader reader);
|
|
void Serialize(IGenericWriter writer);
|
|
void Delete();
|
|
bool Deleted { get; }
|
|
|
|
void MarkDirty()
|
|
{
|
|
SavePosition = -1;
|
|
}
|
|
|
|
void SetTypeRef(Type type);
|
|
|
|
public void InitializeSaveBuffer(byte[] buffer)
|
|
{
|
|
SaveBuffer = new BufferWriter(buffer, true);
|
|
if (UseDirtyChecking)
|
|
{
|
|
SavePosition = SaveBuffer.Position;
|
|
}
|
|
else
|
|
{
|
|
SavePosition = -1;
|
|
}
|
|
}
|
|
|
|
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 (UseDirtyChecking)
|
|
{
|
|
SavePosition = SaveBuffer.Position;
|
|
}
|
|
else
|
|
{
|
|
SavePosition = -1;
|
|
}
|
|
}
|
|
}
|
|
}
|