Skip to content

Commit 0ec5729

Browse files
committed
Replace hard coded command dispatcher to command registry
1 parent b7411e2 commit 0ec5729

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

UnityMcpBridge/Editor/Tools/CommandRegistry.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public static class CommandRegistry
1313
// to the corresponding static HandleCommand method in the appropriate tool class.
1414
private static readonly Dictionary<string, Func<JObject, object>> _handlers = new()
1515
{
16-
{ "HandleManageScript", ManageScript.HandleCommand },
17-
{ "HandleManageScene", ManageScene.HandleCommand },
18-
{ "HandleManageEditor", ManageEditor.HandleCommand },
19-
{ "HandleManageGameObject", ManageGameObject.HandleCommand },
20-
{ "HandleManageAsset", ManageAsset.HandleCommand },
21-
{ "HandleReadConsole", ReadConsole.HandleCommand },
22-
{ "HandleExecuteMenuItem", ExecuteMenuItem.HandleCommand },
23-
{ "HandleManageShader", ManageShader.HandleCommand},
16+
{ "manage_script", ManageScript.HandleCommand },
17+
{ "manage_scene", ManageScene.HandleCommand },
18+
{ "manage_editor", ManageEditor.HandleCommand },
19+
{ "manage_gameobject", ManageGameObject.HandleCommand },
20+
{ "manage_asset", ManageAsset.HandleCommand },
21+
{ "read_console", ReadConsole.HandleCommand },
22+
{ "execute_menu_item", ExecuteMenuItem.HandleCommand },
23+
{ "manage_shader", ManageShader.HandleCommand},
2424
};
2525

2626
/// <summary>
@@ -30,17 +30,18 @@ public static class CommandRegistry
3030
/// <returns>The command handler function if found, null otherwise.</returns>
3131
public static Func<JObject, object> GetHandler(string commandName)
3232
{
33-
// Use case-insensitive comparison for flexibility, although Python side should be consistent
34-
return _handlers.TryGetValue(commandName, out var handler) ? handler : null;
35-
// Consider adding logging here if a handler is not found
36-
/*
37-
if (_handlers.TryGetValue(commandName, out var handler)) {
38-
return handler;
39-
} else {
40-
UnityEngine.Debug.LogError($\"[CommandRegistry] No handler found for command: {commandName}\");
41-
return null;
33+
if (!_handlers.TryGetValue(commandName, out var handler))
34+
{
35+
throw new InvalidOperation(
36+
$"Unknown or unsupported command type: {command.type}");
4237
}
43-
*/
38+
39+
return handler;
40+
}
41+
42+
public static void Add(string commandName, Func<JObject, object> handler)
43+
{
44+
_handlers.Add(commandName, handler);
4445
}
4546
}
4647
}

0 commit comments

Comments
 (0)