]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Content changes for engine delta-state PR (#28134)
authorLeon Friedrich <60421075+ElectroJr@users.noreply.github.com>
Fri, 24 May 2024 04:08:23 +0000 (16:08 +1200)
committerGitHub <noreply@github.com>
Fri, 24 May 2024 04:08:23 +0000 (14:08 +1000)
* Update GasTileOverlayState

* Update DecalGridState

* Update NavMapState

* poke

* poke2

* poke3

* Poke dem tests

Content.Client/Atmos/EntitySystems/GasTileOverlaySystem.cs
Content.Client/Decals/DecalSystem.cs
Content.Client/Pinpointer/NavMapSystem.cs
Content.Shared/Atmos/Components/GasTileOverlayComponent.cs
Content.Shared/Atmos/EntitySystems/SharedGasTileOverlaySystem.cs
Content.Shared/Decals/DecalGridComponent.cs
Content.Shared/Decals/SharedDecalSystem.cs
Content.Shared/Pinpointer/SharedNavMapSystem.cs

index 78185ce6b0e679f9599e38dcf4595cfd1c59a8ac..86cf0a9eb82448984b55363afd8c656a2f8c4cf5 100644 (file)
@@ -1,4 +1,5 @@
 using Content.Client.Atmos.Overlays;
+using Content.Shared.Atmos;
 using Content.Shared.Atmos.Components;
 using Content.Shared.Atmos.EntitySystems;
 using JetBrains.Annotations;
@@ -36,28 +37,38 @@ namespace Content.Client.Atmos.EntitySystems
 
         private void OnHandleState(EntityUid gridUid, GasTileOverlayComponent comp, ref ComponentHandleState args)
         {
-            if (args.Current is not GasTileOverlayState state)
-                return;
+            Dictionary<Vector2i, GasOverlayChunk> modifiedChunks;
 
-            // is this a delta or full state?
-            if (!state.FullState)
+            switch (args.Current)
             {
-                foreach (var index in comp.Chunks.Keys)
+                // is this a delta or full state?
+                case GasTileOverlayDeltaState delta:
                 {
-                    if (!state.AllChunks!.Contains(index))
-                        comp.Chunks.Remove(index);
+                    modifiedChunks = delta.ModifiedChunks;
+                    foreach (var index in comp.Chunks.Keys)
+                    {
+                        if (!delta.AllChunks.Contains(index))
+                            comp.Chunks.Remove(index);
+                    }
+
+                    break;
                 }
-            }
-            else
-            {
-                foreach (var index in comp.Chunks.Keys)
+                case GasTileOverlayState state:
                 {
-                    if (!state.Chunks.ContainsKey(index))
-                        comp.Chunks.Remove(index);
+                    modifiedChunks = state.Chunks;
+                    foreach (var index in comp.Chunks.Keys)
+                    {
+                        if (!state.Chunks.ContainsKey(index))
+                            comp.Chunks.Remove(index);
+                    }
+
+                    break;
                 }
+                default:
+                    return;
             }
 
-            foreach (var (index, data) in state.Chunks)
+            foreach (var (index, data) in modifiedChunks)
             {
                 comp.Chunks[index] = data;
             }
index 901ab270fb590841124a7c08ccb410f4aea88b1e..41e5f39c2867f75eb5a3c1cc50a946c7a7c649cb 100644 (file)
@@ -56,34 +56,43 @@ namespace Content.Client.Decals
 
         private void OnHandleState(EntityUid gridUid, DecalGridComponent gridComp, ref ComponentHandleState args)
         {
-            if (args.Current is not DecalGridState state)
-                return;
-
             // is this a delta or full state?
             _removedChunks.Clear();
+            Dictionary<Vector2i, DecalChunk> modifiedChunks;
 
-            if (!state.FullState)
+            switch (args.Current)
             {
-                foreach (var key in gridComp.ChunkCollection.ChunkCollection.Keys)
+                case DecalGridDeltaState delta:
                 {
-                    if (!state.AllChunks!.Contains(key))
-                        _removedChunks.Add(key);
+                    modifiedChunks = delta.ModifiedChunks;
+                    foreach (var key in gridComp.ChunkCollection.ChunkCollection.Keys)
+                    {
+                        if (!delta.AllChunks.Contains(key))
+                            _removedChunks.Add(key);
+                    }
+
+                    break;
                 }
-            }
-            else
-            {
-                foreach (var key in gridComp.ChunkCollection.ChunkCollection.Keys)
+                case DecalGridState state:
                 {
-                    if (!state.Chunks.ContainsKey(key))
-                        _removedChunks.Add(key);
+                    modifiedChunks = state.Chunks;
+                    foreach (var key in gridComp.ChunkCollection.ChunkCollection.Keys)
+                    {
+                        if (!state.Chunks.ContainsKey(key))
+                            _removedChunks.Add(key);
+                    }
+
+                    break;
                 }
+                default:
+                    return;
             }
 
             if (_removedChunks.Count > 0)
                 RemoveChunks(gridUid, gridComp, _removedChunks);
 
-            if (state.Chunks.Count > 0)
-                UpdateChunks(gridUid, gridComp, state.Chunks);
+            if (modifiedChunks.Count > 0)
+                UpdateChunks(gridUid, gridComp, modifiedChunks);
         }
 
         private void OnChunkUpdate(DecalChunkUpdateEvent ev)
index e33bc5d3291781d3efb4c4b5c7749dc0374a0d32..9aeb792a429f5ed274403c34480e994c03c1fef5 100644 (file)
@@ -14,27 +14,40 @@ public sealed partial class NavMapSystem : SharedNavMapSystem
 
     private void OnHandleState(EntityUid uid, NavMapComponent component, ref ComponentHandleState args)
     {
-        if (args.Current is not NavMapComponentState state)
-            return;
+        Dictionary<Vector2i, int[]> modifiedChunks;
+        Dictionary<NetEntity, NavMapBeacon> beacons;
 
-        if (!state.FullState)
+        switch (args.Current)
         {
-            foreach (var index in component.Chunks.Keys)
+            case NavMapDeltaState delta:
             {
-                if (!state.AllChunks!.Contains(index))
-                    component.Chunks.Remove(index);
+                modifiedChunks = delta.ModifiedChunks;
+                beacons = delta.Beacons;
+                foreach (var index in component.Chunks.Keys)
+                {
+                    if (!delta.AllChunks!.Contains(index))
+                        component.Chunks.Remove(index);
+                }
+
+                break;
             }
-        }
-        else
-        {
-            foreach (var index in component.Chunks.Keys)
+            case NavMapState state:
             {
-                if (!state.Chunks.ContainsKey(index))
-                    component.Chunks.Remove(index);
+                modifiedChunks = state.Chunks;
+                beacons = state.Beacons;
+                foreach (var index in component.Chunks.Keys)
+                {
+                    if (!state.Chunks.ContainsKey(index))
+                        component.Chunks.Remove(index);
+                }
+
+                break;
             }
+            default:
+                return;
         }
 
-        foreach (var (origin, chunk) in state.Chunks)
+        foreach (var (origin, chunk) in modifiedChunks)
         {
             var newChunk = new NavMapChunk(origin);
             Array.Copy(chunk, newChunk.TileData, chunk.Length);
@@ -42,7 +55,7 @@ public sealed partial class NavMapSystem : SharedNavMapSystem
         }
 
         component.Beacons.Clear();
-        foreach (var (nuid, beacon) in state.Beacons)
+        foreach (var (nuid, beacon) in beacons)
         {
             component.Beacons[nuid] = beacon;
         }
index e72a1d67584145a1f47729af7a9063ddc09efd93..2c3149b11a8f40d3d8549d0360fc89f184c45886 100644 (file)
@@ -1,7 +1,6 @@
 using Robust.Shared.GameStates;
 using Robust.Shared.Serialization;
 using Robust.Shared.Timing;
-using Robust.Shared.Utility;
 
 namespace Content.Shared.Atmos.Components;
 
@@ -24,55 +23,47 @@ public sealed partial class GasTileOverlayComponent : Component
     public GameTick ForceTick { get; set; }
 }
 
-
 [Serializable, NetSerializable]
-public sealed class GasTileOverlayState : ComponentState, IComponentDeltaState
+public sealed class GasTileOverlayState(Dictionary<Vector2i, GasOverlayChunk> chunks) : ComponentState
 {
-    public readonly Dictionary<Vector2i, GasOverlayChunk> Chunks;
-    public bool FullState => AllChunks == null;
-
-    // required to infer deleted/missing chunks for delta states
-    public HashSet<Vector2i>? AllChunks;
+    public readonly Dictionary<Vector2i, GasOverlayChunk> Chunks = chunks;
+}
 
-    public GasTileOverlayState(Dictionary<Vector2i, GasOverlayChunk> chunks)
-    {
-        Chunks = chunks;
-    }
+[Serializable, NetSerializable]
+public sealed class GasTileOverlayDeltaState(
+    Dictionary<Vector2i, GasOverlayChunk> modifiedChunks,
+    HashSet<Vector2i> allChunks)
+    : ComponentState, IComponentDeltaState<GasTileOverlayState>
+{
+    public readonly Dictionary<Vector2i, GasOverlayChunk> ModifiedChunks = modifiedChunks;
+    public readonly HashSet<Vector2i> AllChunks = allChunks;
 
-    public void ApplyToFullState(IComponentState fullState)
+    public void ApplyToFullState(GasTileOverlayState state)
     {
-        DebugTools.Assert(!FullState);
-        var state = (GasTileOverlayState) fullState;
-        DebugTools.Assert(state.FullState);
-
         foreach (var key in state.Chunks.Keys)
         {
-            if (!AllChunks!.Contains(key))
+            if (!AllChunks.Contains(key))
                 state.Chunks.Remove(key);
         }
 
-        foreach (var (chunk, data) in Chunks)
+        foreach (var (chunk, data) in ModifiedChunks)
         {
             state.Chunks[chunk] = new(data);
         }
     }
 
-    public IComponentState CreateNewFullState(IComponentState fullState)
+    public GasTileOverlayState CreateNewFullState(GasTileOverlayState state)
     {
-        DebugTools.Assert(!FullState);
-        var state = (GasTileOverlayState) fullState;
-        DebugTools.Assert(state.FullState);
-
-        var chunks = new Dictionary<Vector2i, GasOverlayChunk>(state.Chunks.Count);
+        var chunks = new Dictionary<Vector2i, GasOverlayChunk>(AllChunks.Count);
 
-        foreach (var (chunk, data) in Chunks)
+        foreach (var (chunk, data) in ModifiedChunks)
         {
             chunks[chunk] = new(data);
         }
 
         foreach (var (chunk, data) in state.Chunks)
         {
-            if (AllChunks!.Contains(chunk))
+            if (AllChunks.Contains(chunk))
                 chunks.TryAdd(chunk, new(data));
         }
 
index f468724db33396ed1dd601ab9aac89644f3f98ff..8e7dfdedaf9076aea16d23e80f1bcf39d3f3aaf4 100644 (file)
@@ -55,7 +55,7 @@ namespace Content.Shared.Atmos.EntitySystems
                     data[index] = chunk;
             }
 
-            args.State = new GasTileOverlayState(data) { AllChunks = new(component.Chunks.Keys) };
+            args.State = new GasTileOverlayDeltaState(data, new(component.Chunks.Keys));
         }
 
         public static Vector2i GetGasChunkIndices(Vector2i indices)
index 8ac05cb280fa387871e9c1a3788d9556a61f798e..67a9c037696d7ea1da4971951f18e80882582a25 100644 (file)
@@ -62,46 +62,37 @@ namespace Content.Shared.Decals
     }
 
     [Serializable, NetSerializable]
-    public sealed class DecalGridState : ComponentState, IComponentDeltaState
+    public sealed class DecalGridState(Dictionary<Vector2i, DecalChunk> chunks) : ComponentState
     {
-        public Dictionary<Vector2i, DecalChunk> Chunks;
-        public bool FullState => AllChunks == null;
-
-        // required to infer deleted/missing chunks for delta states
-        public HashSet<Vector2i>? AllChunks;
+        public Dictionary<Vector2i, DecalChunk> Chunks = chunks;
+    }
 
-        public DecalGridState(Dictionary<Vector2i, DecalChunk> chunks)
-        {
-            Chunks = chunks;
-        }
+    [Serializable, NetSerializable]
+    public sealed class DecalGridDeltaState(Dictionary<Vector2i, DecalChunk> modifiedChunks, HashSet<Vector2i> allChunks)
+        : ComponentState, IComponentDeltaState<DecalGridState>
+    {
+        public Dictionary<Vector2i, DecalChunk> ModifiedChunks = modifiedChunks;
+        public HashSet<Vector2i> AllChunks = allChunks;
 
-        public void ApplyToFullState(IComponentState fullState)
+        public void ApplyToFullState(DecalGridState state)
         {
-            DebugTools.Assert(!FullState);
-            var state = (DecalGridState) fullState;
-            DebugTools.Assert(state.FullState);
-
             foreach (var key in state.Chunks.Keys)
             {
                 if (!AllChunks!.Contains(key))
                     state.Chunks.Remove(key);
             }
 
-            foreach (var (chunk, data) in Chunks)
+            foreach (var (chunk, data) in ModifiedChunks)
             {
                 state.Chunks[chunk] = new(data);
             }
         }
 
-        public IComponentState CreateNewFullState(IComponentState fullState)
+        public DecalGridState CreateNewFullState(DecalGridState state)
         {
-            DebugTools.Assert(!FullState);
-            var state = (DecalGridState) fullState;
-            DebugTools.Assert(state.FullState);
-
             var chunks = new Dictionary<Vector2i, DecalChunk>(state.Chunks.Count);
 
-            foreach (var (chunk, data) in Chunks)
+            foreach (var (chunk, data) in ModifiedChunks)
             {
                 chunks[chunk] = new(data);
             }
index 0665ccbf84b3c9115a486ca5593b0a28f8671a60..0a2349ea299abbf7ab144a521ca6b08c757710ad 100644 (file)
@@ -49,7 +49,7 @@ namespace Content.Shared.Decals
                     data[index] = chunk;
             }
 
-            args.State = new DecalGridState(data) { AllChunks = new(component.ChunkCollection.ChunkCollection.Keys) };
+            args.State = new DecalGridDeltaState(data, new(component.ChunkCollection.ChunkCollection.Keys));
         }
 
         private void OnGridInitialize(GridInitializeEvent msg)
index ffe81c2d0ea49e9c04fdb61686102e8ba237d299..7c12321b5db6c60e8e713372d3c466ed82b04c42 100644 (file)
@@ -96,7 +96,7 @@ public abstract class SharedNavMapSystem : EntitySystem
                 chunks.Add(origin, chunk.TileData);
             }
 
-            args.State = new NavMapComponentState(chunks, component.Beacons);
+            args.State = new NavMapState(chunks, component.Beacons);
             return;
         }
 
@@ -109,12 +109,7 @@ public abstract class SharedNavMapSystem : EntitySystem
             chunks.Add(origin, chunk.TileData);
         }
 
-        args.State = new NavMapComponentState(chunks, component.Beacons)
-        {
-            // TODO NAVMAP cache a single AllChunks hashset in the component.
-            // Or maybe just only send them if a chunk gets removed.
-            AllChunks = new(component.Chunks.Keys),
-        };
+        args.State = new NavMapDeltaState(chunks, component.Beacons, new(component.Chunks.Keys));
     }
 
     #endregion
@@ -122,32 +117,35 @@ public abstract class SharedNavMapSystem : EntitySystem
     #region: System messages
 
     [Serializable, NetSerializable]
-    protected sealed class NavMapComponentState(
+    protected sealed class NavMapState(
         Dictionary<Vector2i, int[]> chunks,
         Dictionary<NetEntity, NavMapBeacon> beacons)
-        : ComponentState, IComponentDeltaState
+        : ComponentState
     {
         public Dictionary<Vector2i, int[]> Chunks = chunks;
         public Dictionary<NetEntity, NavMapBeacon> Beacons = beacons;
+    }
 
-        // Required to infer deleted/missing chunks for delta states
-        public HashSet<Vector2i>? AllChunks;
-
-        public bool FullState => AllChunks == null;
+    [Serializable, NetSerializable]
+    protected sealed class NavMapDeltaState(
+        Dictionary<Vector2i, int[]> modifiedChunks,
+        Dictionary<NetEntity, NavMapBeacon> beacons,
+        HashSet<Vector2i> allChunks)
+        : ComponentState, IComponentDeltaState<NavMapState>
+    {
+        public Dictionary<Vector2i, int[]> ModifiedChunks = modifiedChunks;
+        public Dictionary<NetEntity, NavMapBeacon> Beacons = beacons;
+        public HashSet<Vector2i> AllChunks = allChunks;
 
-        public void ApplyToFullState(IComponentState fullState)
+        public void ApplyToFullState(NavMapState state)
         {
-            DebugTools.Assert(!FullState);
-            var state = (NavMapComponentState) fullState;
-            DebugTools.Assert(state.FullState);
-
             foreach (var key in state.Chunks.Keys)
             {
                 if (!AllChunks!.Contains(key))
                     state.Chunks.Remove(key);
             }
 
-            foreach (var (index, data) in Chunks)
+            foreach (var (index, data) in ModifiedChunks)
             {
                 if (!state.Chunks.TryGetValue(index, out var stateValue))
                     state.Chunks[index] = stateValue = new int[data.Length];
@@ -162,12 +160,8 @@ public abstract class SharedNavMapSystem : EntitySystem
             }
         }
 
-        public IComponentState CreateNewFullState(IComponentState fullState)
+        public NavMapState CreateNewFullState(NavMapState state)
         {
-            DebugTools.Assert(!FullState);
-            var state = (NavMapComponentState) fullState;
-            DebugTools.Assert(state.FullState);
-
             var chunks = new Dictionary<Vector2i, int[]>(state.Chunks.Count);
             foreach (var (index, data) in state.Chunks)
             {
@@ -176,13 +170,13 @@ public abstract class SharedNavMapSystem : EntitySystem
 
                 var newData = chunks[index] = new int[ArraySize];
 
-                if (Chunks.TryGetValue(index, out var updatedData))
+                if (ModifiedChunks.TryGetValue(index, out var updatedData))
                     Array.Copy(newData, updatedData, ArraySize);
                 else
                     Array.Copy(newData, data, ArraySize);
             }
 
-            return new NavMapComponentState(chunks, new(Beacons));
+            return new NavMapState(chunks, new(Beacons));
         }
     }