From 82a97857acfa423cb2696299ee2d193e04da7f82 Mon Sep 17 00:00:00 2001 From: DrSmugleaf Date: Thu, 4 Feb 2021 11:04:19 +0100 Subject: [PATCH] Move TagComponent from server to shared (#3076) * Move TagComponent to shared * Fix test * Not a web edited commit No sir * Update Content.Shared/GameObjects/Components/Tag/TagComponentState.cs Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> --- Content.Client/IgnoredComponents.cs | 1 - Content.IntegrationTests/Tests/Tag/TagTest.cs | 7 +- .../Components/Tag/TagComponent.cs | 111 +++++++++++++----- .../Components/Tag/TagComponentExtensions.cs | 2 +- .../Components/Tag/TagComponentState.cs | 15 +++ Content.Shared/GameObjects/ContentNetIDs.cs | 1 + 6 files changed, 100 insertions(+), 37 deletions(-) rename {Content.Server => Content.Shared}/GameObjects/Components/Tag/TagComponent.cs (76%) rename {Content.Server => Content.Shared}/GameObjects/Components/Tag/TagComponentExtensions.cs (99%) create mode 100644 Content.Shared/GameObjects/Components/Tag/TagComponentState.cs diff --git a/Content.Client/IgnoredComponents.cs b/Content.Client/IgnoredComponents.cs index 0ad242868c..c176efa82d 100644 --- a/Content.Client/IgnoredComponents.cs +++ b/Content.Client/IgnoredComponents.cs @@ -228,7 +228,6 @@ namespace Content.Client "MachineFrame", "MachineBoard", "ChemicalAmmo", - "Tag", "BiologicalSurgeryData", "CargoTelepad", "TraitorDeathMatchRedemption", diff --git a/Content.IntegrationTests/Tests/Tag/TagTest.cs b/Content.IntegrationTests/Tests/Tag/TagTest.cs index 3159f38720..90b985fb91 100644 --- a/Content.IntegrationTests/Tests/Tag/TagTest.cs +++ b/Content.IntegrationTests/Tests/Tag/TagTest.cs @@ -1,7 +1,7 @@ #nullable enable using System.Collections.Generic; using System.Threading.Tasks; -using Content.Server.GameObjects.Components.Tag; +using Content.Shared.GameObjects.Components.Tag; using Content.Shared.Prototypes.Tag; using NUnit.Framework; using Robust.Shared.Interfaces.GameObjects; @@ -69,9 +69,8 @@ namespace Content.IntegrationTests.Tests.Tag { // Has one tag, the starting tag Assert.That(sTagComponent.Tags.Count, Is.EqualTo(1)); - - var startingTagPrototype = sPrototypeManager.Index(StartingTag); - Assert.That(sTagComponent.Tags, Contains.Item(startingTagPrototype)); + sPrototypeManager.Index(StartingTag); + Assert.That(sTagComponent.Tags, Contains.Item(StartingTag)); // Single Assert.True(sTagDummy.HasTag(StartingTag)); diff --git a/Content.Server/GameObjects/Components/Tag/TagComponent.cs b/Content.Shared/GameObjects/Components/Tag/TagComponent.cs similarity index 76% rename from Content.Server/GameObjects/Components/Tag/TagComponent.cs rename to Content.Shared/GameObjects/Components/Tag/TagComponent.cs index 4894b0c848..d01333b13d 100644 --- a/Content.Server/GameObjects/Components/Tag/TagComponent.cs +++ b/Content.Shared/GameObjects/Components/Tag/TagComponent.cs @@ -8,7 +8,7 @@ using Robust.Shared.Prototypes; using Robust.Shared.Serialization; using Robust.Shared.ViewVariables; -namespace Content.Server.GameObjects.Components.Tag +namespace Content.Shared.GameObjects.Components.Tag { [RegisterComponent] public class TagComponent : Component @@ -16,9 +16,9 @@ namespace Content.Server.GameObjects.Components.Tag public override string Name => "Tag"; [ViewVariables] - private readonly HashSet _tags = new(); + private readonly HashSet _tags = new(); - public IReadOnlySet Tags => _tags; + public IReadOnlySet Tags => _tags; public override void ExposeData(ObjectSerializer serializer) { @@ -38,17 +38,44 @@ namespace Content.Server.GameObjects.Components.Tag AddTags(ids); }, - () => - { - var ids = new HashSet(); + () => _tags); + } - foreach (var tag in _tags) - { - ids.Add(tag.ID); - } + public override ComponentState GetComponentState() + { + var tags = new string[_tags.Count]; + var i = 0; - return ids; - }); + foreach (var tag in _tags) + { + tags[i] = tag; + } + + return new TagComponentState(tags); + } + + public override void HandleComponentState(ComponentState? curState, ComponentState? nextState) + { + if (curState is not TagComponentState state) + { + return; + } + + _tags.Clear(); + + var prototypeManager = IoCManager.Resolve(); + + foreach (var tag in state.Tags) + { + GetTagOrThrow(tag, prototypeManager); + _tags.Add(tag); + } + } + + private TagPrototype GetTagOrThrow(string id, IPrototypeManager? manager = null) + { + manager ??= IoCManager.Resolve(); + return manager.Index(id); } /// @@ -61,9 +88,16 @@ namespace Content.Server.GameObjects.Components.Tag /// public bool AddTag(string id) { - var tag = IoCManager.Resolve().Index(id); + GetTagOrThrow(id); + var added = _tags.Add(id); - return _tags.Add(tag); + if (added) + { + Dirty(); + return true; + } + + return false; } /// @@ -94,12 +128,17 @@ namespace Content.Server.GameObjects.Components.Tag foreach (var id in ids) { - var tag = prototypeManager.Index(id); - - _tags.Add(tag); + GetTagOrThrow(id, prototypeManager); + _tags.Add(id); } - return _tags.Count > count; + if (_tags.Count > count) + { + Dirty(); + return true; + } + + return false; } /// @@ -112,9 +151,8 @@ namespace Content.Server.GameObjects.Components.Tag /// public bool HasTag(string id) { - var tag = IoCManager.Resolve().Index(id); - - return _tags.Contains(tag); + GetTagOrThrow(id); + return _tags.Contains(id); } /// @@ -144,9 +182,9 @@ namespace Content.Server.GameObjects.Components.Tag foreach (var id in ids) { - var tag = prototypeManager.Index(id); + GetTagOrThrow(id, prototypeManager); - if (!_tags.Contains(tag)) + if (!_tags.Contains(id)) { return false; } @@ -182,9 +220,9 @@ namespace Content.Server.GameObjects.Components.Tag foreach (var id in ids) { - var tag = prototypeManager.Index(id); + GetTagOrThrow(id, prototypeManager); - if (_tags.Contains(tag)) + if (_tags.Contains(id)) { return true; } @@ -205,9 +243,15 @@ namespace Content.Server.GameObjects.Components.Tag /// public bool RemoveTag(string id) { - var tag = IoCManager.Resolve().Index(id); + GetTagOrThrow(id); - return _tags.Remove(tag); + if (_tags.Remove(id)) + { + Dirty(); + return true; + } + + return false; } /// @@ -240,12 +284,17 @@ namespace Content.Server.GameObjects.Components.Tag foreach (var id in ids) { - var tag = prototypeManager.Index(id); - - _tags.Remove(tag); + GetTagOrThrow(id, prototypeManager); + _tags.Remove(id); } - return _tags.Count < count; + if (_tags.Count < count) + { + Dirty(); + return true; + } + + return false; } } } diff --git a/Content.Server/GameObjects/Components/Tag/TagComponentExtensions.cs b/Content.Shared/GameObjects/Components/Tag/TagComponentExtensions.cs similarity index 99% rename from Content.Server/GameObjects/Components/Tag/TagComponentExtensions.cs rename to Content.Shared/GameObjects/Components/Tag/TagComponentExtensions.cs index 437b70af88..9fbbb4fa45 100644 --- a/Content.Server/GameObjects/Components/Tag/TagComponentExtensions.cs +++ b/Content.Shared/GameObjects/Components/Tag/TagComponentExtensions.cs @@ -5,7 +5,7 @@ using Robust.Shared.GameObjects; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Prototypes; -namespace Content.Server.GameObjects.Components.Tag +namespace Content.Shared.GameObjects.Components.Tag { public static class TagComponentExtensions { diff --git a/Content.Shared/GameObjects/Components/Tag/TagComponentState.cs b/Content.Shared/GameObjects/Components/Tag/TagComponentState.cs new file mode 100644 index 0000000000..6c29189c72 --- /dev/null +++ b/Content.Shared/GameObjects/Components/Tag/TagComponentState.cs @@ -0,0 +1,15 @@ +#nullable enable +using Robust.Shared.GameObjects; + +namespace Content.Shared.GameObjects.Components.Tag +{ + public class TagComponentState : ComponentState + { + public TagComponentState(string[] tags) : base(ContentNetIDs.TAG) + { + Tags = tags; + } + + public string[] Tags { get; } + } +} diff --git a/Content.Shared/GameObjects/ContentNetIDs.cs b/Content.Shared/GameObjects/ContentNetIDs.cs index 4ce0dfa358..3c3388662c 100644 --- a/Content.Shared/GameObjects/ContentNetIDs.cs +++ b/Content.Shared/GameObjects/ContentNetIDs.cs @@ -90,6 +90,7 @@ public const uint ACTIONS = 1083; public const uint DAMAGEABLE = 1084; public const uint MAGBOOTS = 1085; + public const uint TAG = 1086; // Net IDs for integration tests. public const uint PREDICTION_TEST = 10001;