Files
OldThink/Content.Server/Access/Systems/IdCardConsoleSystem.cs
Jabak 9f00d4b9aa Upstream (#148)
* Content changes for engine delta-state PR (#28134)

* Update GasTileOverlayState

* Update DecalGridState

* Update NavMapState

* poke

* poke2

* poke3

* Poke dem tests

* Update engine to v223.0.0 (#28239)

* Update RobustToolbox

* Improve InteractionSystem range & BUI checks (#27999)

* Improve InteractionSystem range & BUI checks

* Ghost fixes

* AAA

* Fix test

* fix nullable

* revert to broadcast event

* Fixes for eengine PR

* Ah buckle code

* )

* Update engine to v223.0.0

* Update engine to v223.1.0

* Update engine to v223.1.1

* Hotfix for crashes from bad item names (#28256)

* Fix weapon error logs (#28264)

* Update engine to v223.1.2 (#28273)

* Update RobustToolbox

* Update RobustToolbox

* Fix dud modular grenade visuals (#28265)

* Fix not networking whitelist and blacklist in storage component (#28238)

* fix id card console not updating records (#28237)

* fix id card console not updating records

* test

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>

* Remove the network tab (#28231)

It is useless and bloat, if a user needs to change these settings they are free to modify their cvars manually via the clientconfig.toml file or via the cvar command.

* antag objective issuing refactor (#28216)

* add AntagObjectives from GenericAntag

* add AntagRandomObjectives that traitor and thief can use

* make ObjectivesSystem use initial character name which AntagSelection passes

* make thief and traitor use AntagRandomObjectives

* remove now unused locale

* make sleeper agents rule use baseTraitorRule

* restore dragon rule oop

* bandaid for genericantag

* real

* typo

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>

* move nukie profile loading into its own rule (#28208)

* move profile loading out of nukeops rule

* make BaseNukeopsRule and use AntagLoadProfileRule

* untroll

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>

* fix antagSelect

* Objects ordered through cargo system shouldn't start anchored (#28115)

* Order normal space heater instead of anchored variant

* Make sure ordered objects don't spawn anchored

* Order space heater flatpack instead of a regular space heater

* Remove obsolete TODO

* Remove unnecessary name

---------

Co-authored-by: Eoin Mcloughlin <helloworld@eoinrul.es>

* Move PendingZombieComponent to Shared (#28143)

* Move PendingZombieComponent to Shared

* network me boy

---------

Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>

* Make it possible to hide full health bars below a total damage threshold (#28127)

* Make it possible to hide full health bars below a total damage threshold

* Fix not setting state

* Fix storage UI interactions (#28291)

* Fix storage UI interactions

* Add VV support

* Fix stripping not marking interactions as handled (#28292)

* Make NetworkConfigurator use BoundUserInterfaceCheckRangeEvent (#28293)

* Fix hypodarts not injecting with people that have ANY outerclothing (#28301)

Update darts.yml

* fix borg ui mispredict opening (#28305)

move borg ui junk to shared

* Add loadout group check (#28311)

Forgot to add it back in one of the rewrites.

* fix mirror server crashes (#28318)

* Remove bogus C# finalizers (#28315)

Begging people to learn how this programming language works before throwing random syntax into a file.

None of these finalizers ever worked. I also checked whether they were memory leaks and needed *proper* shutdown logic, but they're all instantiated-once UI controls that last for the entire lifetime of the program so it's probably fine.

* Cleans up some entity-related spawnmenu stuff (#28234)

* cleans up a lot of stuff in the spawnmenu

* skibidi dode

* spawners update

* Revert "spawners update"

This reverts commit bc27d9f556b29f6fb1f89cebbe0ac37e28319fd0.

* fix antag selection being evil (#28197)

* fix antag selection being evil

* fix test

* untroll the other tests

* remove role timer troll

* Allow tests to modify antag preferences

* Fix antag selection

* Misc test fixes

* Add AntagPreferenceTest

* Fix lazy mistakes

* Test cleanup

* Try stop players in lobbies from being assigned mid-round antags

* ranting

* I am going insane

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
Co-authored-by: ElectroJr <leonsfriedrich@gmail.com>

* Revert "fix mirror server crashes (#28318)"

This reverts commit bcb0e555b058a4049d0cdb32d64eaf86c35a67be.

* fix

* Update engine to 223.2.0 (#28329)

* Update RobustToolbox

* Update RobustToolbox

---------

Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com>
Co-authored-by: Tayrtahn <tayrtahn@gmail.com>
Co-authored-by: DrSmugleaf <10968691+DrSmugleaf@users.noreply.github.com>
Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com>
Co-authored-by: Vasilis <vasilis@pikachu.systems>
Co-authored-by: eoineoineoin <github@eoinrul.es>
Co-authored-by: Eoin Mcloughlin <helloworld@eoinrul.es>
Co-authored-by: Ady4ik <141335742+Ady4ik@users.noreply.github.com>
Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
Co-authored-by: Mr. 27 <45323883+Dutch-VanDerLinde@users.noreply.github.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
Co-authored-by: Flareguy <78941145+Flareguy@users.noreply.github.com>
Co-authored-by: ElectroJr <leonsfriedrich@gmail.com>
Co-authored-by: Kara <lunarautomaton6@gmail.com>
2024-10-22 20:47:03 +03:00

246 lines
9.7 KiB
C#

using Content.Server.StationRecords.Systems;
using Content.Shared.Access.Components;
using Content.Shared.Access.Systems;
using Content.Shared.Administration.Logs;
using Content.Shared.Database;
using Content.Shared.Roles;
using Content.Shared.StationRecords;
using Content.Shared.StatusIcon;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.Containers;
using Robust.Shared.Prototypes;
using System.Linq;
using static Content.Shared.Access.Components.IdCardConsoleComponent;
using Content.Shared.Access;
namespace Content.Server.Access.Systems;
[UsedImplicitly]
public sealed class IdCardConsoleSystem : SharedIdCardConsoleSystem
{
[Dependency] private readonly IPrototypeManager _prototype = default!;
[Dependency] private readonly StationRecordsSystem _record = default!;
[Dependency] private readonly UserInterfaceSystem _userInterface = default!;
[Dependency] private readonly AccessReaderSystem _accessReader = default!;
[Dependency] private readonly AccessSystem _access = default!;
[Dependency] private readonly IdCardSystem _idCard = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<IdCardConsoleComponent, WriteToTargetIdMessage>(OnWriteToTargetIdMessage);
// one day, maybe bound user interfaces can be shared too.
SubscribeLocalEvent<IdCardConsoleComponent, ComponentStartup>(UpdateUserInterface);
SubscribeLocalEvent<IdCardConsoleComponent, EntInsertedIntoContainerMessage>(UpdateUserInterface);
SubscribeLocalEvent<IdCardConsoleComponent, EntRemovedFromContainerMessage>(UpdateUserInterface);
}
private void OnWriteToTargetIdMessage(EntityUid uid, IdCardConsoleComponent component, WriteToTargetIdMessage args)
{
if (args.Actor is not { Valid: true } player)
return;
TryWriteToTargetId(uid, args.FullName, args.JobTitle, args.AccessList, args.JobPrototype, args.SelectedIcon, player, component);
UpdateUserInterface(uid, component, args);
}
private void UpdateUserInterface(EntityUid uid, IdCardConsoleComponent component, EntityEventArgs args)
{
if (!component.Initialized)
return;
var privilegedIdName = string.Empty;
List<ProtoId<AccessLevelPrototype>>? possibleAccess = null;
if (component.PrivilegedIdSlot.Item is { Valid: true } item)
{
privilegedIdName = EntityManager.GetComponent<MetaDataComponent>(item).EntityName;
possibleAccess = _accessReader.FindAccessTags(item).ToList();
}
IdCardConsoleBoundUserInterfaceState newState;
// this could be prettier
if (component.TargetIdSlot.Item is not { Valid: true } targetId)
{
newState = new IdCardConsoleBoundUserInterfaceState(
component.PrivilegedIdSlot.HasItem,
PrivilegedIdIsAuthorized(uid, component),
false,
null,
null,
null,
possibleAccess,
string.Empty,
privilegedIdName,
string.Empty,
"JobIconNoId"); // WD EDIt
}
else
{
var targetIdComponent = EntityManager.GetComponent<IdCardComponent>(targetId);
var targetAccessComponent = EntityManager.GetComponent<AccessComponent>(targetId);
var jobProto = new ProtoId<AccessLevelPrototype>(string.Empty);
var jobIcon = targetIdComponent.JobIcon; //WD-EDIT
if (TryComp<StationRecordKeyStorageComponent>(targetId, out var keyStorage)
&& keyStorage.Key is {} key
&& _record.TryGetRecord<GeneralStationRecord>(key, out var record))
{
jobProto = record.JobPrototype;
Dirty(targetId, targetIdComponent);
}
newState = new IdCardConsoleBoundUserInterfaceState(
component.PrivilegedIdSlot.HasItem,
PrivilegedIdIsAuthorized(uid, component),
true,
targetIdComponent.FullName,
targetIdComponent.JobTitle,
targetAccessComponent.Tags.ToList(),
possibleAccess,
jobProto,
privilegedIdName,
Name(targetId),
jobIcon
);
}
_userInterface.SetUiState(uid, IdCardConsoleUiKey.Key, newState);
}
/// <summary>
/// Called whenever an access button is pressed, adding or removing that access from the target ID card.
/// Writes data passed from the UI into the ID stored in <see cref="IdCardConsoleComponent.TargetIdSlot"/>, if present.
/// </summary>
/// WD-INFO: Also called when icon is changed, to update it on ID.
private void TryWriteToTargetId(
EntityUid uid,
string newFullName,
string newJobTitle,
List<ProtoId<AccessLevelPrototype>> newAccessList,
ProtoId<AccessLevelPrototype> newJobProto,
string? newJobIcon,
EntityUid player,
IdCardConsoleComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
if (component.TargetIdSlot.Item is not { Valid: true } targetId || !PrivilegedIdIsAuthorized(uid, component))
return;
_idCard.TryChangeFullName(targetId, newFullName, player: player);
_idCard.TryChangeJobTitle(targetId, newJobTitle, player: player);
// WD EDIT START
if (_prototype.TryIndex<JobPrototype>(newJobProto, out var job))
newJobIcon ??= job.Icon;
if (newJobIcon != null && _prototype.TryIndex<StatusIconPrototype>(newJobIcon, out var jobIcon)) // WD EDIT END
{
_idCard.TryChangeJobIcon(targetId, jobIcon, player: player);
_idCard.TryChangeVisuals(targetId, newJobIcon); // WD
}
if (job != null)
{
_idCard.TryChangeJobDepartment(targetId, job);
}
UpdateStationRecord(uid, targetId, newFullName, newJobTitle, job, newJobIcon);
if (!newAccessList.TrueForAll(x => component.AccessLevels.Contains(x)))
{
_sawmill.Warning($"User {ToPrettyString(uid)} tried to write unknown access tag.");
return;
}
var oldTags = _access.TryGetTags(targetId) ?? new List<ProtoId<AccessLevelPrototype>>();
oldTags = oldTags.ToList();
var privilegedId = component.PrivilegedIdSlot.Item;
if (oldTags.SequenceEqual(newAccessList))
{
UpdateStationRecord(uid, targetId, newFullName, newJobTitle, job, newJobIcon); //WD-EDIT
return;
}
// I hate that C# doesn't have an option for this and don't desire to write this out the hard way.
// var difference = newAccessList.Difference(oldTags);
var difference = newAccessList.Union(oldTags).Except(newAccessList.Intersect(oldTags)).ToHashSet();
// NULL SAFETY: PrivilegedIdIsAuthorized checked this earlier.
var privilegedPerms = _accessReader.FindAccessTags(privilegedId!.Value).ToHashSet();
if (!difference.IsSubsetOf(privilegedPerms))
{
_sawmill.Warning($"User {ToPrettyString(uid)} tried to modify permissions they could not give/take!");
return;
}
var addedTags = newAccessList.Except(oldTags).Select(tag => "+" + tag).ToList();
var removedTags = oldTags.Except(newAccessList).Select(tag => "-" + tag).ToList();
_access.TrySetTags(targetId, newAccessList);
/*TODO: ECS SharedIdCardConsoleComponent and then log on card ejection, together with the save.
This current implementation is pretty shit as it logs 27 entries (27 lines) if someone decides to give themselves AA*/
_adminLogger.Add(LogType.Action, LogImpact.Medium,
$"{ToPrettyString(player):player} has modified {ToPrettyString(targetId):entity} with the following accesses: [{string.Join(", ", addedTags.Union(removedTags))}] [{string.Join(", ", newAccessList)}]");
UpdateStationRecord(uid, targetId, newFullName, newJobTitle, job, newJobIcon);
}
/// <summary>
/// Returns true if there is an ID in <see cref="IdCardConsoleComponent.PrivilegedIdSlot"/> and said ID satisfies the requirements of <see cref="AccessReaderComponent"/>.
/// </summary>
/// <remarks>
/// Other code relies on the fact this returns false if privileged Id is null. Don't break that invariant.
/// </remarks>
private bool PrivilegedIdIsAuthorized(EntityUid uid, IdCardConsoleComponent? component = null)
{
if (!Resolve(uid, ref component))
return true;
if (!TryComp<AccessReaderComponent>(uid, out var reader))
return true;
var privilegedId = component.PrivilegedIdSlot.Item;
return privilegedId != null && _accessReader.IsAllowed(privilegedId.Value, uid, reader);
}
private void UpdateStationRecord(
EntityUid uid,
EntityUid targetId,
string newFullName,
ProtoId<AccessLevelPrototype> newJobTitle,
JobPrototype? newJobProto,
string? newJobIcon) // WD EDIT
{
if (!TryComp<StationRecordKeyStorageComponent>(targetId, out var keyStorage)
|| keyStorage.Key is not { } key
|| !_record.TryGetRecord<GeneralStationRecord>(key, out var record))
{
return;
}
record.Name = newFullName;
record.JobTitle = newJobTitle;
if (newJobProto != null)
{
record.JobPrototype = newJobProto.ID;
record.JobIcon = newJobProto.Icon;
}
if (!string.IsNullOrWhiteSpace(newJobIcon))
record.JobIcon = newJobIcon;
_record.Synchronize(key);
}
}