From 47e8b1cd1fb95d093d4d4af6bc0e11ab156b5e71 Mon Sep 17 00:00:00 2001 From: milimoe Date: Tue, 12 May 2026 22:31:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=91=BD=E4=BB=A4=E8=A1=8C=E5=85=81=E8=AE=B8?= =?UTF-8?q?=E8=BE=93=E5=85=A5""=E6=9D=A5=E8=A1=A8=E7=A4=BA=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=AE=8C=E6=95=B4=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FunGame.Server/Main.cs | 4 +- FunGame.Server/Models/ConsoleModel.cs | 68 +++++++++++++++++++++++++++ FunGame.WebAPI/Program.cs | 4 +- 3 files changed, 72 insertions(+), 4 deletions(-) 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)];