From 30a36b2fd560aa74a283d1e2698b7b83f3518e33 Mon Sep 17 00:00:00 2001 From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Date: Thu, 1 Jun 2023 02:00:09 +1200 Subject: [PATCH] Replace `GetTagOrThrow()` with a debug assert. (#16974) Co-authored-by: metalgearsloth --- Content.IntegrationTests/Tests/Tag/TagTest.cs | 14 +++--- Content.Shared/Tag/TagSystem.cs | 43 +++++++++++-------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/Content.IntegrationTests/Tests/Tag/TagTest.cs b/Content.IntegrationTests/Tests/Tag/TagTest.cs index 5f949a5080..0fa425dfef 100644 --- a/Content.IntegrationTests/Tests/Tag/TagTest.cs +++ b/Content.IntegrationTests/Tests/Tag/TagTest.cs @@ -6,6 +6,7 @@ using NUnit.Framework; using Robust.Shared.GameObjects; using Robust.Shared.Map; using Robust.Shared.Prototypes; +using Robust.Shared.Utility; 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}); var server = pairTracker.Pair.Server; - var sMapManager = server.ResolveDependency(); var sEntityManager = server.ResolveDependency(); var sPrototypeManager = server.ResolveDependency(); var entManager = server.ResolveDependency(); @@ -120,31 +120,31 @@ namespace Content.IntegrationTests.Tests.Tag }); // Single - Assert.Throws(() => + Assert.Throws(() => { tagSystem.HasTag(sTagDummy, UnregisteredTag); }); - Assert.Throws(() => + Assert.Throws(() => { tagSystem.HasTag(sTagComponent, UnregisteredTag); }); // Any - Assert.Throws(() => + Assert.Throws(() => { tagSystem.HasAnyTag(sTagDummy, UnregisteredTag); }); - Assert.Throws(() => + Assert.Throws(() => { tagSystem.HasAnyTag(sTagComponent, UnregisteredTag); }); // All - Assert.Throws(() => + Assert.Throws(() => { tagSystem.HasAllTags(sTagDummy, UnregisteredTag); }); - Assert.Throws(() => + Assert.Throws(() => { tagSystem.HasAllTags(sTagComponent, UnregisteredTag); }); diff --git a/Content.Shared/Tag/TagSystem.cs b/Content.Shared/Tag/TagSystem.cs index d082f1da0e..0f223afa4e 100644 --- a/Content.Shared/Tag/TagSystem.cs +++ b/Content.Shared/Tag/TagSystem.cs @@ -1,6 +1,7 @@ using System.Linq; using Robust.Shared.GameStates; using Robust.Shared.Prototypes; +using Robust.Shared.Utility; namespace Content.Shared.Tag; @@ -11,11 +12,23 @@ public sealed class TagSystem : EntitySystem public override void Initialize() { base.Initialize(); - SubscribeLocalEvent(OnTagInit); SubscribeLocalEvent(OnTagGetState); SubscribeLocalEvent(OnTagHandleState); + +#if DEBUG + SubscribeLocalEvent(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) { if (args.Current is not TagComponentState state) @@ -25,7 +38,7 @@ public sealed class TagSystem : EntitySystem foreach (var tag in state.Tags) { - GetTagOrThrow(tag); + AssertValidTag(tag); component.Tags.Add(tag); } } @@ -44,17 +57,9 @@ public sealed class TagSystem : EntitySystem 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) - { - GetTagOrThrow(tag); - } - } - - private TagPrototype GetTagOrThrow(string id) - { - return _proto.Index(id); + DebugTools.Assert(_proto.HasIndex(id), $"Unknown tag: {id}"); } /// @@ -304,7 +309,7 @@ public sealed class TagSystem : EntitySystem /// public bool AddTag(TagComponent component, string id) { - GetTagOrThrow(id); + AssertValidTag(id); var added = component.Tags.Add(id); if (added) @@ -343,7 +348,7 @@ public sealed class TagSystem : EntitySystem foreach (var id in ids) { - GetTagOrThrow(id); + AssertValidTag(id); component.Tags.Add(id); } @@ -366,7 +371,7 @@ public sealed class TagSystem : EntitySystem /// public bool HasTag(TagComponent component, string id) { - GetTagOrThrow(id); + AssertValidTag(id); return component.Tags.Contains(id); } @@ -395,7 +400,7 @@ public sealed class TagSystem : EntitySystem { foreach (var id in ids) { - GetTagOrThrow(id); + AssertValidTag(id); if (!component.Tags.Contains(id)) return false; @@ -430,7 +435,7 @@ public sealed class TagSystem : EntitySystem { foreach (var id in ids) { - GetTagOrThrow(id); + AssertValidTag(id); if (component.Tags.Contains(id)) { @@ -452,7 +457,7 @@ public sealed class TagSystem : EntitySystem /// public bool RemoveTag(TagComponent component, string id) { - GetTagOrThrow(id); + AssertValidTag(id); if (component.Tags.Remove(id)) { @@ -492,7 +497,7 @@ public sealed class TagSystem : EntitySystem foreach (var id in ids) { - GetTagOrThrow(id); + AssertValidTag(id); component.Tags.Remove(id); }