Catch exceptions thrown by commands
This commit is contained in:
9
CHANGELOG.md
Normal file
9
CHANGELOG.md
Normal file
@@ -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)
|
@@ -1,4 +1,4 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
[assembly: AssemblyVersion("1.0.203.595")]
|
[assembly: AssemblyVersion("1.0.207.7")]
|
||||||
[assembly: AssemblyFileVersion("1.0.203.595")]
|
[assembly: AssemblyFileVersion("1.0.207.7")]
|
@@ -1,4 +1,4 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
[assembly: AssemblyVersion("1.1.203.596")]
|
[assembly: AssemblyVersion("1.1.207.7")]
|
||||||
[assembly: AssemblyFileVersion("1.1.203.596")]
|
[assembly: AssemblyFileVersion("1.1.207.7")]
|
@@ -8,6 +8,7 @@ using NLog;
|
|||||||
using Torch.API;
|
using Torch.API;
|
||||||
using Torch.API.Plugins;
|
using Torch.API.Plugins;
|
||||||
using Torch.Commands.Permissions;
|
using Torch.Commands.Permissions;
|
||||||
|
using VRage.Game;
|
||||||
using VRage.Game.ModAPI;
|
using VRage.Game.ModAPI;
|
||||||
|
|
||||||
namespace Torch.Commands
|
namespace Torch.Commands
|
||||||
@@ -26,6 +27,7 @@ namespace Torch.Commands
|
|||||||
private readonly MethodInfo _method;
|
private readonly MethodInfo _method;
|
||||||
private ParameterInfo[] _parameters;
|
private ParameterInfo[] _parameters;
|
||||||
private int? _requiredParamCount;
|
private int? _requiredParamCount;
|
||||||
|
private static readonly Logger Log = LogManager.GetLogger(nameof(Command));
|
||||||
|
|
||||||
public Command(ITorchPlugin plugin, MethodInfo commandMethod)
|
public Command(ITorchPlugin plugin, MethodInfo commandMethod)
|
||||||
{
|
{
|
||||||
@@ -84,31 +86,41 @@ namespace Torch.Commands
|
|||||||
|
|
||||||
public bool TryInvoke(CommandContext context)
|
public bool TryInvoke(CommandContext context)
|
||||||
{
|
{
|
||||||
var parameters = new object[_parameters.Length];
|
try
|
||||||
|
|
||||||
if (context.Args.Count < _requiredParamCount)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
//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))
|
var parameters = new object[_parameters.Length];
|
||||||
parameters[i] = obj;
|
|
||||||
else
|
if (context.Args.Count < _requiredParamCount)
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
//Fill remaining parameters with default values
|
//Convert args from string
|
||||||
for (var i = 0; i < parameters.Length; i++)
|
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)
|
context.Respond(e.Message, "Error", MyFontEnum.Red);
|
||||||
parameters[i] = _parameters[i].DefaultValue;
|
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user