unity-mcp/UnityMcpBridge/Editor/Tools/CommandRegistry.cs

47 lines
2.0 KiB
C#
Raw Normal View History

2025-03-31 03:58:01 +08:00
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
namespace UnityMcpBridge.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()
{
{ "HandleManageScript", ManageScript.HandleCommand },
{ "HandleManageScene", ManageScene.HandleCommand },
{ "HandleManageEditor", ManageEditor.HandleCommand },
{ "HandleManageGameObject", ManageGameObject.HandleCommand },
{ "HandleManageAsset", ManageAsset.HandleCommand },
{ "HandleReadConsole", ReadConsole.HandleCommand },
{ "HandleExecuteMenuItem", ExecuteMenuItem.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)
{
// 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
/*
2025-03-31 03:58:01 +08:00
if (_handlers.TryGetValue(commandName, out var handler)) {
return handler;
} else {
UnityEngine.Debug.LogError($\"[CommandRegistry] No handler found for command: {commandName}\");
return null;
}
*/
}
}
}