A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
feat(editor): keep Unity-MCP out of batch mode and player builds
eyalgolan77d94183163f
diff --git a/.gitignore b/.gitignoreindex e22a6f255..b284af227 100644--- a/.gitignore+++ b/.gitignore@@ -25,6 +25,24 @@ unity/Assets/Plugins/NuGet/unity/Assets/Plugins/NuGet.metaunity/Assets/packages-merged-link/unity/Assets/packages-merged-link.meta+# Unity-MCP agent files: written per machine by Configure / Generate skills. .mcp.json can carry the local+# server's bearer token, and the skills are regenerated for each plugin version.+.mcp.json+unity/.claude/+unity/.agent/+unity/.agents/+unity/.cline/+unity/.codex/+unity/.cursor/+unity/.gemini/+unity/.github/+unity/.junie/+unity/.kilocode/+unity/.opencode/+unity/.roo/+unity/.ai-game-dev/+unity/.ai-game-developer/+unity/opencode.jsonunity/Library/unity/Temp/unity/Logs/diff --git a/scripts/oss/tests/test_unity_mcp.py b/scripts/oss/tests/test_unity_mcp.pyindex 99b4529ac..b7bc2b697 100644--- a/scripts/oss/tests/test_unity_mcp.py+++ b/scripts/oss/tests/test_unity_mcp.py@@ -45,6 +45,11 @@ def test_project_settings_do_not_commit_the_plugin_gate_defines():assert "UNITY_MCP_" not in text-def test_downloaded_nuget_dlls_are_ignored():+def test_downloads_and_generated_agent_files_are_ignored():+ # The NuGet DLLs are third-party binaries fetched on first open; .mcp.json can carry the local server's bearer+ # token; the generated skills change with every plugin version. UserSettings/ (the plugin's per-user config and+ # local token) was already ignored.ignore = (ROOT / ".gitignore").read_text(encoding="utf-8").splitlines()- assert "unity/Assets/Plugins/NuGet/" in ignore and "unity/Assets/Plugins/NuGet.meta" in ignore+ for rule in ("unity/Assets/Plugins/NuGet/", "unity/Assets/Plugins/NuGet.meta", ".mcp.json", "unity/.claude/",+ "unity/UserSettings/", "unity/Library/"):+ assert rule in ignore, rulediff --git a/unity/Assets/Editor/UnityMcpGuard.cs b/unity/Assets/Editor/UnityMcpGuard.csnew file mode 100644index 000000000..3032180e3--- /dev/null+++ b/unity/Assets/Editor/UnityMcpGuard.cs@@ -0,0 +1,88 @@+#nullable enable+using System;+using System.Linq;+using System.Reflection;+using UnityEditor;+using UnityEditor.Build;+using UnityEngine;++namespace Statecraft.Game.EditorTools+{+ /// <summary>Keeps Unity-MCP (com.ivanmurzak.unity.mcp, the AI-agent bridge; CONTRIBUTING.md) to the interactive editor.+ /// - Batch mode (tests, builds): the plugin would otherwise connect on every domain reload (by default to its cloud+ /// service, whose refusal is logged as an error and fails whichever test is running) or start its local server.+ /// This sets KeepConnected and KeepServerRunning to false for this process only, recorded as the plugin's own+ /// runtime overrides so they never reach the user's UserSettings/AI-Game-Developer-Config.json. Reflection, not a+ /// reference: on a fresh clone the plugin compiles only after its resolver has fetched its NuGet DLLs.+ /// - Player builds: drops the plugin's runtime assemblies and the NuGet DLLs its resolver put in Assets/Plugins/NuGet+ /// (IFilterBuildAssemblies). The game never calls them; the plugin offers no switch of its own for this.</summary>+ [InitializeOnLoad]+ public sealed class UnityMcpGuard : IFilterBuildAssemblies+ {+ const string PluginEditorType = "com.IvanMurzak.Unity.MCP.UnityMcpPluginEditor, com.IvanMurzak.Unity.MCP.Editor";+ static readonly string[] Overrides = { "KeepConnected", "KeepServerRunning" };+ const BindingFlags Any = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy;++ /// <summary>What the batch-mode hold did: "held offline", "interactive editor", "plugin not compiled" or why it failed.</summary>+ public static string Status = "not run";++ static UnityMcpGuard()+ {+ Status = Application.isBatchMode ? HoldOffline() : "interactive editor";+ if (Status != "held offline" && Status != "interactive editor" && Status != "plugin not compiled")+ Debug.LogWarning("UnityMcpGuard: could not hold Unity-MCP offline in batch mode: " + Status);+ }++ static string HoldOffline()+ {+ var editor = Type.GetType(PluginEditorType);+ if (editor == null) return "plugin not compiled";+ try+ {+ var instance = editor.GetProperty("Instance", Any)?.GetValue(null);+ var config = editor.GetField("unityConnectionConfig", Any)?.GetValue(instance);+ var record = editor.GetProperty("RuntimeOverrides", Any)?.GetValue(instance);+ var track = record?.GetType().GetMethod("Track", Any);+ var baselines = record?.GetType().GetProperty("BaselineValues")?.GetValue(record) as System.Collections.IDictionary;+ if (config == null || record == null || track == null) return "plugin internals changed";+ foreach (var name in Overrides)+ {+ var p = config.GetType().GetProperty(name);+ if (p == null) return "plugin internals changed: no " + name;+ // Keep the on-disk value as the baseline the plugin saves, even if an env var already overrode it.+ var baseline = baselines != null && baselines.Contains(name) ? baselines[name] : p.GetValue(config);+ track.Invoke(record, new[] { name, baseline, (object)false });+ p.SetValue(config, false);+ }+ return "held offline";+ }+ catch (Exception e)+ {+ return e.GetType().Name + ": " + e.Message;+ }+ }++ /// <summary>True for the plugin's own assemblies, its PlayerPrefsEx dependency, and any DLL under+ /// Assets/Plugins/NuGet (the folder the plugin's resolver owns).</summary>+ public static bool IsPluginAssembly(string path)+ {+ var p = path.Replace('\\', '/');+ var file = p.Substring(p.LastIndexOf('/') + 1);+ return p.Contains("/Plugins/NuGet/") || p.StartsWith("Assets/Plugins/NuGet/", StringComparison.Ordinal)+ || file.StartsWith("com.IvanMurzak.", StringComparison.Ordinal)+ || file.StartsWith("Extensions.Unity.PlayerPrefsEx", StringComparison.Ordinal);+ }++ public static string[] Filter(string[] assemblies) => assemblies.Where(a => !IsPluginAssembly(a)).ToArray();++ public int callbackOrder => 0;++ public string[] OnFilterAssemblies(BuildOptions buildOptions, string[] assemblies)+ {+ var kept = Filter(assemblies);+ Debug.Log($"UnityMcpGuard: player build keeps {kept.Length} of {assemblies.Length} assemblies; dropped: " ++ string.Join(", ", assemblies.Except(kept).Select(a => a.Substring(a.Replace('\\', '/').LastIndexOf('/') + 1))));+ return kept;+ }+ }+}diff --git a/unity/Assets/Editor/UnityMcpGuard.cs.meta b/unity/Assets/Editor/UnityMcpGuard.cs.metanew file mode 100644index 000000000..f63aa2157--- /dev/null+++ b/unity/Assets/Editor/UnityMcpGuard.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: 451c05538def5412098aabb375bee580\ No newline at end of filediff --git a/unity/Assets/Tests/EditMode/UnityMcpGuardTests.cs b/unity/Assets/Tests/EditMode/UnityMcpGuardTests.csnew file mode 100644index 000000000..78784d959--- /dev/null+++ b/unity/Assets/Tests/EditMode/UnityMcpGuardTests.cs@@ -0,0 +1,64 @@+#nullable enable+using System;+using System.Reflection;+using NUnit.Framework;+using UnityEngine;++namespace Statecraft.Tests.EditMode+{+ /// <summary>Unity-MCP (com.ivanmurzak.unity.mcp) is an interactive-editor tool. In batch mode (tests and builds) it must+ /// not connect anywhere or start its local server, and no part of it may ship in a player build.</summary>+ public sealed class UnityMcpGuardTests+ {+ static Type Guard()+ {+ var t = Type.GetType("Statecraft.Game.EditorTools.UnityMcpGuard, Assembly-CSharp-Editor");+ Assert.IsNotNull(t, "the UnityMcpGuard editor hook exists");+ return t!;+ }++ static string[] Filter(params string[] assemblies) =>+ (string[])Guard().GetMethod("Filter", BindingFlags.Public | BindingFlags.Static)!.Invoke(null, new object[] { assemblies })!;++ [Test]+ public void PlayerBuildsDropThePluginAndTheDllsItsResolverDownloaded()+ {+ var kept = Filter(+ "Library/Bee/PlayerScriptAssemblies/Statecraft.Game.dll",+ "Library/Bee/PlayerScriptAssemblies/com.IvanMurzak.Unity.MCP.Runtime.dll",+ "Library/Bee/PlayerScriptAssemblies/com.IvanMurzak.Unity.MCP.TestFiles.dll",+ "Library/Bee/PlayerScriptAssemblies/Extensions.Unity.PlayerPrefsEx.dll",+ "/abs/unity/Assets/Plugins/NuGet/McpPlugin.dll",+ @"C:\abs\unity\Assets\Plugins\NuGet\System.Text.Json.dll",+ "Assets/Plugins/Statecraft/Statecraft.Sim.dll",+ "Assets/Plugins/Statecraft/YamlDotNet.dll",+ "/Applications/Unity/Managed/UnityEngine/UnityEngine.CoreModule.dll");+ CollectionAssert.AreEqual(new[]+ {+ "Library/Bee/PlayerScriptAssemblies/Statecraft.Game.dll",+ "Assets/Plugins/Statecraft/Statecraft.Sim.dll",+ "Assets/Plugins/Statecraft/YamlDotNet.dll",+ "/Applications/Unity/Managed/UnityEngine/UnityEngine.CoreModule.dll",+ }, kept);+ }++ [Test]+ public void InBatchModeThePluginStaysOfflineForThisProcessOnly()+ {+ var editor = Type.GetType("com.IvanMurzak.Unity.MCP.UnityMcpPluginEditor, com.IvanMurzak.Unity.MCP.Editor");+ Assume.That(Application.isBatchMode, "only batch mode is held offline");+ Assume.That(editor, Is.Not.Null, "the plugin compiles once its resolver has downloaded its DLLs");+ const BindingFlags any = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy;+ Assert.AreEqual("held offline", Guard().GetField("Status", any)!.GetValue(null));+ var instance = editor!.GetProperty("Instance", any)!.GetValue(null);+ var config = editor.GetField("unityConnectionConfig", any)!.GetValue(instance)!;+ Assert.IsFalse((bool)config.GetType().GetProperty("KeepConnected")!.GetValue(config)!, "no connection");+ Assert.IsFalse((bool)config.GetType().GetProperty("KeepServerRunning")!.GetValue(config)!, "no local server");+ // Recorded as runtime overrides, so the plugin never writes them into the user's own settings file.+ var record = editor.GetProperty("RuntimeOverrides", any)!.GetValue(instance)!;+ var contains = record.GetType().GetMethod("Contains")!;+ Assert.IsTrue((bool)contains.Invoke(record, new object[] { "KeepConnected" })!);+ Assert.IsTrue((bool)contains.Invoke(record, new object[] { "KeepServerRunning" })!);+ }+ }+}diff --git a/unity/Assets/Tests/EditMode/UnityMcpGuardTests.cs.meta b/unity/Assets/Tests/EditMode/UnityMcpGuardTests.cs.metanew file mode 100644index 000000000..210e746a0--- /dev/null+++ b/unity/Assets/Tests/EditMode/UnityMcpGuardTests.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: 17491b729cc664c8dbd109cd6e87c255\ No newline at end of file