Skip to content

Game.InitializeObsoleteSystem

Assembly: Game
Namespace: Game.Serialization

Type: class

Base: GameSystemBase

Summary:
Initializes and upgrades entities using obsolete prefab/component layouts to the newer archetype-based representation used by the game's ECS. This system detects entities that still use legacy components (ObjectData, NetData, AggregateNetData, NetLaneArchetypeData, AreaData, etc.), assigns appropriate new archetypes, fills missing default buffer entries (e.g., SubMesh, NetGeometrySection), sets default rendering/lod values, initializes net/aggregate colors, clears deprecated lane flags, and maps Area types to the proper area archetypes. Work is done in a Burst-compiled IJobChunk (InitializeObsoleteJob) scheduled in parallel. The system also temporarily instantiates PrefabBase ScriptableObjects to extract archetype component lists when creating EntityArchetypes, then destroys those instances after use.


Fields

  • private EntityQuery m_ObsoleteQuery
    Query matching entities that contain obsolete data components (ObjectData, NetData, AggregateNetData, NetLaneArchetypeData, AreaData) and excludes PrefabData (disabled). Required for the system to run.

  • private EntityQuery m_MeshSettingsQuery
    Query used to fetch the singleton MeshSettingsData (used to supply default "missing" mesh/section in the job).

  • private HashSet<ComponentType> m_ArchetypeComponents
    Temporary set used when constructing new EntityArchetypes from PrefabBase archetype component lists.

  • private Dictionary<Type, PrefabBase> m_PrefabInstances
    Cache of created ScriptableObject instances (PrefabBase derivatives) used to gather archetype components for creating EntityArchetypes. Instances are destroyed after use.

  • private TypeHandle __TypeHandle
    Struct holding ComponentTypeHandle and BufferTypeHandle fields used by the job. Populated via __AssignHandles and bound to the system's state.


Properties

  • (none exposed)

Constructors

  • public InitializeObsoleteSystem()
    Default constructor. Marked with [Preserve] attribute in the source.

Methods

  • protected override void OnCreate()
    Creates the m_ObsoleteQuery and m_MeshSettingsQuery and registers the obsolete query as a requirement for update (RequireForUpdate). This ensures the system only runs when there are entities matching the obsolete components.

  • protected override void OnUpdate()
    Main update logic:

  • Initializes temporary structures (m_ArchetypeComponents and m_PrefabInstances).
  • Builds EntityArchetypes for various prefab types (ObjectPrefab, StaticObjectPrefab, NetGeometryPrefab for nodes/edges, NetAggregatePrefab, Lot/District/MapTile/Space/Surface prefabs, and several NetLanePrefab variants).
  • Constructs a NetLaneArchetypeData struct containing lane archetype references (area/edge/node, slave/master, etc.).
  • Destroys the temporary PrefabBase instances created during archetype collection to avoid leaving ScriptableObjects in memory.
  • Schedules the Burst-compiled InitializeObsoleteJob (IJobChunk) in parallel using JobChunkExtensions.ScheduleParallel, passing the component/buffer handles, singleton mesh settings, and the newly created archetypes and archetype-data. The job iterates matching chunk entities and performs the actual migration/initialization per-entity.
  • Attaches the returned JobHandle to base.Dependency.

  • private T GetPrefabInstance<T>() where T : PrefabBase
    Creates (and caches) a ScriptableObject instance of the requested PrefabBase-derived type using ScriptableObject.CreateInstance(), or returns the cached instance if already created. Instances are stored in m_PrefabInstances.

  • private EntityArchetype GetArchetype<T>() where T : PrefabBase
    Creates an EntityArchetype for the given prefab type by:

  • Obtaining (and caching) a prefab instance via GetPrefabInstance().
  • Calling the prefab instance's GetArchetypeComponents to populate m_ArchetypeComponents.
  • Adding Created and Updated component types to the set.
  • Creating and returning an archetype from the EntityManager using the collected component types.

  • private EntityArchetype GetArchetype<T, TComponentType>() where T : PrefabBase
    Overload of GetArchetype that ensures a specific component type is present on the archetype (adds TComponentType to the component set) before collecting prefab components, then returns the created archetype. Used for archetypes that must include a particular marker/component (e.g., Area, Lane, Edge, Node, etc.).

  • private EntityArchetype GetArchetype<T, TComponentType1, TComponentType2>() where T : PrefabBase
    Same as above, but adds two explicit component types to the archetype.

  • private EntityArchetype GetArchetype<T, TComponentType1, TComponentType2, TComponentType3>() where T : PrefabBase
    Same as above, but adds three explicit component types to the archetype.

  • private void __AssignQueries(ref SystemState state)
    Compiler-generated helper invoked from OnCreateForCompiler. In this class it creates an EntityQueryBuilder instance and immediately disposes it; primarily a placeholder used by the source generation/compilation pattern.

  • protected override void OnCreateForCompiler()
    Called by generated code path / compiler-time wiring: calls __AssignQueries and __TypeHandle.__AssignHandles to bind handles to the SystemState.

  • private struct TypeHandle
    Holds ComponentTypeHandle and BufferTypeHandle fields for all components/buffers accessed by the job (StackData, AreaGeometryData, ObjectData, MovingObjectData, ObjectGeometryData, NetData, NetGeometryData, AggregateNetData, NetNameData, NetArrowData, NetLaneData, NetLaneArchetypeData, AreaData, BufferTypeHandle, BufferTypeHandle). Contains an __AssignHandles(ref SystemState) method which obtains the runtime ComponentTypeHandle/BufferTypeHandle values from the provided SystemState (via GetComponentTypeHandle / GetBufferTypeHandle).

  • private struct InitializeObsoleteJob : IJobChunk (Burst compiled)
    Burst-compiled job that runs per chunk and performs the actual initialization/migration. Key behaviors:

  • Obtains NativeArray views and BufferAccessors for components/buffers present in the chunk.
  • Iterates entities via a ChunkEntityEnumerator to respect enabled masks.
  • For ObjectData and ObjectGeometryData:
    • Assigns either m_ObjectGeometryArchetype or m_ObjectArchetype to ObjectData.m_Archetype depending on whether ObjectGeometryData is present.
    • If ObjectGeometryData is present, sets ObjectGeometryData.m_MinLod based on RenderingUtils.CalculateLodLimit(...) using the bounds size, and sets m_Layers to MeshLayer.Default.
    • If MovingObjectData present, sets its m_StoppedArchetype to the object geometry archetype.
  • Ensures SubMesh dynamic buffers are populated with one SubMesh entry if empty, using MeshSettingsData.m_MissingObjectMesh; marks SubMeshFlags.IsStackMiddle if StackData is present.
  • For NetData and NetGeometryData:
    • Assigns node/edge archetypes (m_NetGeometryNodeArchetype / m_NetGeometryEdgeArchetype / m_NetNodeCompositionArchetype / m_NetEdgeCompositionArchetype).
    • Ensures NetGeometrySection buffer has an entry pointing to m_MeshSettingsData.m_MissingNetSection when empty.
  • For AggregateNetData:
    • Assigns m_NetAggregateArchetype.
    • If NetNameData exists, sets default colors (m_Color and m_SelectedColor).
    • If NetArrowData exists, sets default road/track colors (m_RoadColor, m_TrackColor).
  • For NetLaneData:
    • Clears LaneFlags.PseudoRandom on m_Flags and writes the provided m_NetLaneArchetypeData to the NetLaneArchetypeData component.
  • For AreaData and AreaGeometryData:
    • Maps AreaGeometryData.m_Type to appropriate Area archetypes (Lot, District, MapTile, Space, Surface).

The job accepts as inputs the various ComponentTypeHandle / BufferTypeHandle fields, MeshSettingsData singleton, multiple EntityArchetype references, and a NetLaneArchetypeData struct. It uses an EntityCommandBuffer.ParallelWriter field in the struct (present but not used in the shown Execute implementation).


Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Creates a query that triggers this system when entities with obsolete prefab data exist
    m_ObsoleteQuery = GetEntityQuery(new EntityQueryDesc {
        Any = new ComponentType[] {
            ComponentType.ReadOnly<ObjectData>(),
            ComponentType.ReadOnly<NetData>(),
            ComponentType.ReadOnly<AggregateNetData>(),
            ComponentType.ReadOnly<NetLaneArchetypeData>(),
            ComponentType.ReadOnly<AreaData>()
        },
        Disabled = new ComponentType[] { ComponentType.ReadOnly<PrefabData>() }
    });
    m_MeshSettingsQuery = GetEntityQuery(ComponentType.ReadOnly<MeshSettingsData>());
    RequireForUpdate(m_ObsoleteQuery);
}

[Preserve]
protected override void OnUpdate()
{
    // OnUpdate builds archetypes from PrefabBase types, constructs NetLaneArchetypeData,
    // then schedules the Burst IJobChunk (InitializeObsoleteJob) to perform migration.
    // The job uses the MeshSettingsData singleton and the created archetypes.
    base.OnUpdate();
}

Notes: - The heavy lifting is done inside the Burst-compiled InitializeObsoleteJob which operates on chunks in parallel. Be careful when modifying component layouts or archetype creation logic—ensure the job receives the correct ComponentTypeHandle/BufferTypeHandle binding. - Prefab ScriptableObjects are transiently created and destroyed in OnUpdate to extract archetype component lists; avoid depending on those instances elsewhere.