2025-03-31 03:58:01 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2025-09-12 23:19:58 +08:00
|
|
|
using MCPForUnity.Editor.Tools.MenuItems;
|
2025-03-31 03:58:01 +08:00
|
|
|
|
2025-08-21 03:59:49 +08:00
|
|
|
namespace MCPForUnity.Editor.Tools
|
2025-03-31 03:58:01 +08:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registry for all MCP command handlers (Refactored Version)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class CommandRegistry
|
|
|
|
|
{
|
|
|
|
|
// Maps command names (matching those called from Python via ctx.bridge.unity_editor.HandlerName)
|
|
|
|
|
// to the corresponding static HandleCommand method in the appropriate tool class.
|
|
|
|
|
private static readonly Dictionary<string, Func<JObject, object>> _handlers = new()
|
|
|
|
|
{
|
2025-09-27 06:05:17 +08:00
|
|
|
{ "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},
|
2025-03-31 03:58:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a command handler by name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="commandName">Name of the command handler (e.g., "HandleManageAsset").</param>
|
|
|
|
|
/// <returns>The command handler function if found, null otherwise.</returns>
|
|
|
|
|
public static Func<JObject, object> GetHandler(string commandName)
|
|
|
|
|
{
|
2025-09-27 06:05:17 +08:00
|
|
|
if (!_handlers.TryGetValue(commandName, out var handler))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
$"Unknown or unsupported command type: {commandName}");
|
2025-03-31 03:58:01 +08:00
|
|
|
}
|
2025-09-27 06:05:17 +08:00
|
|
|
|
|
|
|
|
return handler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Add(string commandName, Func<JObject, object> handler)
|
|
|
|
|
{
|
|
|
|
|
_handlers.Add(commandName, handler);
|
2025-03-31 03:58:01 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-08 18:14:13 +08:00
|
|
|
}
|
|
|
|
|
|