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

@ -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; }
}