您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
157 行
4.8 KiB
157 行
4.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace UnityConsole
|
|
{
|
|
/// <summary>
|
|
/// Use RegisterCommand() to register your own commands. Registered commands persist between scenes but don't persist between multiple application executions.
|
|
/// </summary>
|
|
public static class ConsoleCommandsDatabase
|
|
{
|
|
private static Dictionary<string, ConsoleCommand> database =
|
|
new Dictionary<string, ConsoleCommand>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
/// <summary>
|
|
/// Return all the commands in alphabetical order.
|
|
/// </summary>
|
|
public static IEnumerable<ConsoleCommand> 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<string> CommandsContainingStrings(string[] strings, bool ignorePrefix = false)
|
|
{
|
|
List<string> stringList = new List<string>();
|
|
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<string> CommandsMatchingPrefix(string prefix)
|
|
{
|
|
List<string> commands = new List<string>();
|
|
foreach (var key in database.Keys)
|
|
{
|
|
if (key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
commands.Add(key);
|
|
}
|
|
}
|
|
|
|
return commands;
|
|
}
|
|
}
|
|
}
|