From edb33cacc4694d4b473857d6f22e8d8ddf501632 Mon Sep 17 00:00:00 2001 From: c4llv07e <38111072+c4llv07e@users.noreply.github.com> Date: Wed, 26 Jul 2023 05:34:08 +0000 Subject: [PATCH] Makes AccessReaderComponent support containers (#17927) --- .../Components/AccessReaderComponent.cs | 8 ++++ .../Access/Systems/AccessReaderSystem.cs | 46 +++++++++++++++++-- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/Content.Shared/Access/Components/AccessReaderComponent.cs b/Content.Shared/Access/Components/AccessReaderComponent.cs index 94aec90355..c3ed9eeb17 100644 --- a/Content.Shared/Access/Components/AccessReaderComponent.cs +++ b/Content.Shared/Access/Components/AccessReaderComponent.cs @@ -37,6 +37,14 @@ public sealed class AccessReaderComponent : Component /// [DataField("accessKeys")] public HashSet AccessKeys = new(); + + + /// + /// The name of the container in which additional + /// AccessReaderComponents may be found. + /// + [DataField("containerAccessProvider")] + public string? ContainerAccessProvider = null; } [Serializable, NetSerializable] diff --git a/Content.Shared/Access/Systems/AccessReaderSystem.cs b/Content.Shared/Access/Systems/AccessReaderSystem.cs index 01eb4eb7e6..f01ec061d7 100644 --- a/Content.Shared/Access/Systems/AccessReaderSystem.cs +++ b/Content.Shared/Access/Systems/AccessReaderSystem.cs @@ -6,6 +6,7 @@ using Content.Shared.Emag.Systems; using Content.Shared.PDA; using Content.Shared.Access.Components; using Content.Shared.DeviceLinking.Events; +using Robust.Shared.Containers; using Robust.Shared.Prototypes; using Content.Shared.Hands.EntitySystems; using Content.Shared.StationRecords; @@ -17,6 +18,7 @@ public sealed class AccessReaderSystem : EntitySystem { [Dependency] private readonly InventorySystem _inventorySystem = default!; [Dependency] private readonly SharedHandsSystem _handsSystem = default!; + [Dependency] private readonly SharedContainerSystem _containerSystem = default!; public override void Initialize() { @@ -60,20 +62,54 @@ public sealed class AccessReaderSystem : EntitySystem Dirty(reader); } + /// + /// Finds all AccessReaderComponents in the container of the + /// required entity. + /// + /// The entity to search for a container + private bool FindAccessReadersInContainer(EntityUid target, AccessReaderComponent accessReader, out List result) + { + result = new(); + if (accessReader.ContainerAccessProvider == null) + return false; + + if (!_containerSystem.TryGetContainer(target, accessReader.ContainerAccessProvider, out var container)) + return false; + + foreach (var entity in container.ContainedEntities) + { + if (TryComp(entity, out var entityAccessReader)) + result.Add(entityAccessReader); + } + + return result.Any(); + } + /// /// Searches the source for access tags - /// then compares it with the targets readers access list to see if it is allowed. + /// then compares it with the all targets accesses to see if it is allowed. /// /// The entity that wants access. /// The entity to search for an access reader /// Optional reader from the target entity public bool IsAllowed(EntityUid source, EntityUid target, AccessReaderComponent? reader = null) { - if (!Resolve(target, ref reader, false)) - return true; - return IsAllowed(source, reader); - } + if (!Resolve(target, ref reader, false)) + return true; + if (FindAccessReadersInContainer(target, reader, out var accessReaderList)) + { + foreach (var access in accessReaderList) + { + if (IsAllowed(source, access)) + return true; + } + + return false; + } + + return IsAllowed(source, reader); + } /// /// Searches the given entity for access tags /// then compares it with the readers access list to see if it is allowed.