using Content.Shared.Atmos.Components; using Robust.Shared.GameStates; using Robust.Shared.Serialization; namespace Content.Shared.Atmos.Consoles; public abstract class SharedAtmosMonitoringConsoleSystem : EntitySystem { public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnGetState); } private void OnGetState(EntityUid uid, AtmosMonitoringConsoleComponent component, ref ComponentGetState args) { Dictionary> chunks; // Should this be a full component state or a delta-state? if (args.FromTick <= component.CreationTick || component.ForceFullUpdate) { component.ForceFullUpdate = false; // Full state chunks = new(component.AtmosPipeChunks.Count); foreach (var (origin, chunk) in component.AtmosPipeChunks) { chunks.Add(origin, chunk.AtmosPipeData); } args.State = new AtmosMonitoringConsoleState(chunks, component.AtmosDevices); return; } chunks = new(); foreach (var (origin, chunk) in component.AtmosPipeChunks) { if (chunk.LastUpdate < args.FromTick) continue; chunks.Add(origin, chunk.AtmosPipeData); } args.State = new AtmosMonitoringConsoleDeltaState(chunks, component.AtmosDevices, new(component.AtmosPipeChunks.Keys)); } #region: System messages [Serializable, NetSerializable] protected sealed class AtmosMonitoringConsoleState( Dictionary> chunks, Dictionary atmosDevices) : ComponentState { public Dictionary> Chunks = chunks; public Dictionary AtmosDevices = atmosDevices; } [Serializable, NetSerializable] protected sealed class AtmosMonitoringConsoleDeltaState( Dictionary> modifiedChunks, Dictionary atmosDevices, HashSet allChunks) : ComponentState, IComponentDeltaState { public Dictionary> ModifiedChunks = modifiedChunks; public Dictionary AtmosDevices = atmosDevices; public HashSet AllChunks = allChunks; public void ApplyToFullState(AtmosMonitoringConsoleState state) { foreach (var key in state.Chunks.Keys) { if (!AllChunks!.Contains(key)) state.Chunks.Remove(key); } foreach (var (index, data) in ModifiedChunks) { state.Chunks[index] = new Dictionary(data); } state.AtmosDevices.Clear(); foreach (var (nuid, atmosDevice) in AtmosDevices) { state.AtmosDevices.Add(nuid, atmosDevice); } } public AtmosMonitoringConsoleState CreateNewFullState(AtmosMonitoringConsoleState state) { var chunks = new Dictionary>(state.Chunks.Count); foreach (var (index, data) in state.Chunks) { if (!AllChunks!.Contains(index)) continue; if (ModifiedChunks.ContainsKey(index)) chunks[index] = new Dictionary(ModifiedChunks[index]); else chunks[index] = new Dictionary(state.Chunks[index]); } return new AtmosMonitoringConsoleState(chunks, new(AtmosDevices)); } } #endregion }