Files
tbd-station-14/Content.Server/CartridgeLoader/Cartridges/CrewManifestCartridgeSystem.cs
Pieter-Jan Briers 68ce53ae17 Random spontaneous cleanup PR (#25131)
* Use new Subs.CVar helper

Removes manual config OnValueChanged calls, removes need to remember to manually unsubscribe.

This both reduces boilerplate and fixes many issues where subscriptions weren't removed on entity system shutdown.

* Fix a bunch of warnings

* More warning fixes

* Use new DateTime serializer to get rid of ISerializationHooks in changelog code.

* Get rid of some more ISerializationHooks for enums

* And a little more

* Apply suggestions from code review

Co-authored-by: 0x6273 <0x40@keemail.me>

---------

Co-authored-by: 0x6273 <0x40@keemail.me>
2024-02-13 16:48:39 -05:00

96 lines
3.8 KiB
C#

using Content.Server.CrewManifest;
using Content.Server.Station.Systems;
using Content.Shared.CartridgeLoader;
using Content.Shared.CartridgeLoader.Cartridges;
using Content.Shared.CCVar;
using Robust.Shared.Configuration;
using Robust.Shared.Containers;
using Robust.Shared.Prototypes;
namespace Content.Server.CartridgeLoader.Cartridges;
public sealed class CrewManifestCartridgeSystem : EntitySystem
{
[Dependency] private readonly CartridgeLoaderSystem _cartridgeLoader = default!;
[Dependency] private readonly IConfigurationManager _configManager = default!;
[Dependency] private readonly CrewManifestSystem _crewManifest = default!;
[Dependency] private readonly StationSystem _stationSystem = default!;
[ValidatePrototypeId<EntityPrototype>]
private const string CartridgePrototypeName = "CrewManifestCartridge";
/// <summary>
/// Flag that shows that if crew manifest is allowed to be viewed from 'unsecure' entities,
/// which is the keys for the cartridge.
/// </summary>
private bool _unsecureViewersAllowed = true;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<CrewManifestCartridgeComponent, CartridgeMessageEvent>(OnUiMessage);
SubscribeLocalEvent<CrewManifestCartridgeComponent, CartridgeUiReadyEvent>(OnUiReady);
SubscribeLocalEvent<ProgramInstallationAttempt>(OnInstallationAttempt);
Subs.CVar(_configManager, CCVars.CrewManifestUnsecure, OnCrewManifestUnsecureChanged, true);
}
/// <summary>
/// The ui messages received here get wrapped by a CartridgeMessageEvent and are relayed from the <see cref="CartridgeLoaderSystem"/>
/// </summary>
/// <remarks>
/// The cartridge specific ui message event needs to inherit from the CartridgeMessageEvent
/// </remarks>
private void OnUiMessage(EntityUid uid, CrewManifestCartridgeComponent component, CartridgeMessageEvent args)
{
UpdateUiState(uid, GetEntity(args.LoaderUid), component);
}
/// <summary>
/// This gets called when the ui fragment needs to be updated for the first time after activating
/// </summary>
private void OnUiReady(EntityUid uid, CrewManifestCartridgeComponent component, CartridgeUiReadyEvent args)
{
UpdateUiState(uid, args.Loader, component);
}
private void UpdateUiState(EntityUid uid, EntityUid loaderUid, CrewManifestCartridgeComponent? component)
{
if (!Resolve(uid, ref component))
return;
var owningStation = _stationSystem.GetOwningStation(uid);
if (owningStation is null)
return;
var (stationName, entries) = _crewManifest.GetCrewManifest(owningStation.Value);
var state = new CrewManifestUiState(stationName, entries);
_cartridgeLoader.UpdateCartridgeUiState(loaderUid, state);
}
private void OnInstallationAttempt(ref ProgramInstallationAttempt args)
{
if (args.Prototype == CartridgePrototypeName && !_unsecureViewersAllowed)
args.Cancelled = true;
}
private void OnCrewManifestUnsecureChanged(bool unsecureViewersAllowed)
{
_unsecureViewersAllowed = unsecureViewersAllowed;
var allCartridgeLoaders = AllEntityQuery<CartridgeLoaderComponent, ContainerManagerComponent>();
while (allCartridgeLoaders.MoveNext(out var loaderUid, out var comp, out var cont))
{
if (_unsecureViewersAllowed)
{
_cartridgeLoader.InstallProgram(loaderUid, CartridgePrototypeName, false, comp);
return;
}
if (_cartridgeLoader.TryGetProgram<CrewManifestCartridgeComponent>(loaderUid, out var program, true, comp, cont))
_cartridgeLoader.UninstallProgram(loaderUid, program.Value, comp);
}
}
}