2018-12-05 21:44:26 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2018-12-07 08:22:15 +00:00
|
|
|
|
namespace RecrownedAthenaeum.Tools.CommandProcessor
|
2018-12-05 21:44:26 +00:00
|
|
|
|
{
|
|
|
|
|
internal class CommandEngine
|
|
|
|
|
{
|
2018-12-09 06:51:33 +00:00
|
|
|
|
public bool running = true;
|
|
|
|
|
public readonly List<EngineCommand> commands;
|
2018-12-08 22:26:40 +00:00
|
|
|
|
|
|
|
|
|
public CommandEngine()
|
|
|
|
|
{
|
2018-12-09 06:51:33 +00:00
|
|
|
|
commands = new List<EngineCommand>();
|
2018-12-08 22:26:40 +00:00
|
|
|
|
}
|
2018-12-05 21:44:26 +00:00
|
|
|
|
|
|
|
|
|
internal void Run()
|
|
|
|
|
{
|
|
|
|
|
while (running)
|
|
|
|
|
{
|
2018-12-09 06:51:33 +00:00
|
|
|
|
ConsoleUtilities.WriteWrappedLine("\nAwaiting command.");
|
2018-12-05 21:44:26 +00:00
|
|
|
|
string command = Console.ReadLine();
|
2018-12-08 22:26:40 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Process(command);
|
|
|
|
|
|
|
|
|
|
}
|
2018-12-08 22:32:01 +00:00
|
|
|
|
catch (ArgumentException ae)
|
2018-12-08 22:26:40 +00:00
|
|
|
|
{
|
2018-12-09 06:51:33 +00:00
|
|
|
|
ConsoleUtilities.WriteWrappedLine("Error: " + ae.Message);
|
2018-12-08 22:26:40 +00:00
|
|
|
|
}
|
2018-12-05 21:44:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-08 22:26:40 +00:00
|
|
|
|
|
|
|
|
|
public void Process(string commandAndArguments)
|
2018-12-07 08:27:34 +00:00
|
|
|
|
{
|
2018-12-08 22:26:40 +00:00
|
|
|
|
string command = commandAndArguments;
|
|
|
|
|
string[] arguments = null;
|
|
|
|
|
|
|
|
|
|
if (commandAndArguments.Contains(' '))
|
|
|
|
|
{
|
|
|
|
|
command = command.Remove(command.IndexOf(' '));
|
2018-12-09 06:51:33 +00:00
|
|
|
|
if (!ContainsCommand(command)) throw new ArgumentException("Command not found.");
|
|
|
|
|
string[] argumentsSplit = commandAndArguments.Substring(command.Length + 1).Split(' ');
|
2018-12-08 22:26:40 +00:00
|
|
|
|
List<string> argumentsList = new List<string>();
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2018-12-08 22:32:01 +00:00
|
|
|
|
throw new ArgumentException("Argument is missing terminating \'\"\'");
|
2018-12-08 22:26:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} while (!argumentsSplit[i].Contains('"'));
|
|
|
|
|
quoteBuilder.Append(' ');
|
|
|
|
|
quoteBuilder.Append(argumentsSplit[i]);
|
|
|
|
|
argumentsList.Add(quoteBuilder.ToString().Trim());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
argumentsList.Add(argumentsSplit[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
arguments = argumentsList.ToArray();
|
|
|
|
|
}
|
2018-12-09 06:51:33 +00:00
|
|
|
|
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;
|
2018-12-07 08:27:34 +00:00
|
|
|
|
}
|
2018-12-05 21:44:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|