using System; using System.Collections.Generic; using System.Linq; namespace UnityConsole { /// /// Use RegisterCommand() to register your own commands. Registered commands persist between scenes but don't persist between multiple application executions. /// public static class ConsoleCommandsDatabase { private static Dictionary database = new Dictionary(StringComparer.OrdinalIgnoreCase); /// /// Return all the commands in alphabetical order. /// public static IEnumerable commands { get { return database.OrderBy(kv => kv.Key).Select(kv => kv.Value); } } public static void RegisterCommand(string command, string description, string usage, ConsoleCommandCallback callback) { database[command] = new ConsoleCommand(command, description, usage, callback); } public static void RegisterCommand(string command, ConsoleCommandCallback callback) { database[command] = new ConsoleCommand(command, command, command, callback); } public static void RegisterSimpleCommand( string command, string description, SimpleConsoleCommandCallback callback) { RegisterCommand(command, description, "", args => { callback(); return ConsoleCommandResult.Succeeded(); }); } public static void RegisterAlias( string command, string description, string actualCommand, params string[] actualCommandArgs) { RegisterCommand(command, description, "", args => ExecuteCommand(actualCommand, actualCommandArgs)); } public static void UnRegisterCommand(string command) { database.Remove(command); } public static ConsoleCommandResult ExecuteCommand(string command, params string[] args) { try { if (TryGetCommand(command, out var cmd)) { return cmd.callback(args); } return new ConsoleCommandResult() { succeeded = true, Output = "Command " + command + " not found." }; } catch (Exception ex) { return new ConsoleCommandResult() { succeeded = false, Output = ex.ToString() }; } } public static bool TryGetCommand(string command, out ConsoleCommand result) { try { result = GetCommand(command); return true; } catch (NoSuchCommandException) { result = default; return false; } } public static ConsoleCommand GetCommand(string command) { if (HasCommand(command)) { return database[command]; } else { command = command.ToUpper(); throw new NoSuchCommandException("Command " + command + " not found.", command); } } public static bool HasCommand(string command) { return database.ContainsKey(command); } public static List CommandsContainingStrings(string[] strings, bool ignorePrefix = false) { List stringList = new List(); int index = ignorePrefix ? 1 : 0; foreach (var key in database.Keys) { bool flag = true; foreach (var str in strings) { if (key.IndexOf(str, StringComparison.OrdinalIgnoreCase) < index) { flag = false; break; } } if (flag) { stringList.Add(key); } } return stringList; } public static List CommandsMatchingPrefix(string prefix) { List commands = new List(); foreach (var key in database.Keys) { if (key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) { commands.Add(key); } } return commands; } } }