using Content.Shared.Containers.ItemSlots; using Robust.Shared.Containers; using Robust.Shared.Network; namespace Content.Shared.CartridgeLoader; public abstract class SharedCartridgeLoaderSystem : EntitySystem { public const string InstalledContainerId = "program-container"; [Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!; [Dependency] private readonly SharedAppearanceSystem _appearanceSystem = default!; [Dependency] private readonly SharedContainerSystem _container = default!; [Dependency] private readonly INetManager _netMan = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnComponentInit); SubscribeLocalEvent(OnComponentRemove); SubscribeLocalEvent(OnItemInserted); SubscribeLocalEvent(OnItemRemoved); } private void OnComponentInit(EntityUid uid, CartridgeLoaderComponent loader, ComponentInit args) { _itemSlotsSystem.AddItemSlot(uid, CartridgeLoaderComponent.CartridgeSlotId, loader.CartridgeSlot); } /// /// Marks installed program entities for deletion when the component gets removed /// private void OnComponentRemove(EntityUid uid, CartridgeLoaderComponent loader, ComponentRemove args) { _itemSlotsSystem.RemoveItemSlot(uid, loader.CartridgeSlot); if (_container.TryGetContainer(uid, InstalledContainerId, out var cont)) cont.Shutdown(EntityManager, _netMan); } protected virtual void OnItemInserted(EntityUid uid, CartridgeLoaderComponent loader, EntInsertedIntoContainerMessage args) { UpdateAppearanceData(uid, loader); } protected virtual void OnItemRemoved(EntityUid uid, CartridgeLoaderComponent loader, EntRemovedFromContainerMessage args) { UpdateAppearanceData(uid, loader); } private void UpdateAppearanceData(EntityUid uid, CartridgeLoaderComponent loader) { _appearanceSystem.SetData(uid, CartridgeLoaderVisuals.CartridgeInserted, loader.CartridgeSlot.HasItem); } } /// /// Gets sent to program / cartridge entities when they get inserted or installed /// public sealed class CartridgeAddedEvent : EntityEventArgs { public readonly EntityUid Loader; public CartridgeAddedEvent(EntityUid loader) { Loader = loader; } } /// /// Gets sent to cartridge entities when they get ejected /// public sealed class CartridgeRemovedEvent : EntityEventArgs { public readonly EntityUid Loader; public CartridgeRemovedEvent(EntityUid loader) { Loader = loader; } } /// /// Gets sent to program / cartridge entities when they get activated /// /// /// Don't update the programs ui state in this events listener /// public sealed class CartridgeActivatedEvent : EntityEventArgs { public readonly EntityUid Loader; public CartridgeActivatedEvent(EntityUid loader) { Loader = loader; } } /// /// Gets sent to program / cartridge entities when they get deactivated /// public sealed class CartridgeDeactivatedEvent : EntityEventArgs { public readonly EntityUid Loader; public CartridgeDeactivatedEvent(EntityUid loader) { Loader = loader; } } /// /// Gets sent to program / cartridge entities when the ui is ready to be updated by the cartridge. /// /// /// This is used for the initial ui state update because updating the ui in the activate event doesn't work /// public sealed class CartridgeUiReadyEvent : EntityEventArgs { public readonly EntityUid Loader; public CartridgeUiReadyEvent(EntityUid loader) { Loader = loader; } }