switched to list system while adding stop, test, help, and updating texture packer commands.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user