switched to list system while adding stop, test, help, and updating texture packer commands.
This commit is contained in:
parent
08a3200546
commit
82bdfcbaa2
@ -6,19 +6,21 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor
|
||||
{
|
||||
internal class CommandEngine
|
||||
{
|
||||
public bool running;
|
||||
public readonly Dictionary<string, ICommandEngineCommand> commands;
|
||||
public bool running = true;
|
||||
public readonly List<EngineCommand> commands;
|
||||
|
||||
public CommandEngine()
|
||||
{
|
||||
commands = new Dictionary<string, ICommandEngineCommand>();
|
||||
commands = new List<EngineCommand>();
|
||||
}
|
||||
|
||||
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<string> argumentsList = new List<string>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace RecrownedAthenaeum.Tools.CommandProcessor
|
||||
{
|
||||
public abstract class EngineCommand
|
||||
{
|
||||
bool CaseSensitiveName = false;
|
||||
|
||||
/// <summary>
|
||||
/// the words a user can type that will invoke this command.
|
||||
/// </summary>
|
||||
public string[] invokeStrings;
|
||||
|
||||
public EngineCommand(params string[] invokeStrings)
|
||||
{
|
||||
this.invokeStrings = invokeStrings ?? throw new ArgumentNullException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not this command should be invoked given the string.
|
||||
/// </summary>
|
||||
/// <param name="command">The string that acts as the command.</param>
|
||||
/// <returns>whether or not this command is invoked.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs the command.
|
||||
/// </summary>
|
||||
/// <param name="arguments">arguments to be used. May be null.</param>
|
||||
public virtual void Run(string[] arguments = null)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="argument">The argument the help string is for. Can be null for overall command help.</param>
|
||||
/// <returns>The text to help understand the argument.</returns>
|
||||
public virtual string Help(string argument = null)
|
||||
{
|
||||
return "This command doesn't define a help string. Shame on it.";
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace RecrownedAthenaeum.Tools.CommandProcessor
|
||||
{
|
||||
interface ICommandEngineCommand
|
||||
{
|
||||
/// <summary>
|
||||
/// Runs the command.
|
||||
/// </summary>
|
||||
/// <param name="arguments">Commands to be used. May be null.</param>
|
||||
void Run(string[] arguments);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the help for the given argument.
|
||||
/// If no argument is given (null), then returns overall help statement.
|
||||
/// </summary>
|
||||
/// <param name="argument">The argument the help string is for. Can be null for overall command help.</param>
|
||||
/// <returns>The text to help understand the argument.</returns>
|
||||
string Help(string argument);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
51
RecrownedAthenaeum.ConsoleTools/ConsoleUtilities.cs
Normal file
51
RecrownedAthenaeum.ConsoleTools/ConsoleUtilities.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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")
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user