refactor(battle-node): switch MsgEnvelope.Body to IMsgBody, migrate all sites

This commit is contained in:
gamer147
2026-06-01 10:40:09 -04:00
parent 118be92dc5
commit 5ee270eb16
11 changed files with 183 additions and 231 deletions

View File

@@ -1,12 +1,13 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
namespace SVSim.BattleNode.Protocol;
/// <summary>
/// The shared envelope on every encrypted msg / synchronize frame. Body is opaque
/// (Dictionary&lt;string, object?&gt;) because shape varies per Uri — typed register-action
/// models come in a later slice.
/// The shared envelope on every encrypted msg / synchronize frame. Body is
/// <see cref="IMsgBody"/> — either a typed body record (outbound) or a
/// <see cref="RawBody"/> (inbound).
/// </summary>
public sealed record MsgEnvelope(
NetworkBattleUri Uri,
@@ -17,7 +18,7 @@ public sealed record MsgEnvelope(
EmitCategory Cat,
long? PubSeq,
long? PlaySeq,
Dictionary<string, object?> Body)
IMsgBody Body)
{
private static readonly JsonSerializerOptions Options = CreateOptions();
@@ -30,9 +31,10 @@ public sealed record MsgEnvelope(
{
var opt = new JsonSerializerOptions
{
// Wire-key casing here is bare camelCase — NOT EmulatedEntrypoint's snake_case policy.
// Wire-key casing is bare camelCase via per-field [JsonPropertyName] —
// NOT EmulatedEntrypoint's snake_case policy. The naming-policy line
// that was here previously was dead code (every wire key is explicit).
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
opt.Converters.Add(new JsonStringEnumConverter());
return opt;
@@ -40,26 +42,76 @@ public sealed record MsgEnvelope(
public static string ToJson(MsgEnvelope env)
{
var doc = new Dictionary<string, object?>
JsonObject result;
if (env.Body is RawBody raw)
{
["uri"] = env.Uri.ToString(),
["viewerId"] = env.ViewerId,
["uuid"] = env.Uuid,
["try"] = env.Try,
["cat"] = (int)env.Cat,
};
if (env.Bid is not null) doc["bid"] = env.Bid;
if (env.PubSeq.HasValue) doc["pubSeq"] = env.PubSeq.Value;
if (env.PlaySeq.HasValue) doc["playSeq"] = env.PlaySeq.Value;
foreach (var (k, v) in env.Body)
{
if (ReservedEnvelopeKeys.Contains(k))
throw new ArgumentException(
$"Body key '{k}' collides with a reserved envelope field. Move it to a typed field on MsgEnvelope.",
nameof(env));
doc[k] = v;
// Inbound-echo path: flatten Entries to top-level keys, same as before
// the typed-body refactor.
result = new JsonObject();
foreach (var (k, v) in raw.Entries)
{
if (ReservedEnvelopeKeys.Contains(k))
throw new ArgumentException(
$"RawBody key '{k}' collides with a reserved envelope field. " +
$"Move it to a typed field on MsgEnvelope.",
nameof(env));
result[k] = ToJsonNode(v);
}
}
return JsonSerializer.Serialize(doc, Options);
else
{
// Typed body: serialize via [JsonPropertyName] attributes on the record.
result = (JsonObject)JsonSerializer.SerializeToNode(env.Body, env.Body.GetType(), Options)!;
}
result["uri"] = env.Uri.ToString();
result["viewerId"] = env.ViewerId;
result["uuid"] = env.Uuid;
result["try"] = env.Try;
result["cat"] = (int)env.Cat;
if (env.Bid is not null) result["bid"] = env.Bid;
if (env.PubSeq.HasValue) result["pubSeq"] = env.PubSeq.Value;
if (env.PlaySeq.HasValue) result["playSeq"] = env.PlaySeq.Value;
return result.ToJsonString(Options);
}
/// <summary>
/// Convert a boxed CLR value (as stored in <see cref="RawBody.Entries"/>) to a JsonNode.
/// Explicit type switch on the runtime type — `JsonValue.Create(object?)` would create
/// a `JsonValueCustomized&lt;object&gt;` that requires a TypeInfoResolver at serialize time
/// (introduced in S.T.Json 8.0 source-gen mode).
/// </summary>
private static JsonNode? ToJsonNode(object? value) => value switch
{
null => null,
string s => JsonValue.Create(s),
bool b => JsonValue.Create(b),
long l => JsonValue.Create(l),
int i => JsonValue.Create(i),
double d => JsonValue.Create(d),
decimal m => JsonValue.Create(m),
// Inbound-parsed nested objects come through as Dictionary<string, object?>; nested
// arrays as List<object?>. FromJson is the source of these shapes — see ToObject.
IDictionary<string, object?> dict => DictToJsonObject(dict),
IReadOnlyList<object?> list => ListToJsonArray(list),
_ => throw new InvalidOperationException(
$"RawBody contains a value of unsupported type {value.GetType().FullName}. " +
"Only primitives, nested dicts (object), and nested lists are recognized."),
};
private static JsonObject DictToJsonObject(IDictionary<string, object?> dict)
{
var obj = new JsonObject();
foreach (var (k, v) in dict) obj[k] = ToJsonNode(v);
return obj;
}
private static JsonArray ListToJsonArray(IReadOnlyList<object?> list)
{
var arr = new JsonArray();
foreach (var v in list) arr.Add(ToJsonNode(v));
return arr;
}
public static MsgEnvelope FromJson(string json)
@@ -76,14 +128,14 @@ public sealed record MsgEnvelope(
var pubSeq = root.TryGetProperty("pubSeq", out var psEl) ? psEl.GetInt64() : (long?)null;
var playSeq = root.TryGetProperty("playSeq", out var plsEl) ? plsEl.GetInt64() : (long?)null;
var body = new Dictionary<string, object?>();
var bodyDict = new Dictionary<string, object?>();
foreach (var prop in root.EnumerateObject())
{
if (ReservedEnvelopeKeys.Contains(prop.Name)) continue;
body[prop.Name] = ToObject(prop.Value);
bodyDict[prop.Name] = ToObject(prop.Value);
}
return new MsgEnvelope(uri, viewerId, uuid, bid, @try, cat, pubSeq, playSeq, body);
return new MsgEnvelope(uri, viewerId, uuid, bid, @try, cat, pubSeq, playSeq, new RawBody(bodyDict));
}
private static object? ToObject(JsonElement el) => el.ValueKind switch