recrownedgtk/RecrownedGTK.Tools/CommandProcessor/CommandEngine.cs

107 lines
3.9 KiB
C#
Raw Normal View History

2020-02-17 02:44:21 +00:00
using System;
using System.Collections.Generic;
using System.Text;
namespace RecrownedGTK.Tools.CommandProcessor
{
public class CommandEngine
2020-02-17 02:44:21 +00:00
{
public bool running = true;
public readonly List<EngineCommand> commands;
public CommandEngine()
{
commands = new List<EngineCommand>();
}
public void Run(IUserInput input, IUserOutput output)
2020-02-17 02:44:21 +00:00
{
while (running)
{
output.WrappedOutput("\nAwaiting command.");
string command = input.GetInput();
2020-02-17 02:44:21 +00:00
try
{
Process(input, output, command);
2020-02-17 02:44:21 +00:00
}
catch (ArgumentException ae)
{
output.WrappedOutput("Error: " + ae.Message);
2020-02-17 02:44:21 +00:00
}
}
}
public void Process(IUserInput userInput, IUserOutput userOutput, string commandAndArguments)
2020-02-17 02:44:21 +00:00
{
if (userInput == null || userOutput == null || commandAndArguments == null) {
throw new ArgumentNullException("No arguments should be null.");
}
2020-02-17 02:44:21 +00:00
string command = commandAndArguments;
string[] arguments = null;
if (commandAndArguments.Contains(" "))
2020-02-17 02:44:21 +00:00
{
command = command.Remove(command.IndexOf(' '));
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++)
{
if (argumentsSplit[i].Contains("\"") && !argumentsSplit[i].EndsWith("\""))
2020-02-17 02:44:21 +00:00
{
StringBuilder quoteBuilder = new StringBuilder();
do
{
quoteBuilder.Append(' ');
quoteBuilder.Append(argumentsSplit[i]);
i++;
if (i >= argumentsSplit.Length)
{
throw new ArgumentException("Argument is missing terminating \'\"\'");
}
} while (!argumentsSplit[i].Contains("\""));
quoteBuilder.Append(" ");
2020-02-17 02:44:21 +00:00
quoteBuilder.Append(argumentsSplit[i]);
argumentsList.Add(quoteBuilder.ToString().Replace("\"", "").Trim());
}
else
{
argumentsList.Add(argumentsSplit[i]);
}
}
arguments = argumentsList.ToArray();
}
if (!ContainsCommand(command)) throw new ArgumentException("Command not found. Type \"help\" for a list of commands.");
GetCommand(command).Run(userInput, userOutput, arguments);
2020-02-17 02:44:21 +00:00
}
/// <summary>
/// Check if the command exists using it's invokes string.
/// </summary>
/// <param name="command">A string that will invoke the command.</param>
/// <returns>Returns true if the given invoke string exists as a command and false if command parameter is null.</returns>
2020-02-17 02:44:21 +00:00
public bool ContainsCommand(string command)
{
if (command == null) return false;
2020-02-17 02:44:21 +00:00
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;
}
}
}