From d604c59a771ceeb836b1e66b97e12a87b8519773 Mon Sep 17 00:00:00 2001 From: TheUbMunster <66451362+TheUbMunster@users.noreply.github.com> Date: Tue, 26 Jul 2022 17:09:44 -0600 Subject: [PATCH] Added quotable arguments, potential fix for ban * not banning anyone. --- Server/CommandHandler.cs | 34 ++++++++++++++++++++++++++++++++-- Server/Program.cs | 6 +++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/Server/CommandHandler.cs b/Server/CommandHandler.cs index 9a9634f..6dc5978 100644 --- a/Server/CommandHandler.cs +++ b/Server/CommandHandler.cs @@ -1,4 +1,5 @@ -namespace Server; +using System.Text; +namespace Server; public static class CommandHandler { public delegate Response Handler(string[] args); @@ -19,10 +20,39 @@ public static class CommandHandler { } } - public static Response GetResult(string input) { + /// + /// Modified by TheUbMunster + /// + public static Response GetResult(string input) + { try { string[] args = input.Split(' '); if (args.Length == 0) return "No command entered, see help command for valid commands"; + //this part is to allow single arguments that contain spaces (since the game seems to be able to handle usernames with spaces, we need to as well) + List newArgs = new List(); + newArgs.Add(args[0]); + for (int i = 1; i < args.Length; i++) { + if (args[i].Length == 0) continue; //empty string (>1 whitespace between arguments). + else if (args[i][0] == '\"') { + //concatenate args until a string ends with a quote + StringBuilder sb = new StringBuilder(); + i--; //fix off-by-one issue + do + { + i++; + sb.Append(args[i] + " "); //add space back removed by the string.Split(' ') + if (i >= args.Length) { + return "Unmatching quotes, make sure that whenever quotes are used, another quote is present to close it (no action was performed)."; + } + } while (args[i][^1] != '\"'); + newArgs.Add(sb.ToString(1, sb.Length - 3)); //remove quotes and extra space at the end. + } + else + { + newArgs.Add(args[i]); + } + } + args = newArgs.ToArray(); string commandName = args[0]; return Handlers.TryGetValue(commandName, out Handler? handler) ? handler(args[1..]) : $"Invalid command {args[0]}, see help command for valid commands"; } diff --git a/Server/Program.cs b/Server/Program.cs index 0434204..2b8ff86 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -202,7 +202,7 @@ CommandHandler.RegisterCommand("rejoin", args => { } bool moreThanOne = false; StringBuilder builder = new StringBuilder(); - Client[] clients = (args[0] == "*" + Client[] clients = (args[0].Trim() == "*" ? server.Clients.Where(c => c.Connected) : server.Clients.Where(c => c.Connected && args.Any(x => c.Name.StartsWith(x) || (Guid.TryParse(x, out Guid result) && result == c.Id)))).ToArray(); @@ -222,7 +222,7 @@ CommandHandler.RegisterCommand("crash", args => { } bool moreThanOne = false; StringBuilder builder = new StringBuilder(); - Client[] clients = (args[0] == "*" + Client[] clients = (args[0].Trim() == "*" ? server.Clients.Where(c => c.Connected) : server.Clients.Where(c => c.Connected && args.Any(x => c.Name.StartsWith(x) || (Guid.TryParse(x, out Guid result) && result == c.Id)))).ToArray(); @@ -251,7 +251,7 @@ CommandHandler.RegisterCommand("ban", args => { bool moreThanOne = false; StringBuilder builder = new StringBuilder(); - Client[] clients = (args[0] == "*" + Client[] clients = (args[0].Trim() == "*" ? server.Clients.Where(c => c.Connected) : server.Clients.Where(c => c.Connected && args.Any(x => c.Name.StartsWith(x) || (Guid.TryParse(x, out Guid result) && result == c.Id)))).ToArray();