public bool Load() { Assembly asm; Type loader; Type[] types; try { asm = Assembly.LoadFile(_path); } catch (FileLoadException) { LastError = "An error occurred when loading the file."; return(false); } catch (BadImageFormatException) { LastError = "The assembly specified is invalid."; return(false); } try { types = asm.GetTypes(); } catch (ReflectionTypeLoadException e) { types = e.Types.Where(t => t != null).ToArray(); } if (types.Length == 0) { LastError = "Unable to retrieve any types in the assembly."; return(false); } if ((loader = types.FirstOrDefault(t => t.IsDefined(typeof(GrimoirePluginEntry), true))) == null) { LastError = "Could not find a class marked with the GrimoirePluginEntry attribute."; return(false); } try { _plugin = (IGrimoirePlugin)Activator.CreateInstance(loader); _plugin.Load(); } catch (Exception exc) { LastError = $"Unknown error: {exc.Message}"; return(false); } return(true); }
public bool Load() { try { if (!File.Exists(_pluginPath)) { LastError = $"Could not find file: {_pluginPath}"; return(false); } Assembly asm = Assembly.LoadFile(_pluginPath); Type loader; Type[] types; if ((types = TryGetTypes(asm)) == null) { LastError = "Unable to retrieve any types in the assembly."; return(false); } if ((loader = types.FirstOrDefault(t => t.IsDefined(typeof(GrimoirePluginEntry), true))) == null) { LastError = "Could not find a class marked with the GrimoirePluginEntry attribute."; return(false); } _plugin = (IGrimoirePlugin)Activator.CreateInstance(loader); _plugin.Load(); LoadedPlugins.Add(this); return(true); } catch (Exception ex) { LastError = "Failure! This is either not a Grimoire plugin, or its code does not conform to the Grimoire standard.\n" + ex.Message + "\n" + ex.StackTrace; return(false); } }