Files
SVSimServer/SVSim.BattleNode/Protocol/MsgEnvelope.cs
gamer147 addeb021d2 fix(battlenode): shadow engine tracks live PvP wire-truth (full battle, multiple bid regressions)
Six distinct fixes accumulated over live-test iterations against four bids
(654473755566, 806245601092, 283192092460, 131549100204, 799755786270) — together
they take the shadow engine from "throws on the first non-mulligan play" to
"survives a full PvP battle, only weird-edge-case Unity touches still left to whack".

1. Engine StableRandom seed aligned with clients' Matched.seed
   (BattleSession.EnsureEngineSetup, NodeNativeBattleHarness.Create). Clients seed
   _stableRandom with BattleSeeds.Stable(masterSeed) (the value the node ships in
   Matched.seed); we were passing the RAW masterSeed to engine.Setup, so every
   StableRandom call diverged from call #1 onward — every turn-1+ draw picked a
   different deck position than the clients. Verified Stable(1184631275)=1543475792
   matches the wire on bid 654473755566.

2. SeedDeck advances cardTotalNum to deck.Count+1 + pins BattleStartDeckCardList.
   Mirrors SBattleLoad.InitPlayer's tail (SBattleLoad.cs:1292). Without it,
   skill-generated tokens auto-assigned Index 0,1,... and COLLIDED with deck-loaded
   indices 1..40 — silent until something addressed the deck card with the
   colliding Index (Hoverboarder at deck idx 1 + a token at engine Index 1 made
   GetBattleCardIdx's SingleOrDefault throw on bid 806245601092).

3. BattleCardView.GameObject lazily non-null in the shim (ViewUiTouchStubs.cs).
   The IsRecovery card-create delegate (NetworkBattleManagerBase.cs:379) passes
   null cardGameObject; Skill_metamorphose.cs:147 in the in-play branch then NRE'd
   on `metamorphosedCard.BattleCardView.GameObject.transform.rotation = identity`,
   a purely cosmetic touch with no game-state implication. Bid 283192092460:
   Petrification on a board follower.

4. TranslateChoiceKeyAction unwraps wrapped selectCard on shadow ingest
   (SessionBattleEngine.cs, sibling to TranslateTargetOwners). Live sender-send
   wires Choice plays as selectCard:{cardId:[...], open:0}; engine's
   ConvertToListInt does `value as List<object>` — a Dict casts to null and
   foreach NREs. The receiver's swallow-all catch (NetworkBattleReceiver.cs:1255)
   logs to Debug.LogError + LocalLog — both shimmed/no-op'd headlessly — and
   returns false, but Receive calls ReceivedMessage with checkBreakData:false so
   the false isn't propagated. The play continues with choiceIdList=[], the chosen
   branch never resolves, the played card stays in hand; a later targeted play
   (A's bounce on B's "board" idx 20) then can't find the target → NRE on null in
   ActionProcessor.PlayCard:407. Bid 131549100204: B's Resonance + A's bounce.
   Opponent-relay path is unaffected — node strips selectCard from broadcasts.

5. HeadlessHandViewStub overrides HandUnfocus/HandFocus/FocusRearrangeHandHand
   to return NullVfx. CreateHandControl returns null in headless; the base
   methods unconditionally deref `_handControl.SetHandState(...)`. A follower
   with a when_spell_play Heal trigger fired on its leader for amount 0 — even
   a 0-heal drives ApplyHealing → CreatePullHandInVfx → HandUnfocus → NRE.
   Bid 799755786270: two consecutive spell plays both crashed this stack.
   Added InternalsVisibleTo("SVSim.BattleEngine.Tests") so the shim-level
   regression tests can pin the no-op contracts directly.

Plus the previous-session fixes carried in this same uncommitted state
(see docs/superpowers/plans/2026-06-07-shadow-engine-desync-handoff.md):
  - doesPlayerGoFirst:true + mgr.IsFirst:true (turn-1 draw count correct
    per seat)
  - RecoveryOperationCollection.PlayHandCardOperation routes all type:30
    through PlaySkillSelectHandCardOperation (skips the two-phase user-select
    guard that aborts targeted spells in recovery)
  - ShadowFeed + ToRawBody: server-generated typed bodies (DealBody, etc.)
    converted to RawBody before engine.Receive (`env.Body as RawBody`
    returned null for typed bodies)
  - Ready idxChangeSeed seeds A's XorShift via the receiver; B's seed is
    injected via SeedOppoIdxChange (BattleSeeds.IdxChange + viewerId)
  - ReadySpin defaulted to 0 (was 243) — non-zero double-cranks the shadow
    which ingests BOTH sides' Ready frames on one stream

Test counts: SVSim.UnitTests 1054/1054, SVSim.BattleEngine.Tests 34/34.

Open: known-residual Unity touches are individual whack-a-mole now (per-card
skill edge cases), not the structural divergences fixed here.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-06-07 19:05:07 -04:00

181 lines
7.9 KiB
C#

using System.Text.Json;
using System.Text.Json.Nodes;
using SVSim.BattleNode.Wire;
namespace SVSim.BattleNode.Protocol;
/// <summary>
/// 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,
long ViewerId,
string Uuid,
string? Bid,
int RetryAttempt,
EmitCategory Cat,
long? PubSeq,
long? PlaySeq,
IMsgBody Body)
{
// Bare-camelCase wire serialization, single-sourced in Wire.WireJsonOptions (shared with
// EngineIoHandshake). Every wire key here is explicit via the manual ToJson layering below.
private static readonly JsonSerializerOptions Options = WireJsonOptions.CamelCase;
/// <summary>The fixed envelope wire keys, single-sourced. <see cref="ReservedEnvelopeKeys"/>,
/// the <see cref="ToJson"/> writes, and the <see cref="FromJson"/> reads all draw from here, so
/// the three encodings can't drift — adding a key in one place but not another (which would let a
/// body key silently shadow an envelope field) is no longer possible.</summary>
private static class Keys
{
public const string Uri = "uri";
public const string ViewerId = "viewerId";
public const string Uuid = "uuid";
public const string Bid = "bid";
public const string Try = "try";
public const string Cat = "cat";
public const string PubSeq = "pubSeq";
public const string PlaySeq = "playSeq";
}
private static readonly HashSet<string> ReservedEnvelopeKeys = new()
{
Keys.Uri, Keys.ViewerId, Keys.Uuid, Keys.Bid, Keys.Try, Keys.Cat, Keys.PubSeq, Keys.PlaySeq,
};
public static string ToJson(MsgEnvelope env)
{
// Envelope fields MUST come before body fields on the wire. The client's
// RealTimeNetworkAgent.SetNetworkInfo iterates the dict in insertion order and
// clears _selfDeck on the "uri" key (via GameMgr.InitializeSelfInfo). Any body
// field processed before "uri" is wiped before Matching.StartBattleLoad reads
// it back. The prod wire emits envelope keys first; we must too.
var result = new JsonObject();
result[Keys.Uri] = env.Uri.ToString();
result[Keys.ViewerId] = env.ViewerId;
result[Keys.Uuid] = env.Uuid;
result[Keys.Try] = env.RetryAttempt;
result[Keys.Cat] = (int)env.Cat;
if (env.Bid is not null) result[Keys.Bid] = env.Bid;
if (env.PubSeq.HasValue) result[Keys.PubSeq] = env.PubSeq.Value;
if (env.PlaySeq.HasValue) result[Keys.PlaySeq] = env.PlaySeq.Value;
if (env.Body is RawBody raw)
{
// Inbound-echo path: flatten Entries to top-level keys.
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);
}
}
else
{
// Typed body: serialize via [JsonPropertyName] attributes on the record,
// then layer each field onto `result` after the envelope keys. DeepClone
// because S.T.Json JsonNodes can only have one parent; reassigning a node
// owned by `bodyNode` to `result` would throw without the clone.
var bodyNode = (JsonObject)JsonSerializer.SerializeToNode(env.Body, env.Body.GetType(), Options)!;
foreach (var prop in bodyNode)
{
result[prop.Key] = prop.Value?.DeepClone();
}
}
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)
{
using var doc = JsonDocument.Parse(json);
var root = doc.RootElement;
var uri = Enum.Parse<NetworkBattleUri>(root.GetProperty(Keys.Uri).GetString()!);
var viewerId = root.GetProperty(Keys.ViewerId).GetInt64();
var uuid = root.GetProperty(Keys.Uuid).GetString()!;
var bid = root.TryGetProperty(Keys.Bid, out var bidEl) ? bidEl.GetString() : null;
var retryAttempt = root.TryGetProperty(Keys.Try, out var tryEl) ? tryEl.GetInt32() : 0;
var cat = root.TryGetProperty(Keys.Cat, out var catEl) ? (EmitCategory)catEl.GetInt32() : EmitCategory.Battle;
var pubSeq = root.TryGetProperty(Keys.PubSeq, out var psEl) ? psEl.GetInt64() : (long?)null;
var playSeq = root.TryGetProperty(Keys.PlaySeq, out var plsEl) ? plsEl.GetInt64() : (long?)null;
var bodyDict = new Dictionary<string, object?>();
foreach (var prop in root.EnumerateObject())
{
if (ReservedEnvelopeKeys.Contains(prop.Name)) continue;
bodyDict[prop.Name] = ToObject(prop.Value);
}
return new MsgEnvelope(uri, viewerId, uuid, bid, retryAttempt, cat, pubSeq, playSeq, new RawBody(bodyDict));
}
internal static object? ToObject(JsonElement el) => el.ValueKind switch
{
JsonValueKind.String => el.GetString(),
// Extracted to a helper because writing the conditional inline as
// el.TryGetInt64(out var l) ? l : el.GetDouble()
// unifies the conditional's branches to the common implicit-convertible type. long→double
// is implicit; so the result type collapses to double and the long value silently widens.
// Downstream OfType<long> filters then drop the (now boxed-double) entries, which broke
// the mulligan idxList extraction. Separate method returns object explicitly so each
// branch boxes its own runtime type.
JsonValueKind.Number => ParseNumber(el),
JsonValueKind.True => true,
JsonValueKind.False => false,
JsonValueKind.Null => null,
JsonValueKind.Array => el.EnumerateArray().Select(ToObject).ToList(),
JsonValueKind.Object => el.EnumerateObject().ToDictionary(p => p.Name, p => ToObject(p.Value)),
_ => el.GetRawText(),
};
private static object ParseNumber(JsonElement el)
{
if (el.TryGetInt64(out var l)) return l;
return el.GetDouble();
}
}