Files
tbd-station-14/Content.Shared/Whitelist/EntityWhitelist.cs
Leon Friedrich 5ac5dd6a64 Actions Rework (#6791)
* Rejig Actions

* fix merge errors

* lambda-b-gon

* fix PAI, add innate actions

* Revert "fix PAI, add innate actions"

This reverts commit 4b501ac083e979e31ebd98d7b98077e0dbdd344b.

* Just fix by making nullable.

if only require: true actually did something somehow.

* Make AddActions() ensure an actions component

and misc comments

* misc cleanup

* Limit range even when not checking for obstructions

* remove old guardian code

* rename function and make EntityUid nullable

* fix magboot bug

* fix action search menu

* make targeting toggle all equivalent actions

* fix combat popups (enabling <-> disabling)
2022-02-24 22:12:29 -06:00

97 lines
3.2 KiB
C#

using System.Collections.Generic;
using Content.Shared.Tag;
using Content.Shared.Wires;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
namespace Content.Shared.Whitelist
{
/// <summary>
/// Used to determine whether an entity fits a certain whitelist.
/// Does not whitelist by prototypes, since that is undesirable; you're better off just adding a tag to all
/// entity prototypes that need to be whitelisted, and checking for that.
/// </summary>
/// <code>
/// whitelist:
/// tags:
/// - Cigarette
/// - FirelockElectronics
/// components:
/// - Buckle
/// - AsteroidRock
/// </code>
[DataDefinition]
[Serializable, NetSerializable]
public sealed class EntityWhitelist : ISerializationHooks
{
/// <summary>
/// Component names that are allowed in the whitelist.
/// </summary>
[DataField("components")] public string[]? Components = null;
[NonSerialized]
private List<IComponentRegistration>? _registrations = null;
/// <summary>
/// Tags that are allowed in the whitelist.
/// </summary>
[DataField("tags")]
public string[]? Tags = null;
void ISerializationHooks.AfterDeserialization()
{
UpdateRegistrations();
}
public void UpdateRegistrations()
{
if (Components == null) return;
var compfact = IoCManager.Resolve<IComponentFactory>();
_registrations = new List<IComponentRegistration>();
foreach (var name in Components)
{
var availability = compfact.GetComponentAvailability(name);
if (compfact.TryGetRegistration(name, out var registration)
&& availability == ComponentAvailability.Available)
{
_registrations.Add(registration);
}
else if (availability == ComponentAvailability.Unknown)
{
Logger.Warning($"Unknown component name {name} passed to EntityWhitelist!");
}
}
}
/// <summary>
/// Returns whether a given entity fits the whitelist.
/// </summary>
public bool IsValid(EntityUid uid, IEntityManager? entityManager = null)
{
entityManager ??= IoCManager.Resolve<IEntityManager>();
var tagSystem = EntitySystem.Get<TagSystem>();
if (Tags != null && entityManager.TryGetComponent(uid, out TagComponent? tags))
{
if (tagSystem.HasAnyTag(tags, Tags))
return true;
}
if (_registrations != null)
{
foreach (var reg in _registrations)
{
if (entityManager.HasComponent(uid, reg.Type))
return true;
}
}
return false;
}
}
}