AlertKey.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using Robust.Shared.Prototypes;
  2. using Robust.Shared.Serialization;
  3. namespace Content.Shared.Alert;
  4. /// <summary>
  5. /// Key for an alert which is unique (for equality and hashcode purposes) w.r.t category semantics.
  6. /// I.e., entirely defined by the category, if a category was specified, otherwise
  7. /// falls back to the id.
  8. /// </summary>
  9. [Serializable, NetSerializable]
  10. public struct AlertKey
  11. {
  12. public ProtoId<AlertPrototype>? AlertType { get; private set; } = default!;
  13. public readonly ProtoId<AlertCategoryPrototype>? AlertCategory;
  14. /// NOTE: if the alert has a category you must pass the category for this to work
  15. /// properly as a key. I.e. if the alert has a category and you pass only the alert type, and you
  16. /// compare this to another AlertKey that has both the category and the same alert type, it will not consider them equal.
  17. public AlertKey(ProtoId<AlertPrototype>? alertType, ProtoId<AlertCategoryPrototype>? alertCategory)
  18. {
  19. AlertCategory = alertCategory;
  20. AlertType = alertType;
  21. }
  22. public bool Equals(AlertKey other)
  23. {
  24. // compare only on alert category if we have one
  25. if (AlertCategory.HasValue)
  26. {
  27. return other.AlertCategory == AlertCategory;
  28. }
  29. return AlertType == other.AlertType && AlertCategory == other.AlertCategory;
  30. }
  31. public override bool Equals(object? obj)
  32. {
  33. return obj is AlertKey other && Equals(other);
  34. }
  35. public override int GetHashCode()
  36. {
  37. // use only alert category if we have one
  38. if (AlertCategory.HasValue) return AlertCategory.GetHashCode();
  39. return AlertType.GetHashCode();
  40. }
  41. /// <param name="category">alert category, must not be null</param>
  42. /// <returns>An alert key for the provided alert category. This must only be used for
  43. /// queries and never storage, as it is lacking an alert type.</returns>
  44. public static AlertKey ForCategory(ProtoId<AlertCategoryPrototype> category)
  45. {
  46. return new(null, category);
  47. }
  48. }