]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
prevent borgs unlocking eachother and robotics console (#27888)
authordeltanedas <39013340+deltanedas@users.noreply.github.com>
Thu, 25 Jul 2024 03:54:51 +0000 (03:54 +0000)
committerGitHub <noreply@github.com>
Thu, 25 Jul 2024 03:54:51 +0000 (13:54 +1000)
* prevent borgs from using locks

* e

* bru

* a

* blacklist borgs and robotics console

* frogro

* add IsAllowed to EntityWhitelistSystem

* use IsAllowed

* move thing to new LockingWhitelistSystem

* :trollface:

* review

* use renamed CheckBoth in locking whitelist

* remove unused stuff and add more to doc

* Use target entity instead to remove self check

* Rename to _whitelistSystem

* Add deny lock toggle popup

* Prevent duplicate checks and popups

* Fix wrong entity in popup when toggling another borg

* Make new event

* Update comment to user for new event

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
Co-authored-by: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
Content.Shared/Lock/LockComponent.cs
Content.Shared/Lock/LockSystem.cs
Content.Shared/Lock/LockingWhitelistComponent.cs [new file with mode: 0644]
Content.Shared/Lock/LockingWhitelistSystem.cs [new file with mode: 0644]
Content.Shared/Whitelist/EntityWhitelistSystem.cs
Resources/Locale/en-US/lock/locking-whitelist-component.ftl [new file with mode: 0644]
Resources/Prototypes/Entities/Mobs/Cyborgs/base_borg_chassis.yml

index e3e2bc6df1331573f31e66ea5ffe1e830160d41b..070d5801c1c8139caf47accd2d2d82d6c28f8a60 100644 (file)
@@ -86,6 +86,13 @@ public sealed partial class LockComponent : Component
 [ByRefEvent]
 public record struct LockToggleAttemptEvent(EntityUid User, bool Silent = false, bool Cancelled = false);
 
+/// <summary>
+/// Event raised on the user when a toggle is attempted.
+/// Can be cancelled to prevent it.
+/// </summary>
+[ByRefEvent]
+public record struct UserLockToggleAttemptEvent(EntityUid Target, bool Silent = false, bool Cancelled = false);
+
 /// <summary>
 /// Event raised on a lock after it has been toggled.
 /// </summary>
index 22a90aa0a62561d56caf37957e6ed584c78744aa..8dde7672243133690480777e28e5905cf9304070 100644 (file)
@@ -232,7 +232,12 @@ public sealed class LockSystem : EntitySystem
 
         var ev = new LockToggleAttemptEvent(user, quiet);
         RaiseLocalEvent(uid, ref ev, true);
-        return !ev.Cancelled;
+        if (ev.Cancelled)
+            return false;
+
+        var userEv = new UserLockToggleAttemptEvent(uid, quiet);
+        RaiseLocalEvent(user, ref userEv, true);
+        return !userEv.Cancelled;
     }
 
     // TODO: this should be a helper on AccessReaderSystem since so many systems copy paste it
@@ -377,4 +382,3 @@ public sealed class LockSystem : EntitySystem
         _activatableUI.CloseAll(uid);
     }
 }
-
diff --git a/Content.Shared/Lock/LockingWhitelistComponent.cs b/Content.Shared/Lock/LockingWhitelistComponent.cs
new file mode 100644 (file)
index 0000000..4ac832e
--- /dev/null
@@ -0,0 +1,18 @@
+using Content.Shared.Whitelist;
+using Robust.Shared.GameStates;
+
+namespace Content.Shared.Lock;
+
+/// <summary>
+/// Adds whitelist and blacklist for this mob to lock things.
+/// The whitelist and blacklist are checked against the object being locked, not the mob.
+/// </summary>
+[RegisterComponent, NetworkedComponent, Access(typeof(LockingWhitelistSystem))]
+public sealed partial class LockingWhitelistComponent : Component
+{
+    [DataField]
+    public EntityWhitelist? Whitelist;
+
+    [DataField]
+    public EntityWhitelist? Blacklist;
+}
diff --git a/Content.Shared/Lock/LockingWhitelistSystem.cs b/Content.Shared/Lock/LockingWhitelistSystem.cs
new file mode 100644 (file)
index 0000000..ba495fb
--- /dev/null
@@ -0,0 +1,28 @@
+using Content.Shared.Popups;
+using Content.Shared.Whitelist;
+
+namespace Content.Shared.Lock;
+
+public sealed class LockingWhitelistSystem : EntitySystem
+{
+    [Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
+    [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
+
+    public override void Initialize()
+    {
+        base.Initialize();
+
+        SubscribeLocalEvent<LockingWhitelistComponent, UserLockToggleAttemptEvent>(OnUserLockToggleAttempt);
+    }
+
+    private void OnUserLockToggleAttempt(Entity<LockingWhitelistComponent> ent, ref UserLockToggleAttemptEvent args)
+    {
+        if (_whitelistSystem.CheckBoth(args.Target, ent.Comp.Blacklist, ent.Comp.Whitelist))
+            return;
+
+        if (!args.Silent)
+            _popupSystem.PopupClient(Loc.GetString("locking-whitelist-component-lock-toggle-deny"), ent.Owner);
+
+        args.Cancelled = true;
+    }
+}
index f311946cf9873705820469d759fe2ca02129bffe..57fdb523dd4b5f6444cc7b5ec8a7079b93de9b7b 100644 (file)
@@ -23,6 +23,23 @@ public sealed class EntityWhitelistSystem : EntitySystem
         return uid != null && IsValid(list, uid.Value);
     }
 
+    /// <summary>
+    /// Checks whether a given entity is allowed by a whitelist and not blocked by a blacklist.
+    /// If a blacklist is provided and it matches then this returns false.
+    /// If a whitelist is provided and it does not match then this returns false.
+    /// If either list is null it does not get checked.
+    /// </summary>
+    public bool CheckBoth([NotNullWhen(true)] EntityUid? uid, EntityWhitelist? blacklist = null, EntityWhitelist? whitelist = null)
+    {
+        if (uid == null)
+            return false;
+
+        if (blacklist != null && IsValid(blacklist, uid))
+            return false;
+
+        return whitelist == null || IsValid(whitelist, uid);
+    }
+
     /// <summary>
     /// Checks whether a given entity satisfies a whitelist.
     /// </summary>
diff --git a/Resources/Locale/en-US/lock/locking-whitelist-component.ftl b/Resources/Locale/en-US/lock/locking-whitelist-component.ftl
new file mode 100644 (file)
index 0000000..182814c
--- /dev/null
@@ -0,0 +1 @@
+locking-whitelist-component-lock-toggle-deny = You can't toggle the lock.
index 2618207b7a90d48d2d0119160c77ff4443ba346a..f9422acd72a3f5ae465118c8ae24569b68a468d5 100644 (file)
     doAfterDelay: 10
     allowSelfRepair: false
   - type: BorgChassis
+  - type: LockingWhitelist
+    blacklist:
+      components:
+      - BorgChassis
+      - RoboticsConsole
   - type: WiresPanel
   - type: ActivatableUIRequiresPanel
   - type: NameIdentifier