Skip to content

Game.Serialization.ResetBuildOrderSystem

Assembly:
Assembly-CSharp.dll

Namespace: Game.Serialization

Type:
class

Base:
GameSystemBase

Summary:
ResetBuildOrderSystem is an ECS system used during serialization to compact and remap build-order indices used by two component types: Game.Net.BuildOrder and Game.Zones.BuildOrder. It collects all build-order ranges present on entities, sorts and merges/compacts them into a contiguous index space, creates a mapping from old indices to new indices, updates all BuildOrder components accordingly, and writes the next available build-order ID back into the GenerateEdgesSystem via its build-order value. The heavy work is done inside a Burst-compiled IJob (ResetBuildOrderJob) that operates on archetype chunks and uses Native collections for temporary data and mapping. This reduces the numeric range of build-order IDs so they are densely packed prior to serialization or other processing.


Fields

  • private GenerateEdgesSystem m_GenerateEdgesSystem
    Holds a reference to the GenerateEdgesSystem (fetched with World.GetOrCreateSystemManaged). The system's GetBuildOrder() value is read/written to update the next available build-order index after remapping.

  • private EntityQuery m_BuildOrderQuery
    An EntityQuery that matches entities containing either Game.Net.BuildOrder or Game.Zones.BuildOrder (Any). Used to gather archetype chunks that contain build-order components to be processed by the job.

  • private TypeHandle __TypeHandle
    A private helper struct instance that keeps ComponentTypeHandle and ComponentTypeHandle. These handles are assigned using the SystemState in OnCreateForCompiler and are used to access component data inside the job.

  • (nested) ResetBuildOrderJob (private, BurstCompile)
    An inner IJob that does the remapping work. It:

  • Collects OrderItem entries for each BuildOrder instance (OrderItem stores a min/max range).
  • Sorts these OrderItems.
  • Merges overlapping/adjacent ranges while building a NativeParallelHashMap mapping old start indices to new start indices.
  • Rewrites Game.Net.BuildOrder and Game.Zones.BuildOrder component values using the mapping.
  • Sets m_BuildOrder.value to the new next-available index.
  • Uses NativeArray, NativeList, NativeParallelHashMap and Allocator.Temp/TempJob; designed to run in a jobified, Burst-compiled context.

  • (nested) TypeHandle (private struct)
    Contains:

  • ComponentTypeHandle __Game_Net_BuildOrder_RW_ComponentTypeHandle
  • ComponentTypeHandle __Game_Zones_BuildOrder_RW_ComponentTypeHandle and an __AssignHandles(ref SystemState) method to initialize them.

Properties

  • This system exposes no public properties.
    The system operates via methods and internal fields; component access for the job is provided via the private TypeHandle and InternalCompilerInterface.GetComponentTypeHandle.

Constructors

  • public ResetBuildOrderSystem()
    Default constructor (preserved). No additional runtime initialization is performed here; main initialization happens in OnCreate/OnCreateForCompiler.

Methods

  • protected override void OnCreate()
    Initializes m_GenerateEdgesSystem by obtaining it from the world and sets up m_BuildOrderQuery to match entities that have either Game.Net.BuildOrder or Game.Zones.BuildOrder components. This prepares the system for querying relevant archetype chunks during OnUpdate.

  • protected override void OnUpdate()
    Main update: builds an asynchronous archetype-chunk list (m_BuildOrderQuery.ToArchetypeChunkListAsync) and schedules ResetBuildOrderJob with combined dependencies (base.Dependency + the async chunk handle). The job receives:

  • the chunk list,
  • component type handles via InternalCompilerInterface.GetComponentTypeHandle,
  • and the NativeValue build-order holder from GenerateEdgesSystem.GetBuildOrder(). After scheduling the job it disposes the chunk list with the job handle and assigns the scheduled job to base.Dependency.

  • protected override void OnCreateForCompiler()
    Compiler-time initialization helper that assigns queries and component type handles via __AssignQueries and __TypeHandle.__AssignHandles on the system state (CheckedStateRef). Used to satisfy generated/compiled expectations for ECS systems.

  • private void __AssignQueries(ref SystemState state)
    A method used to initialize entity queries at compile-time (empty in runtime because queries are set up in OnCreate). Present to satisfy the compiler-generated system pattern.

  • (nested TypeHandle) public void __AssignHandles(ref SystemState state)
    Assigns ComponentTypeHandle and ComponentTypeHandle from the provided SystemState. Called by OnCreateForCompiler so the job can access component arrays.

  • (nested ResetBuildOrderJob) public void Execute()
    The job entry point that:

  • Counts total components across chunks.
  • Allocates temporary NativeArray and NativeParallelHashMap.
  • Fills the OrderItem array from both Game.Net.BuildOrder and Game.Zones.BuildOrder components.
  • Sorts OrderItem array by m_Min.
  • Merges overlapping ranges and constructs a mapping from old start -> new start.
  • Rewrites every Game.Net.BuildOrder and Game.Zones.BuildOrder in the chunks to new compacted indices using the mapping.
  • Writes the new next available build-order ID to the NativeValue (m_BuildOrder.value). This method is Burst-compiled and designed for high performance across many entities.

Notes and caveats: - Uses Allocator.Temp and Allocator.TempJob for temporary Native containers; be careful about lifetimes and job scheduling to avoid memory errors. - The job assumes that BuildOrder component ranges are representable as uints and uses math.min/math.max to normalize Game.Net.BuildOrder ranges. - For Game.Net.BuildOrder, the job preserves direction: it recalculates start/end depending on whether original end >= start or not, so ordering direction is maintained. - The mapping only stores mappings for start indices; other indices inside a range are computed by offset from the mapped start.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Acquire the GenerateEdgesSystem reference and set up the query that finds
    // entities that have either Game.Net.BuildOrder or Game.Zones.BuildOrder.
    m_GenerateEdgesSystem = base.World.GetOrCreateSystemManaged<GenerateEdgesSystem>();
    m_BuildOrderQuery = GetEntityQuery(new EntityQueryDesc
    {
        Any = new ComponentType[2]
        {
            ComponentType.ReadOnly<Game.Net.BuildOrder>(),
            ComponentType.ReadOnly<Game.Zones.BuildOrder>()
        }
    });
}

Additional notes: - This system is intended to run as part of the game's ECS pipeline to ensure build-order indices are contiguous before serialization or other subsystems that assume compact indices. - The remapping is deterministic given the input build-order ranges and the chunk ordering used; it relies on a sorted-and-merged approach to generate compact, non-overlapping ranges.