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

@ -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)