Replace command dispatcher with CommandRegistry, allow to add custom command handlers. (#261)
* Replace hard coded command dispatcher to command registry * Bug fixed. * bug fixed. * bug fixed. * Bug fixed. * Fix tests.main
parent
b57a3b8500
commit
da91f256a2
|
|
@ -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";
|
||||
try
|
||||
{
|
||||
var handler = CommandRegistry.GetHandler(unknown);
|
||||
Assert.IsNull(handler, "Expected null handler for unknown command name.");
|
||||
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.");
|
||||
|
|
|
|||
|
|
@ -1040,25 +1040,7 @@ namespace MCPForUnity.Editor
|
|||
// Use JObject for parameters as the new handlers likely expect this
|
||||
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),
|
||||
// Run scene operations on the main thread to avoid deadlocks/hangs (with diagnostics under debug flag)
|
||||
"manage_scene" => HandleManageScene(paramsObject)
|
||||
?? throw new TimeoutException($"manage_scene timed out after {FrameIOTimeoutMs} ms on main thread"),
|
||||
"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),
|
||||
"manage_menu_item" => ManageMenuItem.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 };
|
||||
|
|
|
|||
|
|
@ -14,14 +14,14 @@ namespace MCPForUnity.Editor.Tools
|
|||
// to the corresponding static HandleCommand method in the appropriate tool class.
|
||||
private static readonly Dictionary<string, Func<JObject, object>> _handlers = new()
|
||||
{
|
||||
{ "HandleManageScript", ManageScript.HandleCommand },
|
||||
{ "HandleManageScene", ManageScene.HandleCommand },
|
||||
{ "HandleManageEditor", ManageEditor.HandleCommand },
|
||||
{ "HandleManageGameObject", ManageGameObject.HandleCommand },
|
||||
{ "HandleManageAsset", ManageAsset.HandleCommand },
|
||||
{ "HandleReadConsole", ReadConsole.HandleCommand },
|
||||
{ "HandleManageMenuItem", ManageMenuItem.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 },
|
||||
{ "manage_menu_item", ManageMenuItem.HandleCommand },
|
||||
{ "manage_shader", ManageShader.HandleCommand},
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -31,17 +31,18 @@ namespace MCPForUnity.Editor.Tools
|
|||
/// <returns>The command handler function if found, null otherwise.</returns>
|
||||
public static Func<JObject, object> 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 InvalidOperationException(
|
||||
$"Unknown or unsupported command type: {commandName}");
|
||||
}
|
||||
*/
|
||||
|
||||
return handler;
|
||||
}
|
||||
|
||||
public static void Add(string commandName, Func<JObject, object> handler)
|
||||
{
|
||||
_handlers.Add(commandName, handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue