Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

feat(diag): ?bootlog boot-phase timings and ?navlog input tracing (playtest 2, P2-1 P2-4)

eyalgolan81e28a8ab881
diff --git a/unity/Assets/Scripts/Boot/DiagLog.cs b/unity/Assets/Scripts/Boot/DiagLog.cs
new file mode 100644
index 000000000..3b3721ea0
--- /dev/null
+++ b/unity/Assets/Scripts/Boot/DiagLog.cs
@@ -0,0 +1,47 @@
+#nullable enable
+using System;
+using System.Linq;
+using UnityEngine;
+
+namespace Statecraft.Game.Boot
+{
+ /// <summary>Diagnostic console lines, off unless asked for: "?bootlog" (WebGL page URL) or -bootlog (desktop) logs each
+ /// boot phase with its time since the player started and since the previous phase ("BOOT phase=… at=… ms dt=… ms");
+ /// "?navlog" / -navlog logs every UI pointer and submit event, every screen toggle and every map click with the frame
+ /// it happened in ("NAV f=… …"). Playtest 2, P2-1 and P2-4.</summary>
+ public static class DiagLog
+ {
+ public static bool Boot { get; set; }
+ public static bool Nav { get; set; }
+
+ /// <summary>Whether <paramref name="flag"/> is asked for: a whole query parameter on WebGL (UrlFlags.Has), else a
+ /// command-line flag "-<paramref name="flag"/>".</summary>
+ public static bool Requested(string flag, string? url, string[] args, bool webgl) =>
+ webgl ? UrlFlags.Has(url, flag) : args.Contains("-" + flag);
+
+ /// <summary>Reads both flags from this player's URL or command line.</summary>
+ public static void Init()
+ {
+ var webgl = Application.platform == RuntimePlatform.WebGLPlayer;
+ var url = Application.absoluteURL; var args = Environment.GetCommandLineArgs();
+ Boot = Requested("bootlog", url, args, webgl);
+ Nav = Requested("navlog", url, args, webgl);
+ }
+
+ static double last = -1;
+
+ public static void BootPhase(string phase)
+ {
+ if (!Boot) return;
+ var now = Time.realtimeSinceStartupAsDouble;
+ Debug.Log(FormattableString.Invariant($"BOOT phase={phase} at={now * 1000:0} ms dt={(last < 0 ? now : now - last) * 1000:0} ms"));
+ last = now;
+ }
+
+ public static void NavLine(string what)
+ {
+ if (!Nav) return;
+ Debug.Log(FormattableString.Invariant($"NAV f={Time.frameCount} t={Time.realtimeSinceStartupAsDouble * 1000:0} {what}"));
+ }
+ }
+}
diff --git a/unity/Assets/Scripts/Boot/DiagLog.cs.meta b/unity/Assets/Scripts/Boot/DiagLog.cs.meta
new file mode 100644
index 000000000..572e96780
--- /dev/null
+++ b/unity/Assets/Scripts/Boot/DiagLog.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 5fbbd530ea6a6439095259fdcfc941da
\ No newline at end of file
diff --git a/unity/Assets/Scripts/Boot/GameBoot.cs b/unity/Assets/Scripts/Boot/GameBoot.cs
index 88c216f03..91364bd71 100644
--- a/unity/Assets/Scripts/Boot/GameBoot.cs
+++ b/unity/Assets/Scripts/Boot/GameBoot.cs
@@ -20,6 +20,8 @@ namespace Statecraft.Game.Boot
async void Start()
{
+ DiagLog.Init();
+ DiagLog.BootPhase("player started");
try
{
Inputs = Application.platform == RuntimePlatform.WebGLPlayer
@@ -33,6 +35,7 @@ namespace Statecraft.Game.Boot
if (Application.platform != RuntimePlatform.WebGLPlayer) { Application.Quit(ok ? 0 : 1); return; }
}
map.Build(Inputs.Map);
+ DiagLog.BootPhase("map built");
Debug.Log($"BOOT OK content {Inputs.Content.Hash}");
Ready?.Invoke(Inputs);
}
diff --git a/unity/Assets/Scripts/Boot/GameInputs.cs b/unity/Assets/Scripts/Boot/GameInputs.cs
index a40a60d06..30691ab73 100644
--- a/unity/Assets/Scripts/Boot/GameInputs.cs
+++ b/unity/Assets/Scripts/Boot/GameInputs.cs
@@ -103,7 +103,10 @@ namespace Statecraft.Game.Boot
foreach (var (file, request) in requests) texts[file] = await Text(request, file);
var metaText = await Text(meta, "heightmap.json");
await Done(r16, "heightmap.r16");
- return FromTexts(texts, manifest, metaText, r16.downloadHandler.data);
+ DiagLog.BootPhase($"downloaded {texts.Count + 3} files");
+ var inputs = FromTexts(texts, manifest, metaText, r16.downloadHandler.data);
+ DiagLog.BootPhase("content parsed and validated");
+ return inputs;
}
finally
{
diff --git a/unity/Assets/Scripts/Map/MapBuilder.cs b/unity/Assets/Scripts/Map/MapBuilder.cs
index 620b4b305..ea7ec804a 100644
--- a/unity/Assets/Scripts/Map/MapBuilder.cs
+++ b/unity/Assets/Scripts/Map/MapBuilder.cs
@@ -67,6 +67,7 @@ namespace Statecraft.Game.Map
var data = new TerrainData { heightmapResolution = meta.width };
data.size = new Vector3(widthM, reliefM, lengthM);
data.SetHeights(0, 0, heights);
+ Boot.DiagLog.BootPhase("terrain heights set");
var terrainGo = Terrain.CreateTerrainGameObject(data);
terrainGo.name = "Terrain";
@@ -76,9 +77,11 @@ namespace Statecraft.Game.Map
terrain.materialTemplate = terrainMaterial;
terrain.heightmapPixelError = TerrainPixelError;
ApplyBiomeColours(data, frame, hexFile, assets);
+ Boot.DiagLog.BootPhase("biome colours blended");
var overlay = BuildHexOverlay(frame, hexFile, data, overlayMaterial, assets);
overlay.transform.SetParent(terrainGo.transform, false);
+ Boot.DiagLog.BootPhase("hex overlay draped");
return terrainGo;
}
diff --git a/unity/Assets/Scripts/Map/MapScene.cs b/unity/Assets/Scripts/Map/MapScene.cs
index 17fc4e5fb..67c38527b 100644
--- a/unity/Assets/Scripts/Map/MapScene.cs
+++ b/unity/Assets/Scripts/Map/MapScene.cs
@@ -105,15 +105,19 @@ namespace Statecraft.Game.Map
Clear();
var (meta, heights) = HeightmapLoader.Load(inputs.HeightmapJson, inputs.HeightmapR16, MetaPath);
+ DiagLog.BootPhase("heightmap decoded");
Hexes = MapBuilder.LoadHexes(inputs.HexesJson, HexesPath);
+ DiagLog.BootPhase("hexes parsed");
var terrainGo = MapBuilder.Build(meta, heights, Hexes, terrainMaterial, overlayMaterial);
terrainGo.transform.SetParent(transform, true);
Terrain = terrainGo.GetComponent<Terrain>();
var size = Terrain.terrainData.size;
AddBorderSkirt(terrainGo, meta.elev_min_m);
+ DiagLog.BootPhase("border skirt");
Frame = new GeoFrame(meta.lon0, meta.lon1, meta.lat0, meta.lat1, size.x, size.z);
var list = SettlementPlacer.Parse(inputs.SettlementsYaml, (meta.lon0, meta.lon1, meta.lat0, meta.lat1), SettlementsPath);
SettlementPlacer.Place(list, Frame, Terrain, markerMaterial).transform.SetParent(transform, true);
+ DiagLog.BootPhase("settlements placed");
if (!cam.TryGetComponent(out CameraRig rig)) rig = cam.gameObject.AddComponent<CameraRig>();
rig.LookAt = Frame.ToWorld(34.95, 31.95, 0); rig.Init(); // central Israel: Tel Aviv–Jerusalem corridor
diff --git a/unity/Assets/Scripts/UI/GameRoot.cs b/unity/Assets/Scripts/UI/GameRoot.cs
index 66c768c13..5b7691eed 100644
--- a/unity/Assets/Scripts/UI/GameRoot.cs
+++ b/unity/Assets/Scripts/UI/GameRoot.cs
@@ -114,13 +114,15 @@ namespace Statecraft.Game.UI
var session = smoke > 0
? GameStart.Begin(inputs.Content, store, () => GoldenRun.Seed, notes, text, fresh: true)
: GameStart.Begin(inputs.Content, store, GameStart.NewSeed, notes, text);
+ DiagLog.BootPhase($"session started (turn {session.State.Turn}, replayed)");
var bridge = Bridge = new SimBridge(session, text);
var root = document.rootVisualElement;
// The toast first: the autosave (below) must report a failed write after the toast has shown the turn's refusals.
var toast = Own(new ToastPresenter(root, bridge, text));
Own(new HudPresenter(root, bridge, text));
- Own(new ScreenNav(root)); // hides every screen: the USS has no display:none for them
+ Own(new ScreenNav(root)); // every screen starts hidden (USS); ScreenNav opens one at a time
+ if (DiagLog.Nav) Own(new NavEventLog(root)); // ?navlog: every UI pointer/submit event (playtest 2, P2-4)
var card = Own(new EventCardPresenter(root, bridge, text, portraits));
Own(new DefencePresenter(root, bridge, text));
Own(new BudgetPresenter(root, bridge, text));
@@ -172,6 +174,8 @@ namespace Statecraft.Game.UI
drawn = null;
RenderPlacementsIfChanged();
SyncMinimap();
+ root.Q("root")?.RemoveFromClassList("loading"); // ready: the bar, panels and screens may show (P2-1)
+ DiagLog.BootPhase("screens ready");
if (notes.Count > 0) toast.Show(notes);
Debug.Log(FormattableString.Invariant($"GAME OK {bridge.State.Date:yyyy-MM-dd} turn {bridge.State.Turn} hash {bridge.State.Hash()}"));
diff --git a/unity/Assets/Scripts/UI/NavEventLog.cs b/unity/Assets/Scripts/UI/NavEventLog.cs
new file mode 100644
index 000000000..96e5cbf4d
--- /dev/null
+++ b/unity/Assets/Scripts/UI/NavEventLog.cs
@@ -0,0 +1,67 @@
+#nullable enable
+using System.Collections.Generic;
+using Statecraft.Game.Boot;
+using UnityEngine.InputSystem;
+using UnityEngine.InputSystem.LowLevel;
+using UnityEngine.UIElements;
+
+namespace Statecraft.Game.UI
+{
+ /// <summary>Under ?navlog (DiagLog.Nav): logs every pointer down/up, click and navigation submit the game panel
+ /// receives, with the frame and the target element, before any element handles it (trickle-down). Playtest 2, P2-4.</summary>
+ public sealed class NavEventLog : System.IDisposable
+ {
+ readonly VisualElement root;
+ readonly List<Button> buttons;
+
+ public NavEventLog(VisualElement root)
+ {
+ this.root = root;
+ // On the buttons themselves too: a button holds pointer capture, so its up event never trickles through root.
+ buttons = root.Query<Button>().ToList();
+ foreach (var b in buttons)
+ {
+ b.RegisterCallback<PointerUpEvent>(OnButtonUp);
+ b.RegisterCallback<PointerCaptureOutEvent>(OnCaptureOut);
+ }
+ InputSystem.onEvent += OnRaw;
+ root.RegisterCallback<PointerDownEvent>(OnDown, TrickleDown.TrickleDown);
+ root.RegisterCallback<PointerUpEvent>(OnUp, TrickleDown.TrickleDown);
+ root.RegisterCallback<ClickEvent>(OnClick, TrickleDown.TrickleDown);
+ root.RegisterCallback<NavigationSubmitEvent>(OnSubmit, TrickleDown.TrickleDown);
+ }
+
+ static string Name(IEventHandler? t) => t is VisualElement v ? (string.IsNullOrEmpty(v.name) ? v.GetType().Name : v.name) : "?";
+
+ void OnDown(PointerDownEvent e) => DiagLog.NavLine($"ui pointerdown id={e.pointerId} at ({e.position.x:0}, {e.position.y:0}) target={Name(e.target)}");
+ void OnUp(PointerUpEvent e) => DiagLog.NavLine($"ui pointerup id={e.pointerId} at ({e.position.x:0}, {e.position.y:0}) target={Name(e.target)}");
+ void OnClick(ClickEvent e) => DiagLog.NavLine($"ui click count={e.clickCount} target={Name(e.target)}");
+ void OnButtonUp(PointerUpEvent e) => DiagLog.NavLine($"button pointerup at ({e.position.x:0}, {e.position.y:0}) on={Name(e.currentTarget)} inside={((VisualElement)e.currentTarget).worldBound.Contains(e.position)}");
+ void OnCaptureOut(PointerCaptureOutEvent e) => DiagLog.NavLine($"button capture out on={Name(e.currentTarget)}");
+
+ /// <summary>Every raw mouse state event the Input System processes (after any merging), with its position and buttons.</summary>
+ void OnRaw(InputEventPtr e, InputDevice d)
+ {
+ if (!(d is Mouse m) || !(e.IsA<StateEvent>() || e.IsA<DeltaStateEvent>())) return;
+ var at = m.position.ReadValueFromEvent(e);
+ var left = m.leftButton.ReadValueFromEvent(e);
+ DiagLog.NavLine($"raw mouse left={left:0} at ({at.x:0}, {at.y:0}) t={e.time:0.000}");
+ }
+
+ void OnSubmit(NavigationSubmitEvent e) => DiagLog.NavLine($"ui submit target={Name(e.target)}");
+
+ public void Dispose()
+ {
+ InputSystem.onEvent -= OnRaw;
+ foreach (var b in buttons)
+ {
+ b.UnregisterCallback<PointerUpEvent>(OnButtonUp);
+ b.UnregisterCallback<PointerCaptureOutEvent>(OnCaptureOut);
+ }
+ root.UnregisterCallback<PointerDownEvent>(OnDown, TrickleDown.TrickleDown);
+ root.UnregisterCallback<PointerUpEvent>(OnUp, TrickleDown.TrickleDown);
+ root.UnregisterCallback<ClickEvent>(OnClick, TrickleDown.TrickleDown);
+ root.UnregisterCallback<NavigationSubmitEvent>(OnSubmit, TrickleDown.TrickleDown);
+ }
+ }
+}
diff --git a/unity/Assets/Scripts/UI/NavEventLog.cs.meta b/unity/Assets/Scripts/UI/NavEventLog.cs.meta
new file mode 100644
index 000000000..ba55730e2
--- /dev/null
+++ b/unity/Assets/Scripts/UI/NavEventLog.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 57e43e4f3b87043f0821c728b1f4564b
\ No newline at end of file
diff --git a/unity/Assets/Scripts/UI/ScreenNav.cs b/unity/Assets/Scripts/UI/ScreenNav.cs
index 14436485b..f6da014f5 100644
--- a/unity/Assets/Scripts/UI/ScreenNav.cs
+++ b/unity/Assets/Scripts/UI/ScreenNav.cs
@@ -33,7 +33,11 @@ namespace Statecraft.Game.UI
/// <summary>The open screen's name, or null.</summary>
public string? Current { get; private set; }
- public void Toggle(string screen) => Open(Current == screen ? null : screen);
+ public void Toggle(string screen)
+ {
+ Boot.DiagLog.NavLine($"toggle {screen}: {Current ?? "none"} -> {(Current == screen ? "none" : screen)}");
+ Open(Current == screen ? null : screen);
+ }
public void Open(string? screen)
{