Files
SVSimServer/SVSim.BattleEngine.Tests/HeadlessCardMaster.cs
gamer147 2b506574e7 feat(battle-engine-port): M2 step 1 — SingleBattleMgr constructs headless
First green of the M2 go/no-go probe: `new SingleBattleMgr(StandardBattleMgr-
ContentsCreator)` now builds the two-player pair fully headless against the shim,
no Unity runtime. Verdict: headless construction is feasible; every blocker was a
mechanical no-op shim fill or data seam, not a Unity/logic wall.

Shim fills (authored):
- GameMgr: lazy non-null DataMgr/PrefabMgr/InputMgr/SoundMgr/BattleControl.
- GameObject: lazy cached component model so GetComponent<T>/AddComponent<T> return
  non-null no-op instances for Component-derived T (F1: unguarded view touches).
- Resources.Load(string): cached non-null GameObject so the prefab->Instantiate->
  GetComponent chain (UnityEventAgent) yields a real object.
- ClassBattleCardViewBase: re-attach dropped IClassBattleCardView (no-op members);
  ClassBattleCardBase.Setup casts the created view to it.

Engine copy (DP1/DP3 mis-cut fix):
- CardIconControl.cs copied verbatim (manifested) + generated null-stub deleted.
  SplitAndCompleteIconStr is pure string logic on the resolution path that M1 had
  wrongly stubbed as "View" -> null deref in SkillCreator.CreateBuildInfo.

Test harness (SVSim.BattleEngine.Tests, authored fixture):
- HeadlessContentsCreator/HeadlessPhaseCreator: deterministic replica of the solo
  practice init (StandardBattleMgrContentsCreator + SingleBattlePhaseCreator) with
  no-op recovery/replay managers.
- HeadlessCardMaster: reflects the loader cards.json dump into CardMaster.
- HeadlessMasterData: minimal Data.Master (class-character list, empty collections)
  + Data.Load + player/enemy chara ids.
- ConstructionProbeTests.SingleBattleMgr_constructs_headless — GREEN.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-06 01:36:22 -04:00

97 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.Json;
using Wizard;
namespace SVSim.BattleEngine.Tests
{
// Populates the engine's static CardMaster headless, from the loader's cards.json dump
// (serialized CardCSVData objects). We bypass the network/Resources init path
// (CardMaster.InitializeCardMaster) and the private ctor/field via reflection — CardMaster
// exposes no public injection seam. Class cards (id < 100) resolve via the ctor's
// _classCardParam, so an empty load still satisfies construction; pass real ids for the oracle.
public static class HeadlessCardMaster
{
private static readonly string CardsJsonPath =
Path.Combine(AppContext.BaseDirectory, "Data", "cards.json");
// Load the given card ids (empty = none) into a fresh CardMaster registered as Default.
public static void Load(params int[] cardIds)
{
var want = new HashSet<int>(cardIds);
var rows = new List<CardCSVData>();
if (want.Count > 0)
{
using var doc = JsonDocument.Parse(File.ReadAllText(CardsJsonPath));
int sort = 0;
foreach (var el in doc.RootElement.EnumerateArray())
{
if (!el.TryGetProperty("card_id", out var idEl)) continue;
if (!int.TryParse(idEl.GetString(), out var id) || !want.Contains(id)) continue;
rows.Add(BuildCardCsvData(el, sort++));
}
var missing = want.Except(rows.Select(r => int.Parse(r.card_id))).ToArray();
if (missing.Length > 0)
throw new InvalidOperationException(
"cards.json missing requested ids: " + string.Join(",", missing));
}
var cm = NewCardMaster(rows);
InjectAsDefault(cm);
}
// Construct a CardCSVData without running its CSV ctor; set each member from the JSON object
// by exact name match (cards.json keys == CardCSVData member names).
private static CardCSVData BuildCardCsvData(JsonElement el, int sortIndex)
{
var c = (CardCSVData)FormatterServices.GetUninitializedObject(typeof(CardCSVData));
const BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
foreach (var prop in el.EnumerateObject())
{
string val = prop.Value.ValueKind == JsonValueKind.Null ? null : prop.Value.ToString();
var f = typeof(CardCSVData).GetField(prop.Name, bf);
if (f != null) { SetMember(f.FieldType, val, v => f.SetValue(c, v)); continue; }
var p = typeof(CardCSVData).GetProperty(prop.Name, bf);
if (p != null && p.CanWrite) SetMember(p.PropertyType, val, v => p.SetValue(c, v));
}
// SortIndex is normally set by the ctor; mirror it.
var si = typeof(CardCSVData).GetProperty("SortIndex", bf);
if (si != null && si.CanWrite) si.SetValue(c, sortIndex);
return c;
}
private static void SetMember(Type t, string val, Action<object> set)
{
if (t == typeof(string)) set(val);
else if (t == typeof(int)) set(int.TryParse(val, out var i) ? i : 0);
else if (t == typeof(bool)) set(val == "1" || string.Equals(val, "true", StringComparison.OrdinalIgnoreCase));
// other types left at default
}
private static CardMaster NewCardMaster(List<CardCSVData> rows)
{
var ctor = typeof(CardMaster).GetConstructor(
BindingFlags.Instance | BindingFlags.NonPublic, null,
new[] { typeof(List<CardCSVData>) }, null);
if (ctor == null) throw new InvalidOperationException("CardMaster(List<CardCSVData>) ctor not found");
return (CardMaster)ctor.Invoke(new object[] { rows });
}
private static void InjectAsDefault(CardMaster cm)
{
var idType = typeof(CardMaster).GetNestedType("CardMasterId");
var defaultId = Enum.Parse(idType, "Default");
var dictType = typeof(Dictionary<,>).MakeGenericType(idType, typeof(CardMaster));
var dict = (System.Collections.IDictionary)Activator.CreateInstance(dictType);
dict[defaultId] = cm;
var fld = typeof(CardMaster).GetField("_dictCardMaster",
BindingFlags.Static | BindingFlags.NonPublic);
fld.SetValue(null, dict);
}
}
}