Re-organize all projects (#4166)

This commit is contained in:
DrSmugleaf
2021-06-09 22:19:39 +02:00
committed by GitHub
parent 9f50e4061b
commit ff1a2d97ea
1773 changed files with 5258 additions and 5508 deletions

View File

@@ -0,0 +1,17 @@
using Robust.Shared.GameObjects;
namespace Content.Server.Access
{
public sealed class AccessReaderChangeMessage : EntityEventArgs
{
public IEntity Sender { get; }
public bool Enabled { get; }
public AccessReaderChangeMessage(IEntity entity, bool enabled)
{
Sender = entity;
Enabled = enabled;
}
}
}

View File

@@ -0,0 +1,31 @@
#nullable enable
using System.Collections.Generic;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Access.Components
{
/// <summary>
/// Simple mutable access provider found on ID cards and such.
/// </summary>
[RegisterComponent]
[ComponentReference(typeof(IAccess))]
public class AccessComponent : Component, IAccess
{
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);
}
}
}

View File

@@ -0,0 +1,124 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Server.Hands.Components;
using Content.Server.Inventory.Components;
using Content.Server.Items;
using Content.Shared.Access;
using Content.Shared.Inventory;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Access.Components
{
/// <summary>
/// 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>
/// List of access lists to check allowed against. For an access check to pass
/// there has to be an access list that is a subset of the access in the checking list.
/// </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>();
}
public 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}");
}
}
}
}
}

View File

@@ -0,0 +1,32 @@
#nullable enable
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

@@ -0,0 +1,73 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Access.Components
{
[RegisterComponent]
public class IdCardComponent : Component
{
public override string Name => "IdCard";
/// See <see cref="UpdateEntityName"/>.
[DataField("originalOwnerName")]
private string _originalOwnerName = default!;
[DataField("fullName")]
private string? _fullName;
[ViewVariables(VVAccess.ReadWrite)]
public string? FullName
{
get => _fullName;
set
{
_fullName = value;
UpdateEntityName();
}
}
[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) ? "" : $" ({JobTitle})";
Owner.Name = string.IsNullOrWhiteSpace(FullName)
? Loc.GetString("{0}{1}", _originalOwnerName, jobSuffix)
: Loc.GetString("{0}'s ID card{1}", FullName, jobSuffix);
}
public override void Initialize()
{
base.Initialize();
// ReSharper disable once ConstantNullCoalescingCondition
_originalOwnerName ??= Owner.Name;
UpdateEntityName();
}
}
}

View File

@@ -0,0 +1,318 @@
#nullable enable
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Content.Server.Hands.Components;
using Content.Server.Items;
using Content.Server.Power.Components;
using Content.Server.UserInterface;
using Content.Shared.Access;
using Content.Shared.ActionBlocker;
using Content.Shared.Acts;
using Content.Shared.Interaction;
using Content.Shared.Notification;
using Content.Shared.Verbs;
using Robust.Server.GameObjects;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Access.Components
{
[RegisterComponent]
[ComponentReference(typeof(IActivate))]
public class IdCardConsoleComponent : SharedIdCardConsoleComponent, IActivate, IInteractUsing, IBreakAct
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
private ContainerSlot _privilegedIdContainer = default!;
private ContainerSlot _targetIdContainer = default!;
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(IdCardConsoleUiKey.Key);
[ViewVariables] private bool Powered => !Owner.TryGetComponent(out PowerReceiverComponent? receiver) || receiver.Powered;
private bool PrivilegedIDEmpty => _privilegedIdContainer.ContainedEntities.Count < 1;
private bool TargetIDEmpty => _targetIdContainer.ContainedEntities.Count < 1;
public override void Initialize()
{
base.Initialize();
_privilegedIdContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-privilegedId");
_targetIdContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-targetId");
Owner.EnsureComponentWarn<AccessReader>();
Owner.EnsureComponentWarn<ServerUserInterfaceComponent>();
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += OnUiReceiveMessage;
}
UpdateUserInterface();
}
private void OnUiReceiveMessage(ServerBoundUserInterfaceMessage obj)
{
if (obj.Session.AttachedEntity == null)
{
return;
}
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;
}
UpdateUserInterface();
}
/// <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()
{
if (!Owner.TryGetComponent(out AccessReader? reader))
{
return true;
}
var privilegedIdEntity = _privilegedIdContainer.ContainedEntity;
return privilegedIdEntity != null && reader.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 => _prototypeManager.HasIndex<AccessLevelPrototype>(x)))
{
Logger.Warning("Tried to write unknown access tag.");
return;
}
var targetIdAccess = targetIdEntity.GetComponent<AccessComponent>();
targetIdAccess.SetTags(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))
{
Owner.PopupMessage(user, Loc.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.ActiveHand == null)
{
return;
}
if (!hands.Drop(hands.ActiveHand, container))
{
Owner.PopupMessage(user, Loc.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,
_privilegedIdContainer.ContainedEntity?.Name ?? "",
_targetIdContainer.ContainedEntity?.Name ?? "");
}
else
{
var targetIdComponent = targetIdEntity.GetComponent<IdCardComponent>();
var targetAccessComponent = targetIdEntity.GetComponent<AccessComponent>();
newState = new IdCardConsoleBoundUserInterfaceState(
isPrivilegedIdPresent,
PrivilegedIdIsAuthorized(),
true,
targetIdComponent.FullName,
targetIdComponent.JobTitle,
targetAccessComponent.Tags.ToArray(),
_privilegedIdContainer.ContainedEntity?.Name ?? "",
_targetIdContainer.ContainedEntity?.Name ?? "");
}
UserInterface?.SetState(newState);
}
void IActivate.Activate(ActivateEventArgs eventArgs)
{
if(!eventArgs.User.TryGetComponent(out ActorComponent? actor))
{
return;
}
if(!Powered) return;
UserInterface?.Open(actor.PlayerSession);
}
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
{
var item = eventArgs.Using;
var user = eventArgs.User;
if (!PrivilegedIDEmpty && !TargetIDEmpty)
{
return false;
}
if (!item.TryGetComponent<IdCardComponent>(out var idCardComponent) || !user.TryGetComponent(out IHandsComponent? hand))
{
return false;
}
if (PrivilegedIDEmpty)
{
InsertIdFromHand(user, _privilegedIdContainer, hand);
}
else if (TargetIDEmpty)
{
InsertIdFromHand(user, _targetIdContainer, hand);
}
UpdateUserInterface();
return true;
}
[Verb]
public sealed class EjectPrivilegedIDVerb : Verb<IdCardConsoleComponent>
{
protected override void GetData(IEntity user, IdCardConsoleComponent component, VerbData data)
{
if (!ActionBlockerSystem.CanInteract(user))
{
data.Visibility = VerbVisibility.Invisible;
return;
}
data.Text = Loc.GetString("Eject Privileged ID");
data.IconTexture = "/Textures/Interface/VerbIcons/eject.svg.192dpi.png";
data.Visibility = component.PrivilegedIDEmpty ? VerbVisibility.Invisible : VerbVisibility.Visible;
}
protected override void Activate(IEntity user, IdCardConsoleComponent component)
{
if (!user.TryGetComponent(out IHandsComponent? hand))
{
return;
}
component.PutIdInHand(component._privilegedIdContainer, hand);
}
}
public sealed class EjectTargetIDVerb : Verb<IdCardConsoleComponent>
{
protected override void GetData(IEntity user, IdCardConsoleComponent component, VerbData data)
{
if (!ActionBlockerSystem.CanInteract(user))
{
data.Visibility = VerbVisibility.Invisible;
return;
}
data.Text = Loc.GetString("Eject Target ID");
data.Visibility = component.TargetIDEmpty ? VerbVisibility.Invisible : VerbVisibility.Visible;
data.IconTexture = "/Textures/Interface/VerbIcons/eject.svg.192dpi.png";
}
protected override void Activate(IEntity user, IdCardConsoleComponent component)
{
if (!user.TryGetComponent(out IHandsComponent? hand))
{
return;
}
component.PutIdInHand(component._targetIdContainer, hand);
}
}
public void OnBreak(BreakageEventArgs eventArgs)
{
var privileged = _privilegedIdContainer.ContainedEntity;
if (privileged != null)
_privilegedIdContainer.Remove(privileged);
var target = _targetIdContainer.ContainedEntity;
if (target != null)
_targetIdContainer.Remove(target);
}
}
}

View File

@@ -0,0 +1,34 @@
#nullable enable
using Content.Shared.Roles;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Access.Components
{
[RegisterComponent]
public class PresetIdCardComponent : Component, IMapInit
{
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;
}
}
}