From 3ad902cee71644b5fa3c26ca91be67b54f9ec24e Mon Sep 17 00:00:00 2001 From: Lucas Teles Date: Fri, 22 Nov 2024 17:55:22 -0300 Subject: [PATCH] minor: update store --- samples/ConsoleGame/View.cs | 18 +++++++++++++++++- src/Backdash/Synchronizing/State/SavedFrame.cs | 9 ++++++--- .../State/Stores/ArrayStateStore.cs | 4 ++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/samples/ConsoleGame/View.cs b/samples/ConsoleGame/View.cs index 5e875e0b..db0d47cb 100644 --- a/samples/ConsoleGame/View.cs +++ b/samples/ConsoleGame/View.cs @@ -1,4 +1,5 @@ using System.Numerics; + namespace ConsoleGame; public class View @@ -7,6 +8,7 @@ public class View readonly ConsoleColor targetColor = ConsoleColor.Yellow; readonly ConsoleColor[] playerColors = [ConsoleColor.Green, ConsoleColor.Red]; public View() => Console.CursorVisible = false; + public void Draw(in GameState currentState, NonGameState nonGameState) { Console.Clear(); @@ -17,6 +19,7 @@ public void Draw(in GameState currentState, NonGameState nonGameState) if (nonGameState.RemotePlayerStatus is PlayerStatus.Running) DrawStats(nonGameState); } + void DrawHeader(NonGameState nonGameState) { if (nonGameState.LocalPlayer is { } localPlayer) @@ -31,8 +34,10 @@ void DrawHeader(NonGameState nonGameState) Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("-- Spectator --\n"); } + Console.ForegroundColor = defaultColor; } + void DrawScore(in GameState state) { Console.Write("Score: "); @@ -45,6 +50,7 @@ void DrawScore(in GameState state) Console.ForegroundColor = defaultColor; Console.WriteLine(); } + void DrawField(in GameState currentState, NonGameState nonGameState) { var status1 = nonGameState.RemotePlayer.Number is 1 @@ -70,12 +76,16 @@ void DrawField(in GameState currentState, NonGameState nonGameState) Console.ForegroundColor = defaultColor; continue; } + Console.Write("."); } + Console.WriteLine(); } + Console.WriteLine(); } + bool DrawPlayer(Vector2 pos, int col, int row, ConsoleColor color, PlayerStatus status) { if ((int)pos.X == col && (int)pos.Y == row) @@ -90,8 +100,10 @@ bool DrawPlayer(Vector2 pos, int col, int row, ConsoleColor color, PlayerStatus Console.ForegroundColor = defaultColor; return true; } + return false; } + void DrawConnection(NonGameState nonGameState) { Console.Write(" "); @@ -125,9 +137,11 @@ void DrawConnection(NonGameState nonGameState) Console.WriteLine("Disconnected."); break; } + Console.ForegroundColor = defaultColor; Console.WriteLine(); } + static void DrawProgressBar(double percent) { const int loadingSize = 10; @@ -139,9 +153,11 @@ static void DrawProgressBar(double percent) Console.ForegroundColor = i <= loaded ? ConsoleColor.DarkGreen : ConsoleColor.White; Console.Write('\u2588'); } + Console.ForegroundColor = lastColor; } - void DrawStats(NonGameState nonGameState) + + static void DrawStats(NonGameState nonGameState) { var peer = nonGameState.PeerNetworkStats; var info = nonGameState.SessionInfo; diff --git a/src/Backdash/Synchronizing/State/SavedFrame.cs b/src/Backdash/Synchronizing/State/SavedFrame.cs index a6bd6276..2e230161 100644 --- a/src/Backdash/Synchronizing/State/SavedFrame.cs +++ b/src/Backdash/Synchronizing/State/SavedFrame.cs @@ -1,3 +1,4 @@ +using System.Runtime.InteropServices; using Backdash.Data; namespace Backdash.Synchronizing.State; @@ -9,15 +10,17 @@ namespace Backdash.Synchronizing.State; /// Game state on /// Checksum of state /// Game state type +[Serializable] +[StructLayout(LayoutKind.Sequential)] public record struct SavedFrame(Frame Frame, TState GameState, int Checksum) where TState : notnull { /// Saved frame number public Frame Frame = Frame; - /// Saved game state - public TState GameState = GameState; - /// Saved checksum public int Checksum = Checksum; + + /// Saved game state + public TState GameState = GameState; } diff --git a/src/Backdash/Synchronizing/State/Stores/ArrayStateStore.cs b/src/Backdash/Synchronizing/State/Stores/ArrayStateStore.cs index 018f7584..72e47db3 100644 --- a/src/Backdash/Synchronizing/State/Stores/ArrayStateStore.cs +++ b/src/Backdash/Synchronizing/State/Stores/ArrayStateStore.cs @@ -14,7 +14,7 @@ namespace Backdash.Synchronizing.State.Stores; /// public void Initialize(int saveCount) { - savedStates = new SavedFrame[saveCount]; + savedStates = GC.AllocateArray>(saveCount, pinned: true); for (int i = 0; i < saveCount; i++) savedStates[i] = new(Frame.Null, new(), 0); } @@ -25,7 +25,7 @@ public ref readonly SavedFrame Load(Frame frame) for (var i = 0; i < savedStates.Length; i++) { ref var current = ref savedStates[i]; - if (current.Frame != frame) continue; + if (current.Frame.Number != frame.Number) continue; head = i; AdvanceHead(); return ref current;