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): UnityMcpGuard only looks at a plugin already loaded

eyalgolan4746b0c53092
diff --git a/unity/Assets/Editor/UnityMcpGuard.cs b/unity/Assets/Editor/UnityMcpGuard.cs
index 3032180e3..3408c1e8a 100644
--- a/unity/Assets/Editor/UnityMcpGuard.cs
+++ b/unity/Assets/Editor/UnityMcpGuard.cs
@@ -19,24 +19,31 @@ namespace Statecraft.Game.EditorTools
[InitializeOnLoad]
public sealed class UnityMcpGuard : IFilterBuildAssemblies
{
- const string PluginEditorType = "com.IvanMurzak.Unity.MCP.UnityMcpPluginEditor, com.IvanMurzak.Unity.MCP.Editor";
+ const string PluginEditorAssembly = "com.IvanMurzak.Unity.MCP.Editor";
+ const string PluginEditorType = "com.IvanMurzak.Unity.MCP.UnityMcpPluginEditor";
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>
+ /// <summary>What the batch-mode hold did: "held offline", "interactive editor", "plugin not loaded" 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")
+ if (Status != "held offline" && Status != "interactive editor" && Status != "plugin not loaded")
Debug.LogWarning("UnityMcpGuard: could not hold Unity-MCP offline in batch mode: " + Status);
}
+ /// <summary>The plugin's editor type if its assembly is loaded in this domain, else null. Never Type.GetType: that
+ /// would load a just-compiled plugin assembly on demand into a domain where its [InitializeOnLoad] never ran (the
+ /// first batch run of a fresh clone compiles the plugin after its resolver, then runs the tests without a reload).</summary>
+ public static Type? LoadedPluginEditorType() =>
+ AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == PluginEditorAssembly)?.GetType(PluginEditorType);
+
static string HoldOffline()
{
- var editor = Type.GetType(PluginEditorType);
- if (editor == null) return "plugin not compiled";
+ var editor = LoadedPluginEditorType();
+ if (editor == null) return "plugin not loaded";
try
{
var instance = editor.GetProperty("Instance", Any)?.GetValue(null);
diff --git a/unity/Assets/Tests/EditMode/UnityMcpGuardTests.cs b/unity/Assets/Tests/EditMode/UnityMcpGuardTests.cs
index 78784d959..f23151e57 100644
--- a/unity/Assets/Tests/EditMode/UnityMcpGuardTests.cs
+++ b/unity/Assets/Tests/EditMode/UnityMcpGuardTests.cs
@@ -45,11 +45,14 @@ namespace Statecraft.Tests.EditMode
[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 status = (string)Guard().GetField("Status", any)!.GetValue(null)!;
+ var editor = (Type?)Guard().GetMethod("LoadedPluginEditorType", BindingFlags.Public | BindingFlags.Static)!.Invoke(null, null);
+ // Not Assume: Unity's runner exits 2 on an inconclusive test. The interactive editor is left alone, and on a
+ // fresh clone's first run the plugin compiles after its resolver, so the tests run before a reload loads it.
+ if (!Application.isBatchMode) { Assert.AreEqual("interactive editor", status); return; }
+ if (editor == null) { Assert.AreEqual("plugin not loaded", status); return; }
+ Assert.AreEqual("held offline", status);
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");