1
0

LocalizedDatasetPrototype.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Collections;
  2. using Robust.Shared.Prototypes;
  3. using Robust.Shared.Serialization;
  4. namespace Content.Shared.Dataset;
  5. /// <summary>
  6. /// A variant of <see cref="DatasetPrototype"/> intended to specify a sequence of LocId strings
  7. /// without having to copy-paste a ton of LocId strings into the YAML.
  8. /// </summary>
  9. [Prototype]
  10. public sealed partial class LocalizedDatasetPrototype : IPrototype
  11. {
  12. /// <summary>
  13. /// Identifier for this prototype.
  14. /// </summary>
  15. [ViewVariables]
  16. [IdDataField]
  17. public string ID { get; private set; } = default!;
  18. /// <summary>
  19. /// Collection of LocId strings.
  20. /// </summary>
  21. [DataField]
  22. public LocalizedDatasetValues Values { get; private set; } = [];
  23. }
  24. [Serializable, NetSerializable]
  25. [DataDefinition]
  26. public sealed partial class LocalizedDatasetValues : IReadOnlyList<string>
  27. {
  28. /// <summary>
  29. /// String prepended to the index number to generate each LocId string.
  30. /// For example, a prefix of <c>tips-dataset-</c> will generate <c>tips-dataset-1</c>,
  31. /// <c>tips-dataset-2</c>, etc.
  32. /// </summary>
  33. [DataField(required: true)]
  34. public string Prefix { get; private set; } = default!;
  35. /// <summary>
  36. /// How many values are in the dataset.
  37. /// </summary>
  38. [DataField(required: true)]
  39. public int Count { get; private set; }
  40. public string this[int index]
  41. {
  42. get
  43. {
  44. if (index >= Count || index < 0)
  45. throw new IndexOutOfRangeException();
  46. return Prefix + (index + 1);
  47. }
  48. }
  49. public IEnumerator<string> GetEnumerator()
  50. {
  51. return new Enumerator(this);
  52. }
  53. IEnumerator IEnumerable.GetEnumerator()
  54. {
  55. return GetEnumerator();
  56. }
  57. public sealed class Enumerator : IEnumerator<string>
  58. {
  59. private int _index = 0; // Whee, 1-indexing
  60. private readonly LocalizedDatasetValues _values;
  61. public Enumerator(LocalizedDatasetValues values)
  62. {
  63. _values = values;
  64. }
  65. public string Current => _values.Prefix + _index;
  66. object IEnumerator.Current => Current;
  67. public void Dispose() { }
  68. public bool MoveNext()
  69. {
  70. _index++;
  71. return _index <= _values.Count;
  72. }
  73. public void Reset()
  74. {
  75. _index = 0;
  76. }
  77. }
  78. }