Entity Whitelist changes (#7426)

This commit is contained in:
Leon Friedrich
2022-04-06 17:21:45 +12:00
committed by GitHub
parent c2867cd9e5
commit f583d0b96a
5 changed files with 33 additions and 38 deletions

View File

@@ -1,11 +1,5 @@
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 Content.Shared.Tag;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
namespace Content.Shared.Whitelist
@@ -26,7 +20,7 @@ namespace Content.Shared.Whitelist
/// </code>
[DataDefinition]
[Serializable, NetSerializable]
public sealed class EntityWhitelist : ISerializationHooks
public sealed class EntityWhitelist
{
/// <summary>
/// Component names that are allowed in the whitelist.
@@ -39,13 +33,15 @@ namespace Content.Shared.Whitelist
/// <summary>
/// Tags that are allowed in the whitelist.
/// </summary>
[DataField("tags")]
public string[]? Tags = null;
[DataField("tags", customTypeSerializer:typeof(PrototypeIdListSerializer<TagPrototype>))]
public List<string>? Tags = null;
void ISerializationHooks.AfterDeserialization()
{
UpdateRegistrations();
}
/// <summary>
/// If false, an entity only requires one of these components or tags to pass the whitelist. If true, an
/// entity requires to have ALL of these components and tags to pass.
/// </summary>
[DataField("requireAll")]
public bool RequireAll = false;
public void UpdateRegistrations()
{
@@ -73,23 +69,30 @@ namespace Content.Shared.Whitelist
/// </summary>
public bool IsValid(EntityUid uid, IEntityManager? entityManager = null)
{
if (Components != null && _registrations == null)
UpdateRegistrations();
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;
{
if (!RequireAll)
return true;
}
else if (RequireAll)
return false;
}
}
if (Tags != null && entityManager.TryGetComponent(uid, out TagComponent? tags))
{
var tagSystem = EntitySystem.Get<TagSystem>();
return RequireAll ? tagSystem.HasAllTags(tags, Tags) : tagSystem.HasAnyTag(tags, Tags);
}
return false;
}
}