Replace GetTagOrThrow() with a debug assert. (#16974)

Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
Leon Friedrich
2023-06-01 02:00:09 +12:00
committed by GitHub
parent 77a954345b
commit 30a36b2fd5
2 changed files with 31 additions and 26 deletions

View File

@@ -6,6 +6,7 @@ using NUnit.Framework;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.IntegrationTests.Tests.Tag namespace Content.IntegrationTests.Tests.Tag
{ {
@@ -47,7 +48,6 @@ namespace Content.IntegrationTests.Tests.Tag
await using var pairTracker = await PoolManager.GetServerClient(new PoolSettings{NoClient = true, ExtraPrototypes = Prototypes}); await using var pairTracker = await PoolManager.GetServerClient(new PoolSettings{NoClient = true, ExtraPrototypes = Prototypes});
var server = pairTracker.Pair.Server; var server = pairTracker.Pair.Server;
var sMapManager = server.ResolveDependency<IMapManager>();
var sEntityManager = server.ResolveDependency<IEntityManager>(); var sEntityManager = server.ResolveDependency<IEntityManager>();
var sPrototypeManager = server.ResolveDependency<IPrototypeManager>(); var sPrototypeManager = server.ResolveDependency<IPrototypeManager>();
var entManager = server.ResolveDependency<IEntitySystemManager>(); var entManager = server.ResolveDependency<IEntitySystemManager>();
@@ -120,31 +120,31 @@ namespace Content.IntegrationTests.Tests.Tag
}); });
// Single // Single
Assert.Throws<UnknownPrototypeException>(() => Assert.Throws<DebugAssertException>(() =>
{ {
tagSystem.HasTag(sTagDummy, UnregisteredTag); tagSystem.HasTag(sTagDummy, UnregisteredTag);
}); });
Assert.Throws<UnknownPrototypeException>(() => Assert.Throws<DebugAssertException>(() =>
{ {
tagSystem.HasTag(sTagComponent, UnregisteredTag); tagSystem.HasTag(sTagComponent, UnregisteredTag);
}); });
// Any // Any
Assert.Throws<UnknownPrototypeException>(() => Assert.Throws<DebugAssertException>(() =>
{ {
tagSystem.HasAnyTag(sTagDummy, UnregisteredTag); tagSystem.HasAnyTag(sTagDummy, UnregisteredTag);
}); });
Assert.Throws<UnknownPrototypeException>(() => Assert.Throws<DebugAssertException>(() =>
{ {
tagSystem.HasAnyTag(sTagComponent, UnregisteredTag); tagSystem.HasAnyTag(sTagComponent, UnregisteredTag);
}); });
// All // All
Assert.Throws<UnknownPrototypeException>(() => Assert.Throws<DebugAssertException>(() =>
{ {
tagSystem.HasAllTags(sTagDummy, UnregisteredTag); tagSystem.HasAllTags(sTagDummy, UnregisteredTag);
}); });
Assert.Throws<UnknownPrototypeException>(() => Assert.Throws<DebugAssertException>(() =>
{ {
tagSystem.HasAllTags(sTagComponent, UnregisteredTag); tagSystem.HasAllTags(sTagComponent, UnregisteredTag);
}); });

View File

@@ -1,6 +1,7 @@
using System.Linq; using System.Linq;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Shared.Tag; namespace Content.Shared.Tag;
@@ -11,11 +12,23 @@ public sealed class TagSystem : EntitySystem
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<TagComponent, ComponentInit>(OnTagInit);
SubscribeLocalEvent<TagComponent, ComponentGetState>(OnTagGetState); SubscribeLocalEvent<TagComponent, ComponentGetState>(OnTagGetState);
SubscribeLocalEvent<TagComponent, ComponentHandleState>(OnTagHandleState); SubscribeLocalEvent<TagComponent, ComponentHandleState>(OnTagHandleState);
#if DEBUG
SubscribeLocalEvent<TagComponent, ComponentInit>(OnTagInit);
} }
private void OnTagInit(EntityUid uid, TagComponent component, ComponentInit args)
{
foreach (var tag in component.Tags)
{
AssertValidTag(tag);
}
#endif
}
private void OnTagHandleState(EntityUid uid, TagComponent component, ref ComponentHandleState args) private void OnTagHandleState(EntityUid uid, TagComponent component, ref ComponentHandleState args)
{ {
if (args.Current is not TagComponentState state) if (args.Current is not TagComponentState state)
@@ -25,7 +38,7 @@ public sealed class TagSystem : EntitySystem
foreach (var tag in state.Tags) foreach (var tag in state.Tags)
{ {
GetTagOrThrow(tag); AssertValidTag(tag);
component.Tags.Add(tag); component.Tags.Add(tag);
} }
} }
@@ -44,17 +57,9 @@ public sealed class TagSystem : EntitySystem
args.State = new TagComponentState(tags); args.State = new TagComponentState(tags);
} }
private void OnTagInit(EntityUid uid, TagComponent component, ComponentInit args) private void AssertValidTag(string id)
{ {
foreach (var tag in component.Tags) DebugTools.Assert(_proto.HasIndex<TagPrototype>(id), $"Unknown tag: {id}");
{
GetTagOrThrow(tag);
}
}
private TagPrototype GetTagOrThrow(string id)
{
return _proto.Index<TagPrototype>(id);
} }
/// <summary> /// <summary>
@@ -304,7 +309,7 @@ public sealed class TagSystem : EntitySystem
/// </exception> /// </exception>
public bool AddTag(TagComponent component, string id) public bool AddTag(TagComponent component, string id)
{ {
GetTagOrThrow(id); AssertValidTag(id);
var added = component.Tags.Add(id); var added = component.Tags.Add(id);
if (added) if (added)
@@ -343,7 +348,7 @@ public sealed class TagSystem : EntitySystem
foreach (var id in ids) foreach (var id in ids)
{ {
GetTagOrThrow(id); AssertValidTag(id);
component.Tags.Add(id); component.Tags.Add(id);
} }
@@ -366,7 +371,7 @@ public sealed class TagSystem : EntitySystem
/// </exception> /// </exception>
public bool HasTag(TagComponent component, string id) public bool HasTag(TagComponent component, string id)
{ {
GetTagOrThrow(id); AssertValidTag(id);
return component.Tags.Contains(id); return component.Tags.Contains(id);
} }
@@ -395,7 +400,7 @@ public sealed class TagSystem : EntitySystem
{ {
foreach (var id in ids) foreach (var id in ids)
{ {
GetTagOrThrow(id); AssertValidTag(id);
if (!component.Tags.Contains(id)) if (!component.Tags.Contains(id))
return false; return false;
@@ -430,7 +435,7 @@ public sealed class TagSystem : EntitySystem
{ {
foreach (var id in ids) foreach (var id in ids)
{ {
GetTagOrThrow(id); AssertValidTag(id);
if (component.Tags.Contains(id)) if (component.Tags.Contains(id))
{ {
@@ -452,7 +457,7 @@ public sealed class TagSystem : EntitySystem
/// </exception> /// </exception>
public bool RemoveTag(TagComponent component, string id) public bool RemoveTag(TagComponent component, string id)
{ {
GetTagOrThrow(id); AssertValidTag(id);
if (component.Tags.Remove(id)) if (component.Tags.Remove(id))
{ {
@@ -492,7 +497,7 @@ public sealed class TagSystem : EntitySystem
foreach (var id in ids) foreach (var id in ids)
{ {
GetTagOrThrow(id); AssertValidTag(id);
component.Tags.Remove(id); component.Tags.Remove(id);
} }