/// <summary> /// Executes the command passed. /// </summary> public static void Execute(string command) { // Guard if (instance == null) { return; } // Get method and arguments // We want to ONLY split once! string[] methodNameAndArgs = command.Trim().TrimEnd(')').Split(new char[] { '(' }, 2); string method = command; string args = string.Empty; IEnumerable <CommandBase> commandsToCall; if (methodNameAndArgs.Length == 2) { // If both method and arguments exist split them method = methodNameAndArgs[0]; args = methodNameAndArgs[1]; } else if (method.Trim().EndsWith("?")) { // This is help string testString = method.ToLower().Trim().TrimEnd('?'); commandsToCall = instance.consoleCommands.Where(cB => cB.Title.ToLower() == testString); foreach (CommandBase commandToCall in commandsToCall) { ShowHelpMethod(commandToCall); } return; } // Execute command commandsToCall = instance.consoleCommands.Where(cB => cB.Title.ToLower() == method.ToLower().Trim()); foreach (CommandBase commandToCall in commandsToCall) { ICommandRunnable runnable = (ICommandRunnable)commandToCall; if (runnable != null) { runnable.ExecuteCommand(args); } } }
/// <summary> /// Executes the command passed. /// </summary> public static void Execute(string command) { // Guard if (instance == null || command.Trim() == string.Empty) { return; } // Get method and arguments // We want to ONLY split once! string[] methodNameAndArgs = command.Trim().TrimEnd(')').Split(new char[] { '(' }, 2); string method = command; string args = string.Empty; IEnumerable <CommandBase> commandsToCall; if (methodNameAndArgs.Length == 2) { // If both method and arguments exist split them method = methodNameAndArgs[0]; args = methodNameAndArgs[1]; } else if (method.Trim().EndsWith("?")) { // This is help string testString = method.ToLower().Trim().TrimEnd('?'); commandsToCall = instance.consoleCommands.Where(cB => cB.Title.ToLower() == testString); foreach (CommandBase commandToCall in commandsToCall) { ShowHelpMethod(commandToCall); } return; } else { // Do a default // We will do a null checker // If its null then set to default which is "" by default :P args = null; } // Execute command commandsToCall = instance.consoleCommands.Where(cB => cB.Title.ToLower() == method.ToLower().Trim()); if (commandsToCall.Count() > 0) { foreach (CommandBase commandToCall in commandsToCall) { ICommandRunnable runnable = (ICommandRunnable)commandToCall; if (runnable != null) { if (commandToCall.Parameters == null || commandToCall.Parameters == string.Empty) { args = string.Empty; } else { if (args == null && commandToCall.DefaultValue != null) { args = commandToCall.DefaultValue; } // They really need a better literal system... // This is the closet we can get basically if (args == string.Empty || args == '"'.ToString()) { args = @""""; } } runnable.ExecuteCommand(args); } } } else { // User entered a command that doesn't 'exist' LogWarning("Command doesn't exist? You entered: " + command); LogWarning("Did you mean?"); IEnumerable <CommandBase> commandsToShow = instance.consoleCommands.Where(cB => cB.Title.ToLower().Contains(method.Substring(0, (method.Length >= 3) ? (int)Mathf.Ceil(method.Length / 1.5f) : method.Length).ToLower())); if (commandsToShow.Count() == 0) { LogWarning("No close matches found so looking with less precision"); commandsToShow = instance.consoleCommands.Where(cB => cB.Title.ToLower().Contains(method.Substring(0, (int)Mathf.Ceil(method.Length / 3f)).ToLower())); } foreach (CommandBase commandToShow in commandsToShow) { // Yah its close enough either 2/3rds similar or 1/3rd if no matches found Log(commandToShow.Title, "green"); } } }