feat(gamecli): add JSON protocol DTOs with source-gen context

This commit is contained in:
meelstorm
2026-07-17 16:49:01 +00:00
committed by EugeneTes
parent 5b35d65bd2
commit f5ee2049da
3 changed files with 141 additions and 0 deletions

View File

@@ -7,4 +7,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="GameCli.Tests" />
</ItemGroup>
</Project>

56
GameCli/Protocol.cs Normal file
View File

@@ -0,0 +1,56 @@
using System.Text.Json.Serialization;
namespace GameCli;
/// <summary>Input line from the client (per step OR a reset command).</summary>
public sealed class StepInput
{
[JsonPropertyName("action")] public int? Action { get; set; }
[JsonPropertyName("target")] public float[]? Target { get; set; }
[JsonPropertyName("cmd")] public string? Cmd { get; set; }
[JsonPropertyName("seed")] public int? Seed { get; set; }
}
/// <summary>Output line from the CLI after each step or reset.</summary>
public sealed class StepOutput
{
[JsonPropertyName("obs")] public float[] Obs { get; set; } = System.Array.Empty<float>();
[JsonPropertyName("state")] public ShipStateDto State { get; set; } = new();
[JsonPropertyName("reward")] public float Reward { get; set; }
[JsonPropertyName("done")] public bool Done { get; set; }
[JsonPropertyName("step")] public int Step { get; set; }
}
/// <summary>Renderable ship pose (subset of ShipState that the UI actually draws).</summary>
public sealed class ShipStateDto
{
[JsonPropertyName("x")] public float X { get; set; }
[JsonPropertyName("y")] public float Y { get; set; }
[JsonPropertyName("angle")] public float Angle { get; set; }
[JsonPropertyName("engine")] public int Engine { get; set; }
}
/// <summary>Startup handshake emitted as the very first stdout line.</summary>
public sealed class InitMessage
{
[JsonPropertyName("init")] public InitPayload Init { get; set; } = new();
}
public sealed class InitPayload
{
[JsonPropertyName("world")] public float[] World { get; set; } = new[] { 1f, 1f };
[JsonPropertyName("dt")] public float Dt { get; set; }
[JsonPropertyName("obs_dim")] public int ObsDim { get; set; }
[JsonPropertyName("n_actions")] public int NActions { get; set; }
}
/// <summary>Source-generated JSON contracts for AOT-friendly, allocation-lean (de)serialization.</summary>
[JsonSourceGenerationOptions(WriteIndented = false)]
[JsonSerializable(typeof(StepInput))]
[JsonSerializable(typeof(StepOutput))]
[JsonSerializable(typeof(ShipStateDto))]
[JsonSerializable(typeof(InitMessage))]
[JsonSerializable(typeof(InitPayload))]
internal partial class ProtocolJsonContext : JsonSerializerContext
{
}