Skip to content

Game.ServiceUpgradeSystem

Assembly:
Namespace: Game.Buildings

Type: Class

Base: GameSystemBase

Summary:
ServiceUpgradeSystem is a game system that manages installation and removal of building service upgrades. It listens for entities that have ServiceUpgrade or InstalledUpgrade data and, when upgrades are added or removed (or when owners are deleted), it updates the owner entity and upgrade entities accordingly. It uses PrefabSystem to inspect upgrade prefabs for components implementing IServiceUpgrade to determine which components should be added to or removed from owner entities. It also uses a ModificationBarrier (ModificationBarrier4) to record EntityCommandBuffer operations for structural changes.


Fields

  • private EntityQuery m_UpgradeQuery
    m_UpgradeQuery selects entities relevant to upgrade installation/removal: entities having ServiceUpgrade and Object with Created or Deleted (or Temp excluded), and also entities that have InstalledUpgrade (and Deleted) to detect owner deletion cases.

  • private EntityQuery m_UpgradePrefabQuery
    m_UpgradePrefabQuery selects prefab entities that contain ServiceUpgradeData and PrefabData. This is used as a fallback to enumerate upgrade prefab data when a prefab reference cannot be resolved directly.

  • private PrefabSystem m_PrefabSystem
    Reference to the PrefabSystem used to get PrefabBase instances and their component lists. Used to inspect upgrade and owner prefabs for archetype/component information.

  • private ModificationBarrier4 m_ModificationBarrier
    Barrier system used to create an EntityCommandBuffer to perform structural changes (add/remove components, mark Deleted) safely.

  • private TypeHandle __TypeHandle
    Compiler-generated container for cached ComponentTypeHandle/BufferTypeHandle instances used during OnUpdate. It provides an __AssignHandles method to populate those handles from the SystemState.

  • private struct TypeHandle
    Inner compiler-generated struct that contains read-only ComponentTypeHandle/BufferTypeHandle fields for Deleted, Owner, ServiceUpgrade, PrefabRef and InstalledUpgrade buffer. It helps avoid re-querying component handles every frame.


Properties

  • None (this system exposes no public properties).

Constructors

  • public ServiceUpgradeSystem()
    Default constructor. The real initialization happens in OnCreate. The constructor is marked with [Preserve] in the source to prevent stripping.

Methods

  • protected override void OnCreate()
    Initializes the system: obtains PrefabSystem and ModificationBarrier4 from the World, creates the m_UpgradeQuery and m_UpgradePrefabQuery EntityQueries, and calls RequireForUpdate(m_UpgradeQuery) so the system only runs when relevant entities exist.

  • protected override void OnUpdate()
    Main update loop. Creates an EntityCommandBuffer via the modification barrier, obtains component and buffer type handles from the cached __TypeHandle, and iterates over archetype chunks matching m_UpgradeQuery. For each chunk it:

  • If the chunk has ServiceUpgrade component, determines whether the chunk also has Deleted for the owner to decide between UpgradeRemoved and UpgradeInstalled and calls the appropriate handler for each entity in the chunk.
  • Otherwise (chunks without ServiceUpgrade but with InstalledUpgrade buffer) calls OwnerDeleted for each buffer accessor to mark upgrades as deleted when their owner is gone. The method disposes temporary arrays and completes dependencies as needed.

  • private void UpgradeInstalled(EntityCommandBuffer commandBuffer, Owner owner, PrefabRef prefabRef)
    Handles installing an upgrade onto an owner entity. It reads the upgrade prefab (via m_PrefabSystem.GetPrefab(prefabRef)), collects all component types reported by any IServiceUpgrade component on the upgrade prefab, and then adds those component types to the owner entity using the provided EntityCommandBuffer (only if the owner does not already have them).

  • private void UpgradeRemoved(EntityCommandBuffer commandBuffer, Owner owner, PrefabRef prefabRef)
    Handles removal of an upgrade. If the owner is already marked Deleted, it returns early. Otherwise it:

  • Collects component types provided by the upgrade prefab that was removed (hashSet).
  • Collects component types provided by the owner prefab and from all other installed upgrades on that owner (hashSet2).
  • Removes from the owner any component type that was provided by the removed upgrade (in hashSet) but is not provided by any remaining upgrade or the owner prefab (not in hashSet2) using the EntityCommandBuffer. The method uses a fallback by enumerating m_UpgradePrefabQuery when specific prefabs cannot be immediately resolved.

  • private void OwnerDeleted(EntityCommandBuffer commandBuffer, DynamicBuffer<InstalledUpgrade> installedUpgrades)
    When an owner entity is being removed, mark each upgrade entity in its InstalledUpgrade buffer as Deleted (by adding a Deleted component) if the upgrade entity does not already have Object or Deleted components. This ensures upgrades are cleaned up when their owner no longer exists.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Compiler-generated helper used during OnCreateForCompiler; in the provided code it constructs and immediately disposes an EntityQueryBuilder (placeholder for generated query wiring).

  • protected override void OnCreateForCompiler()
    Compiler-time initialization: assigns queries and component handles used by the system (calls __AssignQueries and __TypeHandle.__AssignHandles).

  • private void TypeHandle.__AssignHandles(ref SystemState state)
    (Called from OnCreateForCompiler) Populates the ComponentTypeHandle and BufferTypeHandle fields for Deleted, Owner, ServiceUpgrade, PrefabRef and InstalledUpgrade (read-only) by querying the SystemState.


Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // ServiceUpgradeSystem obtains PrefabSystem and sets up queries automatically.
    // If you add a custom upgrade prefab, implement IServiceUpgrade on a component
    // in the prefab so the system can apply/remove archetype components to owners.

    // Example: a prefab component implementing IServiceUpgrade:
    // public class MyUpgradeComponent : ComponentBase, IServiceUpgrade
    // {
    //     public void GetUpgradeComponents(HashSet<ComponentType> set) {
    //         set.Add(ComponentType.ReadWrite<MyCustomMarker>());
    //     }
    // }
}

Notes: - The system relies on prefabs exposing IServiceUpgrade implementations to declare which components an upgrade adds to an owner. Mods that introduce new upgrades should implement IServiceUpgrade on prefab components to integrate with this system. - Structural changes are recorded to an EntityCommandBuffer via ModificationBarrier4 to be applied safely after the system job completes.