Skip to content

Game.Serialization.SerializerSystem

Assembly:
Game

Namespace:
Game.Serialization

Type:
class

Base:
GameSystemBase

Summary:
Manages serialization and deserialization of ECS entities for save/load. It coordinates component and system serializer libraries, constructs the EntityQuery used to select entities to (de)serialize, and drives the actual serialization/deserialization work during the save/load phases (via EntitySerializer/EntityDeserializer). It also tracks the total serialized size and exposes the serializer libraries for other systems to query or modify.


Fields

  • private SaveGameSystem m_SaveGameSystem
    Reference to the SaveGameSystem used to obtain the save context when serializing.

  • private LoadGameSystem m_LoadGameSystem
    Reference to the LoadGameSystem used to obtain/set the load context when deserializing.

  • private WriteSystem m_WriteSystem
    WriteSystem used by EntitySerializer when writing data out.

  • private ReadSystem m_ReadSystem
    ReadSystem used by EntityDeserializer when reading data in.

  • private UpdateSystem m_UpdateSystem
    Used to determine the current serialization phase (SystemUpdatePhase), e.g., Serialize or Deserialize.

  • private ComponentSerializerLibrary m_ComponentSerializerLibrary
    Holds component serializers. Lazily created and re-initialized when marked dirty. Disposed in OnDestroy.

  • private SystemSerializerLibrary m_SystemSerializerLibrary
    Holds system serializers. Lazily created and re-initialized when marked dirty. Disposed in OnDestroy.

  • private EntityQuery m_Query
    EntityQuery built from a set of core component types plus any discovered serializable components. Used to select the entities that will be (de)serialized. The query excludes certain runtime-only types (NetCompositionData, EffectInstance, LivePath, Temp, Deleted).

Properties

  • public ComponentSerializerLibrary componentLibrary { get; }
    Accessor for the ComponentSerializerLibrary (returns m_ComponentSerializerLibrary). Use to inspect available component serializers.

  • public SystemSerializerLibrary systemLibrary { get; }
    Accessor for the SystemSerializerLibrary (returns m_SystemSerializerLibrary). Use to inspect available system serializers.

  • public int totalSize { get; set; }
    Holds the accumulated size (in bytes) of the serialization operation. Reset to 0 at the start of serialization/deserialization runs.

Constructors

  • public SerializerSystem()
    Default constructor. Marked with [Preserve] in source. No custom initialization beyond the base constructor; actual initialization occurs in OnCreate/OnUpdate.

Methods

  • protected override void OnCreate() : System.Void
    Initializes references to other systems via World.GetOrCreateSystemManaged() (SaveGameSystem, LoadGameSystem, WriteSystem, ReadSystem, UpdateSystem) and calls CreateQuery with an empty component list to create the initial EntityQuery.

  • protected override void OnDestroy() : System.Void
    Disposes m_ComponentSerializerLibrary and m_SystemSerializerLibrary if they exist, then calls base.OnDestroy().

  • protected override void OnUpdate() : System.Void
    Main update driver:

  • Ensures component and system serializer libraries exist; if marked dirty, initializes them.
  • Depending on m_UpdateSystem.currentPhase:
    • Serialize: creates EntitySerializer, resets totalSize, gets save context from m_SaveGameSystem.context and calls Serialize(...) using BufferFormat.CompressedZStd and including PrefabData as a ReadWrite component filter.
    • Deserialize: creates EntityDeserializer, resets totalSize, reads and updates context via Deserialize(ref context, ...). Logs serialized version and format tags. If Deserialize returns false, adjusts the context purpose for legacy load-to-new logic and recreates the context if needed. Finally assigns the possibly-updated context back to m_LoadGameSystem.context.

Both serializer/deserializer are disposed in finally blocks to ensure cleanup.

  • public void SetDirty() : System.Void
    Marks both the component and system serializer libraries as dirty (if they exist) so they'll be re-initialized on the next update. Useful when new serializers are registered at runtime.

  • private void CreateQuery(IEnumerable<ComponentType> serializableComponents) : System.Void
    Constructs the EntityQuery used for (de)serialization. Starts from a fixed set of core ReadOnly component types required by the save system (LoadedIndex, PrefabRef, ElectricityFlowNode, ElectricityFlowEdge, WaterPipeNode, WaterPipeEdge, ServiceRequest, Game.Simulation.WaterSourceData, Game.City.City, SchoolSeeker, JobSeeker, CityStatistic, ServiceBudgetData, FloodCounterData, CoordinatedMeeting, LookingForPartner, AtmosphereData, BiomeData, TimeData). Adds any additional serializable component types passed in, then creates an EntityQuery whose Any set is that combined list and whose None set excludes NetCompositionData, EffectInstance, LivePath, Temp, and Deleted.

Usage Example

// Force the serializer system to re-scan available serializers (e.g. after registering new serializers)
[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    var serializer = World.GetOrCreateSystemManaged<Game.Serialization.SerializerSystem>();
    // mark the libraries dirty so they reinitialize on next update
    serializer.SetDirty();

    // Inspect the component serializer library if needed
    var compLib = serializer.componentLibrary;
}