Skip to content

Game.ProductionCompanyUISystem

Assembly: Assembly-CSharp.dll
Namespace: Game.UI.InGame

Type: class

Base: UISystemBase

Summary:
ProductionCompanyUISystem is a UI system used by the in-game production panel. It collects and exposes aggregated statistics for production companies (industrial and commercial) by resource and production level. The system: - Binds values and triggers to the UI (group "production") to provide production-company statistics and wealth values. - Uses a burst-compiled IJobChunk (MapCompanyStatisticsJob) to scan company entities and enqueue per-company info into a NativeQueue. - Aggregates per-level statistics into NativeArray structures and patches only changed values to the UI to minimize traffic. - Integrates with ResourceSystem, PrefabSystem and BudgetSystem to map resource IDs to Resource enums, lookup prefabs, and retrieve company wealth. - Maintains persistent native containers and must dispose them when destroyed.

This class is intended for internal game UI use but can be inspected or referenced by mods to understand how production-related UI data is computed and exposed.


Fields

  • private static readonly string kGroup = "production"
    Group name used for all bindings in this UI system.

  • private static readonly int kLevels = 5
    Number of production levels the UI reports (array length for level-based statistics).

  • private IBudgetSystem m_BudgetSystem
    Reference to the game's BudgetSystem (used to query company wealth by resource).

  • private ResourceSystem m_ResourceSystem
    Reference to the ResourceSystem used to iterate resources and map resource prefabs.

  • private PrefabSystem m_PrefabSystem
    Reference to PrefabSystem for resolving ResourcePrefab and other prefab data.

  • private Dictionary<string, Resource> m_ResourceIDMap
    Map from resource string ID (prefab name) to Resource enum used by the game; rebuilt on load.

  • private RawValueBinding m_ProductionCompanyInfoBinding
    Raw binding used to provide the production company info array (per-level statistics) to the UI.

  • private ValueBinding<int> m_IndustrialCompanyWealthBinding
    Value binding for industrial company wealth for the currently selected resource.

  • private ValueBinding<int> m_CommercialCompanyWealthBinding
    Value binding for commercial company wealth for the currently selected resource.

  • private NativeArray<ProductionLevelInfo> m_CachedValues
    Cached values previously sent to the UI; used to detect changes and send patches only when necessary. Allocated with Allocator.Persistent.

  • private NativeArray<ProductionLevelInfo> m_Values
    Current aggregated values computed during the update. Allocated with Allocator.Persistent.

  • private Resource m_SelectedResource
    Currently selected resource (or Resource.NoResource if none). Determines which companies are counted.

  • private NativeQueue<ProductionCompanyInfo> m_ProductionCompanyInfoQueue
    Temporary queue used by the job to enqueue per-company info; used to aggregate results on the main thread. Allocated with Allocator.Persistent.

  • private EntityQuery m_CompanyQuery
    EntityQuery used to select all companies with PropertyRenter, Employee buffer, and PrefabRef, and either IndustrialCompany or CommercialCompany components.

  • private TypeHandle __TypeHandle
    Holds ComponentTypeHandle / BufferTypeHandle / ComponentLookup instances assigned during OnCreateForCompiler to be used by the job scheduling path.

  • private struct MapCompanyStatisticsJob (nested, private)
    Burst-compiled IJobChunk struct that inspects companies matching the query and enqueues ProductionCompanyInfo entries for those whose industrial process output resource equals the selected resource.

  • private struct ProductionCompanyInfo (nested, private)
    Simple POD struct used to pass m_Industrial (bool), m_Level (int), and m_Workers (int) from the job to the main thread aggregator.

  • private struct ProductionLevelInfo (nested, private)
    Aggregated results per level: counts and worker totals for industrial and commercial companies. Implements IEquatable to allow comparisons.

  • private struct TypeHandle (nested, private)
    Container for storing and assigning component/buffer type handles and lookups used by jobs.

Properties

  • None (this system exposes no public properties).

Constructors

  • public ProductionCompanyUISystem()
    Default constructor. The heavy initialization for the system happens in OnCreate (overridden) where native containers and bindings are created.

Methods

  • protected override void OnCreate()
    Initializes the system: retrieves ResourceSystem, PrefabSystem and BudgetSystem references; builds the EntityQuery for companies; initializes native arrays and queue (persistent allocators); creates and registers UI bindings (productionCompanyInfo, industrialCompanyWealth, commercialCompanyWealth and the selectResource trigger); and builds the initial resource ID map.

Notes: - m_CachedValues and m_Values are created with length kLevels and Allocator.Persistent. - Adds a TriggerBinding(kGroup, "selectResource", OnSelectResource) so the UI can change the selected resource.

  • protected override void OnDestroy()
    Disposes persistent native containers (m_CachedValues, m_Values, m_ProductionCompanyInfoQueue) and calls base.OnDestroy.

  • protected override void OnUpdate()
    Per-frame update that:

  • If the productionCompanyInfo binding is active, calls PatchProductionCompanyInfo to recompute and patch the UI.
  • Updates industrial and commercial wealth bindings (if active) using m_BudgetSystem.GetCompanyWealth for the selected resource; writes 0 if no resource selected.

  • private void UpdateProductionCompanyInfo(IJsonWriter binder)
    Writes the default/initial JSON array for production company info with kLevels entries. This method is the "value provider" that produces the initial structure the UI expects (each entry contains industrialCompanies, industrialWorkers, commercialCompanies, commercialWorkers).

  • private void PatchProductionCompanyInfo()
    Main aggregation routine. Steps:

  • Clears the m_ProductionCompanyInfoQueue and resets m_Values to defaults.
  • If a resource is selected:
    • Prepares a MapCompanyStatisticsJob with the correct type handles and lookups (via InternalCompilerInterface.GetComponentTypeHandle / GetBufferTypeHandle / GetComponentLookup) and the selected resource.
    • Schedules the job in parallel over m_CompanyQuery, waits for completion, and drains the queue to aggregate per-level ProductionLevelInfo into m_Values.
  • Compares each entry of m_Values to m_CachedValues and calls Patch(index, fieldName, value) for any changed field, then updates the cached snapshot.

  • private void Patch(int index, string fieldName, int value)
    Helper that writes a JSON patch for one array element's field using the m_ProductionCompanyInfoBinding.PatchBegin / PatchEnd API. The patch format: [index, fieldName] -> value.

  • protected override void OnGameLoaded(Context serializationContext)
    Called when the game is loaded from serialization: rebuilds the resource ID map, clears cached/working NativeArrays and the queue.

  • private void RebuildResourceIDMap()
    Repopulates m_ResourceIDMap by iterating all resources via ResourceIterator and mapping each resource prefab's name to the Resource enum value using PrefabSystem and ResourceSystem.

  • private void OnSelectResource(string resourceID)
    Callback used by the "selectResource" trigger binding. It looks up resourceID in m_ResourceIDMap and sets m_SelectedResource accordingly; sets Resource.NoResource if not found.

  • private void __AssignQueries(ref SystemState state)
    Compiler helper used to assign queries; in this build it only constructs (and disposes) an EntityQueryBuilder temporarily.

  • protected override void OnCreateForCompiler()
    Compiler-time initialization: calls __AssignQueries and __TypeHandle.__AssignHandles to populate the saved TypeHandle instances for later job scheduling.

  • void MapCompanyStatisticsJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask) (nested job method)
    Executes per chunk, checks whether the IndustrialCompany component exists on the chunk to mark company type, reads PropertyRenter, PrefabRef and Employee buffers, uses the industrial process data and selected resource to decide whether the company produces the selected resource, and enqueues ProductionCompanyInfo entries (m_Industrial, m_Level, m_Workers) into the parallel NativeQueue.

Usage Example

// The system is created/managed by the game's world/system manager.
// UI code triggers "selectResource" with a resource prefab name (string),
// and the ProductionCompanyUISystem will update its bindings automatically.

// Example of triggering the resource selection from UI-side JSON/event:
{
  "group": "production",
  "command": "trigger",
  "name": "selectResource",
  "args": [ "Wood" ] // "Wood" must match the resource prefab name mapped by the system
}

// Example (conceptual) of what happens inside the system after selection:
[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Systems and bindings are set up here as seen in the source:
    // - m_ResourceSystem, m_PrefabSystem, m_BudgetSystem retrieved
    // - m_CachedValues / m_Values created
    // - m_ProductionCompanyInfoBinding, wealth bindings and selectResource trigger are added
    RebuildResourceIDMap();
}

Note: This documentation focuses on the public behavior and important internal mechanics relevant to modders. The system uses persistent native containers and jobs — ensure proper disposal patterns if you copy or adapt this approach.