Skip to content

Commit b6480ce

Browse files
committed
add configure action for json options state viewer
1 parent 1ecafba commit b6480ce

File tree

5 files changed

+30
-22
lines changed

5 files changed

+30
-22
lines changed

src/Backdash/Network/Client/PeerClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ namespace Backdash.Network.Client;
1313
public interface IMessageHandler<T> where T : struct
1414
{
1515
/// <summary>
16-
/// Handles sent message.
16+
/// Handles messages sent.
1717
/// </summary>
1818
void AfterSendMessage(int bytesSent);
1919

2020
/// <summary>
21-
/// Prepare message to be sent.
21+
/// Prepare messages to be sent.
2222
/// </summary>
2323
void BeforeSendMessage(ref T message);
2424
}
@@ -133,6 +133,7 @@ async Task StartSending(CancellationToken cancellationToken)
133133

134134
entry.Callback?.BeforeSendMessage(ref entry.Body);
135135

136+
// LATER: move the .Span outside the loop (requires >=.NET10)
136137
var bodySize = serializer.Serialize(in entry.Body, buffer.Span);
137138
var sentSize = await socket.SendToAsync(buffer[..bodySize], entry.Recipient, cancellationToken)
138139
.ConfigureAwait(false);

src/Backdash/Options/SyncTestOptions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ public SyncTestOptions<TInput> UseJsonStateParser(JsonSerializerOptions? options
8282
return this;
8383
}
8484

85+
/// <inheritdoc cref="UseJsonStateParser(JsonSerializerOptions?)" />
86+
public SyncTestOptions<TInput> UseJsonStateParser(Action<JsonSerializerOptions> configure)
87+
{
88+
var options = JsonStateStringParser.CreateDefaultJsonOptions();
89+
configure.Invoke(options);
90+
return UseJsonStateParser(options);
91+
}
92+
8593
/// <inheritdoc cref="StateStringParser" />
8694
public SyncTestOptions<TInput> UseStateStringParser<T>() where T : IStateStringParser, new()
8795
{

src/Backdash/Session/NetcodeSessionBuilder.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ public NetcodeSessionBuilder<TInput> WithSocketFactory(Func<int, NetcodeOptions,
488488
WithSocketFactory(new DelegateSocketFactory(factory));
489489

490490
/// <summary>
491-
/// Add plugin type
491+
/// Register a plugin by type
492492
/// </summary>
493493
/// <seealso cref="ServicesConfig{TInput}" />
494494
[MemberNotNull(nameof(sessionServices))]
@@ -508,19 +508,17 @@ public NetcodeSessionBuilder<TInput> UsePlugin(INetcodePlugin plugin)
508508

509509

510510
/// <summary>
511-
/// Add new custom job
511+
/// Add a new custom job
512512
/// </summary>
513+
/// <seealso cref="ServicesConfig{TInput}" />
513514
[MemberNotNull(nameof(sessionServices))]
514515
public NetcodeSessionBuilder<TInput> AddJob(INetcodeJob job)
515516
{
516517
ArgumentNullException.ThrowIfNull(job);
517518
return ConfigureServices(services => services.Jobs.Add(job));
518519
}
519520

520-
/// <summary>
521-
/// Add new custom job
522-
/// </summary>
523-
/// <seealso cref="ServicesConfig{TInput}" />
521+
/// <inheritdoc cref="AddJob(INetcodeJob)" />
524522
[MemberNotNull(nameof(sessionServices))]
525523
public NetcodeSessionBuilder<TInput> AddJob<TPlugin>() where TPlugin : INetcodeJob, new() =>
526524
AddJob(new TPlugin());
@@ -700,9 +698,9 @@ public InputTypeSelected<T> Integer<T>(bool isUnsigned) where T : unmanaged, IBi
700698
new(e => IntegerBinarySerializer.Create<T>(isUnsigned, e));
701699

702700
/// <summary>
703-
/// Choose a raw unmanaged value type as input type.
701+
/// Choose a raw unmanaged value type as an input type.
704702
/// Must not be a reference type or a value type that contains references.
705-
/// This *DO NOT* use custom <see cref="Endianness" /> for <typeparamref name="T" /> integer fields.
703+
/// This *DOES NOT* use custom <see cref="Endianness" /> for <typeparamref name="T" /> integer fields.
706704
/// </summary>
707705
/// <seealso cref="RuntimeHelpers.IsReferenceOrContainsReferences{T}" />
708706
public InputTypeSelected<T> Struct<T>() where T : unmanaged =>

src/Backdash/Synchronizing/State/IStateStringParser.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,21 @@ public sealed class JsonStateStringParser(
4949
) : IStateStringParser
5050
{
5151
internal Logger? Logger = null;
52-
53-
readonly JsonSerializerOptions jsonOptions = options ?? new()
54-
{
55-
WriteIndented = true,
56-
IncludeFields = true,
57-
AllowTrailingCommas = true,
58-
ReferenceHandler = ReferenceHandler.IgnoreCycles,
59-
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
60-
Converters = { new JsonStringEnumConverter() },
61-
};
62-
52+
readonly JsonSerializerOptions jsonOptions = options ?? CreateDefaultJsonOptions();
6353
readonly IStateStringParser fallback = stateStringFallback ?? new DefaultStateStringParser();
6454
readonly HexStateStringParser nullFallback = new();
6555

56+
internal static JsonSerializerOptions CreateDefaultJsonOptions() =>
57+
new()
58+
{
59+
WriteIndented = true,
60+
IncludeFields = true,
61+
AllowTrailingCommas = true,
62+
ReferenceHandler = ReferenceHandler.IgnoreCycles,
63+
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
64+
Converters = { new JsonStringEnumConverter() },
65+
};
66+
6667
/// <inheritdoc />
6768
public string GetStateString(in Frame frame, ref readonly BinaryBufferReader reader, object? currentState)
6869
{

tests/Backdash.Tests/Usings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
global using FluentAssertions;
77
global using FsCheck;
88
global using Xunit;
9-
global using Max = Backdash.Core.NetcodeConstants.Max;
109
global using GameInput = Backdash.Synchronizing.Input.GameInput<Backdash.Tests.TestUtils.TestInput>;
10+
global using Max = Backdash.Core.NetcodeConstants.Max;
1111
global using Random = System.Random;

0 commit comments

Comments
 (0)