diff --git a/FunGame.Server/Main.cs b/FunGame.Server/Main.cs index 37cd2c8..d3c45d3 100644 --- a/FunGame.Server/Main.cs +++ b/FunGame.Server/Main.cs @@ -101,8 +101,8 @@ while (Running) ServerHelper.Type(); if (input != "" && Running) { - string[] strings = input.Split(' ', StringSplitOptions.RemoveEmptyEntries); - if (strings.Length > 0) + List strings = ConsoleModel.ParseCommandLine(input); + if (strings.Count > 0) { string order = strings[0].ToLower(); string[] inputArgs = [.. strings.Skip(1)]; diff --git a/FunGame.Server/Models/ConsoleModel.cs b/FunGame.Server/Models/ConsoleModel.cs index 8905962..5e5ee61 100644 --- a/FunGame.Server/Models/ConsoleModel.cs +++ b/FunGame.Server/Models/ConsoleModel.cs @@ -204,6 +204,74 @@ namespace Milimoe.FunGame.Server.Model } } + public static List ParseCommandLine(string input) + { + ReadOnlySpan inputSpan = input.AsSpan(); + List strings = []; + StringBuilder current = new(); + bool inQuotes = false; + + for (int i = 0; i < inputSpan.Length; i++) + { + char c = inputSpan[i]; + + if (inQuotes) + { + if (c == '"') + { + // 检查是否是转义引号 \" + if (i + 1 < inputSpan.Length && inputSpan[i + 1] == '"') + { + current.Append('"'); + // 跳过下一个引号 + i++; + } + else + { + // 结束引号 + inQuotes = false; + } + } + else if (c == '\\' && i + 1 < inputSpan.Length && inputSpan[i + 1] == '"') + { + current.Append('"'); + i++; + } + else + { + current.Append(c); + } + } + else + { + if (c == '"') + { + inQuotes = true; + } + else if (char.IsWhiteSpace(c)) + { + if (current.Length > 0) + { + strings.Add(current.ToString()); + current.Clear(); + } + } + else + { + current.Append(c); + } + } + } + + // 收尾最后一个参数 + if (current.Length > 0) + { + strings.Add(current.ToString()); + } + + return strings; + } + public static string GetOrderAliases(string order) { string[] alias = [.. FunGameSystem.OrderAliasList.Where(kv => kv.Value == order).Select(kv => kv.Key)]; diff --git a/FunGame.WebAPI/Program.cs b/FunGame.WebAPI/Program.cs index f103ee7..d72548c 100644 --- a/FunGame.WebAPI/Program.cs +++ b/FunGame.WebAPI/Program.cs @@ -296,8 +296,8 @@ async Task GetConsoleOrder() ServerHelper.Type(); if (input != "") { - string[] strings = input.Split(' ', StringSplitOptions.RemoveEmptyEntries); - if (strings.Length > 0) + List strings = ConsoleModel.ParseCommandLine(input); + if (strings.Count > 0) { string order = strings[0].ToLower(); string[] args = [.. strings.Skip(1)];