From 9c06049628ba828a577b612bf46035235acc9855 Mon Sep 17 00:00:00 2001 From: sirhamsteralot Date: Thu, 27 Dec 2018 00:57:58 +0100 Subject: [PATCH] Add extra command constructor for easier use of dynamic methods. --- Torch/Commands/Command.cs | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Torch/Commands/Command.cs b/Torch/Commands/Command.cs index 2922f67..55d8c52 100644 --- a/Torch/Commands/Command.cs +++ b/Torch/Commands/Command.cs @@ -19,6 +19,7 @@ namespace Torch.Commands public string Name { get; } public string Description { get; } public string HelpText { get; } + public Delegate Action { get; } public Type Module { get; } public List Path { get; } = new List(); public ITorchPlugin Plugin { get; } @@ -29,6 +30,51 @@ namespace Torch.Commands private int? _requiredParamCount; private static readonly Logger Log = LogManager.GetCurrentClassLogger(); + public Command(string name, string description, Delegate action, ITorchPlugin plugin, MyPromoteLevel? minimumPromoteLevel = null, string helpText = null) + { + 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 commandMethod = action.Method; + + _method = commandMethod; + Module = commandMethod.DeclaringType; + + //parameters + _parameters = commandMethod.GetParameters(); + + var sb = new StringBuilder(); + sb.Append($"!{string.Join(" ", Path)} "); + for (var i = 0; i < _parameters.Length; i++) + { + var param = _parameters[i]; + + if (param.HasDefaultValue) + { + _requiredParamCount = _requiredParamCount ?? i; + + sb.Append($"[{param.ParameterType.Name} {param.Name}] "); + } + else + { + sb.Append($"<{param.ParameterType.Name} {param.Name}> "); + } + } + + _requiredParamCount = _requiredParamCount ?? _parameters.Length; + Log.Debug($"Params: {_parameters.Length} ({_requiredParamCount} required)"); + SyntaxHelp = sb.ToString(); + } + public Command(ITorchPlugin plugin, MethodInfo commandMethod) { Plugin = plugin;