Skip to content

Commit b5e18af

Browse files
committed
add state string helper function
1 parent 4748ef7 commit b5e18af

File tree

10 files changed

+60
-22
lines changed

10 files changed

+60
-22
lines changed

src/Backdash/Options/SyncTestOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public SyncTestOptions<TInput> UseRandomInputProvider()
7878
/// </summary>
7979
public SyncTestOptions<TInput> UseJsonStateParser(JsonSerializerOptions? options = null)
8080
{
81-
StateStringParser = new JsonStateStringParser(options);
81+
StateStringParser = options is null ? JsonStateStringParser.Singleton : new JsonStateStringParser(options);
8282
return this;
8383
}
8484

src/Backdash/Session/Backends/LocalSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ SessionServices<TInput> services
6666
public Endianness StateSerializationEndianness => endianness.Endianness;
6767
public Endianness InputSerializationEndianness => options.Protocol.SerializationEndianness;
6868
public INetcodeRandom Random => random;
69-
69+
public INetcodeSessionHandler GetHandler() => callbacks;
7070
public ReadOnlySpan<SynchronizedInput<TInput>> CurrentSynchronizedInputs => syncInputBuffer;
7171
public ReadOnlySpan<TInput> CurrentInputs => inputBuffer;
7272

src/Backdash/Session/Backends/RemoteSession.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ void Close()
203203
public int LocalPort => udp.BindPort;
204204
public Endianness StateSerializationEndianness => synchronizer.SerializationEndianness;
205205
public Endianness InputSerializationEndianness => options.Protocol.SerializationEndianness;
206+
public INetcodeSessionHandler GetHandler() => callbacks;
206207
public SessionMode Mode => SessionMode.Remote;
207208

208209
public IReadOnlySet<NetcodePlayer> GetPlayers() => addedPlayers;

src/Backdash/Session/Backends/ReplaySession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void Close()
108108
public int NumberOfPlayers { get; private set; }
109109

110110
public INetcodeRandom Random => random;
111-
111+
public INetcodeSessionHandler GetHandler() => callbacks;
112112
public SessionMode Mode => SessionMode.Replay;
113113
public void DisconnectPlayer(NetcodePlayer player) { }
114114
public ResultCode AddLocalInput(NetcodePlayer player, in TInput localInput) => ResultCode.Ok;

src/Backdash/Session/Backends/SpectatorSession.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public void Close()
159159
public bool IsInRollback => false;
160160
public SavedFrame GetCurrentSavedFrame() => stateStore.Last();
161161
public INetcodeRandom Random => random;
162+
public INetcodeSessionHandler GetHandler() => callbacks;
162163
public int NumberOfPlayers { get; private set; }
163164
public int NumberOfSpectators => 0;
164165
public int LocalPort => udp.BindPort;

src/Backdash/Session/Backends/SyncTestSession.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public async ValueTask DisposeAsync()
116116
public int NumberOfSpectators => addedSpectators.Count;
117117
public int LocalPort => 0;
118118
public INetcodeRandom Random => random;
119+
public INetcodeSessionHandler GetHandler() => callbacks;
119120
public Frame CurrentFrame => synchronizer.CurrentFrame;
120121
public SessionMode Mode => SessionMode.SyncTest;
121122
public FrameSpan FramesBehind => synchronizer.FramesBehind;

src/Backdash/Session/INetcodeSession.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ public interface INetcodeSessionInfo
7979
/// </summary>
8080
SavedFrame GetCurrentSavedFrame();
8181

82-
/// <summary>
83-
/// Returns the last saved state snapshot.
84-
/// </summary>
85-
StateSnapshot CurrentStateSnapshot() => GetCurrentSavedFrame().ToSnapshot();
86-
8782
/// <summary>
8883
/// Returns the checksum of the current saved state.
8984
/// </summary>
@@ -105,6 +100,11 @@ public interface INetcodeSession : INetcodeSessionInfo, IDisposable, IAsyncDispo
105100
/// </summary>
106101
INetcodeSessionInfo GetInfo() => this;
107102

103+
/// <summary>
104+
/// Returns session info
105+
/// </summary>
106+
INetcodeSessionHandler GetHandler();
107+
108108
/// <summary>
109109
/// Disconnects a remote player from a game.
110110
/// </summary>
@@ -234,18 +234,6 @@ public interface INetcodeSession : INetcodeSessionInfo, IDisposable, IAsyncDispo
234234
/// <returns><see cref="ResultCode.Ok" /> if success.</returns>
235235
ResultCode AddPlayer(NetcodePlayer player);
236236

237-
/// <summary>
238-
/// Add a list of <see name="Player" /> into current session.
239-
/// </summary>
240-
/// <returns>An equivalent <see cref="ResultCode" /> list.</returns>
241-
IReadOnlyList<ResultCode> AddPlayers(IReadOnlyList<NetcodePlayer> players)
242-
{
243-
var result = new ResultCode[players.Count];
244-
for (var index = 0; index < players.Count; index++)
245-
result[index] = AddPlayer(players[index]);
246-
return result;
247-
}
248-
249237
/// <summary>
250238
/// Find player by unique ID
251239
/// </summary>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Backdash.Serialization;
2+
using Backdash.Synchronizing.State;
3+
4+
namespace Backdash;
5+
6+
/// <summary>
7+
/// Extensions for <see cref="INetcodeSession{T}"/>
8+
/// </summary>
9+
public static class NetcodeSessionExtensions
10+
{
11+
/// <summary>
12+
/// Returns the current state string representation
13+
/// </summary>
14+
public static string GetStateString<T>(
15+
this INetcodeSession<T> @this,
16+
IStateStringParser? parser = null
17+
) where T : unmanaged =>
18+
@this.GetStateString(@this.GetCurrentSavedFrame(), parser);
19+
20+
/// <summary>
21+
/// Returns string representation for given <paramref name="state"/>
22+
/// </summary>
23+
public static string GetStateString<T>(
24+
this INetcodeSession<T> @this,
25+
SavedFrame state,
26+
IStateStringParser? parser = null
27+
) where T : unmanaged
28+
{
29+
parser ??= JsonStateStringParser.Singleton;
30+
var currentOffset = 0;
31+
var currentBytes = state.GameState.WrittenSpan;
32+
BinaryBufferReader currentReader = new(currentBytes, ref currentOffset, @this.StateSerializationEndianness);
33+
var currentObject = @this.GetHandler().CreateState(state.Frame, ref currentReader);
34+
return parser.GetStateString(state.Frame, in currentReader, currentObject);
35+
}
36+
37+
/// <summary>
38+
/// Returns the last saved state snapshot.
39+
/// </summary>
40+
public static StateSnapshot CurrentStateSnapshot<T>(this INetcodeSession<T> @this) where T : unmanaged =>
41+
@this.GetCurrentSavedFrame().ToSnapshot();
42+
}

src/Backdash/Synchronizing/State/IStateStringParser.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public sealed class JsonStateStringParser(
4848
IStateStringParser? stateStringFallback = null
4949
) : IStateStringParser
5050
{
51+
static readonly Lazy<IStateStringParser> singleton = new(new JsonStateStringParser());
52+
53+
/// <summary>Singleton instance</summary>
54+
public static IStateStringParser Singleton => singleton.Value;
55+
5156
internal Logger? Logger = null;
5257
readonly JsonSerializerOptions jsonOptions = options ?? CreateDefaultJsonOptions();
5358
readonly IStateStringParser fallback = stateStringFallback ?? new DefaultStateStringParser();

src/Backdash/Synchronizing/State/StateSnapshot.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public sealed class StateSnapshot(Frame frame, byte[] state)
1616
public StateSnapshot() : this(Frame.Null, []) { }
1717

1818
/// <summary>Saved frame number</summary>
19-
public Frame Frame = frame;
19+
public readonly Frame Frame = frame;
2020

2121
/// <summary>Saved game state</summary>
22-
public byte[] State = state;
22+
public readonly byte[] State = state;
2323

2424
/// <summary>Saved state size</summary>
2525
public ByteSize Size => ByteSize.FromBytes(State.Length);

0 commit comments

Comments
 (0)