AotEntitySerializer Class
Definition
Namespace: Brine2D.ECS.Serialization
AOT-compatible serializer for entity worlds. Functionally equivalent to EntitySerializer but backed by an explicit ComponentTypeRegistry instead of runtime AppDomain scanning, eliminating all reflection in the hot path.
Inheritance System.Object → AotEntitySerializer
Remarks
Typical setup (non-trimmed publishing). Two calls cover everything — all built-in engine components and every component in your game assembly:
var registry = new ComponentTypeRegistry();
registry.RegisterBrineComponents(); // all engine components
registry.RegisterAllComponents(GetType().Assembly); // all your game components
var serializer = new AotEntitySerializer(registry);
await serializer.SaveWorldAsync(world, "save.json");
Trimmed / NativeAOT publishing. Use Register<T>\(JsonTypeInfo<T>\)
with a source-generated JsonSerializerContext for each of your custom component
types. Built-in engine components do not yet have a fully AOT-safe registration path
(post-1.0 roadmap item).
Behaviors are not serialized. Only components are persisted. Re-add behaviors after restore (e.g., by instantiating through a prefab) or add them manually.
Entity ID remapping. When a world is restored, every entity receives a new runtime ID assigned by the global counter. The original IDs captured in the snapshot are used only to reconstruct the parent–child hierarchy and are discarded afterwards. Any component that stores a cross-entity reference as a long entity ID will hold a stale value after restore. Re-resolve such references by entity name or tag after calling RestoreWorldFromSnapshot\(IEntityWorld, WorldSnapshot\).
Unregistered component types are skipped with a warning during both snapshot creation and restore. No exception is thrown; the entity is still created with whatever components were registered.
| Methods | |
|---|---|
| CreateSnapshot\(Entity\) | Creates a snapshot of a single entity. Components whose types are not registered in the ComponentTypeRegistry are skipped with a warning. |
| CreateWorldSnapshot\(IEntityWorld\) | Creates a snapshot of the entire world. |
| LoadAndRestoreWorldAsync\(IEntityWorld, string, CancellationToken\) | Loads and restores a world from a file. Clears the existing world first. |
| LoadWorldAsync\(string, CancellationToken\) | Loads a world snapshot from a file. |
| RestoreEntity\(IEntityWorld, EntitySnapshot\) | Restores a single entity from a snapshot. |
| RestoreWorldFromSnapshot\(IEntityWorld, WorldSnapshot\) | Restores entities from a snapshot into the world, preserving the parent–child hierarchy. Clears the existing world's entities first but leaves registered systems intact. |
| SaveWorldAsync\(IEntityWorld, string, CancellationToken\) | Saves a world snapshot to a file. |