fix: Adds AfterSerialize support. Removes BeforeSerialize support. (#1208)

### Changes
* Implements an AfterSerialize method that is executed synchronously.
* Removes `BeforeSerialize` support since it was dangerous in its current implementation.
* Moves PlayerMobile kill/virtual decay to AfterSerialize.
* Adds kill decay to after Deserialize.
This commit is contained in:
Kamron Batman 2022-10-27 23:27:22 -07:00 committed by GitHub
parent deabab575a
commit 49e6c6f2d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 132 additions and 61 deletions

View file

@ -26,6 +26,7 @@ public static class GenericPersistence
string name,
Action<IGenericWriter> serializer,
Action<IGenericReader> deserializer,
Action afterSerialize = null,
int priority = Persistence.DefaultPriority
)
{
@ -49,6 +50,6 @@ public static class GenericPersistence
void Deserialize(string savePath, Dictionary<ulong, string> typesDb) =>
AdhocPersistence.Deserialize(Path.Combine(savePath, name, $"{name}.bin"), deserializer);
Persistence.Register(name, Serialize, WriteSnapshot, Deserialize, priority);
Persistence.Register(name, Serialize, WriteSnapshot, Deserialize, afterSerialize, priority);
}
}

View file

@ -31,13 +31,17 @@ public interface ISerializable
Serial Serial { get; }
// Executed on every entity, before it's serialized.
// For example, this is used to clean up weak references and mark them dirty.
void BeforeSerialize();
void Deserialize(IGenericReader reader);
void Serialize(IGenericWriter writer);
void Delete();
// Determines if AfterSerialize should execute. This is checked on a worker thread.
bool ShouldExecuteAfterSerialize { get; }
// Executes after serialization if ShouldExecuteAfterSerialize is true. This is run on the game thread synchronously.
void AfterSerialize();
bool Deleted { get; }
void Delete();
public void InitializeSaveBuffer(byte[] buffer, ConcurrentQueue<Type> types)
{
@ -56,7 +60,12 @@ public interface ISerializable
{
SaveBuffer ??= new BufferWriter(true, types);
BeforeSerialize();
// Queue for post serialization if this entity has it enabled
// This will run AfterSerialize in the main game thread after the world is done saving
if (ShouldExecuteAfterSerialize)
{
World.EnqueueAfterSerialization(this);
}
// Clean, don't bother serializing
if (SavePosition > -1)

View file

@ -32,6 +32,7 @@ public static class Persistence
Action serializer,
Action<string> snapshotWriter,
Action<string, Dictionary<ulong, string>> deserializer,
Action afterSerialize = null,
int priority = DefaultPriority
)
{
@ -41,6 +42,7 @@ public static class Persistence
Name = name,
Priority = priority,
Serialize = serializer,
AfterSerialize = afterSerialize,
WriteSnapshot = snapshotWriter,
Deserialize = deserializer
}
@ -89,6 +91,12 @@ public static class Persistence
public static void Serialize()
{
Parallel.ForEach(_registry, entry => entry.Serialize());
// Synchronously run the AfterSerialize on the main game thread
foreach (var entry in _registry)
{
entry.AfterSerialize?.Invoke();
}
}
public static void WriteSnapshot(string path, ConcurrentQueue<Type> types)
@ -130,7 +138,10 @@ public static class Persistence
{
public string Name { get; init; }
public int Priority { get; init; }
public Action Serialize { get; init; } // Serializing to memory buffers
// Serializes to memory buffers and run in parallel
public Action Serialize { get; init; }
public Action AfterSerialize { get; init; }
public Action<string> WriteSnapshot { get; init; }
public Action<string, Dictionary<ulong, string>> Deserialize { get; init; }
}