Skip to content

Game.Serialization.DataMigration.TradeCostFixSystem

Assembly:
Game

Namespace: Game.Serialization.DataMigration

Type:
class

Base:
GameSystemBase

Summary:
TradeCostFixSystem is a data-migration ECS system used when loading older save data to correct trade-related buffers for entities that represent outside connections. When a migration tag (FormatTags.TradeCostFix) is not present on the loaded save, this system schedules a Burst-compiled IJobChunk (TradeCostFixJob) to: - Clear existing TradeCost buffers, - Only process entities that have an OutsideConnection component, - Read the Resources dynamic buffer together with the entity's PrefabRef, - Use StorageCompanyData and StorageLimitData associated with the prefab to compute and set per-resource stored amounts (special-casing OutgoingMail to 0), - Recalculate resources based on stored resource count and storage limits.

The system also calls TradeSystem.SetDefaults() after scheduling the job. The implementation contains compiler-generated wiring (TypeHandle) and helper methods used by the game's ECS codegen/runtime. Attributes of note: [CompilerGenerated] on the class, [Preserve] on lifecycle methods, and [BurstCompile] on the job.


Fields

  • private LoadGameSystem m_LoadGameSystem
    Reference to the LoadGameSystem; used to inspect the load context (to check whether the save format has the TradeCostFix tag).

  • private TradeSystem m_TradeSystem
    Reference to the TradeSystem; used to reset trade defaults after migration (calls SetDefaults()).

  • private EntityQuery m_TradeCostQuery
    EntityQuery used to find entities that have a TradeCost buffer but are not marked Created or Deleted. Used to decide if the migration job should be scheduled.

  • private TypeHandle __TypeHandle
    Compiler-generated container that holds BufferTypeHandle/ComponentTypeHandle/ComponentLookup instances required to create the job type handles. Populated via __AssignHandles.

Properties

  • None (this system does not expose public properties)

Constructors

  • public TradeCostFixSystem()
    Default constructor. The system is initialized by the ECS runtime; lifecycle setup is performed in OnCreate / OnCreateForCompiler.

Methods

  • protected override void OnCreate()
    [Preserve] Initializes the system by caching references to required managed systems (LoadGameSystem and TradeSystem) and creating the EntityQuery (m_TradeCostQuery) that finds TradeCost buffers that need migration. This method is called by the ECS runtime when the system is created.

  • protected override void OnUpdate()
    Main system update. Behavior:

  • Checks the load context (m_LoadGameSystem.context.format) for FormatTags.TradeCostFix.
  • If the tag is missing and any matching entities exist (m_TradeCostQuery is not empty), it constructs a TradeCostFixJob, populating its buffer/component handles via InternalCompilerInterface.Get* using the compiler-generated __TypeHandle, and schedules the job in parallel using JobChunkExtensions.ScheduleParallel.
  • Regardless of whether the job was scheduled, it calls m_TradeSystem.SetDefaults() to ensure trade defaults are applied.

  • private void __AssignQueries(ref SystemState state)
    Compiler helper that (currently) creates and disposes an EntityQueryBuilder. Present for codegen/compatibility; real query handle assignments are performed in OnCreate/OnCreateForCompiler.

  • protected override void OnCreateForCompiler()
    [Called by codegen] Performs compiler-time wiring: calls __AssignQueries and __TypeHandle.__AssignHandles to populate the TypeHandle with the required ECS type handles based on the current SystemState.

  • (Nested) private struct TypeHandle -> public void __AssignHandles(ref SystemState state)
    Assigns BufferTypeHandle, BufferTypeHandle, ComponentTypeHandle (read-only), and ComponentLookup (read-only) using state.Get* methods. This is used by OnCreateForCompiler to prepare handles for the job.

  • (Nested) private struct TradeCostFixJob : IJobChunk -> public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    Burst-compiled job that performs the migration for a chunk:

  • Clears the TradeCost dynamic buffer for all entities in the chunk.
  • Skips the chunk if entities do not have OutsideConnection.
  • For each entity: reads its Resources buffer, PrefabRef, then looks up StorageCompanyData and StorageLimitData using the prefab.
  • Counts the number of stored resources and distributes storage limit evenly across resources; sets each resource amount to half of that per-resource share (num2 / 2). For OutgoingMail, explicitly sets the resource to 0.
  • Uses EconomyUtils and ResourceIterator helpers for resource manipulation.

The job is scheduled in parallel from OnUpdate when needed. It is Burst-compiled for performance.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Cache systems used by the migration system
    m_LoadGameSystem = base.World.GetOrCreateSystemManaged<LoadGameSystem>();
    m_TradeSystem = base.World.GetOrCreateSystemManaged<TradeSystem>();

    // Query for entities that have TradeCost buffers and are not newly created or deleted
    m_TradeCostQuery = GetEntityQuery(ComponentType.ReadOnly<TradeCost>(),
                                     ComponentType.Exclude<Created>(),
                                     ComponentType.Exclude<Deleted>());
}

Notes: - The actual migration logic runs inside a Burst-compiled IJobChunk (TradeCostFixJob). The system uses the game's InternalCompilerInterface and generated TypeHandle to obtain safe scheduleable handles. - This system is intended for one-time migration when loading older saves; presence of the FormatTags.TradeCostFix tag in the save format will skip the job.