ReagentId.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Content.Shared.FixedPoint;
  2. using Robust.Shared.Serialization;
  3. using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
  4. using System.Linq;
  5. namespace Content.Shared.Chemistry.Reagent;
  6. /// <summary>
  7. /// Struct used to uniquely identify a reagent. This is usually just a ReagentPrototype id string, however some reagents
  8. /// contain additional data (e.g., blood could store DNA data).
  9. /// </summary>
  10. [Serializable, NetSerializable]
  11. [DataDefinition]
  12. public partial struct ReagentId : IEquatable<ReagentId>
  13. {
  14. // TODO rename data field.
  15. [DataField("ReagentId", customTypeSerializer: typeof(PrototypeIdSerializer<ReagentPrototype>), required: true)]
  16. public string Prototype { get; private set; }
  17. /// <summary>
  18. /// Any additional data that is unique to this reagent type. E.g., for blood this could be DNA data.
  19. /// </summary>
  20. [DataField("data")]
  21. public List<ReagentData>? Data { get; private set; } = new();
  22. public ReagentId(string prototype, List<ReagentData>? data)
  23. {
  24. Prototype = prototype;
  25. Data = data ?? new();
  26. }
  27. public ReagentId()
  28. {
  29. Prototype = default!;
  30. Data = new();
  31. }
  32. public List<ReagentData> EnsureReagentData()
  33. {
  34. return (Data != null) ? Data : new List<ReagentData>();
  35. }
  36. public bool Equals(ReagentId other)
  37. {
  38. if (Prototype != other.Prototype)
  39. return false;
  40. if (Data == null)
  41. return other.Data == null;
  42. if (other.Data == null)
  43. return false;
  44. if (Data.Except(other.Data).Any() || other.Data.Except(Data).Any() || Data.Count != other.Data.Count)
  45. return false;
  46. return true;
  47. }
  48. public bool Equals(string prototype, List<ReagentData>? otherData = null)
  49. {
  50. if (Prototype != prototype)
  51. return false;
  52. if (Data == null)
  53. return otherData == null;
  54. return Data.Equals(otherData);
  55. }
  56. public override bool Equals(object? obj)
  57. {
  58. return obj is ReagentId other && Equals(other);
  59. }
  60. public override int GetHashCode()
  61. {
  62. return HashCode.Combine(Prototype, Data);
  63. }
  64. public string ToString(FixedPoint2 quantity)
  65. {
  66. return $"{Prototype}:{quantity}";
  67. }
  68. public override string ToString()
  69. {
  70. return $"{Prototype}";
  71. }
  72. public static bool operator ==(ReagentId left, ReagentId right)
  73. {
  74. return left.Equals(right);
  75. }
  76. public static bool operator !=(ReagentId left, ReagentId right)
  77. {
  78. return !(left == right);
  79. }
  80. }