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.

fix(editor): open the game scene (Assets/Map.unity) on first open of a fresh clone

eyalgolana5806d22cc42
diff --git a/unity/Assets/Editor/OpenMapScene.cs b/unity/Assets/Editor/OpenMapScene.cs
new file mode 100644
index 000000000..f0e92bee8
--- /dev/null
+++ b/unity/Assets/Editor/OpenMapScene.cs
@@ -0,0 +1,35 @@
+#nullable enable
+using UnityEditor;
+using UnityEditor.SceneManagement;
+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>
+ [InitializeOnLoad]
+ public static class OpenMapScene
+ {
+ public const string MapScenePath = "Assets/Map.unity";
+ const string SessionKey = "Statecraft.OpenMapScene.Done";
+
+ static OpenMapScene() => EditorApplication.delayCall += TryOpen;
+
+ 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()
+ {
+ var active = SceneManager.GetActiveScene();
+ var open = ShouldOpen(active.path, active.isDirty, SceneManager.sceneCount,
+ EditorApplication.isPlayingOrWillChangePlaymode, UnityEngine.Application.isBatchMode,
+ SessionState.GetBool(SessionKey, false));
+ SessionState.SetBool(SessionKey, true);
+ if (open && AssetDatabase.LoadAssetAtPath<SceneAsset>(MapScenePath) != null)
+ EditorSceneManager.OpenScene(MapScenePath, OpenSceneMode.Single);
+ }
+ }
+}
diff --git a/unity/Assets/Editor/OpenMapScene.cs.meta b/unity/Assets/Editor/OpenMapScene.cs.meta
new file mode 100644
index 000000000..4f72779b8
--- /dev/null
+++ b/unity/Assets/Editor/OpenMapScene.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 6058f9d38e9bd415cab239d5665423c4
\ No newline at end of file
diff --git a/unity/Assets/Tests/EditMode/OpenMapSceneTests.cs b/unity/Assets/Tests/EditMode/OpenMapSceneTests.cs
new file mode 100644
index 000000000..55acae871
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/OpenMapSceneTests.cs
@@ -0,0 +1,33 @@
+#nullable enable
+using System;
+using System.Reflection;
+using NUnit.Framework;
+
+namespace Statecraft.Tests.EditMode
+{
+ /// <summary>A fresh clone opens on an empty "Untitled" scene; the editor hook opens Assets/Map.unity instead, but only
+ /// when nothing the user made could be lost, never in batch mode or play mode, and once per editor session.</summary>
+ public sealed class OpenMapSceneTests
+ {
+ static bool ShouldOpen(string activePath, bool dirty, int loaded, bool playing, bool batch, bool done)
+ {
+ var t = Type.GetType("Statecraft.Game.EditorTools.OpenMapScene, Assembly-CSharp-Editor");
+ Assert.IsNotNull(t, "the OpenMapScene editor hook exists");
+ var m = t!.GetMethod("ShouldOpen", BindingFlags.Public | BindingFlags.Static)!;
+ return (bool)m.Invoke(null, new object[] { activePath, dirty, loaded, playing, batch, done })!;
+ }
+
+ [Test]
+ public void AFreshUntitledEditorOpensTheMap() =>
+ Assert.IsTrue(ShouldOpen("", dirty: false, loaded: 1, playing: false, batch: false, done: false));
+
+ [TestCase("Assets/Other.unity", false, 1, false, false, false, TestName = "a saved scene the user opened stays")]
+ [TestCase("", true, 1, false, false, false, TestName = "an edited untitled scene is never thrown away")]
+ [TestCase("", false, 2, false, false, false, TestName = "several loaded scenes stay")]
+ [TestCase("", false, 1, true, false, false, TestName = "never in play mode")]
+ [TestCase("", false, 1, false, true, false, TestName = "never in batch mode (tests and builds)")]
+ [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));
+ }
+}
diff --git a/unity/Assets/Tests/EditMode/OpenMapSceneTests.cs.meta b/unity/Assets/Tests/EditMode/OpenMapSceneTests.cs.meta
new file mode 100644
index 000000000..b6ae7e461
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/OpenMapSceneTests.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: ff257c9af1fe34c588d20a2068282bfc
\ No newline at end of file