Ejemplo n.º 1
0
        public void LoadAll()
        {
            BeforeLoad();

            // Plugins are stored in format {PluginDir}/{PluginName}/AthamePlugin.*.dll
            var subDirs = Directory.GetDirectories(PluginDirectory);

            isLoading = true;
            foreach (var dir in subDirs)
            {
                var name = Path.GetFileName(dir);
                try
                {
                    // Attempt to load a .pdb if one exists
                    var basePath    = Path.Combine(dir, PluginDllPrefix + name);
                    var dllFilename = basePath + ".dll";
                    var pdbFilename = basePath + ".pdb";

                    var theAssembly = File.Exists(pdbFilename)
                        ? Assembly.Load(File.ReadAllBytes(dllFilename), File.ReadAllBytes(pdbFilename))
                        : Assembly.Load(File.ReadAllBytes(dllFilename));

                    // Set basic information about the assembly
                    var plugin = new PluginInstance
                    {
                        Assembly          = theAssembly,
                        AssemblyDirectory = dir,
                        Name = name
                    };
                    Plugins.Add(plugin);

                    Activate(plugin);
                    AreAnyLoaded = true;
                }
                catch (Exception ex)
                {
                    Log.WriteException(Level.Error, Tag, ex, $"While loading plugin {name}");
                    var eventArgs = new PluginLoadExceptionEventArgs {
                        PluginName = name, Exception = ex, Continue = true
                    };
                    LoadException?.Invoke(this, eventArgs);
                    if (!eventArgs.Continue)
                    {
                        return;
                    }
                }
            }
            isLoading = false;
        }
Ejemplo n.º 2
0
        public void LoadAll()
        {
            if (Plugins != null)
            {
                throw new InvalidOperationException("Plugins can only be loaded once");
            }
            Plugins = new List <PluginInstance>();
            // Cache current AppDomain loaded assemblies
            loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();

            // Plugins are stored in format {PluginDir}/{PluginName}/AthamePlugin.*.dll
            var subDirs    = Directory.GetDirectories(PluginDirectory);
            var pluginDlls = new List <string>();

            foreach (var subDir in subDirs)
            {
                pluginDlls.AddRange(Directory.GetFiles(subDir, $"{PluginDllPrefix}*.dll"));
            }
            isLoading = true;
            // Load and activate all plugins
            var assemblies = from dllPath in pluginDlls select Assembly.LoadFile(dllPath);

            foreach (var assembly in assemblies)
            {
                try
                {
                    Load(assembly);
                }
                catch (Exception ex)
                {
                    Log.WriteException(Level.Error, Tag, ex, $"While loading assembly {assembly}");
#if DEBUG
                    Debugger.Break();
#endif
                    var eventArgs = new PluginLoadExceptionEventArgs {
                        Exception = ex, Continue = true
                    };
                    LoadException?.Invoke(this, eventArgs);
                    if (!eventArgs.Continue)
                    {
                        return;
                    }
                }
            }
            isLoading = false;
        }