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);
serverActions.Remove(serverAct);
if (act is EntityTargetAction entAct)
{
entAct.Whitelist?.UpdateRegistrations();
}
}
// Anything that remains is a new action
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.
component.Actions.Add((ActionType) newAct.Clone());
}

View File

@@ -81,8 +81,8 @@ namespace Content.IntegrationTests.Tests.Utility
// Test instantiated on its own
var whitelistInst = new EntityWhitelist
{
Components = new[] {$"{ValidComponent}"},
Tags = new[] {"ValidTag"}
Components = new[] { $"{ValidComponent}"},
Tags = new() {"ValidTag"}
};
whitelistInst.UpdateRegistrations();
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.Shared.Storage.Components;
using Content.Shared.Storage.EntitySystems;
@@ -24,7 +24,7 @@ namespace Content.Server.Storage.EntitySystems
{
foreach (var entity in containedLayers)
{
if (mapLayerData.Whitelist.IsValid(entity))
if (mapLayerData.ServerWhitelist.IsValid(entity))
{
list.Add(mapLayerData.Layer);
break;

View File

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

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))
{
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;
}
}