Move Access to ECS (#4826)

* Moved access to ecs

* Fixed tests

* Moved test to integration

* Better IoC

* Moved preset ID card

* Moved id card to ECS

* Moved access component to ECS

* Fixed pda access

* Final touches

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
Alex Evgrashin
2021-10-22 05:31:07 +03:00
committed by GitHub
parent 430485de06
commit a3f16295ea
26 changed files with 409 additions and 466 deletions

View File

@@ -1,4 +1,6 @@
using System.Collections.Generic;
using Content.Server.Access.Systems;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
@@ -9,22 +11,13 @@ namespace Content.Server.Access.Components
/// Simple mutable access provider found on ID cards and such.
/// </summary>
[RegisterComponent]
[ComponentReference(typeof(IAccess))]
public class AccessComponent : Component, IAccess
[Friend(typeof(AccessSystem))]
public class AccessComponent : Component
{
public override string Name => "Access";
[DataField("tags")]
[ViewVariables]
private readonly HashSet<string> _tags = new();
public ISet<string> Tags => _tags;
public bool IsReadOnly => false;
public void SetTags(IEnumerable<string> newTags)
{
_tags.Clear();
_tags.UnionWith(newTags);
}
public HashSet<string> Tags = new();
}
}

View File

@@ -20,13 +20,15 @@ namespace Content.Server.Access.Components
/// Stores access levels necessary to "use" an entity
/// and allows checking if something or somebody is authorized with these access levels.
/// </summary>
[PublicAPI]
[RegisterComponent]
public class AccessReader : Component
{
public override string Name => "AccessReader";
private readonly HashSet<string> _denyTags = new();
/// <summary>
/// The set of tags that will automatically deny an allowed check, if any of them are present.
/// </summary>
public HashSet<string> DenyTags = new();
/// <summary>
/// List of access lists to check allowed against. For an access check to pass
@@ -34,90 +36,6 @@ namespace Content.Server.Access.Components
/// </summary>
[DataField("access")]
[ViewVariables]
public List<HashSet<string>> AccessLists { get; } = new();
/// <summary>
/// The set of tags that will automatically deny an allowed check, if any of them are present.
/// </summary>
[ViewVariables] public ISet<string> DenyTags => _denyTags;
/// <summary>
/// Searches an <see cref="IAccess"/> in the entity itself, in its active hand or in its ID slot.
/// Then compares the found access with the configured access lists to see if it is allowed.
/// </summary>
/// <remarks>
/// If no access is found, an empty set is used instead.
/// </remarks>
/// <param name="entity">The entity to be searched for access.</param>
public bool IsAllowed(IEntity entity)
{
var tags = FindAccessTags(entity);
return IsAllowed(tags);
}
public bool IsAllowed(IAccess access)
{
return IsAllowed(access.Tags);
}
public bool IsAllowed(ICollection<string> accessTags)
{
if (_denyTags.Overlaps(accessTags))
{
// Sec owned by cargo.
return false;
}
return AccessLists.Count == 0 || AccessLists.Any(a => a.IsSubsetOf(accessTags));
}
public static ICollection<string> FindAccessTags(IEntity entity)
{
if (entity.TryGetComponent(out IAccess? accessComponent))
{
return accessComponent.Tags;
}
if (entity.TryGetComponent(out IHandsComponent? handsComponent))
{
var activeHandEntity = handsComponent.GetActiveHand?.Owner;
if (activeHandEntity != null &&
activeHandEntity.TryGetComponent(out IAccess? handAccessComponent))
{
return handAccessComponent.Tags;
}
}
else
{
return Array.Empty<string>();
}
if (entity.TryGetComponent(out InventoryComponent? inventoryComponent))
{
if (inventoryComponent.HasSlot(EquipmentSlotDefines.Slots.IDCARD) &&
inventoryComponent.TryGetSlotItem(EquipmentSlotDefines.Slots.IDCARD, out ItemComponent? item) &&
item.Owner.TryGetComponent(out IAccess? idAccessComponent)
)
{
return idAccessComponent.Tags;
}
}
return Array.Empty<string>();
}
protected override void Initialize()
{
base.Initialize();
var proto = IoCManager.Resolve<IPrototypeManager>();
foreach (var level in AccessLists.SelectMany(c => c).Union(DenyTags))
{
if (!proto.HasIndex<AccessLevelPrototype>(level))
{
Logger.ErrorS("access", $"Invalid access level: {level}");
}
}
}
public List<HashSet<string>> AccessLists = new();
}
}

View File

@@ -1,31 +0,0 @@
using System;
using System.Collections.Generic;
namespace Content.Server.Access.Components
{
/// <summary>
/// Contains access levels that can be checked to see if somebody has access with an <see cref="AccessReader"/>.
/// </summary>
public interface IAccess
{
/// <summary>
/// The set of access tags this thing has.
/// </summary>
/// <remarks>
/// This set may be read-only. Check <see cref="IsReadOnly"/> if you want to mutate it.
/// </remarks>
ISet<string> Tags { get; }
/// <summary>
/// Whether the <see cref="Tags"/> list is read-only.
/// </summary>
bool IsReadOnly { get; }
/// <summary>
/// Replaces the set of access tags we have with the provided set.
/// </summary>
/// <param name="newTags">The new access tags</param>
/// <exception cref="NotSupportedException">If this access tag list is read-only.</exception>
void SetTags(IEnumerable<string> newTags);
}
}

View File

@@ -1,77 +1,24 @@
using Content.Server.Access.Systems;
using Content.Server.PDA;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Access.Components
{
[RegisterComponent]
[Friend(typeof(IdCardSystem), typeof(PDASystem))]
public class IdCardComponent : Component
{
public override string Name => "IdCard";
/// See <see cref="UpdateEntityName"/>.
[DataField("originalOwnerName")]
private string _originalOwnerName = default!;
public string OriginalOwnerName = default!;
[DataField("fullName")]
private string? _fullName;
[ViewVariables(VVAccess.ReadWrite)]
public string? FullName
{
get => _fullName;
set
{
_fullName = value;
UpdateEntityName();
}
}
public string? FullName;
[DataField("jobTitle")]
private string? _jobTitle;
[ViewVariables(VVAccess.ReadWrite)]
public string? JobTitle
{
get => _jobTitle;
set
{
_jobTitle = value;
UpdateEntityName();
}
}
/// <summary>
/// Changes the <see cref="Entity.Name"/> of <see cref="Component.Owner"/>.
/// </summary>
/// <remarks>
/// If either <see cref="FullName"/> or <see cref="JobTitle"/> is empty, it's replaced by placeholders.
/// If both are empty, the original entity's name is restored.
/// </remarks>
private void UpdateEntityName()
{
if (string.IsNullOrWhiteSpace(FullName) && string.IsNullOrWhiteSpace(JobTitle))
{
Owner.Name = _originalOwnerName;
return;
}
var jobSuffix = string.IsNullOrWhiteSpace(JobTitle) ? string.Empty : $" ({JobTitle})";
Owner.Name = string.IsNullOrWhiteSpace(FullName)
? Loc.GetString("access-id-card-component-owner-name-job-title-text",
("originalOwnerName", _originalOwnerName),
("jobSuffix", jobSuffix))
: Loc.GetString("access-id-card-component-owner-full-name-job-title-text",
("fullName", FullName),
("jobSuffix", jobSuffix));
}
protected override void Initialize()
{
base.Initialize();
// ReSharper disable once ConstantNullCoalescingCondition
_originalOwnerName ??= Owner.Name;
UpdateEntityName();
}
public string? JobTitle;
}
}

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Content.Server.Access.Systems;
using Content.Server.Power.Components;
using Content.Server.UserInterface;
using Content.Shared.Access;
@@ -91,7 +92,8 @@ namespace Content.Server.Access.Components
}
var privilegedIdEntity = PrivilegedIdContainer.ContainedEntity;
return privilegedIdEntity != null && reader.IsAllowed(privilegedIdEntity);
var accessSystem = EntitySystem.Get<AccessReaderSystem>();
return privilegedIdEntity != null && accessSystem.IsAllowed(reader, privilegedIdEntity.Uid);
}
/// <summary>
@@ -107,17 +109,18 @@ namespace Content.Server.Access.Components
var targetIdEntity = TargetIdContainer.ContainedEntity;
var targetIdComponent = targetIdEntity.GetComponent<IdCardComponent>();
targetIdComponent.FullName = newFullName;
targetIdComponent.JobTitle = newJobTitle;
var cardSystem = EntitySystem.Get<IdCardSystem>();
cardSystem.TryChangeFullName(targetIdEntity.Uid, newFullName);
cardSystem.TryChangeJobTitle(targetIdEntity.Uid, newJobTitle);
if (!newAccessList.TrueForAll(x => _prototypeManager.HasIndex<AccessLevelPrototype>(x)))
{
Logger.Warning("Tried to write unknown access tag.");
return;
}
var targetIdAccess = targetIdEntity.GetComponent<AccessComponent>();
targetIdAccess.SetTags(newAccessList);
var accessSystem = EntitySystem.Get<AccessSystem>();
accessSystem.TrySetTags(targetIdEntity.Uid, newAccessList);
}
/// <summary>

View File

@@ -7,27 +7,11 @@ using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Access.Components
{
[RegisterComponent]
public class PresetIdCardComponent : Component, IMapInit
public class PresetIdCardComponent : Component
{
public override string Name => "PresetIdCard";
[DataField("job")]
private string? _jobName;
void IMapInit.MapInit()
{
if (_jobName == null)
{
return;
}
var prototypes = IoCManager.Resolve<IPrototypeManager>();
var job = prototypes.Index<JobPrototype>(_jobName);
var access = Owner.GetComponent<AccessComponent>();
var idCard = Owner.GetComponent<IdCardComponent>();
access.SetTags(job.Access);
idCard.JobTitle = job.Name;
}
public readonly string? JobName;
}
}

View File

@@ -0,0 +1,119 @@
using Content.Server.Access.Components;
using Content.Server.Inventory.Components;
using Content.Server.Items;
using Content.Server.PDA;
using Content.Shared.Access;
using Content.Shared.Hands.Components;
using Content.Shared.Inventory;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace Content.Server.Access.Systems
{
public class AccessReaderSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<AccessReader, ComponentInit>(OnInit);
}
private void OnInit(EntityUid uid, AccessReader reader, ComponentInit args)
{
var allTags = reader.AccessLists.SelectMany(c => c).Union(reader.DenyTags);
foreach (var level in allTags)
{
if (!_prototypeManager.HasIndex<AccessLevelPrototype>(level))
{
Logger.ErrorS("access", $"Invalid access level: {level}");
}
}
}
/// <summary>
/// Searches an <see cref="AccessComponent"/> in the entity itself, in its active hand or in its ID slot.
/// Then compares the found access with the configured access lists to see if it is allowed.
/// </summary>
/// <remarks>
/// If no access is found, an empty set is used instead.
/// </remarks>
/// <param name="entity">The entity to be searched for access.</param>
public bool IsAllowed(AccessReader reader, EntityUid entity)
{
var tags = FindAccessTags(entity);
return IsAllowed(reader, tags);
}
public bool IsAllowed(AccessReader reader, ICollection<string> accessTags)
{
if (reader.DenyTags.Overlaps(accessTags))
{
// Sec owned by cargo.
return false;
}
return reader.AccessLists.Count == 0 || reader.AccessLists.Any(a => a.IsSubsetOf(accessTags));
}
public ICollection<string> FindAccessTags(EntityUid uid)
{
// check entity itself
if (FindAccessTagsItem(uid, out var tags))
return tags;
// maybe access component inside its hands?
if (EntityManager.TryGetComponent(uid, out SharedHandsComponent? hands))
{
if (hands.TryGetActiveHeldEntity(out var heldItem) &&
FindAccessTagsItem(heldItem.Uid, out tags))
{
return tags;
}
}
// maybe its inside an inventory slot?
if (EntityManager.TryGetComponent(uid, out InventoryComponent? inventoryComponent))
{
if (inventoryComponent.HasSlot(EquipmentSlotDefines.Slots.IDCARD) &&
inventoryComponent.TryGetSlotItem(EquipmentSlotDefines.Slots.IDCARD, out ItemComponent? item) &&
FindAccessTagsItem(item.Owner.Uid, out tags)
)
{
return tags;
}
}
return Array.Empty<string>();
}
/// <summary>
/// Try to find <see cref="AccessComponent"/> on this item
/// or inside this item (if it's pda)
/// </summary>
private bool FindAccessTagsItem(EntityUid uid, [NotNullWhen(true)] out HashSet<string>? tags)
{
if (EntityManager.TryGetComponent(uid, out AccessComponent? access))
{
tags = access.Tags;
return true;
}
if (EntityManager.TryGetComponent(uid, out PDAComponent? pda))
{
tags = pda?.ContainedID?.Owner?.GetComponent<AccessComponent>()?.Tags;
return tags != null;
}
tags = null;
return false;
}
}
}

View File

@@ -0,0 +1,24 @@
using Content.Server.Access.Components;
using Robust.Shared.GameObjects;
using System.Collections.Generic;
namespace Content.Server.Access.Systems
{
public class AccessSystem : EntitySystem
{
/// <summary>
/// Replaces the set of access tags we have with the provided set.
/// </summary>
/// <param name="newTags">The new access tags</param>
public bool TrySetTags(EntityUid uid, IEnumerable<string> newTags, AccessComponent? access = null)
{
if (!Resolve(uid, ref access))
return false;
access.Tags.Clear();
access.Tags.UnionWith(newTags);
return true;
}
}
}

View File

@@ -0,0 +1,70 @@
using Content.Server.Access.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
namespace Content.Server.Access.Systems
{
public class IdCardSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<IdCardComponent, ComponentInit>(OnInit);
}
private void OnInit(EntityUid uid, IdCardComponent id, ComponentInit args)
{
id.OriginalOwnerName ??= id.Owner.Name;
UpdateEntityName(uid, id);
}
public bool TryChangeJobTitle(EntityUid uid, string jobTitle, IdCardComponent? id = null)
{
if (!Resolve(uid, ref id))
return false;
id.JobTitle = jobTitle;
UpdateEntityName(uid, id);
return true;
}
public bool TryChangeFullName(EntityUid uid, string fullName, IdCardComponent? id = null)
{
if (!Resolve(uid, ref id))
return false;
id.FullName = fullName;
UpdateEntityName(uid, id);
return true;
}
/// <summary>
/// Changes the <see cref="Entity.Name"/> of <see cref="Component.Owner"/>.
/// </summary>
/// <remarks>
/// If either <see cref="FullName"/> or <see cref="JobTitle"/> is empty, it's replaced by placeholders.
/// If both are empty, the original entity's name is restored.
/// </remarks>
private void UpdateEntityName(EntityUid uid, IdCardComponent? id = null)
{
if (!Resolve(uid, ref id))
return;
if (string.IsNullOrWhiteSpace(id.FullName) && string.IsNullOrWhiteSpace(id.JobTitle))
{
id.Owner.Name = id.OriginalOwnerName;
return;
}
var jobSuffix = string.IsNullOrWhiteSpace(id.JobTitle) ? string.Empty : $" ({id.JobTitle})";
id.Owner.Name = string.IsNullOrWhiteSpace(id.FullName)
? Loc.GetString("access-id-card-component-owner-name-job-title-text",
("originalOwnerName", id.OriginalOwnerName),
("jobSuffix", jobSuffix))
: Loc.GetString("access-id-card-component-owner-full-name-job-title-text",
("fullName", id.FullName),
("jobSuffix", jobSuffix));
}
}
}

View File

@@ -0,0 +1,40 @@
using Content.Server.Access.Components;
using Content.Shared.Roles;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
using System;
namespace Content.Server.Access.Systems
{
public class PresetIdCardSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IdCardSystem _cardSystem = default!;
[Dependency] private readonly AccessSystem _accessSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PresetIdCardComponent, MapInitEvent>(OnMapInit);
}
private void OnMapInit(EntityUid uid, PresetIdCardComponent id, MapInitEvent args)
{
if (id.JobName == null) return;
if (!_prototypeManager.TryIndex(id.JobName, out JobPrototype? job))
{
Logger.ErrorS("access", $"Invalid job id ({id.JobName}) for preset card");
return;
}
// set access for access component
_accessSystem.TrySetTags(uid, job.Access);
// and also change job title on a card id
_cardSystem.TryChangeJobTitle(uid, job.Name);
}
}
}