Files
wwdpublic/Content.Server/Access/Systems/PresetIdCardSystem.cs
SimpleStation14 71299f6019 Mirror: Fix presetidcard extended access throwing (#232)
## Mirror of PR #26195: [Fix presetidcard extended access
throwing](https://github.com/space-wizards/space-station-14/pull/26195)
from <img src="https://avatars.githubusercontent.com/u/10567778?v=4"
alt="space-wizards" width="22"/>
[space-wizards](https://github.com/space-wizards)/[space-station-14](https://github.com/space-wizards/space-station-14)

###### `677fd3c6db4550431d8801203c03aa2ab5075c29`

PR opened by <img
src="https://avatars.githubusercontent.com/u/31366439?v=4"
width="16"/><a href="https://github.com/metalgearsloth">
metalgearsloth</a> at 2024-03-17 00:47:51 UTC

---

PR changed 1 files with 4 additions and 2 deletions.

The PR had the following labels:


---

<details open="true"><summary><h1>Original Body</h1></summary>

> Resolves
https://github.com/space-wizards/space-station-14/issues/25798
> 
> 🆑
> - fix: Fix ID cards sometimes not loading properly.


</details>

Co-authored-by: SimpleStation14 <Unknown>
2024-05-12 00:39:00 -04:00

91 lines
3.1 KiB
C#

using Content.Server.Access.Components;
using Content.Server.GameTicking;
using Content.Server.Station.Components;
using Content.Server.Station.Systems;
using Content.Shared.Access.Systems;
using Content.Shared.Roles;
using Content.Shared.StatusIcon;
using Robust.Shared.Prototypes;
namespace Content.Server.Access.Systems;
public sealed class PresetIdCardSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IdCardSystem _cardSystem = default!;
[Dependency] private readonly SharedAccessSystem _accessSystem = default!;
[Dependency] private readonly StationSystem _stationSystem = default!;
public override void Initialize()
{
SubscribeLocalEvent<PresetIdCardComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<RulePlayerJobsAssignedEvent>(PlayerJobsAssigned);
}
private void PlayerJobsAssigned(RulePlayerJobsAssignedEvent ev)
{
// Go over all ID cards and make sure they're correctly configured for extended access.
var query = EntityQueryEnumerator<PresetIdCardComponent>();
while (query.MoveNext(out var uid, out var card))
{
var station = _stationSystem.GetOwningStation(uid);
// If we're not on an extended access station, the ID is already configured correctly from MapInit.
if (station == null || !Comp<StationJobsComponent>(station.Value).ExtendedAccess)
return;
SetupIdAccess(uid, card, true);
SetupIdName(uid, card);
}
}
private void OnMapInit(EntityUid uid, PresetIdCardComponent id, MapInitEvent args)
{
// If a preset ID card is spawned on a station at setup time,
// the station may not exist,
// or may not yet know whether it is on extended access (players not spawned yet).
// PlayerJobsAssigned makes sure extended access is configured correctly in that case.
var station = _stationSystem.GetOwningStation(uid);
var extended = false;
// Station not guaranteed to have jobs (e.g. nukie outpost).
if (TryComp(station, out StationJobsComponent? stationJobs))
extended = stationJobs.ExtendedAccess;
SetupIdAccess(uid, id, extended);
SetupIdName(uid, id);
}
private void SetupIdName(EntityUid uid, PresetIdCardComponent id)
{
if (id.IdName == null)
return;
_cardSystem.TryChangeFullName(uid, id.IdName);
}
private void SetupIdAccess(EntityUid uid, PresetIdCardComponent id, bool extended)
{
if (id.JobName == null)
return;
if (!_prototypeManager.TryIndex(id.JobName, out JobPrototype? job))
{
Log.Error($"Invalid job id ({id.JobName}) for preset card");
return;
}
_accessSystem.SetAccessToJob(uid, job, extended);
_cardSystem.TryChangeJobTitle(uid, job.LocalizedName);
_cardSystem.TryChangeJobDepartment(uid, job);
if (_prototypeManager.TryIndex<StatusIconPrototype>(job.Icon, out var jobIcon))
{
_cardSystem.TryChangeJobIcon(uid, jobIcon);
}
}
}