CatStation/Content.Shared/FingerprintReader/FingerprintReaderSystem.cs
Whatstone a88d2c5164
Cleanup: Resolve remaining PopupSystem warnings in Content.Shared (#44685)
* PopupClient to PopupEntity/Coordinates

* PopupPredictedX to PopupEntity/Cursor/Coordinates

* The rest of the damn owl

* nullability fixes

* Buckle cleanup

* cleanup, address PR comments

* ExecutionSystem: one popup method

* correct my filthy lies

* Apply most of ajcm's suggestions

* return user to SharedItemRecallSystem.cs

* Fix lingering Ensnarable broadcast popups

* targeted drain popups

* fix lingering untargeted Guardian popups

I read the comment on GuardianHostRecall.  Oops.

* target stethoscope popups

* fix the PDA vibration popup

* Fix target for encryption key removal popup

* anomaly scanner: args.User, not uid

* disposal unit comment, bcast pda vibration popup
2026-07-16 21:54:33 +00:00

137 lines
5 KiB
C#

using System.Diagnostics.CodeAnalysis;
using Content.Shared.Forensics.Components;
using Content.Shared.Inventory;
using Content.Shared.Lock;
using Content.Shared.Popups;
using JetBrains.Annotations;
namespace Content.Shared.FingerprintReader;
// TODO: This has a lot of overlap with the AccessReaderSystem, maybe merge them in the future?
public sealed partial class FingerprintReaderSystem : EntitySystem
{
[Dependency] private InventorySystem _inventory = default!;
[Dependency] private SharedPopupSystem _popup = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FingerprintReaderComponent, FindAvailableLocksEvent>(OnFindAvailableLocks);
SubscribeLocalEvent<FingerprintReaderComponent, CheckUserHasLockAccessEvent>(OnCheckLockAccess);
}
private void OnFindAvailableLocks(Entity<FingerprintReaderComponent> ent, ref FindAvailableLocksEvent args)
{
args.FoundReaders |= LockTypes.Fingerprint;
}
private void OnCheckLockAccess(Entity<FingerprintReaderComponent> ent, ref CheckUserHasLockAccessEvent args)
{
// Are we looking for a fingerprint lock?
if (!args.FoundReaders.HasFlag(LockTypes.Fingerprint))
return;
// If the user has access to this lock, we pass it into the event.
if (IsAllowed(ent.Owner, args.User, out var denyReason))
args.HasAccess |= LockTypes.Fingerprint;
else
args.DenyReason = denyReason;
}
/// <summary>
/// Checks if the given user has fingerprint access to the target entity.
/// </summary>
/// <param name="target">The target entity.</param>
/// <param name="user">User trying to gain access.</param>
/// <param name="showPopup">Whether to display a popup with the reason you are not allowed to access this.</param>
/// <param name="denyReason">The reason why access was denied.</param>
/// <param name="checkGloves">Allows bypassing glove check regardless of the component's IgnoreGloves param.</param>
/// <returns>True if access was granted, otherwise false.</returns>
// TODO: Remove showPopup, just keeping it here for backwards compatibility while I refactor mail
[PublicAPI]
public bool IsAllowed(Entity<FingerprintReaderComponent?> target, EntityUid user, [NotNullWhen(false)] out string? denyReason, bool showPopup = true, bool checkGloves = true)
{
denyReason = null;
if (!Resolve(target, ref target.Comp, false))
return true;
if (target.Comp.AllowedFingerprints.Count == 0)
return true;
// Check for gloves first
if (checkGloves && !target.Comp.IgnoreGloves && TryGetBlockingGloves(user, out var gloves))
{
denyReason = Loc.GetString("fingerprint-reader-fail-gloves", ("blocker", gloves));
if (showPopup)
_popup.PopupEntity(denyReason, target, user);
return false;
}
// Check fingerprint match
if (!TryComp<FingerprintComponent>(user, out var fingerprint) || fingerprint.Fingerprint == null ||
!target.Comp.AllowedFingerprints.Contains(fingerprint.Fingerprint))
{
denyReason = Loc.GetString("fingerprint-reader-fail");
if (showPopup)
_popup.PopupEntity(denyReason, target, user);
return false;
}
return true;
}
/// <summary>
/// Gets the blocking gloves of a user. Gloves count as blocking if they hide fingerprints.
/// </summary>
/// <param name="user">Entity wearing the gloves.</param>
/// <param name="blocker">The returned gloves, if they exist.</param>
/// <returns>True if blocking gloves were found, otherwise False.</returns>
[PublicAPI]
public bool TryGetBlockingGloves(EntityUid user, [NotNullWhen(true)] out EntityUid? blocker)
{
blocker = null;
if (_inventory.TryGetSlotEntity(user, "gloves", out var gloves) && HasComp<FingerprintMaskComponent>(gloves))
{
blocker = gloves;
return true;
}
return false;
}
/// <summary>
/// Sets the allowed fingerprints for a fingerprint reader
/// </summary>
[PublicAPI]
public void SetAllowedFingerprints(Entity<FingerprintReaderComponent> target, HashSet<string> fingerprints)
{
target.Comp.AllowedFingerprints = fingerprints;
Dirty(target);
}
/// <summary>
/// Adds an allowed fingerprint to a fingerprint reader
/// </summary>
[PublicAPI]
public void AddAllowedFingerprint(Entity<FingerprintReaderComponent> target, string fingerprint)
{
target.Comp.AllowedFingerprints.Add(fingerprint);
Dirty(target);
}
/// <summary>
/// Removes an allowed fingerprint from a fingerprint reader
/// </summary>
[PublicAPI]
public void RemoveAllowedFingerprint(Entity<FingerprintReaderComponent> target, string fingerprint)
{
target.Comp.AllowedFingerprints.Remove(fingerprint);
Dirty(target);
}
}