From 82bdfcbaa2f7adff16ff9a6709cb33ae42a6be05 Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Sun, 9 Dec 2018 00:51:33 -0600 Subject: [PATCH] switched to list system while adding stop, test, help, and updating texture packer commands. --- .../CommandProcessor/CommandEngine.cs | 37 ++++++++--- .../CommandProcessor/EngineCommand.cs | 62 +++++++++++++++++++ .../CommandProcessor/HelpCommand.cs | 56 +++++++++++++++++ .../CommandProcessor/ICommandEngineCommand.cs | 23 ------- .../CommandProcessor/StopCommand.cs | 26 ++++++++ .../CommandProcessor/TestCommand.cs | 31 ++++++++++ .../ConsoleUtilities.cs | 51 +++++++++++++++ .../TextureAtlasTools/TexturePackerCommand.cs | 13 ++-- RecrownedAthenaeum.ConsoleTools/Tools.cs | 11 +++- 9 files changed, 271 insertions(+), 39 deletions(-) create mode 100644 RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommand.cs create mode 100644 RecrownedAthenaeum.ConsoleTools/CommandProcessor/HelpCommand.cs delete mode 100644 RecrownedAthenaeum.ConsoleTools/CommandProcessor/ICommandEngineCommand.cs create mode 100644 RecrownedAthenaeum.ConsoleTools/CommandProcessor/StopCommand.cs create mode 100644 RecrownedAthenaeum.ConsoleTools/CommandProcessor/TestCommand.cs create mode 100644 RecrownedAthenaeum.ConsoleTools/ConsoleUtilities.cs diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/CommandEngine.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/CommandEngine.cs index 793928c..18232f5 100644 --- a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/CommandEngine.cs +++ b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/CommandEngine.cs @@ -6,19 +6,21 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor { internal class CommandEngine { - public bool running; - public readonly Dictionary commands; + public bool running = true; + public readonly List commands; public CommandEngine() { - commands = new Dictionary(); + commands = new List(); } internal void Run() { while (running) { + ConsoleUtilities.WriteWrappedLine("\nAwaiting command."); string command = Console.ReadLine(); + Console.WriteLine(); try { Process(command); @@ -26,7 +28,7 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor } catch (ArgumentException ae) { - Console.WriteLine("Error: " + ae.Message); + ConsoleUtilities.WriteWrappedLine("Error: " + ae.Message); } } } @@ -39,8 +41,8 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor if (commandAndArguments.Contains(' ')) { command = command.Remove(command.IndexOf(' ')); - if (!commands.ContainsKey(command)) throw new ArgumentException("Command not found."); - string[] argumentsSplit = commandAndArguments.Remove(command.Length).Split(' '); + if (!ContainsCommand(command)) throw new ArgumentException("Command not found."); + string[] argumentsSplit = commandAndArguments.Substring(command.Length + 1).Split(' '); List argumentsList = new List(); for (int i = 0; i < argumentsSplit.Length; i++) @@ -70,8 +72,27 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor } arguments = argumentsList.ToArray(); } - if (!commands.ContainsKey(command)) throw new ArgumentException("Command not found."); - commands[command].Run(arguments); + if (!ContainsCommand(command)) throw new ArgumentException("Command not found. Type \"help\" for a list of commands."); + GetCommand(command).Run(arguments); + } + + public bool ContainsCommand(string command) + { + for (int i = 0; i < commands.Count; i++) + { + if (commands[i].IsInvoked(command)) return true; + } + return false; + } + + public EngineCommand GetCommand(string command) + { + for (int i = 0; i < commands.Count; i++) + { + if (commands[i].IsInvoked(command)) return commands[i]; + } + + return null; } } } diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommand.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommand.cs new file mode 100644 index 0000000..2d20039 --- /dev/null +++ b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommand.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RecrownedAthenaeum.Tools.CommandProcessor +{ + public abstract class EngineCommand + { + bool CaseSensitiveName = false; + + /// + /// the words a user can type that will invoke this command. + /// + public string[] invokeStrings; + + public EngineCommand(params string[] invokeStrings) + { + this.invokeStrings = invokeStrings ?? throw new ArgumentNullException(); + } + + /// + /// Whether or not this command should be invoked given the string. + /// + /// The string that acts as the command. + /// whether or not this command is invoked. + public bool IsInvoked(string command) + { + if (!CaseSensitiveName) command = command.ToLower(); + for (int i = 0; i < invokeStrings.Length; i++) + { + if (!CaseSensitiveName) + { + if (invokeStrings[i].ToLower() == command) return true; + } else + { + if (invokeStrings[i] == command) return true; + } + } + return false; + } + + /// + /// Runs the command. + /// + /// arguments to be used. May be null. + public virtual void Run(string[] arguments = null) + { + + } + + /// + /// Returns the help for the given argument for this command. If no argument is defined, general help for the command should be given. + /// If no argument is given (null), then returns overall help statement. + /// + /// The argument the help string is for. Can be null for overall command help. + /// The text to help understand the argument. + public virtual string Help(string argument = null) + { + return "This command doesn't define a help string. Shame on it."; + } + } +} diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/HelpCommand.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/HelpCommand.cs new file mode 100644 index 0000000..370e3b2 --- /dev/null +++ b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/HelpCommand.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RecrownedAthenaeum.Tools.CommandProcessor +{ + internal class HelpCommand : EngineCommand + { + CommandEngine commandEngine; + + public HelpCommand(CommandEngine commandEngine) : base("help") + { + this.commandEngine = commandEngine; + } + + public override string Help(string argument) + { + return "Prints this."; + } + + + public override void Run(string[] arguments) + { + if (arguments != null) + { + if (commandEngine.ContainsCommand(arguments[0])) + { + ConsoleUtilities.WriteWrappedLine(commandEngine.GetCommand(arguments[0]).Help(null)); + } else + { + throw new ArgumentException(arguments[0] + " not a command. Type \"help\" for a list of commands."); + } + } else + { + ConsoleUtilities.WriteWrappedLine("Tools for RecrownedAthenaeum library. Possible commands are as follows:\n"); + foreach (EngineCommand engineCommand in commandEngine.commands) + { + for (int i = 0; i < engineCommand.invokeStrings.Length; i++) + { + ConsoleUtilities.WriteWrapped(engineCommand.invokeStrings[i]); + if (i + 1 < engineCommand.invokeStrings.Length) + { + ConsoleUtilities.WriteWrapped(", "); + } + } + foreach (string name in engineCommand.invokeStrings) + { + } + ConsoleUtilities.WriteWrapped(" : "); + ConsoleUtilities.WriteWrapped(engineCommand.Help().Replace("\n", "\n\t"), true); + Console.WriteLine(); + } + } + } + } +} diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/ICommandEngineCommand.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/ICommandEngineCommand.cs deleted file mode 100644 index e71744e..0000000 --- a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/ICommandEngineCommand.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace RecrownedAthenaeum.Tools.CommandProcessor -{ - interface ICommandEngineCommand - { - /// - /// Runs the command. - /// - /// Commands to be used. May be null. - void Run(string[] arguments); - - /// - /// Returns the help for the given argument. - /// If no argument is given (null), then returns overall help statement. - /// - /// The argument the help string is for. Can be null for overall command help. - /// The text to help understand the argument. - string Help(string argument); - } -} diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/StopCommand.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/StopCommand.cs new file mode 100644 index 0000000..a1c0f71 --- /dev/null +++ b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/StopCommand.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RecrownedAthenaeum.Tools.CommandProcessor +{ + internal class StopCommand : EngineCommand + { + CommandEngine commandEngine; + + public StopCommand(CommandEngine commandEngine) : base("quit", "stop", "q", "exit") + { + this.commandEngine = commandEngine; + } + + public override string Help(string argument = null) + { + return "Exits the tool."; + } + + public override void Run(string[] arguments = null) + { + commandEngine.running = false; + } + } +} diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/TestCommand.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/TestCommand.cs new file mode 100644 index 0000000..ce3630c --- /dev/null +++ b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/TestCommand.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RecrownedAthenaeum.Tools.CommandProcessor +{ + class TestCommand : EngineCommand + { + public TestCommand() : base("test") + { + + } + + public override string Help(string argument = null) + { + return "Test command."; + } + + public override void Run(string[] arguments = null) + { + if (arguments != null && arguments[0] == "-lb") + { + ConsoleUtilities.WriteWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit.[here]\n Duis semper lacinia nisl, eget efficitur massa feugiat vel. Ut egestas elit id sollicitudin pellentesque.[here no space after break.]\nFusce ullamcorper nec turpis at aliquam. Donec cursus mi nec porttitor convallis. Nullam condimentum sollicitudin volutpat. Pellentesque tellus ligula, eleifend sit amet ante ac, accumsan volutpat lorem. Suspendisse potenti. Vestibulum at sodales ipsum. Mauris dignissim maximus purus sagittis elementum.", true); + ConsoleUtilities.WriteWrapped("test"); + ConsoleUtilities.WriteWrapped(". "); + ConsoleUtilities.WriteWrapped("test."); + } + base.Run(arguments); + } + } +} diff --git a/RecrownedAthenaeum.ConsoleTools/ConsoleUtilities.cs b/RecrownedAthenaeum.ConsoleTools/ConsoleUtilities.cs new file mode 100644 index 0000000..3999d18 --- /dev/null +++ b/RecrownedAthenaeum.ConsoleTools/ConsoleUtilities.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RecrownedAthenaeum.Tools +{ + internal class ConsoleUtilities + { + public static void WriteWrapped(string message, bool line = false) + { + message = WrapMessageToConsoleWidth(message); + if (line) message = message + "\n"; + Console.Write(message); + } + + public static string WrapMessageToConsoleWidth(string message) + { + string[] words = message.Replace("\t", "").Split(' '); + StringBuilder stringBuilder = new StringBuilder(); + int currentLineSize = 0; + for (int i = 0; i < words.Length; i++) + { + if (currentLineSize + words[i].Length >= Console.BufferWidth) + { + currentLineSize = 0; + stringBuilder.AppendLine(); + } + if (words[i].Contains("\n")) + { + currentLineSize = 0; + } + currentLineSize += words[i].Length + 1; + if (words[i].Contains("\n")) + { + currentLineSize -= 2; + } + stringBuilder.Append(words[i]); + if (i + 1 < words.Length) + { + stringBuilder.Append(' '); + } + } + return stringBuilder.ToString(); + } + + public static void WriteWrappedLine(string message) + { + WriteWrapped(message, true); + } + } +} diff --git a/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs b/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs index c591f60..702f378 100644 --- a/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs +++ b/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs @@ -7,20 +7,23 @@ using System.Text; namespace RecrownedAthenaeum.Tools.TextureAtlasTools { - class TexturePackerCommand : ICommandEngineCommand + class TexturePackerCommand : EngineCommand { TexturePacker texturePacker; - public string Help(string argument) + public TexturePackerCommand() : base("texturepacker") { } + + public override string Help(string argument) { return "Packs a given directory composed of png and jpg files into an atlas. Can also add 9patch properties.\n" + - "-i path for input directory containing the textures. Required." + - "-o path for output files. Should point to a non-existent file with no extension as the extension will be created for both the atlas definition file and texture file. Required.\n" + + "-i path for input directory containing the textures. Required.\n\n" + + "-o path for output files. Points to non-existent file. Will create texture and definitions file with name. Required.\n\n" + "-9p can be used multiple times for defining a 9patch. This parameter requires a name, left patch, right patch, top patch, and bottom patch in the format name,a,b,c,d. Optional."; } - public void Run(string[] arguments) + public override void Run(string[] arguments) { + if (arguments == null) throw new ArgumentException("Requires arguments. Type \"help texturepacker\" for more info."); for (int i = 0; i < arguments.Length; i++) { if (arguments[i] == "-i") diff --git a/RecrownedAthenaeum.ConsoleTools/Tools.cs b/RecrownedAthenaeum.ConsoleTools/Tools.cs index 997eaf8..681e90c 100644 --- a/RecrownedAthenaeum.ConsoleTools/Tools.cs +++ b/RecrownedAthenaeum.ConsoleTools/Tools.cs @@ -9,10 +9,15 @@ namespace RecrownedAthenaeum.Tools { static void Main(string[] args) { - Console.WriteLine("Recrowned Athenaeum Console Tools version " + Assembly.GetExecutingAssembly().GetName().Version.ToString()); - CommandEngine ce = new CommandEngine(); - ce.commands.Add("TexturePacker", new TexturePackerCommand()); + + ConsoleUtilities.WriteWrappedLine("Recrowned Athenaeum Console Tools version " + Assembly.GetExecutingAssembly().GetName().Version.ToString()); + ConsoleUtilities.WriteWrappedLine("Type \"help\" for help."); + + ce.commands.Add(new HelpCommand(ce)); + ce.commands.Add(new TexturePackerCommand()); + ce.commands.Add(new StopCommand(ce)); + ce.commands.Add(new TestCommand()); ce.Run(); } }