From a0735826fbe1e57f63fd0dec193d57d152dffe33 Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Tue, 21 Jan 2020 11:02:36 -0800 Subject: [PATCH] Add VisParamDescr default value type check The VisParamDescrs specify a type and a default value. If the value has the wrong type, things would blow up in the editor. We now check the type at plugin load time, and refuse to load the plugin at all if an entry has a bad type. --- PluginCommon/Interfaces.cs | 4 ++++ PluginCommon/PluginManager.cs | 18 ++++++++++++++++-- SourceGen/Sandbox/ScriptManager.cs | 6 +++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/PluginCommon/Interfaces.cs b/PluginCommon/Interfaces.cs index f4cc058..bbc89c0 100644 --- a/PluginCommon/Interfaces.cs +++ b/PluginCommon/Interfaces.cs @@ -259,6 +259,10 @@ namespace PluginCommon { /// public VisParamDescr(string uiLabel, string name, Type csType, object min, object max, SpecialMode special, object defVal) { + if (defVal.GetType() != csType) { + throw new ArgumentException("Mismatch between type and default value in \"" + + name + "\""); + } UiLabel = uiLabel; Name = name; CsType = csType; diff --git a/PluginCommon/PluginManager.cs b/PluginCommon/PluginManager.cs index 754e83e..0112c1b 100644 --- a/PluginCommon/PluginManager.cs +++ b/PluginCommon/PluginManager.cs @@ -86,9 +86,10 @@ namespace PluginCommon { /// Full path to compiled assembly. /// Identifier to use in e.g. GetPlugin(). /// Reference to plugin instance. - public IPlugin LoadPlugin(string dllPath, string scriptIdent) { + public IPlugin LoadPlugin(string dllPath, string scriptIdent, out string failMsg) { if (mActivePlugins.TryGetValue(dllPath, out IPlugin ip)) { Debug.WriteLine("PM: returning cached plugin for " + dllPath); + failMsg = string.Empty; return ip; } @@ -100,9 +101,22 @@ namespace PluginCommon { type.GetInterfaces().Contains(typeof(IPlugin))) { ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes); - IPlugin iplugin = (IPlugin)ctor.Invoke(null); + IPlugin iplugin; + try { + iplugin = (IPlugin)ctor.Invoke(null); + } catch (Exception ex) { + if (ex.InnerException != null) { + failMsg = ex.InnerException.Message; + } else { + failMsg = ex.Message; + } + Debug.WriteLine("LoadPlugin: failed to load '" + scriptIdent + "': " + + failMsg); + return null; + } Debug.WriteLine("PM created instance: " + iplugin); mActivePlugins.Add(scriptIdent, iplugin); + failMsg = string.Empty; return iplugin; } } diff --git a/SourceGen/Sandbox/ScriptManager.cs b/SourceGen/Sandbox/ScriptManager.cs index 1bb53ec..dc3b1be 100644 --- a/SourceGen/Sandbox/ScriptManager.cs +++ b/SourceGen/Sandbox/ScriptManager.cs @@ -118,7 +118,11 @@ namespace SourceGen.Sandbox { report = new FileLoadReport(dllPath); // empty report return true; } else { - IPlugin plugin = DomainMgr.PluginMgr.LoadPlugin(dllPath, scriptIdent); + IPlugin plugin = DomainMgr.PluginMgr.LoadPlugin(dllPath, scriptIdent, + out string failMsg); + if (plugin == null) { + report.Add(FileLoadItem.Type.Error, "Failed loading plugin: " + failMsg); + } return plugin != null; } }