1
0

StationRecordSet.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Linq;
  3. using Content.Shared.StationRecords;
  4. using Robust.Shared.Utility;
  5. namespace Content.Server.StationRecords;
  6. /// <summary>
  7. /// Set of station records for a single station. StationRecordsComponent stores these.
  8. /// Keyed by the record id, which should be obtained from
  9. /// an entity that stores a reference to it.
  10. /// A StationRecordKey has both the station entity (use to get the record set) and id (use for this).
  11. /// </summary>
  12. [DataDefinition]
  13. public sealed partial class StationRecordSet
  14. {
  15. [DataField("currentRecordId")]
  16. private uint _currentRecordId;
  17. /// <summary>
  18. /// Every key id that has a record(s) stored.
  19. /// Presumably this is faster than iterating the dictionary to check if any tables have a key.
  20. /// </summary>
  21. [DataField]
  22. public HashSet<uint> Keys = new();
  23. /// <summary>
  24. /// Recently accessed key ids which are used to synchronize them efficiently.
  25. /// </summary>
  26. [DataField]
  27. private HashSet<uint> _recentlyAccessed = new();
  28. /// <summary>
  29. /// Dictionary between a record's type and then each record indexed by id.
  30. /// </summary>
  31. [DataField]
  32. private Dictionary<Type, Dictionary<uint, object>> _tables = new();
  33. /// <summary>
  34. /// Gets all records of a specific type stored in the record set.
  35. /// </summary>
  36. /// <typeparam name="T">The type of record to fetch.</typeparam>
  37. /// <returns>An enumerable object that contains a pair of both a station key, and the record associated with it.</returns>
  38. public IEnumerable<(uint, T)> GetRecordsOfType<T>()
  39. {
  40. if (!_tables.ContainsKey(typeof(T)))
  41. {
  42. yield break;
  43. }
  44. foreach (var (key, entry) in _tables[typeof(T)])
  45. {
  46. if (entry is not T cast)
  47. {
  48. continue;
  49. }
  50. _recentlyAccessed.Add(key);
  51. yield return (key, cast);
  52. }
  53. }
  54. /// <summary>
  55. /// Create a new record with an entry.
  56. /// Returns an id that can only be used to access the record for this station.
  57. /// </summary>
  58. /// <param name="entry">Entry to add.</param>
  59. /// <typeparam name="T">Type of the entry that's being added.</typeparam>
  60. public uint? AddRecordEntry<T>(T entry)
  61. {
  62. if (entry == null)
  63. return null;
  64. var key = _currentRecordId++;
  65. AddRecordEntry(key, entry);
  66. return key;
  67. }
  68. /// <summary>
  69. /// Add an entry into an existing record.
  70. /// </summary>
  71. /// <param name="key">Key id for the record.</param>
  72. /// <param name="entry">Entry to add.</param>
  73. /// <typeparam name="T">Type of the entry that's being added.</typeparam>
  74. public void AddRecordEntry<T>(uint key, T entry)
  75. {
  76. if (entry == null)
  77. return;
  78. Keys.Add(key);
  79. _tables.GetOrNew(typeof(T))[key] = entry;
  80. }
  81. /// <summary>
  82. /// Try to get an record entry by type, from this record key.
  83. /// </summary>
  84. /// <param name="key">The record id to get the entries from.</param>
  85. /// <param name="entry">The entry that is retrieved from the record set.</param>
  86. /// <typeparam name="T">The type of entry to search for.</typeparam>
  87. /// <returns>True if the record exists and was retrieved, false otherwise.</returns>
  88. public bool TryGetRecordEntry<T>(uint key, [NotNullWhen(true)] out T? entry)
  89. {
  90. entry = default;
  91. if (!Keys.Contains(key)
  92. || !_tables.TryGetValue(typeof(T), out var table)
  93. || !table.TryGetValue(key, out var entryObject))
  94. {
  95. return false;
  96. }
  97. entry = (T) entryObject;
  98. _recentlyAccessed.Add(key);
  99. return true;
  100. }
  101. /// <summary>
  102. /// Checks if the record associated with this key has an entry of a certain type.
  103. /// </summary>
  104. /// <param name="key">The record key id.</param>
  105. /// <typeparam name="T">Type to check.</typeparam>
  106. /// <returns>True if the entry exists, false otherwise.</returns>
  107. public bool HasRecordEntry<T>(uint key)
  108. {
  109. return Keys.Contains(key)
  110. && _tables.TryGetValue(typeof(T), out var table)
  111. && table.ContainsKey(key);
  112. }
  113. /// <summary>
  114. /// Get the recently accessed keys from this record set.
  115. /// </summary>
  116. /// <returns>All recently accessed keys from this record set.</returns>
  117. public IEnumerable<uint> GetRecentlyAccessed()
  118. {
  119. return _recentlyAccessed.ToArray();
  120. }
  121. /// <summary>
  122. /// Clears the recently accessed keys from the set.
  123. /// </summary>
  124. public void ClearRecentlyAccessed()
  125. {
  126. _recentlyAccessed.Clear();
  127. }
  128. /// <summary>
  129. /// Removes a recently accessed key from the set.
  130. /// </summary>
  131. public void RemoveFromRecentlyAccessed(uint key)
  132. {
  133. _recentlyAccessed.Remove(key);
  134. }
  135. /// <summary>
  136. /// Removes all record entries related to this key from this set.
  137. /// </summary>
  138. /// <param name="key">The key to remove.</param>
  139. /// <returns>True if successful, false otherwise.</returns>
  140. public bool RemoveAllRecords(uint key)
  141. {
  142. if (!Keys.Remove(key))
  143. return false;
  144. foreach (var table in _tables.Values)
  145. {
  146. table.Remove(key);
  147. }
  148. return true;
  149. }
  150. }