A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
fix(editor): wait until the editor is ready before opening the game scene
eyalgolanad31c44b8718
diff --git a/unity/Assets/Editor/OpenMapScene.cs b/unity/Assets/Editor/OpenMapScene.csindex f0e92bee8..1b6f71ce7 100644--- a/unity/Assets/Editor/OpenMapScene.cs+++ b/unity/Assets/Editor/OpenMapScene.cs@@ -6,24 +6,36 @@ using UnityEngine.SceneManagement;namespace Statecraft.Game.EditorTools{/// <summary>A fresh clone opens on Unity's empty "Untitled" scene, and new contributors could not find the game- /// (it is Assets/Map.unity, the boot scene). On editor load this opens the map once per session, and only when- /// nothing the user made could be lost: one untitled, unedited scene, outside play mode and batch mode.</summary>+ /// (it is Assets/Map.unity, the boot scene). Once the editor is ready (not compiling or importing, with a valid+ /// active scene), this opens the map once per session, and only when nothing the user made could be lost: one+ /// untitled, unedited scene, outside play mode and batch mode. On a first launch the domain loads before Unity has+ /// created its starting scene, so the decision waits on EditorApplication.update instead of running at load.</summary>[InitializeOnLoad]public static class OpenMapScene{public const string MapScenePath = "Assets/Map.unity";const string SessionKey = "Statecraft.OpenMapScene.Done";- static OpenMapScene() => EditorApplication.delayCall += TryOpen;+ static OpenMapScene()+ {+ if (UnityEngine.Application.isBatchMode || SessionState.GetBool(SessionKey, false)) return;+ EditorApplication.update += Poll;+ }public static bool ShouldOpen(string activeScenePath, bool activeSceneDirty, int loadedScenes, bool isPlaying,bool isBatchMode, bool alreadyDoneThisSession) =>!alreadyDoneThisSession && !isBatchMode && !isPlaying && loadedScenes == 1&& string.IsNullOrEmpty(activeScenePath) && !activeSceneDirty;- static void TryOpen()+ /// <summary>True once the editor has finished starting: nothing compiling or importing and a valid scene.</summary>+ public static bool EditorReady(bool isCompiling, bool isUpdating, bool activeSceneValid) =>+ !isCompiling && !isUpdating && activeSceneValid;++ static void Poll(){var active = SceneManager.GetActiveScene();+ if (!EditorReady(EditorApplication.isCompiling, EditorApplication.isUpdating, active.IsValid())) return;+ EditorApplication.update -= Poll;var open = ShouldOpen(active.path, active.isDirty, SceneManager.sceneCount,EditorApplication.isPlayingOrWillChangePlaymode, UnityEngine.Application.isBatchMode,SessionState.GetBool(SessionKey, false));diff --git a/unity/Assets/Tests/EditMode/OpenMapSceneTests.cs b/unity/Assets/Tests/EditMode/OpenMapSceneTests.csindex 55acae871..a15819aa7 100644--- a/unity/Assets/Tests/EditMode/OpenMapSceneTests.cs+++ b/unity/Assets/Tests/EditMode/OpenMapSceneTests.cs@@ -29,5 +29,22 @@ namespace Statecraft.Tests.EditMode[TestCase("", false, 1, false, false, true, TestName = "only once per editor session")]public void LeavesTheEditorAlone(string path, bool dirty, int loaded, bool playing, bool batch, bool done) =>Assert.IsFalse(ShouldOpen(path, dirty, loaded, playing, batch, done));++ static bool EditorReady(bool compiling, bool updating, bool valid)+ {+ var t = Type.GetType("Statecraft.Game.EditorTools.OpenMapScene, Assembly-CSharp-Editor")!;+ return (bool)t.GetMethod("EditorReady", BindingFlags.Public | BindingFlags.Static)!.Invoke(null, new object[] { compiling, updating, valid })!;+ }++ [Test]+ public void TheDecisionWaitsUntilTheEditorHasFinishedStarting()+ {+ // A fresh clone's first launch loads the domain before Unity creates its starting scene; deciding then saw no+ // valid scene, marked the session done and never opened the map (found by opening a real fresh clone).+ Assert.IsTrue(EditorReady(false, false, true));+ Assert.IsFalse(EditorReady(true, false, true), "still compiling");+ Assert.IsFalse(EditorReady(false, true, true), "still importing");+ Assert.IsFalse(EditorReady(false, false, false), "no valid scene yet");+ }}}