1
0
mirror of https://github.com/fadden/6502bench.git synced 2024-05-31 22:41:37 +00:00

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.
This commit is contained in:
Andy McFadden 2020-01-21 11:02:36 -08:00
parent b5deea713f
commit a0735826fb
3 changed files with 25 additions and 3 deletions

View File

@ -259,6 +259,10 @@ namespace PluginCommon {
/// </summary>
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;

View File

@ -86,9 +86,10 @@ namespace PluginCommon {
/// <param name="dllPath">Full path to compiled assembly.</param>
/// <param name="scriptIdent">Identifier to use in e.g. GetPlugin().</param>
/// <returns>Reference to plugin instance.</returns>
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;
}
}

View File

@ -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;
}
}