fix: Cleans up entity persistence. Generalizes Mobiles/Items/Guilds (#1528)
### Summary - [X] Fixed a bug where entity persistence was serialized out of order, causing world corruption. - [X] Fixed LastSerialized not being utilized properly and dangling references still becoming an issue. - [X] Added a new `GenericEntityPersistence<T>` type to encapsulate `ISerializable` serialization. - [X] Removing the custom logic and moved Items, Mobiles, Guilds, and Accounts to GenericEntityPersistence. - [X] Changed serialization to use the singleton pattern to reduce calling methods from stored variables.
This commit is contained in:
parent
e0225c6e59
commit
f2de2fbb77
34 changed files with 721 additions and 970 deletions
|
|
@ -19,35 +19,33 @@ using System.IO;
|
|||
|
||||
namespace Server;
|
||||
|
||||
public static class GenericPersistence
|
||||
public abstract class GenericPersistence : Persistence
|
||||
{
|
||||
public static void Register(
|
||||
string name,
|
||||
Action<IGenericWriter> serializer,
|
||||
Action<IGenericReader> deserializer,
|
||||
int priority = Persistence.DefaultPriority
|
||||
)
|
||||
private BufferWriter _saveBuffer;
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public GenericPersistence(string name, int priority) : base(priority) => Name = name;
|
||||
|
||||
public override void Serialize()
|
||||
{
|
||||
BufferWriter saveBuffer = null;
|
||||
_saveBuffer ??= new BufferWriter(true, World.SerializedTypes);
|
||||
_saveBuffer.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
void Serialize()
|
||||
{
|
||||
saveBuffer ??= new BufferWriter(true, World.SerializedTypes);
|
||||
saveBuffer.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
serializer(saveBuffer);
|
||||
}
|
||||
|
||||
void WriteSnapshot(string savePath)
|
||||
{
|
||||
string binPath = Path.Combine(savePath, name, $"{name}.bin");
|
||||
var buffer = saveBuffer!.Buffer.AsSpan(0, (int)saveBuffer.Position);
|
||||
AdhocPersistence.WriteSnapshot(new FileInfo(binPath), buffer);
|
||||
}
|
||||
|
||||
void Deserialize(string savePath, Dictionary<ulong, string> typesDb) =>
|
||||
AdhocPersistence.Deserialize(Path.Combine(savePath, name, $"{name}.bin"), deserializer);
|
||||
|
||||
Persistence.Register(name, Serialize, WriteSnapshot, Deserialize, priority);
|
||||
Serialize(_saveBuffer);
|
||||
}
|
||||
|
||||
public abstract void Serialize(IGenericWriter writer);
|
||||
|
||||
public override void WriteSnapshot(string basePath)
|
||||
{
|
||||
string binPath = Path.Combine(basePath, Name, $"{Name}.bin");
|
||||
var buffer = _saveBuffer!.Buffer.AsSpan(0, (int)_saveBuffer.Position);
|
||||
AdhocPersistence.WriteSnapshot(new FileInfo(binPath), buffer);
|
||||
}
|
||||
|
||||
public override void Deserialize(string savePath, Dictionary<ulong, string> typesDb) =>
|
||||
AdhocPersistence.Deserialize(Path.Combine(savePath, Name, $"{Name}.bin"), Deserialize);
|
||||
|
||||
public abstract void Deserialize(IGenericReader reader);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue