using System.Diagnostics.CodeAnalysis; namespace Content.Shared.StationRecords; public abstract class SharedStationRecordsSystem : EntitySystem { public StationRecordKey? Convert((NetEntity, uint)? input) { return input == null ? null : Convert(input.Value); } public (NetEntity, uint)? Convert(StationRecordKey? input) { return input == null ? null : Convert(input.Value); } public StationRecordKey Convert((NetEntity, uint) input) { return new StationRecordKey(input.Item2, GetEntity(input.Item1)); } public (NetEntity, uint) Convert(StationRecordKey input) { return (GetNetEntity(input.OriginStation), input.Id); } public List<(NetEntity, uint)> Convert(ICollection input) { var result = new List<(NetEntity, uint)>(input.Count); foreach (var entry in input) { result.Add(Convert(entry)); } return result; } public List Convert(ICollection<(NetEntity, uint)> input) { var result = new List(input.Count); foreach (var entry in input) { result.Add(Convert(entry)); } return result; } /// /// Try to get a record from this station's record entries, /// from the provided station record key. Will always return /// null if the key does not match the station. /// /// Station and key to try and index from the record set. /// The resulting entry. /// Station record component. /// Type to get from the record set. /// True if the record was obtained, false otherwise. Always false on client. public bool TryGetRecord(StationRecordKey key, [NotNullWhen(true)] out T? entry, StationRecordsComponent? records = null) { entry = default; if (!Resolve(key.OriginStation, ref records)) return false; return records.Records.TryGetRecordEntry(key.Id, out entry); } /// /// Gets all records of a specific type from a station. /// /// The station to get the records from. /// Station records component. /// Type of record to fetch /// Enumerable of pairs with a station record key, and the entry in question of type T. Always empty on client. public IEnumerable<(uint, T)> GetRecordsOfType(EntityUid station, StationRecordsComponent? records = null) { if (!Resolve(station, ref records)) return Array.Empty<(uint, T)>(); return records.Records.GetRecordsOfType(); } /// /// Returns an id if a record with the same name exists. /// /// /// Linear search so O(n) time complexity. /// /// Returns a station record id. Always null on client. public uint? GetRecordByName(EntityUid station, string name, StationRecordsComponent? records = null) { if (!Resolve(station, ref records, false)) return null; foreach (var (id, record) in GetRecordsOfType(station, records)) { if (record.Name == name) return id; } return null; } }