committed by
Pieter-Jan Briers
parent
ba8b495ec0
commit
6e2799f048
@@ -9,14 +9,13 @@ namespace Content.Server.GameObjects.Components.Access
|
||||
public class AccessComponent : Component
|
||||
{
|
||||
public override string Name => "Access";
|
||||
private List<string> _tags;
|
||||
[ViewVariables]
|
||||
public List<string> Tags => _tags;
|
||||
public List<string> Tags;
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
base.ExposeData(serializer);
|
||||
|
||||
serializer.DataField(ref _tags, "tags", new List<string>());
|
||||
serializer.DataField(ref Tags, "tags", new List<string>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,17 @@ namespace Content.Server.GameObjects.Components.Access
|
||||
public class AccessReader : Component
|
||||
{
|
||||
public override string Name => "AccessReader";
|
||||
[ViewVariables]
|
||||
private List<string> _necessaryTags;
|
||||
[ViewVariables]
|
||||
private List<string> _sufficientTags;
|
||||
|
||||
/// <summary>
|
||||
/// Searches an <see cref="AccessComponent"/> in the entity itself, in its active hand or in its ID slot.
|
||||
/// Returns true if an <see cref="AccessComponent"/> is found and its tags list contains
|
||||
/// at least one of <see cref="_sufficientTags"/> or all of <see cref="_necessaryTags"/>.
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to be searched for access.</param>
|
||||
public bool IsAllowed(IEntity entity)
|
||||
{
|
||||
var accessProvider = FindAccessProvider(entity);
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System.Security.Cryptography;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.ViewVariables;
|
||||
@@ -9,10 +8,61 @@ namespace Content.Server.GameObjects.Components.Access
|
||||
public class IdCardComponent : Component
|
||||
{
|
||||
public override string Name => "IdCard";
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
|
||||
/// See <see cref="UpdateEntityName"/>.
|
||||
private string _ownerOriginalName;
|
||||
|
||||
private string _fullName;
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public string FullName
|
||||
{
|
||||
get => _fullName;
|
||||
set
|
||||
{
|
||||
_fullName = value;
|
||||
UpdateEntityName();
|
||||
}
|
||||
}
|
||||
|
||||
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 = _ownerOriginalName;
|
||||
return;
|
||||
}
|
||||
|
||||
var tempFullName = string.IsNullOrWhiteSpace(FullName) ? "Unknown" : FullName;
|
||||
var tempJobTitle = string.IsNullOrWhiteSpace(JobTitle) ? "N/A" : JobTitle;
|
||||
|
||||
Owner.Name = $"{_ownerOriginalName} ({tempFullName}, {tempJobTitle})";
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_ownerOriginalName = Owner.Name;
|
||||
UpdateEntityName();
|
||||
}
|
||||
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Content.Server.Interfaces;
|
||||
using Content.Server.Interfaces.GameObjects;
|
||||
using Content.Shared.Access;
|
||||
using Content.Shared.GameObjects.Components.Access;
|
||||
using Robust.Server.GameObjects.Components.Container;
|
||||
using Robust.Server.GameObjects.Components.UserInterface;
|
||||
using Robust.Server.Interfaces.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Log;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Access
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
public class IdCardConsoleComponent : SharedIdCardConsoleComponent, IActivate
|
||||
{
|
||||
#pragma warning disable 649
|
||||
[Dependency] private readonly IServerNotifyManager _notifyManager;
|
||||
[Dependency] private readonly ILocalizationManager _localizationManager;
|
||||
#pragma warning restore 649
|
||||
|
||||
private BoundUserInterface _userInterface;
|
||||
private ContainerSlot _privilegedIdContainer;
|
||||
private ContainerSlot _targetIdContainer;
|
||||
private AccessReader _accessReader;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_privilegedIdContainer = ContainerManagerComponent.Ensure<ContainerSlot>($"{Name}-privilegedId", Owner);
|
||||
_targetIdContainer = ContainerManagerComponent.Ensure<ContainerSlot>($"{Name}-targetId", Owner);
|
||||
|
||||
_accessReader = Owner.GetComponent<AccessReader>();
|
||||
|
||||
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>()
|
||||
.GetBoundUserInterface(IdCardConsoleUiKey.Key);
|
||||
_userInterface.OnReceiveMessage += OnUiReceiveMessage;
|
||||
UpdateUserInterface();
|
||||
}
|
||||
|
||||
private void OnUiReceiveMessage(ServerBoundUserInterfaceMessage obj)
|
||||
{
|
||||
switch (obj.Message)
|
||||
{
|
||||
case IdButtonPressedMessage msg:
|
||||
switch (msg.Button)
|
||||
{
|
||||
case UiButton.PrivilegedId:
|
||||
HandleId(obj.Session.AttachedEntity, _privilegedIdContainer);
|
||||
break;
|
||||
case UiButton.TargetId:
|
||||
HandleId(obj.Session.AttachedEntity, _targetIdContainer);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case WriteToTargetIdMessage msg:
|
||||
TryWriteToTargetId(msg.FullName, msg.JobTitle, msg.AccessList);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if there is an ID in <see cref="_privilegedIdContainer"/> and said ID satisfies the requirements of <see cref="_accessReader"/>.
|
||||
/// </summary>
|
||||
private bool PrivilegedIdIsAuthorized()
|
||||
{
|
||||
var privilegedIdEntity = _privilegedIdContainer.ContainedEntity;
|
||||
return privilegedIdEntity != null && _accessReader.IsAllowed(privilegedIdEntity);
|
||||
}
|
||||
/// <summary>
|
||||
/// Called when the "Submit" button in the UI gets pressed.
|
||||
/// Writes data passed from the UI into the ID stored in <see cref="_targetIdContainer"/>, if present.
|
||||
/// </summary>
|
||||
private void TryWriteToTargetId(string newFullName, string newJobTitle, List<string> newAccessList)
|
||||
{
|
||||
if (!PrivilegedIdIsAuthorized() || _targetIdContainer.ContainedEntity == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var targetIdEntity = _targetIdContainer.ContainedEntity;
|
||||
|
||||
var targetIdComponent = targetIdEntity.GetComponent<IdCardComponent>();
|
||||
targetIdComponent.FullName = newFullName;
|
||||
targetIdComponent.JobTitle = newJobTitle;
|
||||
|
||||
if (!newAccessList.TrueForAll(x => SharedAccess.AllAccess.Contains(x)))
|
||||
{
|
||||
Logger.Warning($"Tried to write unknown access tag.");
|
||||
return;
|
||||
}
|
||||
var targetIdAccess = targetIdEntity.GetComponent<AccessComponent>();
|
||||
targetIdAccess.Tags = newAccessList;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when one of the insert/remove ID buttons gets pressed.
|
||||
/// </summary>
|
||||
private void HandleId(IEntity user, ContainerSlot container)
|
||||
{
|
||||
if (!user.TryGetComponent(out IHandsComponent hands))
|
||||
{
|
||||
_notifyManager.PopupMessage(Owner.Transform.GridPosition, user, _localizationManager.GetString("You have no hands."));
|
||||
return;
|
||||
}
|
||||
|
||||
if (container.ContainedEntity == null)
|
||||
{
|
||||
InsertIdFromHand(user, container, hands);
|
||||
}
|
||||
else
|
||||
{
|
||||
PutIdInHand(container, hands);
|
||||
}
|
||||
}
|
||||
|
||||
private void InsertIdFromHand(IEntity user, ContainerSlot container, IHandsComponent hands)
|
||||
{
|
||||
var isId = hands.GetActiveHand?.Owner.HasComponent<IdCardComponent>();
|
||||
if (isId != true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if(!hands.Drop(hands.ActiveIndex, container))
|
||||
{
|
||||
_notifyManager.PopupMessage(Owner.Transform.GridPosition, user, _localizationManager.GetString("You can't let go of the ID card!"));
|
||||
return;
|
||||
}
|
||||
UpdateUserInterface();
|
||||
}
|
||||
|
||||
private void PutIdInHand(ContainerSlot container, IHandsComponent hands)
|
||||
{
|
||||
var idEntity = container.ContainedEntity;
|
||||
if (idEntity == null || !container.Remove(idEntity))
|
||||
{
|
||||
return;
|
||||
}
|
||||
UpdateUserInterface();
|
||||
|
||||
hands.PutInHand(idEntity.GetComponent<ItemComponent>());
|
||||
}
|
||||
|
||||
private void UpdateUserInterface()
|
||||
{
|
||||
var isPrivilegedIdPresent = _privilegedIdContainer.ContainedEntity != null;
|
||||
var targetIdEntity = _targetIdContainer.ContainedEntity;
|
||||
IdCardConsoleBoundUserInterfaceState newState;
|
||||
// this could be prettier
|
||||
if (targetIdEntity == null)
|
||||
{
|
||||
newState = new IdCardConsoleBoundUserInterfaceState(
|
||||
isPrivilegedIdPresent,
|
||||
PrivilegedIdIsAuthorized(),
|
||||
false,
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
}
|
||||
else
|
||||
{
|
||||
var targetIdComponent = targetIdEntity.GetComponent<IdCardComponent>();
|
||||
var targetAccessComponent = targetIdEntity.GetComponent<AccessComponent>();
|
||||
newState = new IdCardConsoleBoundUserInterfaceState(
|
||||
isPrivilegedIdPresent,
|
||||
PrivilegedIdIsAuthorized(),
|
||||
true,
|
||||
targetIdComponent.FullName,
|
||||
targetIdComponent.JobTitle,
|
||||
targetAccessComponent.Tags);
|
||||
}
|
||||
_userInterface.SetState(newState);
|
||||
}
|
||||
|
||||
public void Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
if(!eventArgs.User.TryGetComponent(out IActorComponent actor))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_userInterface.Open(actor.playerSession);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user