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.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<IMapManager>();
var sEntityManager = server.ResolveDependency<IEntityManager>();
var sPrototypeManager = server.ResolveDependency<IPrototypeManager>();
var entManager = server.ResolveDependency<IEntitySystemManager>();
@@ -120,31 +120,31 @@ namespace Content.IntegrationTests.Tests.Tag
});
// Single
Assert.Throws<UnknownPrototypeException>(() =>
Assert.Throws<DebugAssertException>(() =>
{
tagSystem.HasTag(sTagDummy, UnregisteredTag);
});
Assert.Throws<UnknownPrototypeException>(() =>
Assert.Throws<DebugAssertException>(() =>
{
tagSystem.HasTag(sTagComponent, UnregisteredTag);
});
// Any
Assert.Throws<UnknownPrototypeException>(() =>
Assert.Throws<DebugAssertException>(() =>
{
tagSystem.HasAnyTag(sTagDummy, UnregisteredTag);
});
Assert.Throws<UnknownPrototypeException>(() =>
Assert.Throws<DebugAssertException>(() =>
{
tagSystem.HasAnyTag(sTagComponent, UnregisteredTag);
});
// All
Assert.Throws<UnknownPrototypeException>(() =>
Assert.Throws<DebugAssertException>(() =>
{
tagSystem.HasAllTags(sTagDummy, UnregisteredTag);
});
Assert.Throws<UnknownPrototypeException>(() =>
Assert.Throws<DebugAssertException>(() =>
{
tagSystem.HasAllTags(sTagComponent, UnregisteredTag);
});

View File

@@ -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<TagComponent, ComponentInit>(OnTagInit);
SubscribeLocalEvent<TagComponent, ComponentGetState>(OnTagGetState);
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)
{
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<TagPrototype>(id);
DebugTools.Assert(_proto.HasIndex<TagPrototype>(id), $"Unknown tag: {id}");
}
/// <summary>
@@ -304,7 +309,7 @@ public sealed class TagSystem : EntitySystem
/// </exception>
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
/// </exception>
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
/// </exception>
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);
}