Esempio n. 1
0
        public void LoadExtensions()
        {
            Plugins = new List <PluginInfo>();

            var signatureVerifier = new AssemblySignatureVerifier();

            var enabledPlugins = info.Settings.Get("extmgr.enabled", "").Split(';');
            var currentVersion = update.CurrentVersion;

            var           path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            DirectoryInfo dir  = new DirectoryInfo(path);

            foreach (var file in dir.GetFiles("*.dll"))
            {
                try
                {
                    var assembly = Assembly.LoadFrom(file.FullName);

                    foreach (var type in assembly.GetTypes())
                    {
                        if (!type.IsClass || !type.IsPublic || type.IsAbstract || type == typeof(IPlugin))
                        {
                            continue;
                        }

                        if (!typeof(IPlugin).IsAssignableFrom(type))
                        {
                            continue;
                        }

                        object[] attribs = type.GetCustomAttributes(typeof(PluginAttribute), false);
                        if (attribs.Length != 1)
                        {
                            continue;
                        }

                        var attr = (PluginAttribute)attribs[0];
                        if (Version.TryParse(attr.MinVer, out Version min) && VersionCompare(currentVersion, min) < 0)
                        {
                            continue; // Inkompatible Erweiterung (Programm zu alt)
                        }
                        if (Version.TryParse(attr.MaxVer, out Version max) && VersionCompare(currentVersion, max) > 0)
                        {
                            continue; // Inkompatible Erweiterung (Programm zu neu)
                        }
                        var securityContext = signatureVerifier.Validate(file.FullName);

                        if (enabledPlugins.Contains(type.FullName))
                        {
                            IPlugin plugin = (IPlugin)Activator.CreateInstance(type);

                            var info = new PluginInfo(plugin, securityContext);
                            Plugins.Add(info);
                        }
                        else
                        {
                            var info = new PluginInfo(type, securityContext);
                            Plugins.Add(info);
                        }
                    }
                }
                catch (FileLoadException)
                {
                    info.Logger.Warning("Erweiterung " + file.Name + " konnte nicht geladen werden, bitte überprüfen ob diese noch geblockt ist!");
                }
                catch
                {
                }
            }
        }
Esempio n. 2
0
        public IEnumerable <string> LoadExtensions()
        {
            if (pluginsInitialized)
            {
                throw new InvalidOperationException("Extensions have already been initialized!");
            }
            if (filesLoaded)
            {
                throw new InvalidOperationException("Extensions already loaded from filesystem!");
            }
            filesLoaded = true;

            var signatureVerifier = new AssemblySignatureVerifier();

            var enabledPlugins = pluginInterface.Settings.Get("extmgr.enabled", "").Split(';');
            var currentVersion = VersionInformation.Current.AppBaseVersion;

            DirectoryInfo dir = new DirectoryInfo(pluginInterface.ExecutableDir);

            // Filter some unwanted files, for performance reasons as we don't have to check them
            var files = dir.GetFiles("*.dll")
                        .Where(FilterFileNames)
                        .ToArray();

            var warnings = new List <string>();

            foreach (var file in files)
            {
                try
                {
                    var assembly = Assembly.LoadFrom(file.FullName);

                    foreach (var type in assembly.GetTypes())
                    {
                        if (!type.IsClass || !type.IsPublic || type.IsAbstract || type == typeof(IPlugin))
                        {
                            continue;
                        }

                        if (!typeof(IPlugin).IsAssignableFrom(type))
                        {
                            continue;
                        }

                        var attr = type.GetCustomAttribute <PluginAttribute>();
                        if (attr == null)
                        {
                            continue;
                        }

                        if (Version.TryParse(attr.MinVer, out Version min) && VersionCompare(currentVersion, min) < 0)
                        {
                            continue; // Inkompatible Erweiterung (Programm zu alt)
                        }
                        if (Version.TryParse(attr.MaxVer, out Version max) && VersionCompare(currentVersion, max) > 0)
                        {
                            continue; // Inkompatible Erweiterung (Programm zu neu)
                        }
                        var securityContext = signatureVerifier.Validate(file.FullName);

                        if (enabledPlugins.Contains(type.FullName))
                        {
                            IPlugin plugin = (IPlugin)Activator.CreateInstance(type);

                            var pluginInfo = new PluginInfo(plugin, securityContext);
                            plugins.Add(pluginInfo);
                            if (plugin is IDisposable d)
                            {
                                disposablePlugins.Add(d);
                            }
                        }
                        else
                        {
                            var pluginInfo = new PluginInfo(type, securityContext);
                            plugins.Add(pluginInfo);
                        }
                    }
                }
                catch (FileLoadException)
                {
                    warnings.Add(T._("Erweiterung {0} konnte nicht geladen werden, bitte überprüfen ob diese noch geblockt ist!", file.Name));
                }
                catch
                {
                }
            }

            return(warnings);
        }