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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 100 additions and 37 deletions

View File

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

View File

@ -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<TagPrototype>(StartingTag);
Assert.That(sTagComponent.Tags, Contains.Item(startingTagPrototype));
sPrototypeManager.Index<TagPrototype>(StartingTag);
Assert.That(sTagComponent.Tags, Contains.Item(StartingTag));
// Single
Assert.True(sTagDummy.HasTag(StartingTag));

View File

@ -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<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)
{
@ -38,17 +38,44 @@ namespace Content.Server.GameObjects.Components.Tag
AddTags(ids);
},
() =>
{
var ids = new HashSet<string>();
() => _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<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>
@ -61,9 +88,16 @@ namespace Content.Server.GameObjects.Components.Tag
/// </exception>
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>
@ -94,12 +128,17 @@ namespace Content.Server.GameObjects.Components.Tag
foreach (var id in ids)
{
var tag = prototypeManager.Index<TagPrototype>(id);
_tags.Add(tag);
GetTagOrThrow(id, prototypeManager);
_tags.Add(id);
}
return _tags.Count > count;
if (_tags.Count > count)
{
Dirty();
return true;
}
return false;
}
/// <summary>
@ -112,9 +151,8 @@ namespace Content.Server.GameObjects.Components.Tag
/// </exception>
public bool HasTag(string id)
{
var tag = IoCManager.Resolve<IPrototypeManager>().Index<TagPrototype>(id);
return _tags.Contains(tag);
GetTagOrThrow(id);
return _tags.Contains(id);
}
/// <summary>
@ -144,9 +182,9 @@ namespace Content.Server.GameObjects.Components.Tag
foreach (var id in ids)
{
var tag = prototypeManager.Index<TagPrototype>(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<TagPrototype>(id);
GetTagOrThrow(id, prototypeManager);
if (_tags.Contains(tag))
if (_tags.Contains(id))
{
return true;
}
@ -205,9 +243,15 @@ namespace Content.Server.GameObjects.Components.Tag
/// </exception>
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>
@ -240,12 +284,17 @@ namespace Content.Server.GameObjects.Components.Tag
foreach (var id in ids)
{
var tag = prototypeManager.Index<TagPrototype>(id);
_tags.Remove(tag);
GetTagOrThrow(id, prototypeManager);
_tags.Remove(id);
}
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.Prototypes;
namespace Content.Server.GameObjects.Components.Tag
namespace Content.Shared.GameObjects.Components.Tag
{
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 DAMAGEABLE = 1084;
public const uint MAGBOOTS = 1085;
public const uint TAG = 1086;
// Net IDs for integration tests.
public const uint PREDICTION_TEST = 10001;