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

@@ -155,19 +155,11 @@ namespace Content.Client.Actions
act.CopyFrom(serverAct); act.CopyFrom(serverAct);
serverActions.Remove(serverAct); serverActions.Remove(serverAct);
if (act is EntityTargetAction entAct)
{
entAct.Whitelist?.UpdateRegistrations();
}
} }
// Anything that remains is a new action // Anything that remains is a new action
foreach (var newAct in serverActions) foreach (var newAct in serverActions)
{ {
if (newAct is EntityTargetAction entAct)
entAct.Whitelist?.UpdateRegistrations();
// We create a new action, not just sorting a reference to the state's action. // We create a new action, not just sorting a reference to the state's action.
component.Actions.Add((ActionType) newAct.Clone()); component.Actions.Add((ActionType) newAct.Clone());
} }

View File

@@ -81,8 +81,8 @@ namespace Content.IntegrationTests.Tests.Utility
// Test instantiated on its own // Test instantiated on its own
var whitelistInst = new EntityWhitelist var whitelistInst = new EntityWhitelist
{ {
Components = new[] {$"{ValidComponent}"}, Components = new[] { $"{ValidComponent}"},
Tags = new[] {"ValidTag"} Tags = new() {"ValidTag"}
}; };
whitelistInst.UpdateRegistrations(); whitelistInst.UpdateRegistrations();
Assert.That(whitelistInst, Is.Not.Null); Assert.That(whitelistInst, Is.Not.Null);

View File

@@ -1,4 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using Content.Server.Storage.Components; using Content.Server.Storage.Components;
using Content.Shared.Storage.Components; using Content.Shared.Storage.Components;
using Content.Shared.Storage.EntitySystems; using Content.Shared.Storage.EntitySystems;
@@ -24,7 +24,7 @@ namespace Content.Server.Storage.EntitySystems
{ {
foreach (var entity in containedLayers) foreach (var entity in containedLayers)
{ {
if (mapLayerData.Whitelist.IsValid(entity)) if (mapLayerData.ServerWhitelist.IsValid(entity))
{ {
list.Add(mapLayerData.Layer); list.Add(mapLayerData.Layer);
break; break;

View File

@@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Content.Shared.Whitelist; using Content.Shared.Whitelist;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
@@ -19,8 +19,8 @@ namespace Content.Shared.Storage.Components
{ {
public string Layer = string.Empty; public string Layer = string.Empty;
[DataField("whitelist", required: true)] [DataField("whitelist", required: true, serverOnly: true)]
public EntityWhitelist Whitelist { get; set; } = new(); public EntityWhitelist ServerWhitelist { get; set; } = new();
} }
[Serializable, NetSerializable] [Serializable, NetSerializable]

View File

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