CompanyAndCargoFixSystem
Assembly:
Assembly-CSharp
Namespace:
Game.Serialization.DataMigration
Type:
public class CompanyAndCargoFixSystem
Base:
GameSystemBase
Summary:
A data-migration system run during game load to fix company profitability and cargo/resource data for older saves that predate the "CompanyAndCargoFix" migration tag. When the migration tag is not present and there are existing companies (Profitability components) it schedules a burst-compiled IJobChunk (ProfitabilityFixJob) that:
- Recalculates and resets company profitability state (sets Profitability.m_Profitability to 127).
- Recomputes Profitability.m_LastTotalWorth using EconomyUtils.GetCompanyTotalWorth (taking company resources, owned vehicles, layout elements and delivery truck data into account).
- Clears legacy Concrete and Timber resource counts for industrial companies that don't use the newer/expected resource types (uses IndustrialProcessData to determine applicable resources).
- Uses ResourceSystem prefabs and multiple component/buffer lookups to perform safe, parallel chunk processing.
The system uses a query for entities with Profitability (excluding Created/Deleted), obtains needed system references during OnCreate, and schedules the job in OnUpdate only when migration hasn't been applied.
Fields
-
private LoadGameSystem m_LoadGameSystem
Reference to the LoadGameSystem. Used to inspect the load context and format tags (specifically checking FormatTags.CompanyAndCargoFix) to determine whether the migration must run. -
private ResourceSystem m_ResourceSystem
Reference to the ResourceSystem used to obtain resource prefab lookups (ResourcePrefabs) required by the ProfitabilityFixJob. -
private EntityQuery m_ProfitabilityCompanyQuery
EntityQuery selecting entities that have a Profitability component and do not have Created or Deleted. This query is used to decide if the migration job should be scheduled and to schedule it across relevant chunks. -
private TypeHandle __TypeHandle
Internal container of ComponentTypeHandle/BufferTypeHandle/ComponentLookup/BufferLookup instances used to construct the job's handle/lookup fields. __TypeHandle.__AssignHandles is used to initialize these handles from the SystemState.
Properties
- No public properties exposed by this system.
Constructors
public CompanyAndCargoFixSystem()
Default constructor. The system is [Preserve] decorated at the OnCreate/OnUpdate methods, but the constructor itself just exists to allow managed instantiation by the ECS runtime.
Methods
protected override void OnCreate()
Initializes references to other systems and builds the EntityQuery used by the system:- Obtains LoadGameSystem and ResourceSystem instances from the World.
-
Creates m_ProfitabilityCompanyQuery to select Profitability components while excluding entities marked Created or Deleted.
-
protected override void OnUpdate()
Main runtime/migration logic: - Checks the load context in m_LoadGameSystem.context.format to see if the FormatTags.CompanyAndCargoFix flag is present.
- If the flag is absent and there are profit-bearing companies (m_ProfitabilityCompanyQuery not empty), constructs a ProfitabilityFixJob and fills all component/buffer/lookup handles via InternalCompilerInterface.Get* wrappers using __TypeHandle members and the CheckedStateRef.
- Passes ResourcePrefabs from m_ResourceSystem into the job.
-
Schedules the job in parallel using JobChunkExtensions.ScheduleParallel and assigns the resulting handle into base.Dependency so it executes as part of the current frame's job graph.
-
protected override void OnCreateForCompiler()
Called to prepare the system for the source-generated/compiled runtime. It: - Calls __AssignQueries to ensure any queries are created (no-op here beyond a temporary builder).
-
Calls __TypeHandle.__AssignHandles to initialize type handles from the SystemState; these are later converted into runtime ComponentTypeHandle/BufferTypeHandle/Lookup instances in OnUpdate.
-
private void __AssignQueries(ref SystemState state)
Helper used by OnCreateForCompiler to initialize any EntityQuery objects required by source-generation/compilation pathways. Here it creates and disposes a temporary EntityQueryBuilder (effectively a placeholder to satisfy compiler expectations). -
ProfitabilityFixJob (nested, [BurstCompile], implements IJobChunk)
Job fields:- ComponentTypeHandle
m_ProfitabilityType (read/write) - ComponentTypeHandle
m_PrefabRefType (read-only) - BufferTypeHandle
m_ResourcesBufType (read/write) - BufferTypeHandle
m_OwnedVehicleType (read-only) - ComponentLookup
m_ResourceDatas (read-only) - ComponentLookup
m_IndustrialProcessDatas (read-only) - ComponentLookup
m_DeliveryTrucks (read-only) - BufferLookup
m_LayoutElementBufs (read-only) - ResourcePrefabs m_ResourcePrefabs (copy of prefab set from ResourceSystem)
- ComponentTypeHandle
Job behavior (Execute): - Iterates over chunks that match the query. - For each entity in a chunk: - Sets Profitability.m_Profitability = 127 (resetting/normalizing profitability state for migration). - Computes m_LastTotalWorth via EconomyUtils.GetCompanyTotalWorth using the entity's Resources buffer and OwnedVehicle buffer plus lookups/prefabs. - Writes the modified Profitability back to the component array. - Reads the entity's PrefabRef and looks up its IndustrialProcessData. If the industrial process' inputs/outputs do not include any of the targeted resource mask ((Resource)268435584uL), then if the Resources buffer contains Concrete or Timber it sets those to zero via EconomyUtils.SetResources. This clears legacy cargo counts for company types that shouldn't have those resources anymore.
Notes: - The job is burst-compiled and scheduled in parallel across matching chunks to make the migration cost-efficient. - The job uses BufferAccessor and BufferLookup types to safely access variable-length buffers like Resources and OwnedVehicle in chunked data.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_LoadGameSystem = base.World.GetOrCreateSystemManaged<LoadGameSystem>();
m_ResourceSystem = base.World.GetOrCreateSystemManaged<ResourceSystem>();
m_ProfitabilityCompanyQuery = GetEntityQuery(ComponentType.ReadOnly<Profitability>(), ComponentType.Exclude<Created>(), ComponentType.Exclude<Deleted>());
}
Notes and implementation hints: - This system only runs its migration when loading older saves that lack the FormatTags.CompanyAndCargoFix tag. Once the migration runs, saving/loading should include the tag so it won't run again. - The real work is done inside the ProfitabilityFixJob; when modifying or extending the migration logic, prefer updating the job so the work remains burstable and parallelizable. - The system uses internal/checked state helpers (InternalCompilerInterface + __TypeHandle) — these are generated/compiled patterns typical for Unity DOTS source-generated/IL2CPP-friendly code paths; adjust carefully if refactoring.