Skip to content

Commit

Permalink
prevent borgs unlocking eachother and robotics console (space-wizards…
Browse files Browse the repository at this point in the history
…#27888)

* 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 <[email protected]>
  • Loading branch information
deltanedas and ShadowCommander authored Jul 25, 2024
1 parent b6811d3 commit 8f6326c
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Content.Shared/Lock/LockComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
8 changes: 6 additions & 2 deletions Content.Shared/Lock/LockSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,12 @@ public bool CanToggleLock(EntityUid uid, EntityUid user, bool quiet = true)

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
Expand Down Expand Up @@ -377,4 +382,3 @@ private void LockToggled(EntityUid uid, ActivatableUIRequiresLockComponent compo
_activatableUI.CloseAll(uid);
}
}

18 changes: 18 additions & 0 deletions Content.Shared/Lock/LockingWhitelistComponent.cs
Original file line number Diff line number Diff line change
@@ -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;
}
28 changes: 28 additions & 0 deletions Content.Shared/Lock/LockingWhitelistSystem.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
17 changes: 17 additions & 0 deletions Content.Shared/Whitelist/EntityWhitelistSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ public bool IsValid(EntityWhitelist list, [NotNullWhen(true)] EntityUid? uid)
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>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
locking-whitelist-component-lock-toggle-deny = You can't toggle the lock.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@
doAfterDelay: 10
allowSelfRepair: false
- type: BorgChassis
- type: LockingWhitelist
blacklist:
components:
- BorgChassis
- RoboticsConsole
- type: WiresPanel
- type: ActivatableUIRequiresPanel
- type: NameIdentifier
Expand Down

0 comments on commit 8f6326c

Please sign in to comment.