Skip to content

Game.Simulation.CountCompanyDataSystem

Assembly: Assembly-CSharp
Namespace: Game.Simulation

Type: class

Base: GameSystemBase, IDefaultSerializable, ISerializable

Summary:
CountCompanyDataSystem collects and aggregates economic data for companies (production and service companies) every update interval. It scans processing companies, reads their prefabs and work/employee buffers, computes per-resource production, demand, worker counts and capacities, and aggregates those into NativeArray "resource arrays" that other systems can read. The heavy work is done in two Burst-compiled jobs: CountCompanyDataJob (per-chunk collection) and SumJob (sums queued per-chunk results into the persistent arrays). The system is serializable and exposes getters to obtain compact views of commercial and industrial aggregates along with job dependency output so callers can safely read the arrays.


Fields

  • private ResourceSystem m_ResourceSystem
    Used to access resource prefabs and data required by the jobs (via m_ResourceSystem.GetPrefabs()).

  • private NativeQueue<CompanyDataItem> m_DataQueue
    A persistent NativeQueue used to collect per-chunk CompanyDataItem entries produced by CountCompanyDataJob; consumed by the SumJob.

  • private EntityQuery m_EconomyParameterQuery
    Query used to fetch the EconomyParameterData singleton required for production calculations.

  • private EntityQuery m_DemandParameterQuery
    Query required for update gating related to demand parameters.

  • private EntityQuery m_FreeIndustrialQuery
    (Declared but not used explicitly in OnUpdate) likely reserved for queries related to free/unused industrial buildings.

  • private EntityQuery m_IndustrialCompanyQuery
    (Declared) Query related to industrial companies (not directly used in OnUpdate in this file).

  • private EntityQuery m_StorageCompanyQuery
    (Declared) Query for storage companies (excluded from the company query used for processing companies).

  • private EntityQuery m_ProcessDataQuery
    Query that ensures IndustrialProcessData prefabs exist (required for update).

  • private EntityQuery m_CityServiceQuery
    (Declared) Query for city services (not directly used here).

  • private EntityQuery m_SpawnableQuery
    (Declared) Query for spawnable prefabs/entities.

  • [DebugWatchDeps] private JobHandle m_WriteDependencies
    Tracks the last scheduled write JobHandle for consumers to depend on (returned by getters).

  • private JobHandle m_ReadDependencies
    Accumulated read dependencies; used to combine with scheduled jobs so readers/writers are synchronized.

  • [ResourceArray][DebugWatchValue] private NativeArray<int> m_CurrentProductionWorkers
    Persistent per-resource array storing current production workers aggregated across companies.

  • [ResourceArray][DebugWatchValue] private NativeArray<int> m_MaxProductionWorkers
    Persistent per-resource array storing max production workers (capacity).

  • [ResourceArray][DebugWatchValue] private NativeArray<int> m_CurrentServiceWorkers
    Persistent per-resource array storing current service workers aggregated.

  • [ResourceArray][DebugWatchValue] private NativeArray<int> m_MaxServiceWorkers
    Persistent per-resource array storing max service worker capacity.

  • [ResourceArray][DebugWatchValue] private NativeArray<int> m_Production
    Persistent per-resource array storing aggregated production per day.

  • [ResourceArray][DebugWatchValue] private NativeArray<int> m_SalesCapacities
    Persistent per-resource array storing sales capacities (production/company capacity for selling).

  • [ResourceArray][DebugWatchValue] private NativeArray<int> m_CurrentAvailables
    Persistent per-resource array of current available service amounts (for service companies).

  • [ResourceArray][DebugWatchValue] private NativeArray<int] m_TotalAvailables
    Persistent per-resource array of total available service capacity.

  • [ResourceArray][DebugWatchValue] private NativeArray<int> m_Demand
    Persistent per-resource array storing aggregated demand (consumption) from production processes.

  • [ResourceArray][DebugWatchValue] private NativeArray<int> m_ProductionCompanies
    Persistent per-resource array counting production companies for each resource.

  • [ResourceArray][DebugWatchValue] private NativeArray<int> m_ServiceCompanies
    Persistent per-resource array counting service companies for each resource.

  • [ResourceArray][DebugWatchValue] private NativeArray<int> m_ProductionPropertyless
    Persistent per-resource array counting production companies without a property (propertyless).

  • [ResourceArray][DebugWatchValue] private NativeArray<int] m_ServicePropertyless
    Persistent per-resource array counting service companies without a property.

  • private EntityQuery m_CompanyQuery
    EntityQuery used to select processing companies (ProcessingCompany + PrefabRef + WorkProvider + Resources) while excluding storage companies, deleted and temporary entities.

  • private TypeHandle __TypeHandle
    Internal struct bundling Entity/Component type handles used to schedule CountCompanyDataJob.

  • (Nested private structs) CompanyDataItem, CountCompanyDataJob, SumJob, TypeHandle
    CompanyDataItem is the per-resource data structure enqueued by per-chunk job; CountCompanyDataJob performs per-chunk scanning and enqueues CompanyDataItem entries; SumJob dequeues and sums into the persistent NativeArrays. All are Burst-compiled where marked.


Properties

  • None (this system exposes getters via methods rather than public properties; access is controlled through job dependency outputs).

Constructors

  • public CountCompanyDataSystem()
    Default constructor. The system initializes persistent NativeArrays, the NativeQueue and sets up required queries in OnCreate (override) rather than in the constructor body.

Methods

  • public CommercialCompanyDatas GetCommercialCompanyDatas(out JobHandle deps)
    Returns a snapshot-like struct (CommercialCompanyDatas) of NativeArray references relevant to commercial/service companies. The out parameter deps is set to the system's m_WriteDependencies so callers can depend on the last write job before reading.

  • public IndustrialCompanyDatas GetIndustrialCompanyDatas(out JobHandle deps)
    Returns a struct (IndustrialCompanyDatas) containing NativeArray references for industrial/production metrics. The out JobHandle deps must be used by callers to ensure safe reading (it is the system's current m_WriteDependencies).

  • public NativeArray<int> GetProduction(out JobHandle deps)
    Returns the m_Production NativeArray and sets deps to m_WriteDependencies. Use deps to synchronize reads.

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns the update interval for this system. Implementation returns 16 (system runs every 16 frames).

  • public override int GetUpdateOffset(SystemUpdatePhase phase)
    Returns the update offset for this system. Implementation returns 1.

  • public void AddReader(JobHandle reader)
    Combine an external reader JobHandle into m_ReadDependencies so the system will wait for it when scheduling its own jobs.

  • protected override void OnGameLoaded(Context serializationContext)
    Called when a game is loaded; implementation resets m_Production to zero (calls Fill(0)).

  • [Preserve] protected override void OnCreate()
    Initializes queries, persistent NativeArrays (size = EconomyUtils.ResourceCount), and the m_DataQueue. Also acquires the ResourceSystem from the world and calls RequireForUpdate for relevant queries to gate system execution.

  • [Preserve] protected override void OnDestroy()
    Disposes all persistent NativeArrays and the NativeQueue, then calls base.OnDestroy().

  • public void SetDefaults(Context context)
    Resets all persistent NativeArrays to zero. Used when initializing default values for serialization contexts.

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes all persistent NativeArrays to the given writer in a fixed order so state can be saved.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads arrays back from reader into the persistent arrays. Handles one format nuance: if the reader format doesn't have the FishResource tag, it adjusts for one fewer resource index and zeroes the last resource slot.

  • [Preserve] protected override void OnUpdate()
    Schedules the CountCompanyDataJob in parallel across m_CompanyQuery and then schedules a SumJob that consumes the queue and sums into the persistent arrays. It sets base.Dependency to the SumJob and updates m_WriteDependencies accordingly. CountCompanyDataJob is provided with component type handles and lookups (via __TypeHandle) and with m_ResourceSystem.GetPrefabs() and the economy parameters singleton.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Internal/auto-generated helper used by compiler-time OnCreateForCompiler; in this file it creates a temporary EntityQueryBuilder and disposes it (placeholder for compiler pipeline).

  • protected override void OnCreateForCompiler()
    Auto-generated helper used in compiled builds to assign queries and component type handles into __TypeHandle.

  • (Nested private) CountCompanyDataJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    Per-chunk Burst job that: iterates entities in the chunk, checks for IndustrialProcessData on the entity's prefab, determines resource outputs, reads property/employee buffers, computes companyProductionPerDay, computes rounded consumption for inputs, accumulates per-resource temporary arrays, then enqueues a CompanyDataItem for each resource index. It allocates and disposes several temporary NativeArray buffers (per resource) per Execute call.

  • (Nested private) SumJob.Execute()
    Burst job that fills the persistent output arrays with zeros then dequeues CompanyDataItem entries from the m_DataQueue and accumulates their numeric fields into the corresponding persistent NativeArray indices.

Notes on threading and safety: - The system uses a NativeQueue with AsParallelWriter to feed per-chunk results into a single consumer job (SumJob). The SumJob runs after CountCompanyDataJob completes. - Callers that read the returned NativeArrays must use the out JobHandle deps to ensure they don't read while the system is still writing.


Usage Example

// Example: reading commercial data safely from another system or mod code
JobHandle deps;
var commercial = countCompanyDataSystem.GetCommercialCompanyDatas(out deps);

// Make sure to depend on the system's write job before reading the arrays
deps.Complete();

// now you can read arrays; for example, total available service for resource index 0:
int totalAvailableResource0 = commercial.m_TotalAvailables[0];

If you need to schedule a job that reads the arrays instead of calling Complete(), pass the returned deps as a dependency to your job (JobHandle.CombineDependencies) to avoid race conditions with CountCompanyDataSystem's write jobs.