From 0ec5729dc47492cc0453faa3fe61b7672a6605ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E5=85=B4=E9=80=B8?= <853974536@qq.com> Date: Thu, 4 Sep 2025 11:44:45 +0800 Subject: [PATCH 1/6] Replace hard coded command dispatcher to command registry --- .../Editor/Tools/CommandRegistry.cs | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/UnityMcpBridge/Editor/Tools/CommandRegistry.cs b/UnityMcpBridge/Editor/Tools/CommandRegistry.cs index 55c7425b..68ca22b9 100644 --- a/UnityMcpBridge/Editor/Tools/CommandRegistry.cs +++ b/UnityMcpBridge/Editor/Tools/CommandRegistry.cs @@ -13,14 +13,14 @@ public static class CommandRegistry // to the corresponding static HandleCommand method in the appropriate tool class. private static readonly Dictionary> _handlers = new() { - { "HandleManageScript", ManageScript.HandleCommand }, - { "HandleManageScene", ManageScene.HandleCommand }, - { "HandleManageEditor", ManageEditor.HandleCommand }, - { "HandleManageGameObject", ManageGameObject.HandleCommand }, - { "HandleManageAsset", ManageAsset.HandleCommand }, - { "HandleReadConsole", ReadConsole.HandleCommand }, - { "HandleExecuteMenuItem", ExecuteMenuItem.HandleCommand }, - { "HandleManageShader", ManageShader.HandleCommand}, + { "manage_script", ManageScript.HandleCommand }, + { "manage_scene", ManageScene.HandleCommand }, + { "manage_editor", ManageEditor.HandleCommand }, + { "manage_gameobject", ManageGameObject.HandleCommand }, + { "manage_asset", ManageAsset.HandleCommand }, + { "read_console", ReadConsole.HandleCommand }, + { "execute_menu_item", ExecuteMenuItem.HandleCommand }, + { "manage_shader", ManageShader.HandleCommand}, }; /// @@ -30,17 +30,18 @@ public static class CommandRegistry /// The command handler function if found, null otherwise. public static Func GetHandler(string commandName) { - // Use case-insensitive comparison for flexibility, although Python side should be consistent - return _handlers.TryGetValue(commandName, out var handler) ? handler : null; - // Consider adding logging here if a handler is not found - /* - if (_handlers.TryGetValue(commandName, out var handler)) { - return handler; - } else { - UnityEngine.Debug.LogError($\"[CommandRegistry] No handler found for command: {commandName}\"); - return null; + if (!_handlers.TryGetValue(commandName, out var handler)) + { + throw new InvalidOperation( + $"Unknown or unsupported command type: {command.type}"); } - */ + + return handler; + } + + public static void Add(string commandName, Func handler) + { + _handlers.Add(commandName, handler); } } } From 6bce7b8558d156a306ad028d61b947b72e3afd29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E5=85=B4=E9=80=B8?= <853974536@qq.com> Date: Thu, 4 Sep 2025 11:47:05 +0800 Subject: [PATCH 2/6] Bug fixed. --- UnityMcpBridge/Editor/Tools/CommandRegistry.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnityMcpBridge/Editor/Tools/CommandRegistry.cs b/UnityMcpBridge/Editor/Tools/CommandRegistry.cs index 68ca22b9..c5a99afd 100644 --- a/UnityMcpBridge/Editor/Tools/CommandRegistry.cs +++ b/UnityMcpBridge/Editor/Tools/CommandRegistry.cs @@ -32,7 +32,7 @@ public static Func GetHandler(string commandName) { if (!_handlers.TryGetValue(commandName, out var handler)) { - throw new InvalidOperation( + throw new InvalidOperationException( $"Unknown or unsupported command type: {command.type}"); } From 453868afeeffcbe4b8379cf3df656713a535ab8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E5=85=B4=E9=80=B8?= <853974536@qq.com> Date: Thu, 4 Sep 2025 11:47:53 +0800 Subject: [PATCH 3/6] bug fixed. --- UnityMcpBridge/Editor/Tools/CommandRegistry.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnityMcpBridge/Editor/Tools/CommandRegistry.cs b/UnityMcpBridge/Editor/Tools/CommandRegistry.cs index c5a99afd..eb9df38b 100644 --- a/UnityMcpBridge/Editor/Tools/CommandRegistry.cs +++ b/UnityMcpBridge/Editor/Tools/CommandRegistry.cs @@ -33,7 +33,7 @@ public static Func GetHandler(string commandName) if (!_handlers.TryGetValue(commandName, out var handler)) { throw new InvalidOperationException( - $"Unknown or unsupported command type: {command.type}"); + $"Unknown or unsupported command type: {commandName}"); } return handler; From e70171a741d2a695ca3d7aa2f4997802bf3ec15a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E5=85=B4=E9=80=B8?= <853974536@qq.com> Date: Thu, 4 Sep 2025 11:50:39 +0800 Subject: [PATCH 4/6] bug fixed. --- UnityMcpBridge/Editor/MCPForUnityBridge.cs | 27 +++++----------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/UnityMcpBridge/Editor/MCPForUnityBridge.cs b/UnityMcpBridge/Editor/MCPForUnityBridge.cs index 1a175847..6c2eff0c 100644 --- a/UnityMcpBridge/Editor/MCPForUnityBridge.cs +++ b/UnityMcpBridge/Editor/MCPForUnityBridge.cs @@ -37,13 +37,13 @@ private static Dictionary< private static bool isAutoConnectMode = false; private const ulong MaxFrameBytes = 64UL * 1024 * 1024; // 64 MiB hard cap for framed payloads private const int FrameIOTimeoutMs = 30000; // Per-read timeout to avoid stalled clients - + // Debug helpers private static bool IsDebugEnabled() { try { return EditorPrefs.GetBool("MCPForUnity.DebugLogs", false); } catch { return false; } } - + private static void LogBreadcrumb(string stage) { if (IsDebugEnabled()) @@ -62,7 +62,7 @@ private static void LogBreadcrumb(string stage) public static void StartAutoConnect() { Stop(); // Stop current connection - + try { // Prefer stored project port and start using the robust Start() path (with retries/options) @@ -251,7 +251,7 @@ public static void Start() const int maxImmediateRetries = 3; const int retrySleepMs = 75; int attempt = 0; - for (;;) + for (; ; ) { try { @@ -578,7 +578,7 @@ private static async System.Threading.Tasks.Task ReadFrameAsUtf8Async(Ne { byte[] header = await ReadExactAsync(stream, 8, timeoutMs); ulong payloadLen = ReadUInt64BigEndian(header); - if (payloadLen > MaxFrameBytes) + if (payloadLen > MaxFrameBytes) { throw new System.IO.IOException($"Invalid framed length: {payloadLen}"); } @@ -795,22 +795,7 @@ private static string ExecuteCommand(Command command) JObject paramsObject = command.@params ?? new JObject(); // Route command based on the new tool structure from the refactor plan - object result = command.type switch - { - // Maps the command type (tool name) to the corresponding handler's static HandleCommand method - // Assumes each handler class has a static method named 'HandleCommand' that takes JObject parameters - "manage_script" => ManageScript.HandleCommand(paramsObject), - "manage_scene" => ManageScene.HandleCommand(paramsObject), - "manage_editor" => ManageEditor.HandleCommand(paramsObject), - "manage_gameobject" => ManageGameObject.HandleCommand(paramsObject), - "manage_asset" => ManageAsset.HandleCommand(paramsObject), - "manage_shader" => ManageShader.HandleCommand(paramsObject), - "read_console" => ReadConsole.HandleCommand(paramsObject), - "execute_menu_item" => ExecuteMenuItem.HandleCommand(paramsObject), - _ => throw new ArgumentException( - $"Unknown or unsupported command type: {command.type}" - ), - }; + object result = CommandRegistry.GetHandler(command.type)(paramsObject); // Standard success response format var response = new { status = "success", result }; From 949e23a89fdea4ea84682f244a4852549d1bb8c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E5=85=B4=E9=80=B8?= <853974536@qq.com> Date: Fri, 26 Sep 2025 00:18:05 +0800 Subject: [PATCH 5/6] Bug fixed. --- UnityMcpBridge/Editor/Tools/CommandRegistry.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnityMcpBridge/Editor/Tools/CommandRegistry.cs b/UnityMcpBridge/Editor/Tools/CommandRegistry.cs index fe5f1710..c53642c0 100644 --- a/UnityMcpBridge/Editor/Tools/CommandRegistry.cs +++ b/UnityMcpBridge/Editor/Tools/CommandRegistry.cs @@ -20,7 +20,7 @@ public static class CommandRegistry { "manage_gameobject", ManageGameObject.HandleCommand }, { "manage_asset", ManageAsset.HandleCommand }, { "read_console", ReadConsole.HandleCommand }, - { "execute_menu_item", ExecuteMenuItem.HandleCommand }, + { "manage_menu_item", ManageMenuItem.HandleCommand }, { "manage_shader", ManageShader.HandleCommand}, }; From 4893a8bf284022dcbf6d1ebba3e1b168c42fbc67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E5=85=B4=E9=80=B8?= <853974536@qq.com> Date: Fri, 26 Sep 2025 07:37:57 +0800 Subject: [PATCH 6/6] Fix tests. --- .../EditMode/Tools/CommandRegistryTests.cs | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/CommandRegistryTests.cs b/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/CommandRegistryTests.cs index c12d1fd1..2bbe4616 100644 --- a/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/CommandRegistryTests.cs +++ b/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/CommandRegistryTests.cs @@ -8,18 +8,29 @@ namespace MCPForUnityTests.Editor.Tools public class CommandRegistryTests { [Test] - public void GetHandler_ReturnsNull_ForUnknownCommand() + public void GetHandler_ThrowException_ForUnknownCommand() { var unknown = "HandleDoesNotExist"; - var handler = CommandRegistry.GetHandler(unknown); - Assert.IsNull(handler, "Expected null handler for unknown command name."); + try + { + var handler = CommandRegistry.GetHandler(unknown); + Assert.Fail("Should throw InvalidOperation for unknown handler."); + } + catch (InvalidOperationException) + { + + } + catch + { + Assert.Fail("Should throw InvalidOperation for unknown handler."); + } } [Test] public void GetHandler_ReturnsManageGameObjectHandler() { - var handler = CommandRegistry.GetHandler("HandleManageGameObject"); - Assert.IsNotNull(handler, "Expected a handler for HandleManageGameObject."); + var handler = CommandRegistry.GetHandler("manage_gameobject"); + Assert.IsNotNull(handler, "Expected a handler for manage_gameobject."); var methodInfo = handler.Method; Assert.AreEqual("HandleCommand", methodInfo.Name, "Handler method name should be HandleCommand.");