-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandProcessor.cs
More file actions
144 lines (124 loc) · 4.71 KB
/
CommandProcessor.cs
File metadata and controls
144 lines (124 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using McpUnity.Core;
using UnityEngine;
namespace McpUnity
{
/// <summary>
/// 命令处理器 - 使用 CommandRouter 分发命令到各个模块
/// </summary>
public static class CommandProcessor
{
public static string Process(string jsonRequest)
{
try
{
// 手动解析JSON获取命令名
string command = ExtractJsonValue(jsonRequest, "command");
string parametersJson = ExtractJsonObject(jsonRequest, "parameters");
if (string.IsNullOrEmpty(command))
{
return ResponseHelper.Error("Missing command");
}
// 解析参数
var parameters = ParseParameters(parametersJson);
// 使用 CommandRouter 执行命令
var result = CommandRouter.Execute(command, parameters);
// 直接传递对象,让 ResponseHelper 处理序列化
return ResponseHelper.Success(result ?? new object());
}
catch (Exception ex)
{
return ResponseHelper.Error(ex.Message);
}
}
#region JSON Parsing Helpers
private static string ExtractJsonValue(string json, string key)
{
string pattern = $"\"{key}\":\\s*\"([^\"]*)\"";
var match = System.Text.RegularExpressions.Regex.Match(json, pattern);
if (match.Success)
{
return match.Groups[1].Value;
}
pattern = $"\"{key}\":\\s*([^,\\}}]+)";
match = System.Text.RegularExpressions.Regex.Match(json, pattern);
if (match.Success)
{
return match.Groups[1].Value.Trim();
}
return null;
}
private static string ExtractJsonObject(string json, string key)
{
int keyIndex = json.IndexOf($"\"{key}\":");
if (keyIndex < 0) return "{}";
int start = keyIndex + key.Length + 3;
while (start < json.Length && char.IsWhiteSpace(json[start])) start++;
if (start >= json.Length) return "{}";
if (json[start] == '{')
{
int depth = 1;
int end = start + 1;
while (end < json.Length && depth > 0)
{
if (json[end] == '{') depth++;
else if (json[end] == '}') depth--;
end++;
}
return json.Substring(start, end - start);
}
return "{}";
}
private static Dictionary<string, string> ParseParameters(string parametersJson)
{
var dict = new Dictionary<string, string>();
if (string.IsNullOrEmpty(parametersJson) || parametersJson == "{}")
return dict;
parametersJson = parametersJson.Trim('{', '}');
var pairs = SplitJsonPairs(parametersJson);
foreach (var pair in pairs)
{
var colonIndex = pair.IndexOf(':');
if (colonIndex > 0)
{
string key = pair.Substring(0, colonIndex).Trim().Trim('"');
string value = pair.Substring(colonIndex + 1).Trim();
if (value.StartsWith("\"") && value.EndsWith("\""))
{
value = value.Substring(1, value.Length - 2);
}
dict[key] = value;
}
}
return dict;
}
private static List<string> SplitJsonPairs(string json)
{
var result = new List<string>();
int depth = 0;
int start = 0;
bool inString = false;
for (int i = 0; i < json.Length; i++)
{
char c = json[i];
if (c == '"' && (i == 0 || json[i - 1] != '\\'))
inString = !inString;
else if (!inString)
{
if (c == '{' || c == '[') depth++;
else if (c == '}' || c == ']') depth--;
else if (c == ',' && depth == 0)
{
result.Add(json.Substring(start, i - start));
start = i + 1;
}
}
}
if (start < json.Length)
result.Add(json.Substring(start));
return result;
}
#endregion
}
}