prevent borgs unlocking eachother and robotics console (#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 <10494922+ShadowCommander@users.noreply.github.com>
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
18
Content.Shared/Lock/LockingWhitelistComponent.cs
Normal file
18
Content.Shared/Lock/LockingWhitelistComponent.cs
Normal file
@@ -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
Content.Shared/Lock/LockingWhitelistSystem.cs
Normal file
28
Content.Shared/Lock/LockingWhitelistSystem.cs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
locking-whitelist-component-lock-toggle-deny = You can't toggle the lock.
|
||||
@@ -99,6 +99,11 @@
|
||||
doAfterDelay: 10
|
||||
allowSelfRepair: false
|
||||
- type: BorgChassis
|
||||
- type: LockingWhitelist
|
||||
blacklist:
|
||||
components:
|
||||
- BorgChassis
|
||||
- RoboticsConsole
|
||||
- type: WiresPanel
|
||||
- type: ActivatableUIRequiresPanel
|
||||
- type: NameIdentifier
|
||||
|
||||
Reference in New Issue
Block a user