From 6c37b11e071a4c76d76ce484b66bcc4f2d62bf7f Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Sat, 8 Dec 2018 16:26:40 -0600 Subject: [PATCH] basic command system and argument processing added. --- .../CommandProcessor/CommandEngine.cs | 58 ++++++++++++++++++- .../CommandProcessor/ICommandEngineCommand.cs | 11 ++++ .../TextureAtlasTools/TexturePackerCommand.cs | 17 ++++++ RecrownedAthenaeum.ConsoleTools/Tools.cs | 2 + 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 RecrownedAthenaeum.ConsoleTools/CommandProcessor/ICommandEngineCommand.cs create mode 100644 RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/CommandEngine.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/CommandEngine.cs index 07e9b82..644b8b1 100644 --- a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/CommandEngine.cs +++ b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/CommandEngine.cs @@ -7,17 +7,71 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor internal class CommandEngine { public bool running; + public readonly Dictionary commands; + + public CommandEngine() + { + commands = new Dictionary(); + } internal void Run() { while (running) { string command = Console.ReadLine(); - Process(command); + try + { + Process(command); + + } + catch (ArgumentException) + { + Console.WriteLine("Command not found."); + } } } - public void Process(string command) + + public void Process(string commandAndArguments) { + string command = commandAndArguments; + string[] arguments = null; + + if (commandAndArguments.Contains(' ')) + { + command = command.Remove(command.IndexOf(' ')); + if (!commands.ContainsKey(command)) throw new ArgumentException(); + string[] argumentsSplit = commandAndArguments.Remove(command.Length).Split(' '); + List argumentsList = new List(); + + for (int i = 0; i < argumentsSplit.Length; i++) + { + if (argumentsSplit[i].Contains('"') && !argumentsSplit[i].EndsWith('"')) + { + StringBuilder quoteBuilder = new StringBuilder(); + do + { + quoteBuilder.Append(' '); + quoteBuilder.Append(argumentsSplit[i]); + i++; + if (i >= argumentsSplit.Length) + { + throw new ArgumentException(); + } + + } while (!argumentsSplit[i].Contains('"')); + quoteBuilder.Append(' '); + quoteBuilder.Append(argumentsSplit[i]); + argumentsList.Add(quoteBuilder.ToString().Trim()); + } + else + { + argumentsList.Add(argumentsSplit[i]); + } + } + arguments = argumentsList.ToArray(); + } + if (!commands.ContainsKey(command)) throw new ArgumentException(); + commands[command].Run(arguments); } } } diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/ICommandEngineCommand.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/ICommandEngineCommand.cs new file mode 100644 index 0000000..703f0e1 --- /dev/null +++ b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/ICommandEngineCommand.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RecrownedAthenaeum.Tools.CommandProcessor +{ + interface ICommandEngineCommand + { + void Run(string[] arguments); + } +} diff --git a/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs b/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs new file mode 100644 index 0000000..14265b9 --- /dev/null +++ b/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs @@ -0,0 +1,17 @@ +using RecrownedAthenaeum.Tools.CommandProcessor; +using System; +using System.Collections.Generic; +using System.Text; + +namespace RecrownedAthenaeum.Tools.TextureAtlasTools +{ + class TexturePackerCommand : ICommandEngineCommand + { + + + public void Run(string[] arguments) + { + throw new NotImplementedException(); + } + } +} diff --git a/RecrownedAthenaeum.ConsoleTools/Tools.cs b/RecrownedAthenaeum.ConsoleTools/Tools.cs index b98f2bc..997eaf8 100644 --- a/RecrownedAthenaeum.ConsoleTools/Tools.cs +++ b/RecrownedAthenaeum.ConsoleTools/Tools.cs @@ -1,4 +1,5 @@ using RecrownedAthenaeum.Tools.CommandProcessor; +using RecrownedAthenaeum.Tools.TextureAtlasTools; using System; using System.Reflection; @@ -11,6 +12,7 @@ namespace RecrownedAthenaeum.Tools Console.WriteLine("Recrowned Athenaeum Console Tools version " + Assembly.GetExecutingAssembly().GetName().Version.ToString()); CommandEngine ce = new CommandEngine(); + ce.commands.Add("TexturePacker", new TexturePackerCommand()); ce.Run(); } }