Began work on the tools command processing.

This commit is contained in:
Harrison Deng 2020-05-26 14:47:20 -05:00
parent 065e6d878f
commit d149e12433
6 changed files with 148 additions and 1 deletions

View File

@ -1,4 +1,7 @@
using System;
using System.Reflection;
using SlatedGameToolkit.Tools.System;
using SlatedGameToolkit.Tools.System.Interaction;
namespace SlatedGameToolkit.Tools
{
@ -6,7 +9,17 @@ namespace SlatedGameToolkit.Tools
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
string input = null;
CommandMap commands = new CommandMap();
CommandProcessor processor = new CommandProcessor("The command \"{input}\" was not understood. Please type \"help\" for more information.", commands);
AssemblyName name = Assembly.GetExecutingAssembly().GetName();
ConsoleInteracter consoleInteracter = new ConsoleInteracter("Tools");
consoleInteracter.Tell(String.Format("{0} Version: {1}", name.Name, name.Version));
consoleInteracter.Tell("Welcome to SlatedGameToolkit.Tools! These tools are meant for the developers using the SlatedGameToolkit. Type \"help\" for a list of things this tool can currently do!");
while (!(input = consoleInteracter.Listen()).ToLower().Equals("exit")) {
}
consoleInteracter.Tell("Goodbye!");
}
}
}

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using SlatedGameToolkit.Tools.System.Interaction;
namespace SlatedGameToolkit.Tools.System
{
public class CommandMap {
Dictionary<string, IInvocable> invocations;
public IInvocable this[string invocation] {
get {
IInvocable result = null;
invocations.TryGetValue(invocation, out result);
return result;
}
}
public CommandMap(params IInvocable[] invokables) {
invocations = new Dictionary<string, IInvocable>();
foreach (IInvocable item in invokables)
{
string[] invokers = item.GetInvokers();
foreach (string invoker in invokers)
{
try {
invocations.Add(invoker, item);
} catch (ArgumentException e) {
throw new ArgumentException("A duplicate invocation string was found!", e);
}
}
}
}
}
}

View File

@ -0,0 +1,37 @@
using System;
using SlatedGameToolkit.Tools.System.Interaction;
namespace SlatedGameToolkit.Tools.System
{
public class CommandProcessor
{
private string generalHelpMessage;
CommandMap commandMap;
/// <summary>
/// The general help is the string that is printed when the input is not understood.
/// {input} in the string is replaced with the user input.
/// </summary>
/// <param name="generalHelp"></param>
/// <param name="commands"></param>
public CommandProcessor(string generalHelp, CommandMap commands) {
this.commandMap = commands;
this.generalHelpMessage = generalHelp;
}
public void Process(IInteractable interactable) {
string message = interactable.Listen();
string[] splitMessage = message.Split(' ');
string invocation = splitMessage[0];
IInvocable invocable = commandMap[invocation];
if (invocable != null) {
string[] args = new string[splitMessage.Length - 1];
Array.Copy(splitMessage, 1, args, 0, splitMessage.Length - 1);
if (invocable.Execute(args)) {
return;
}
}
interactable.Tell(generalHelpMessage);
}
}
}

View File

@ -0,0 +1,27 @@
namespace SlatedGameToolkit.Tools.System
{
public interface IInvocable
{
/// <summary>
/// Invokers are the strings that should invoke this command.
/// This should not conflict with any other commands invokers.
/// </summary>
/// <returns>A list of the strings that will invoke this invokable.</returns>
string[] GetInvokers();
/// <summary>
/// The help should be a description of how to properly use this invokable.
/// If an argument is specified, the help should cater to that specific argument.
/// </summary>
/// <param name="arg">An argument this invokable should describe. May be null, to which should return a string that describes this invokable in general.</param>
/// <returns></returns>
string getHelp(string arg);
/// <summary>
/// Executes this invokable.
/// </summary>
/// <param name="args">The arguments that followed the invokable.</param>
/// <returns>True if the invokable was successful, false otherwise to display a generic help message.</returns>
bool Execute(string[] args);
}
}

View File

@ -0,0 +1,28 @@
using System;
namespace SlatedGameToolkit.Tools.System.Interaction
{
public class ConsoleInteracter : IInteractable
{
string prefix;
public ConsoleInteracter(string prefix) {
this.prefix = prefix;
}
public void LineBreak()
{
Console.WriteLine();
}
public string Listen()
{
Console.Write(prefix + "> ");
return Console.ReadLine();
}
public void Tell(string message)
{
Console.WriteLine(message);
}
}
}

View File

@ -0,0 +1,10 @@
namespace SlatedGameToolkit.Tools.System.Interaction
{
public interface IInteractable
{
void Tell(string message);
void LineBreak();
string Listen();
}
}