Marking.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System.Linq;
  2. using Content.Shared.Humanoid.Prototypes;
  3. using Robust.Shared.Prototypes;
  4. using Robust.Shared.Serialization;
  5. namespace Content.Shared.Humanoid.Markings
  6. {
  7. [DataDefinition]
  8. [Serializable, NetSerializable]
  9. public sealed partial class Marking : IEquatable<Marking>, IComparable<Marking>, IComparable<string>
  10. {
  11. [DataField("markingColor")]
  12. private List<Color> _markingColors = new();
  13. private Marking()
  14. {
  15. }
  16. public Marking(string markingId,
  17. List<Color> markingColors)
  18. {
  19. MarkingId = markingId;
  20. _markingColors = markingColors;
  21. }
  22. public Marking(string markingId,
  23. IReadOnlyList<Color> markingColors)
  24. : this(markingId, new List<Color>(markingColors))
  25. {
  26. }
  27. public Marking(string markingId, int colorCount)
  28. {
  29. MarkingId = markingId;
  30. List<Color> colors = new();
  31. for (int i = 0; i < colorCount; i++)
  32. colors.Add(Color.White);
  33. _markingColors = colors;
  34. }
  35. public Marking(Marking other)
  36. {
  37. MarkingId = other.MarkingId;
  38. _markingColors = new(other.MarkingColors);
  39. Visible = other.Visible;
  40. Forced = other.Forced;
  41. }
  42. /// <summary>
  43. /// ID of the marking prototype.
  44. /// </summary>
  45. [DataField("markingId", required: true)]
  46. public string MarkingId { get; private set; } = default!;
  47. /// <summary>
  48. /// All colors currently on this marking.
  49. /// </summary>
  50. [ViewVariables]
  51. public IReadOnlyList<Color> MarkingColors => _markingColors;
  52. /// <summary>
  53. /// If this marking is currently visible.
  54. /// </summary>
  55. [DataField("visible")]
  56. public bool Visible = true;
  57. /// <summary>
  58. /// If this marking should be forcefully applied, regardless of points.
  59. /// </summary>
  60. [ViewVariables]
  61. public bool Forced;
  62. public void SetColor(int colorIndex, Color color) =>
  63. _markingColors[colorIndex] = color;
  64. public void SetColor(Color color)
  65. {
  66. for (int i = 0; i < _markingColors.Count; i++)
  67. {
  68. _markingColors[i] = color;
  69. }
  70. }
  71. public int CompareTo(Marking? marking)
  72. {
  73. if (marking == null)
  74. {
  75. return 1;
  76. }
  77. return string.Compare(MarkingId, marking.MarkingId, StringComparison.Ordinal);
  78. }
  79. public int CompareTo(string? markingId)
  80. {
  81. if (markingId == null)
  82. return 1;
  83. return string.Compare(MarkingId, markingId, StringComparison.Ordinal);
  84. }
  85. public bool Equals(Marking? other)
  86. {
  87. if (other == null)
  88. {
  89. return false;
  90. }
  91. return MarkingId.Equals(other.MarkingId)
  92. && _markingColors.SequenceEqual(other._markingColors)
  93. && Visible.Equals(other.Visible)
  94. && Forced.Equals(other.Forced);
  95. }
  96. // VERY BIG TODO: TURN THIS INTO JSONSERIALIZER IMPLEMENTATION
  97. // look this could be better but I don't think serializing
  98. // colors is the correct thing to do
  99. //
  100. // this is still janky imo but serializing a color and feeding
  101. // it into the default JSON serializer (which is just *fine*)
  102. // doesn't seem to have compatible interfaces? this 'works'
  103. // for now but should eventually be improved so that this can,
  104. // in fact just be serialized through a convenient interface
  105. new public string ToString()
  106. {
  107. // reserved character
  108. string sanitizedName = this.MarkingId.Replace('@', '_');
  109. List<string> colorStringList = new();
  110. foreach (Color color in _markingColors)
  111. colorStringList.Add(color.ToHex());
  112. return $"{sanitizedName}@{String.Join(',', colorStringList)}";
  113. }
  114. public static Marking? ParseFromDbString(string input)
  115. {
  116. if (input.Length == 0) return null;
  117. var split = input.Split('@');
  118. if (split.Length != 2) return null;
  119. List<Color> colorList = new();
  120. foreach (string color in split[1].Split(','))
  121. colorList.Add(Color.FromHex(color));
  122. return new Marking(split[0], colorList);
  123. }
  124. }
  125. }