diff --git a/src/SlatedGameToolkit.Tools/Program.cs b/src/SlatedGameToolkit.Tools/Program.cs index d2a5a57..01a8ab6 100644 --- a/src/SlatedGameToolkit.Tools/Program.cs +++ b/src/SlatedGameToolkit.Tools/Program.cs @@ -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!"); } } } diff --git a/src/SlatedGameToolkit.Tools/System/CommandMap.cs b/src/SlatedGameToolkit.Tools/System/CommandMap.cs new file mode 100644 index 0000000..30837c9 --- /dev/null +++ b/src/SlatedGameToolkit.Tools/System/CommandMap.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using SlatedGameToolkit.Tools.System.Interaction; + +namespace SlatedGameToolkit.Tools.System +{ + public class CommandMap { + Dictionary 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(); + 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); + } + } + } + } + } +} \ No newline at end of file diff --git a/src/SlatedGameToolkit.Tools/System/CommandProcessor.cs b/src/SlatedGameToolkit.Tools/System/CommandProcessor.cs new file mode 100644 index 0000000..cd25d59 --- /dev/null +++ b/src/SlatedGameToolkit.Tools/System/CommandProcessor.cs @@ -0,0 +1,37 @@ +using System; +using SlatedGameToolkit.Tools.System.Interaction; + +namespace SlatedGameToolkit.Tools.System +{ + public class CommandProcessor + { + private string generalHelpMessage; + CommandMap commandMap; + + /// + /// 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. + /// + /// + /// + 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); + } + } +} \ No newline at end of file diff --git a/src/SlatedGameToolkit.Tools/System/IInvocable.cs b/src/SlatedGameToolkit.Tools/System/IInvocable.cs new file mode 100644 index 0000000..815d06e --- /dev/null +++ b/src/SlatedGameToolkit.Tools/System/IInvocable.cs @@ -0,0 +1,27 @@ +namespace SlatedGameToolkit.Tools.System +{ + public interface IInvocable + { + /// + /// Invokers are the strings that should invoke this command. + /// This should not conflict with any other commands invokers. + /// + /// A list of the strings that will invoke this invokable. + string[] GetInvokers(); + + /// + /// 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. + /// + /// An argument this invokable should describe. May be null, to which should return a string that describes this invokable in general. + /// + string getHelp(string arg); + + /// + /// Executes this invokable. + /// + /// The arguments that followed the invokable. + /// True if the invokable was successful, false otherwise to display a generic help message. + bool Execute(string[] args); + } +} \ No newline at end of file diff --git a/src/SlatedGameToolkit.Tools/System/Interaction/ConsoleInteracter.cs b/src/SlatedGameToolkit.Tools/System/Interaction/ConsoleInteracter.cs new file mode 100644 index 0000000..b64063e --- /dev/null +++ b/src/SlatedGameToolkit.Tools/System/Interaction/ConsoleInteracter.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/src/SlatedGameToolkit.Tools/System/Interaction/IInteractable.cs b/src/SlatedGameToolkit.Tools/System/Interaction/IInteractable.cs new file mode 100644 index 0000000..84df2d7 --- /dev/null +++ b/src/SlatedGameToolkit.Tools/System/Interaction/IInteractable.cs @@ -0,0 +1,10 @@ +namespace SlatedGameToolkit.Tools.System.Interaction +{ + public interface IInteractable + { + void Tell(string message); + void LineBreak(); + + string Listen(); + } +} \ No newline at end of file