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>
This commit is contained in:
DrSmugleaf
2021-02-04 11:04:19 +01:00
committed by GitHub
parent b9aa789bc4
commit 82a97857ac
6 changed files with 100 additions and 37 deletions

View File

@@ -228,7 +228,6 @@ namespace Content.Client
"MachineFrame", "MachineFrame",
"MachineBoard", "MachineBoard",
"ChemicalAmmo", "ChemicalAmmo",
"Tag",
"BiologicalSurgeryData", "BiologicalSurgeryData",
"CargoTelepad", "CargoTelepad",
"TraitorDeathMatchRedemption", "TraitorDeathMatchRedemption",

View File

@@ -1,7 +1,7 @@
#nullable enable #nullable enable
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Content.Server.GameObjects.Components.Tag; using Content.Shared.GameObjects.Components.Tag;
using Content.Shared.Prototypes.Tag; using Content.Shared.Prototypes.Tag;
using NUnit.Framework; using NUnit.Framework;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
@@ -69,9 +69,8 @@ namespace Content.IntegrationTests.Tests.Tag
{ {
// Has one tag, the starting tag // Has one tag, the starting tag
Assert.That(sTagComponent.Tags.Count, Is.EqualTo(1)); Assert.That(sTagComponent.Tags.Count, Is.EqualTo(1));
sPrototypeManager.Index<TagPrototype>(StartingTag);
var startingTagPrototype = sPrototypeManager.Index<TagPrototype>(StartingTag); Assert.That(sTagComponent.Tags, Contains.Item(StartingTag));
Assert.That(sTagComponent.Tags, Contains.Item(startingTagPrototype));
// Single // Single
Assert.True(sTagDummy.HasTag(StartingTag)); Assert.True(sTagDummy.HasTag(StartingTag));

View File

@@ -8,7 +8,7 @@ using Robust.Shared.Prototypes;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Tag namespace Content.Shared.GameObjects.Components.Tag
{ {
[RegisterComponent] [RegisterComponent]
public class TagComponent : Component public class TagComponent : Component
@@ -16,9 +16,9 @@ namespace Content.Server.GameObjects.Components.Tag
public override string Name => "Tag"; public override string Name => "Tag";
[ViewVariables] [ViewVariables]
private readonly HashSet<TagPrototype> _tags = new(); private readonly HashSet<string> _tags = new();
public IReadOnlySet<TagPrototype> Tags => _tags; public IReadOnlySet<string> Tags => _tags;
public override void ExposeData(ObjectSerializer serializer) public override void ExposeData(ObjectSerializer serializer)
{ {
@@ -38,17 +38,44 @@ namespace Content.Server.GameObjects.Components.Tag
AddTags(ids); AddTags(ids);
}, },
() => () => _tags);
{ }
var ids = new HashSet<string>();
foreach (var tag in _tags) public override ComponentState GetComponentState()
{ {
ids.Add(tag.ID); 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<IPrototypeManager>();
foreach (var tag in state.Tags)
{
GetTagOrThrow(tag, prototypeManager);
_tags.Add(tag);
}
}
private TagPrototype GetTagOrThrow(string id, IPrototypeManager? manager = null)
{
manager ??= IoCManager.Resolve<IPrototypeManager>();
return manager.Index<TagPrototype>(id);
} }
/// <summary> /// <summary>
@@ -61,9 +88,16 @@ namespace Content.Server.GameObjects.Components.Tag
/// </exception> /// </exception>
public bool AddTag(string id) public bool AddTag(string id)
{ {
var tag = IoCManager.Resolve<IPrototypeManager>().Index<TagPrototype>(id); GetTagOrThrow(id);
var added = _tags.Add(id);
return _tags.Add(tag); if (added)
{
Dirty();
return true;
}
return false;
} }
/// <summary> /// <summary>
@@ -94,12 +128,17 @@ namespace Content.Server.GameObjects.Components.Tag
foreach (var id in ids) foreach (var id in ids)
{ {
var tag = prototypeManager.Index<TagPrototype>(id); GetTagOrThrow(id, prototypeManager);
_tags.Add(id);
_tags.Add(tag);
} }
return _tags.Count > count; if (_tags.Count > count)
{
Dirty();
return true;
}
return false;
} }
/// <summary> /// <summary>
@@ -112,9 +151,8 @@ namespace Content.Server.GameObjects.Components.Tag
/// </exception> /// </exception>
public bool HasTag(string id) public bool HasTag(string id)
{ {
var tag = IoCManager.Resolve<IPrototypeManager>().Index<TagPrototype>(id); GetTagOrThrow(id);
return _tags.Contains(id);
return _tags.Contains(tag);
} }
/// <summary> /// <summary>
@@ -144,9 +182,9 @@ namespace Content.Server.GameObjects.Components.Tag
foreach (var id in ids) foreach (var id in ids)
{ {
var tag = prototypeManager.Index<TagPrototype>(id); GetTagOrThrow(id, prototypeManager);
if (!_tags.Contains(tag)) if (!_tags.Contains(id))
{ {
return false; return false;
} }
@@ -182,9 +220,9 @@ namespace Content.Server.GameObjects.Components.Tag
foreach (var id in ids) foreach (var id in ids)
{ {
var tag = prototypeManager.Index<TagPrototype>(id); GetTagOrThrow(id, prototypeManager);
if (_tags.Contains(tag)) if (_tags.Contains(id))
{ {
return true; return true;
} }
@@ -205,9 +243,15 @@ namespace Content.Server.GameObjects.Components.Tag
/// </exception> /// </exception>
public bool RemoveTag(string id) public bool RemoveTag(string id)
{ {
var tag = IoCManager.Resolve<IPrototypeManager>().Index<TagPrototype>(id); GetTagOrThrow(id);
return _tags.Remove(tag); if (_tags.Remove(id))
{
Dirty();
return true;
}
return false;
} }
/// <summary> /// <summary>
@@ -240,12 +284,17 @@ namespace Content.Server.GameObjects.Components.Tag
foreach (var id in ids) foreach (var id in ids)
{ {
var tag = prototypeManager.Index<TagPrototype>(id); GetTagOrThrow(id, prototypeManager);
_tags.Remove(id);
_tags.Remove(tag);
} }
return _tags.Count < count; if (_tags.Count < count)
{
Dirty();
return true;
}
return false;
} }
} }
} }

View File

@@ -5,7 +5,7 @@ using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
namespace Content.Server.GameObjects.Components.Tag namespace Content.Shared.GameObjects.Components.Tag
{ {
public static class TagComponentExtensions public static class TagComponentExtensions
{ {

View File

@@ -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; }
}
}

View File

@@ -90,6 +90,7 @@
public const uint ACTIONS = 1083; public const uint ACTIONS = 1083;
public const uint DAMAGEABLE = 1084; public const uint DAMAGEABLE = 1084;
public const uint MAGBOOTS = 1085; public const uint MAGBOOTS = 1085;
public const uint TAG = 1086;
// Net IDs for integration tests. // Net IDs for integration tests.
public const uint PREDICTION_TEST = 10001; public const uint PREDICTION_TEST = 10001;