diff --git a/Torch.Server/Commands/TestCommands.cs b/Torch.Server/Commands/TestCommands.cs new file mode 100644 index 00000000..35f6c1e3 --- /dev/null +++ b/Torch.Server/Commands/TestCommands.cs @@ -0,0 +1,19 @@ +using Torch.Commands; +using Torch.Commands.Permissions; +using VRage.Game.ModAPI; + +namespace Torch.Server.Commands +{ + public class TestCommands : CommandModule + { + private TorchConfig Config => (TorchConfig)Context.Torch.Config; + + [Command("test")] + [Alias("ThisISAnAlias")] + [Permission(MyPromoteLevel.None)] + public void Test() + { + Context.Respond("Hello World!"); + } + } +} \ No newline at end of file diff --git a/Torch.Server/Torch.Server.csproj b/Torch.Server/Torch.Server.csproj index 7bcdbdda..381e5b84 100644 --- a/Torch.Server/Torch.Server.csproj +++ b/Torch.Server/Torch.Server.csproj @@ -240,6 +240,7 @@ Properties\AssemblyVersion.cs + diff --git a/Torch.Server/TorchServer.cs b/Torch.Server/TorchServer.cs index 29acabe7..7a7c1781 100644 --- a/Torch.Server/TorchServer.cs +++ b/Torch.Server/TorchServer.cs @@ -234,6 +234,7 @@ private void OnSessionStateChanged(ITorchSession session, TorchSessionState newS { _multiplayerManagerDedicated = CurrentSession.Managers.GetManager(); CurrentSession.Managers.GetManager().RegisterCommandModule(typeof(WhitelistCommands)); + CurrentSession.Managers.GetManager().RegisterCommandModule(typeof(TestCommands)); ModCommunication.Register(); } } diff --git a/Torch/Commands/AliasAttribute.cs b/Torch/Commands/AliasAttribute.cs new file mode 100644 index 00000000..4a743fa3 --- /dev/null +++ b/Torch/Commands/AliasAttribute.cs @@ -0,0 +1,14 @@ +using System; + +namespace Torch.Commands.Permissions +{ + public class AliasAttribute : Attribute + { + public string Alias { get; } + + public AliasAttribute(string alias) + { + Alias = alias; + } + } +} \ No newline at end of file diff --git a/Torch/Commands/Command.cs b/Torch/Commands/Command.cs index ffc303c0..7c6c500f 100644 --- a/Torch/Commands/Command.cs +++ b/Torch/Commands/Command.cs @@ -21,6 +21,7 @@ public class Command public string Name { get; } public string Description { get; } public string HelpText { get; } + public string Alias { get; } public CommandAction Action { get; } public Type Module { get; } public List Path { get; } = new List(); @@ -31,44 +32,17 @@ public class Command private ParameterInfo[] _parameters; private int? _requiredParamCount; private static readonly Logger Log = LogManager.GetCurrentClassLogger(); - - public Command(string name, string description, CommandAction action, ITorchPlugin plugin, MyPromoteLevel? minimumPromoteLevel = null, string helpText = null, int requiredParamCount = 0) - { - HelpText = helpText; - Action = action; - Plugin = plugin; - MinimumPromoteLevel = minimumPromoteLevel ?? MyPromoteLevel.Admin; - - var split = name.Split(' '); - Name = split.Last(); - Description = description; - HelpText = helpText ?? description; - - Path.AddRange(split); - - var sb = new StringBuilder(); - sb.Append($"!{string.Join(" ", Path)} "); - - _requiredParamCount = requiredParamCount; - Log.Debug($"Params: ({_requiredParamCount} required)"); - SyntaxHelp = sb.ToString(); - } - - public Command(ITorchPlugin plugin, MethodInfo commandMethod) + + public Command(ITorchPlugin plugin, MethodInfo commandMethod, CommandAttribute attributeData) { Plugin = plugin; - - var commandAttribute = commandMethod.GetCustomAttribute(); - if (commandAttribute == null) - throw new TypeLoadException($"Method does not have a {nameof(CommandAttribute)}"); - + var permissionAttribute = commandMethod.GetCustomAttribute(); MinimumPromoteLevel = permissionAttribute?.PromoteLevel ?? MyPromoteLevel.Admin; - if (!commandMethod.DeclaringType.IsSubclassOf(typeof(CommandModule))) - throw new TypeLoadException($"Command {commandMethod.Name}'s declaring type {commandMethod.DeclaringType.FullName} is not a subclass of {nameof(CommandModule)}"); - var moduleAttribute = commandMethod.DeclaringType.GetCustomAttribute(); + + var aliasAttribute = commandMethod.GetCustomAttribute(); _method = commandMethod; Module = commandMethod.DeclaringType; @@ -77,12 +51,18 @@ public Command(ITorchPlugin plugin, MethodInfo commandMethod) { Path.AddRange(moduleAttribute.Path); } - Path.AddRange(commandAttribute.Path); - - Name = commandAttribute.Name; - Description = commandAttribute.Description; - HelpText = commandAttribute.HelpText; + + if(aliasAttribute != null) + { + Alias = aliasAttribute.Alias; + } + + Path.AddRange(attributeData.Path); + Name = attributeData.Name; + Description = attributeData.Description; + HelpText = attributeData.HelpText; + //parameters _parameters = commandMethod.GetParameters(); diff --git a/Torch/Commands/CommandManager.cs b/Torch/Commands/CommandManager.cs index 80fcdb46..ffdf68b9 100644 --- a/Torch/Commands/CommandManager.cs +++ b/Torch/Commands/CommandManager.cs @@ -61,9 +61,9 @@ public void RegisterCommandModule(Type moduleType, ITorchPlugin plugin = null) if (commandAttrib == null) continue; - var command = new Command(plugin, method); + var command = new Command(plugin, method, commandAttrib); var cmdPath = string.Join(".", command.Path); - _log.Info($"Registering command '{cmdPath}'"); + _log.Info($"Registering command '{cmdPath}'" + (!string.IsNullOrEmpty(command.Alias) ? $" (alias: {string.Join(", ", command.Alias)})" : "")); if (!Commands.AddCommand(command)) _log.Error($"Command path {cmdPath} is already registered."); diff --git a/Torch/Commands/CommandTree.cs b/Torch/Commands/CommandTree.cs index d67a1772..c07ad141 100644 --- a/Torch/Commands/CommandTree.cs +++ b/Torch/Commands/CommandTree.cs @@ -21,11 +21,17 @@ public CommandTree() public bool AddCommand(Command command) { + bool hasAlias = !string.IsNullOrEmpty(command.Alias); var root = command.Path.First(); if (!_root.ContainsKey(root)) { _root.Add(root, new CommandNode(root)); + + if (hasAlias) + { + _root.Add(command.Alias, _root[root]); + } } var node = _root[root]; @@ -65,7 +71,9 @@ public int GetNode(List path, out CommandNode commandNode) return -1; if (!_root.ContainsKey(root)) + { return -1; + } var node = _root[root]; var i = 1; @@ -85,15 +93,7 @@ public int GetNode(List path, out CommandNode commandNode) commandNode = node; return i; } - - public Command GetCommand(List path, out List args) - { - args = new List(); - var skip = GetNode(path, out CommandNode node); - args.AddRange(path.Skip(skip)); - return node.Command; - } - + public Command GetCommand(string commandText, out string argText) { var split = commandText.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries).ToList(); diff --git a/Torch/Torch.csproj b/Torch/Torch.csproj index 318535ab..c9a4f2de 100644 --- a/Torch/Torch.csproj +++ b/Torch/Torch.csproj @@ -208,6 +208,7 @@ +