From 87d9825c91b52acf7536d6dc4c6afae025a8c325 Mon Sep 17 00:00:00 2001 From: John Gross Date: Wed, 26 Jul 2017 00:15:31 -0700 Subject: [PATCH] Catch exceptions thrown by commands --- CHANGELOG.md | 9 +++++ Torch.Client/Properties/AssemblyInfo.cs | 4 +- Torch.Server/Properties/AssemblyInfo.cs | 4 +- Torch/Commands/Command.cs | 52 +++++++++++++++---------- 4 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..34ae355 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +# Torch 1.0.182.329 + * Improved logging, logs now to go the Logs folder and aren't deleted on start + * Fixed chat tab not enabling with -autostart + * Fixed player list + * Watchdog time-out is now configurable in TorchConfig.xml + * Fixed infinario log spam + * Fixed crash when sending empty message from chat tab + * Fixed permissions on Torch commands + * Changed plugin StoragePath to the current instance path (per-instance configs) \ No newline at end of file diff --git a/Torch.Client/Properties/AssemblyInfo.cs b/Torch.Client/Properties/AssemblyInfo.cs index cba9e1a..742eb73 100644 --- a/Torch.Client/Properties/AssemblyInfo.cs +++ b/Torch.Client/Properties/AssemblyInfo.cs @@ -1,4 +1,4 @@ using System.Reflection; -[assembly: AssemblyVersion("1.0.203.595")] -[assembly: AssemblyFileVersion("1.0.203.595")] \ No newline at end of file +[assembly: AssemblyVersion("1.0.207.7")] +[assembly: AssemblyFileVersion("1.0.207.7")] \ No newline at end of file diff --git a/Torch.Server/Properties/AssemblyInfo.cs b/Torch.Server/Properties/AssemblyInfo.cs index e61981a..90c0163 100644 --- a/Torch.Server/Properties/AssemblyInfo.cs +++ b/Torch.Server/Properties/AssemblyInfo.cs @@ -1,4 +1,4 @@ using System.Reflection; -[assembly: AssemblyVersion("1.1.203.596")] -[assembly: AssemblyFileVersion("1.1.203.596")] \ No newline at end of file +[assembly: AssemblyVersion("1.1.207.7")] +[assembly: AssemblyFileVersion("1.1.207.7")] \ No newline at end of file diff --git a/Torch/Commands/Command.cs b/Torch/Commands/Command.cs index d2c10ee..dc95c51 100644 --- a/Torch/Commands/Command.cs +++ b/Torch/Commands/Command.cs @@ -8,6 +8,7 @@ using NLog; using Torch.API; using Torch.API.Plugins; using Torch.Commands.Permissions; +using VRage.Game; using VRage.Game.ModAPI; namespace Torch.Commands @@ -26,6 +27,7 @@ namespace Torch.Commands private readonly MethodInfo _method; private ParameterInfo[] _parameters; private int? _requiredParamCount; + private static readonly Logger Log = LogManager.GetLogger(nameof(Command)); public Command(ITorchPlugin plugin, MethodInfo commandMethod) { @@ -84,31 +86,41 @@ namespace Torch.Commands public bool TryInvoke(CommandContext context) { - var parameters = new object[_parameters.Length]; - - if (context.Args.Count < _requiredParamCount) - return false; - - //Convert args from string - for (var i = 0; i < _parameters.Length && i < context.Args.Count; i++) + try { - if (context.Args[i].TryConvert(_parameters[i].ParameterType, out object obj)) - parameters[i] = obj; - else + var parameters = new object[_parameters.Length]; + + if (context.Args.Count < _requiredParamCount) return false; - } - //Fill remaining parameters with default values - for (var i = 0; i < parameters.Length; i++) + //Convert args from string + for (var i = 0; i < _parameters.Length && i < context.Args.Count; i++) + { + if (context.Args[i].TryConvert(_parameters[i].ParameterType, out object obj)) + parameters[i] = obj; + else + return false; + } + + //Fill remaining parameters with default values + for (var i = 0; i < parameters.Length; i++) + { + if (parameters[i] == null) + parameters[i] = _parameters[i].DefaultValue; + } + + var moduleInstance = (CommandModule)Activator.CreateInstance(Module); + moduleInstance.Context = context; + _method.Invoke(moduleInstance, parameters); + return true; + } + catch (Exception e) { - if (parameters[i] == null) - parameters[i] = _parameters[i].DefaultValue; + context.Respond(e.Message, "Error", MyFontEnum.Red); + Log.Error($"Command '{SyntaxHelp}' from '{Plugin.Name ?? "Torch"}' threw an exception. Args: {string.Join(", ", context.Args)}"); + Log.Error(e); + return true; } - - var moduleInstance = (CommandModule)Activator.CreateInstance(Module); - moduleInstance.Context = context; - _method.Invoke(moduleInstance, parameters); - return true; } }