FingerprintReaderSystem.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Shared.Forensics.Components;
  3. using Content.Shared.Inventory;
  4. using Content.Shared.Popups;
  5. using JetBrains.Annotations;
  6. namespace Content.Shared.FingerprintReader;
  7. // TODO: This has a lot of overlap with the AccessReaderSystem, maybe merge them in the future?
  8. public sealed class FingerprintReaderSystem : EntitySystem
  9. {
  10. [Dependency] private readonly InventorySystem _inventory = default!;
  11. [Dependency] private readonly SharedPopupSystem _popup = default!;
  12. /// <summary>
  13. /// Checks if the given user has fingerprint access to the target entity.
  14. /// </summary>
  15. /// <param name="target">The target entity.</param>
  16. /// <param name="user">User trying to gain access.</param>
  17. /// <returns>True if access was granted, otherwise false.</returns>
  18. [PublicAPI]
  19. public bool IsAllowed(Entity<FingerprintReaderComponent?> target, EntityUid user)
  20. {
  21. if (!Resolve(target, ref target.Comp, false))
  22. return true;
  23. if (target.Comp.AllowedFingerprints.Count == 0)
  24. return true;
  25. // Check for gloves first
  26. if (!target.Comp.IgnoreGloves && TryGetBlockingGloves(user, out var gloves))
  27. {
  28. if (target.Comp.FailGlovesPopup != null)
  29. _popup.PopupClient(Loc.GetString(target.Comp.FailGlovesPopup, ("blocker", gloves)), target, user);
  30. return false;
  31. }
  32. // Check fingerprint match
  33. if (!TryComp<FingerprintComponent>(user, out var fingerprint) || fingerprint.Fingerprint == null ||
  34. !target.Comp.AllowedFingerprints.Contains(fingerprint.Fingerprint))
  35. {
  36. if (target.Comp.FailPopup != null)
  37. _popup.PopupClient(Loc.GetString(target.Comp.FailPopup), target, user);
  38. return false;
  39. }
  40. return true;
  41. }
  42. /// <summary>
  43. /// Gets the blocking gloves of a user. Gloves count as blocking if they hide fingerprints.
  44. /// </summary>
  45. /// <param name="user">Entity wearing the gloves.</param>
  46. /// <param name="blocker">The returned gloves, if they exist.</param>
  47. /// <returns>True if blocking gloves were found, otherwise False.</returns>
  48. [PublicAPI]
  49. public bool TryGetBlockingGloves(EntityUid user, [NotNullWhen(true)] out EntityUid? blocker)
  50. {
  51. blocker = null;
  52. if (_inventory.TryGetSlotEntity(user, "gloves", out var gloves) && HasComp<FingerprintMaskComponent>(gloves))
  53. {
  54. blocker = gloves;
  55. return true;
  56. }
  57. return false;
  58. }
  59. /// <summary>
  60. /// Sets the allowed fingerprints for a fingerprint reader
  61. /// </summary>
  62. [PublicAPI]
  63. public void SetAllowedFingerprints(Entity<FingerprintReaderComponent> target, HashSet<string> fingerprints)
  64. {
  65. target.Comp.AllowedFingerprints = fingerprints;
  66. Dirty(target);
  67. }
  68. /// <summary>
  69. /// Adds an allowed fingerprint to a fingerprint reader
  70. /// </summary>
  71. [PublicAPI]
  72. public void AddAllowedFingerprint(Entity<FingerprintReaderComponent> target, string fingerprint)
  73. {
  74. target.Comp.AllowedFingerprints.Add(fingerprint);
  75. Dirty(target);
  76. }
  77. /// <summary>
  78. /// Removes an allowed fingerprint from a fingerprint reader
  79. /// </summary>
  80. [PublicAPI]
  81. public void RemoveAllowedFingerprint(Entity<FingerprintReaderComponent> target, string fingerprint)
  82. {
  83. target.Comp.AllowedFingerprints.Remove(fingerprint);
  84. Dirty(target);
  85. }
  86. }