forked from space-wizards/space-station-14
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LocalizedDatasetPrototype (space-wizards#28310)
- Loading branch information
Showing
3 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System.Collections; | ||
using Robust.Shared.Prototypes; | ||
using Robust.Shared.Serialization; | ||
|
||
namespace Content.Shared.Dataset; | ||
|
||
/// <summary> | ||
/// A variant of <see cref="DatasetPrototype"/> intended to specify a sequence of LocId strings | ||
/// without having to copy-paste a ton of LocId strings into the YAML. | ||
/// </summary> | ||
[Prototype] | ||
public sealed partial class LocalizedDatasetPrototype : IPrototype | ||
{ | ||
/// <summary> | ||
/// Identifier for this prototype. | ||
/// </summary> | ||
[ViewVariables] | ||
[IdDataField] | ||
public string ID { get; private set; } = default!; | ||
|
||
/// <summary> | ||
/// Collection of LocId strings. | ||
/// </summary> | ||
[DataField] | ||
public LocalizedDatasetValues Values { get; private set; } = []; | ||
} | ||
|
||
[Serializable, NetSerializable] | ||
[DataDefinition] | ||
public sealed partial class LocalizedDatasetValues : IReadOnlyList<string> | ||
{ | ||
/// <summary> | ||
/// String prepended to the index number to generate each LocId string. | ||
/// For example, a prefix of <c>tips-dataset-</c> will generate <c>tips-dataset-1</c>, | ||
/// <c>tips-dataset-2</c>, etc. | ||
/// </summary> | ||
[DataField(required: true)] | ||
public string Prefix { get; private set; } = default!; | ||
|
||
/// <summary> | ||
/// How many values are in the dataset. | ||
/// </summary> | ||
[DataField(required: true)] | ||
public int Count { get; private set; } | ||
|
||
public string this[int index] | ||
{ | ||
get | ||
{ | ||
if (index > Count || index < 0) | ||
throw new IndexOutOfRangeException(); | ||
return Prefix + index; | ||
} | ||
} | ||
|
||
public IEnumerator<string> GetEnumerator() | ||
{ | ||
return new Enumerator(this); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
|
||
public sealed class Enumerator : IEnumerator<string> | ||
{ | ||
private int _index = 0; // Whee, 1-indexing | ||
|
||
private readonly LocalizedDatasetValues _values; | ||
|
||
public Enumerator(LocalizedDatasetValues values) | ||
{ | ||
_values = values; | ||
} | ||
|
||
public string Current => _values.Prefix + _index; | ||
|
||
object IEnumerator.Current => Current; | ||
|
||
public void Dispose() { } | ||
|
||
public bool MoveNext() | ||
{ | ||
_index++; | ||
return _index <= _values.Count; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
_index = 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using Content.Shared.Dataset; | ||
using NUnit.Framework; | ||
using Robust.Shared.Collections; | ||
using Robust.Shared.IoC; | ||
using Robust.Shared.Prototypes; | ||
using Robust.Shared.Serialization.Manager; | ||
|
||
namespace Content.Tests.Shared; | ||
|
||
[TestFixture] | ||
[TestOf(typeof(LocalizedDatasetPrototype))] | ||
public sealed class LocalizedDatasetPrototypeTest : ContentUnitTest | ||
{ | ||
private IPrototypeManager _prototypeManager; | ||
|
||
[OneTimeSetUp] | ||
public void OneTimeSetup() | ||
{ | ||
IoCManager.Resolve<ISerializationManager>().Initialize(); | ||
_prototypeManager = IoCManager.Resolve<IPrototypeManager>(); | ||
_prototypeManager.Initialize(); | ||
_prototypeManager.LoadString(TestPrototypes); | ||
_prototypeManager.ResolveResults(); | ||
} | ||
|
||
private const string TestPrototypes = @" | ||
- type: localizedDataset | ||
id: Test | ||
values: | ||
prefix: test-dataset- | ||
count: 4 | ||
"; | ||
|
||
[Test] | ||
public void LocalizedDatasetTest() | ||
{ | ||
var testPrototype = _prototypeManager.Index<LocalizedDatasetPrototype>("Test"); | ||
var values = new ValueList<string>(); | ||
foreach (var value in testPrototype.Values) | ||
{ | ||
values.Add(value); | ||
} | ||
|
||
// Make sure we get the right number of values | ||
Assert.That(values, Has.Count.EqualTo(4)); | ||
|
||
// Make sure indexing works as expected | ||
Assert.That(values[0], Is.EqualTo("test-dataset-1")); | ||
Assert.That(values[1], Is.EqualTo("test-dataset-2")); | ||
Assert.That(values[2], Is.EqualTo("test-dataset-3")); | ||
Assert.That(values[3], Is.EqualTo("test-dataset-4")); | ||
Assert.Throws<IndexOutOfRangeException>(() => { var x = values[4]; }); | ||
Assert.Throws<IndexOutOfRangeException>(() => { var x = values[-1]; }); | ||
|
||
// Make sure that the enumerator gets all of the values | ||
Assert.That(testPrototype.Values[testPrototype.Values.Count], Is.EqualTo("test-dataset-4")); | ||
} | ||
} |