using System; using System.Text; using RecrownedGTK.Tools.CommandProcessor; namespace RecrownedGTK.Tools.CommandProcessor { public abstract class EngineCommand { bool caseSensitiveName = false; /// /// the words a user can type that will invoke this command. /// private string[] invokeStrings; public string[] InvokeStrings { get { return invokeStrings; } } /// /// Arguments that this command should accept and take into account. /// protected EngineCommandArgument[] commandArguments; protected string help; public EngineCommand(string help, params string[] invokeStrings) { this.help = help ?? throw new ArgumentNullException(); this.invokeStrings = invokeStrings ?? throw new ArgumentNullException(); for (int i = 0; i < invokeStrings.Length; i++) { if (invokeStrings[i].Contains(" ")) { throw new ArgumentException("Can't have spaces in command invocation strings."); } } } /// /// Whether or not this command should be invoked given the string. /// /// The string that acts as the command. /// whether or not this command is invoked. 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; } /// /// Runs the command. /// /// arguments to be used. Each string should be the argument input. May be null. public abstract void Run(IUserInput userInput = null, IUserOutput userOutput = null, params string[] arguments); /// /// Check if given argument is viable for command. /// /// Argument to check. /// True if valid. public bool Validate(string argument) { return EngineCommandArgumentOf(argument) == null ? false : true; } /// /// Returns the of the given argument or null if the string is an invalid argument. /// /// The argument string. /// null if not viable or the if viable. public EngineCommandArgument EngineCommandArgumentOf(string argument) { for (int i = 0; i < commandArguments.Length; i++) { if (commandArguments[i].invokeString == argument) { return commandArguments[i]; } } return null; } /// /// Finds the index of the argument given in an array of arguments for the command. /// /// The argument to find the index of. /// The array containing all arguments. /// The index or throws argument exception if it doesn't exist. public static int IndexOfArgument(string argument, string[] arguments) { if (argument == null) throw new ArgumentNullException("Parameter \"argument\" is null."); if (arguments == null) throw new ArgumentNullException("Parameter \"arguments\" is null."); for (int i = 0; i < arguments.Length; i++) { if (arguments[i] == argument) { return i; } } throw new ArgumentException(String.Format("The argument \"{0}\" doesn't exist in the given array of arguments.", argument)); } public static bool HasArgument(string argument, string[] arguments) { if (argument == null) throw new ArgumentNullException("Parameter \"argument\" is null."); if (arguments == null) throw new ArgumentNullException("Parameter \"arguments\" is null."); try { IndexOfArgument(argument, arguments); } catch (ArgumentException) { return false; } return true; } public static bool HasArgument(EngineCommandArgument argument, string[] arguments) { return HasArgument(argument.invokeString, arguments); } /// /// Finds the index of the argument given in an array of arguments for the command. /// /// The argument to find the index of. /// The array containing all arguments. /// The index or throws argument exception if it doesn't exist. public static int IndexOfArgument(EngineCommandArgument argument, string[] arguments) { return IndexOfArgument(argument.invokeString, arguments); } /// /// Called when help "command trigger" [argument] is invoked. Argument is optional and therefore, may be null. If the valid arguments are properly registered, this command will take care of it. If no arguments are provided, it will return the general help defined for this command. /// /// Potential argument to request help for. /// The string for the help. public string Help(string argument = null) { if (argument != null && commandArguments != null) { if (Validate(argument)) { EngineCommandArgument eca = EngineCommandArgumentOf(argument); string helpString = eca.help; if (eca.required) helpString = helpString + " required."; return EngineCommandArgumentOf(argument).help; } else { return "The argument " + argument + " does not exist. Type \"help " + invokeStrings[0] + "\" (or any of its aliases) for a list of arguments."; } } else { StringBuilder helpBuilder = new StringBuilder(); helpBuilder.Append(help); if (commandArguments != null) { helpBuilder.AppendLine(); helpBuilder.Append("Possible arguments: "); for (int i = 0; i < commandArguments.Length; i++) { helpBuilder.Append(commandArguments[i].invokeString); if (commandArguments[i].required) helpBuilder.Append('*'); if (i == commandArguments.Length - 2) { helpBuilder.Append(", and "); } else if (i < commandArguments.Length - 2) { helpBuilder.Append(", "); } } helpBuilder.Append('.'); helpBuilder.AppendLine(" (* are required arguments.)"); } return helpBuilder.ToString(); } } } }