* 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>
186 lines
6.1 KiB
C#
186 lines
6.1 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using Content.Shared.Item;
|
|
using Content.Shared.Tag;
|
|
|
|
namespace Content.Shared.Whitelist;
|
|
|
|
public sealed class EntityWhitelistSystem : EntitySystem
|
|
{
|
|
[Dependency] private readonly IComponentFactory _factory = default!;
|
|
[Dependency] private readonly TagSystem _tag = default!;
|
|
|
|
private EntityQuery<ItemComponent> _itemQuery;
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
_itemQuery = GetEntityQuery<ItemComponent>();
|
|
}
|
|
|
|
/// <inheritdoc cref="IsValid(Content.Shared.Whitelist.EntityWhitelist,Robust.Shared.GameObjects.EntityUid)"/>
|
|
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>
|
|
public bool IsValid(EntityWhitelist list, EntityUid uid)
|
|
{
|
|
if (list.Components != null)
|
|
EnsureRegistrations(list);
|
|
|
|
if (list.Registrations != null)
|
|
{
|
|
foreach (var reg in list.Registrations)
|
|
{
|
|
if (HasComp(uid, reg.Type))
|
|
{
|
|
if (!list.RequireAll)
|
|
return true;
|
|
}
|
|
else if (list.RequireAll)
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (list.Sizes != null && _itemQuery.TryComp(uid, out var itemComp))
|
|
{
|
|
if (list.Sizes.Contains(itemComp.Size))
|
|
return true;
|
|
}
|
|
|
|
if (list.Tags != null)
|
|
{
|
|
return list.RequireAll
|
|
? _tag.HasAllTags(uid, list.Tags)
|
|
: _tag.HasAnyTag(uid, list.Tags);
|
|
}
|
|
|
|
return list.RequireAll;
|
|
}
|
|
/// The following are a list of "helper functions" that are basically the same as each other
|
|
/// to help make code that uses EntityWhitelist a bit more readable because at the moment
|
|
/// it is quite clunky having to write out component.Whitelist == null ? true : _whitelist.IsValid(component.Whitelist, uid)
|
|
/// several times in a row and makes comparisons easier to read
|
|
|
|
/// <summary>
|
|
/// Helper function to determine if Whitelist is not null and entity is on list
|
|
/// </summary>
|
|
public bool IsWhitelistPass(EntityWhitelist? whitelist, EntityUid uid)
|
|
{
|
|
if (whitelist == null)
|
|
return false;
|
|
|
|
return IsValid(whitelist, uid);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper function to determine if Whitelist is not null and entity is not on the list
|
|
/// </summary>
|
|
public bool IsWhitelistFail(EntityWhitelist? whitelist, EntityUid uid)
|
|
{
|
|
if (whitelist == null)
|
|
return false;
|
|
|
|
return !IsValid(whitelist, uid);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper function to determine if Whitelist is either null or the entity is on the list
|
|
/// </summary>
|
|
public bool IsWhitelistPassOrNull(EntityWhitelist? whitelist, EntityUid uid)
|
|
{
|
|
if (whitelist == null)
|
|
return true;
|
|
|
|
return IsValid(whitelist, uid);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper function to determine if Whitelist is either null or the entity is not on the list
|
|
/// </summary>
|
|
public bool IsWhitelistFailOrNull(EntityWhitelist? whitelist, EntityUid uid)
|
|
{
|
|
if (whitelist == null)
|
|
return true;
|
|
|
|
return !IsValid(whitelist, uid);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper function to determine if Blacklist is not null and entity is on list
|
|
/// Duplicate of equivalent Whitelist function
|
|
/// </summary>
|
|
public bool IsBlacklistPass(EntityWhitelist? blacklist, EntityUid uid)
|
|
{
|
|
return IsWhitelistPass(blacklist, uid);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper function to determine if Blacklist is not null and entity is not on the list
|
|
/// Duplicate of equivalent Whitelist function
|
|
/// </summary>
|
|
public bool IsBlacklistFail(EntityWhitelist? blacklist, EntityUid uid)
|
|
{
|
|
return IsWhitelistFail(blacklist, uid);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper function to determine if Blacklist is either null or the entity is on the list
|
|
/// Duplicate of equivalent Whitelist function
|
|
/// </summary>
|
|
public bool IsBlacklistPassOrNull(EntityWhitelist? blacklist, EntityUid uid)
|
|
{
|
|
return IsWhitelistPassOrNull(blacklist, uid);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper function to determine if Blacklist is either null or the entity is not on the list
|
|
/// Duplicate of equivalent Whitelist function
|
|
/// </summary>
|
|
public bool IsBlacklistFailOrNull(EntityWhitelist? blacklist, EntityUid uid)
|
|
{
|
|
return IsWhitelistFailOrNull(blacklist, uid);
|
|
}
|
|
|
|
private void EnsureRegistrations(EntityWhitelist list)
|
|
{
|
|
if (list.Components == null)
|
|
return;
|
|
|
|
list.Registrations = new List<ComponentRegistration>();
|
|
foreach (var name in list.Components)
|
|
{
|
|
var availability = _factory.GetComponentAvailability(name);
|
|
if (_factory.TryGetRegistration(name, out var registration)
|
|
&& availability == ComponentAvailability.Available)
|
|
{
|
|
list.Registrations.Add(registration);
|
|
}
|
|
else if (availability == ComponentAvailability.Unknown)
|
|
{
|
|
Log.Warning($"Unknown component name {name} passed to EntityWhitelist!");
|
|
}
|
|
}
|
|
}
|
|
}
|