Merge pull request #266 from sirhamsteralot/master

Add extra functionality to the command system to make dynamic commands remotely plausible
This commit is contained in:
John Gross
2019-01-01 22:43:18 -08:00
committed by GitHub
2 changed files with 72 additions and 17 deletions

View File

@@ -15,10 +15,13 @@ namespace Torch.Commands
{ {
public class Command public class Command
{ {
public delegate void CommandAction(CommandContext context, object[] arguments);
public MyPromoteLevel MinimumPromoteLevel { get; } public MyPromoteLevel MinimumPromoteLevel { get; }
public string Name { get; } public string Name { get; }
public string Description { get; } public string Description { get; }
public string HelpText { get; } public string HelpText { get; }
public CommandAction Action { get; }
public Type Module { get; } public Type Module { get; }
public List<string> Path { get; } = new List<string>(); public List<string> Path { get; } = new List<string>();
public ITorchPlugin Plugin { get; } public ITorchPlugin Plugin { get; }
@@ -29,6 +32,28 @@ namespace Torch.Commands
private int? _requiredParamCount; private int? _requiredParamCount;
private static readonly Logger Log = LogManager.GetCurrentClassLogger(); 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)
{ {
Plugin = plugin; Plugin = plugin;
@@ -88,11 +113,16 @@ namespace Torch.Commands
{ {
try try
{ {
var parameters = new object[_parameters.Length]; var invokeByAction = Action != null;
object[] parameters;
if (context.Args.Count < _requiredParamCount) if (context.Args.Count < _requiredParamCount)
return false; return false;
if (!invokeByAction)
{
parameters = new object[_parameters.Length];
//Convert args from string //Convert args from string
for (var i = 0; i < _parameters.Length && i < context.Args.Count; i++) for (var i = 0; i < _parameters.Length && i < context.Args.Count; i++)
{ {
@@ -113,6 +143,18 @@ namespace Torch.Commands
moduleInstance.Context = context; moduleInstance.Context = context;
_method.Invoke(moduleInstance, parameters); _method.Invoke(moduleInstance, parameters);
return true; return true;
} else
{
parameters = new object[context.Args.Count];
for (var i = 0; i < parameters.Length && i < context.Args.Count; i++)
{
parameters[i] = context.Args[i];
}
}
Action.Invoke(context, parameters);
return true;
} }
catch (Exception e) catch (Exception e)
{ {

View File

@@ -141,6 +141,19 @@ namespace Torch.Commands
} }
} }
public bool DeleteNode(CommandNode node)
{
if (node.Parent != null)
{
node.Parent.RemoveNode(node);
return true;
}
if (node.Command?.Path != null)
return _root.Remove(node.Command.Path.First());
return false;
}
private void DebugNode(CommandNode commandNode, StringBuilder sb, ref int indent) private void DebugNode(CommandNode commandNode, StringBuilder sb, ref int indent)
{ {
sb.AppendLine(new string(' ', indent) + commandNode.Name); sb.AppendLine(new string(' ', indent) + commandNode.Name);