Skip to content

Game.StorageSection

Assembly:
Game

Namespace:
Game.UI.InGame

Type:
class

Base:
InfoSectionBase

Summary:
StorageSection is an InfoSection used by the in-game UI to display storage-related information for buildings, companies and cargo stations. It reads resource buffers from the selected entity (or its company), categorizes resources into raw materials / processed goods / mail via UIResource helpers, computes stored amount, capacity and a human-friendly storage status (Empty, NearlyEmpty, Balanced, NearlyFull, Full). The section supports upgraded components (via TryGetComponentWithUpgrades) and warehouse-specific adjustments to storage limits. It uses Unity.Entities/ECS types and NativeList for collecting UIResource entries.


Fields

  • private Entity m_CompanyEntity
    Holds the Entity reference to the company associated with the selected building (if any). Used as an alternate source for resource buffers when a company aggregates storage.

  • private ResourcePrefabs m_ResourcePrefabs
    Cached reference to the ResourcePrefabs provided by the ResourceSystem. Used for weight/metadata lookups when categorizing resources.

Properties

  • protected override string group => "StorageSection"
    Identifier group string for the InfoSection system. Used by the base class to register/identify the section.

  • private int stored { get; set; }
    Current total stored amount accumulated from the resource buffer(s). Clamped to [0, capacity] during processing.

  • private int capacity { get; set; }
    Computed storage capacity for the current selection (considering upgrades, warehouses, cargo stations, etc.). If capacity is 0 the section will be hidden (Visible() returns false).

  • private StorageStatus status { get; set; }
    Current categorized storage status (None, Empty, NearlyEmpty, Balanced, NearlyFull, Full). Determined from stored and capacity thresholds (0%, 20%, 80%, 100%).

  • private UIResource.StorageType storageType { get; set; }
    Indicates the storage type used for categorization and tooltips (e.g., Company, Cargo, Warehouse, None). This value also contributes a tooltip key.

  • private NativeList<UIResource> rawMaterials { get; set; }
    NativeList used to collect categorized raw material UIResource entries for serialization/display. Allocated with Allocator.Persistent in OnCreate and disposed in OnDestroy.

  • private NativeList<UIResource> processedGoods { get; set; }
    NativeList used to collect categorized processed goods UIResource entries.

  • private NativeList<UIResource> mail { get; set; }
    NativeList used to collect mail UIResource entries.

Constructors

  • public StorageSection()
    Default constructor. Class is marked CompilerGenerated in the source. Initialization of NativeLists and ResourcePrefabs happens in OnCreate rather than the constructor.

Methods

  • protected override void Reset()
    Resets internal state between uses: clears the NativeLists (rawMaterials, processedGoods, mail), sets stored and capacity to 0, clears m_CompanyEntity, sets status to None and storageType to UIResource.StorageType.Company. Note: Clear() on NativeList keeps capacity; lists remain allocated.

  • [Preserve] protected override void OnCreate()
    Initializes resources required by the section:

  • Calls base.OnCreate().
  • Allocates rawMaterials, processedGoods and mail as NativeList(Allocator.Persistent).
  • Retrieves ResourcePrefabs via base.World.GetOrCreateSystemManaged().GetPrefabs() and caches it into m_ResourcePrefabs. Important: NativeLists are created with Allocator.Persistent and must be disposed in OnDestroy to avoid leaks.

  • [Preserve] protected override void OnDestroy()
    Disposes the NativeLists (rawMaterials.Dispose(), processedGoods.Dispose(), mail.Dispose()) and calls base.OnDestroy(). Always present to free persistent native memory allocated in OnCreate.

  • private bool Visible()
    Determines whether this section should be visible and computes capacity and storageType:

  • Checks for CargoTransportStationData on the selected prefab and storage upgrades via TryGetComponentWithUpgrades.
  • Resolves company prefab and its StorageLimitData when a company is present; examines IndustrialProcessData to decide whether the building handles resources (input/output weights) and adjusts capacity for warehouses via StorageLimitData.GetAdjustedLimitForWarehouse when applicable.
  • Handles attached parent entities and installed upgrades to let upgrades affect capacity.
  • Sets capacity and storageType appropriately and returns capacity > 0. Notes for modders: This method relies heavily on TryGetComponent variants (possibly utility methods provided by the base or game code) and EntityManager queries. Upgrades and property renter patterns are considered.

  • [Preserve] protected override void OnUpdate()
    Updates section visibility each frame by calling Visible() and assigning to base.visible. Minimal work here; heavier work happens in OnProcess.

  • protected override void OnProcess()
    Core processing routine that prepares data for serialization/display:

  • Attempts to read a DynamicBuffer from selectedEntity (read-only) or m_CompanyEntity.
  • Iterates buffer entries, filters by EconomyUtils.GetWeight(...) != 0 (ignores zero-weight resources), and uses UIResource.CategorizeResources(...) to append categorized UIResource instances into rawMaterials, processedGoods, and mail lists.
  • Sums amounts into stored and clamps it to [0, capacity].
  • Computes status from stored/capacity:
    • Full if stored == capacity
    • NearlyFull if stored >= capacity * 0.8
    • Balanced if stored >= capacity * 0.2
    • NearlyEmpty if stored > 0 and below 20%
    • Empty if stored <= 0
  • Ensures that resources indicated as "allowed stored" by StorageCompanyData are present in lists even if amount is 0 (so they show up in UI), by enumerating all Resource enum values and adding missing entries with amount 0.
  • Adds storageType.ToString() to base.tooltipKeys when storageType != None. Performance notes: iterating buffers and creating UIResource objects may be frequent; these steps are done on the main thread here. Keep NativeList growth and allocations in mind.

  • public override void OnWriteProperties(IJsonWriter writer)
    Serializes the section state to JSON for the UI:

  • Writes properties "stored", "capacity", "status" (enum name), and "storageType".
  • Sorts each NativeList (rawMaterials, processedGoods, mail) and writes them as arrays using writer.ArrayBegin/ArrayEnd and writer.Write(uiResource).
  • UIResource must be serializable via the writer.Write overload used. Note: Sorting is done in-place on the NativeLists prior to writing.

  • private enum StorageStatus
    Nested private enum with values: None, Empty, NearlyEmpty, Balanced, NearlyFull, Full. Used by the status property to represent the storage state.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    rawMaterials = new NativeList<UIResource>(Allocator.Persistent);
    processedGoods = new NativeList<UIResource>(Allocator.Persistent);
    mail = new NativeList<UIResource>(Allocator.Persistent);
    m_ResourcePrefabs = base.World.GetOrCreateSystemManaged<ResourceSystem>().GetPrefabs();
}

Additional notes for modders: - This class uses Unity.Entities (ECS) types: Entity, DynamicBuffer, and NativeList. Be careful to match allocation/disposal lifetimes to avoid leaks or invalid memory access. - The section depends on other game systems/utilities: EconomyUtils, UIResource, ResourceSystem, CompanyUIUtils, and various data components (StorageLimitData, StorageCompanyData, IndustrialProcessData, BuildingPropertyData, SpawnableBuildingData, BuildingData, PropertyRenter, Attached, InstalledUpgrade). When modifying or extending behavior, ensure compatibility with those systems. - The code expects Resource enum values and resource prefab weights for filtering. Resources with zero weight are ignored for display. - Preserve attribute on lifecycle methods indicates they should not be stripped by linkers; keep it when overriding or using reflection.