Esempio n. 1
0
        /// <summary>
        /// Loads the manifest and returns true if it permits the loading of the plugin. If it prohibits
        /// the load then <see cref="IgnoredPlugins"/> is updated.
        /// </summary>
        /// <param name="manifestStorage"></param>
        /// <param name="applicationVersion"></param>
        /// <param name="dllFileName"></param>
        /// <returns></returns>
        private bool ManifestAllowsLoad(IPluginManifestStorage manifestStorage, Version applicationVersion, string dllFileName)
        {
            PluginManifest manifest = null;

            try {
                manifest = manifestStorage.LoadForPlugin(dllFileName);
                if (manifest == null)
                {
                    IgnoredPlugins.Add(dllFileName, Strings.CouldNotFindManifest);
                }
            } catch (Exception ex) {
                IgnoredPlugins.Add(dllFileName, String.Format(Strings.CouldNotParseManifest, ex.Message));
            }

            bool result = manifest != null;

            if (result && !String.IsNullOrEmpty(manifest.MinimumVersion))
            {
                result = CompareManifestVersions(manifest.MinimumVersion, applicationVersion, dllFileName, true);
            }
            if (result && !String.IsNullOrEmpty(manifest.MaximumVersion))
            {
                result = CompareManifestVersions(manifest.MaximumVersion, applicationVersion, dllFileName, false);
            }

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// See interface docs.
        /// </summary>
        public void LoadPlugins()
        {
            var log                = Factory.Singleton.Resolve <ILog>().Singleton;
            var manifestStorage    = Factory.Singleton.Resolve <IPluginManifestStorage>();
            var applicationVersion = Factory.Singleton.Resolve <IApplicationInformation>().Version;

            var rootFolder = Path.Combine(Provider.ApplicationStartupPath, "Plugins");

            if (Provider.DirectoryExists(rootFolder))
            {
                foreach (var subFolder in Provider.DirectoryGetDirectories(rootFolder))
                {
                    foreach (var dllFileName in Provider.DirectoryGetFiles(subFolder, "VirtualRadar.Plugin.*.dll"))
                    {
                        if (ManifestAllowsLoad(manifestStorage, applicationVersion, dllFileName))
                        {
                            try {
                                var pluginTypes = Provider.LoadTypes(dllFileName).Where(t => t.IsClass && typeof(IPlugin).IsAssignableFrom(t)).ToList();
                                if (pluginTypes.Count != 1)
                                {
                                    IgnoredPlugins.Add(dllFileName, Strings.PluginDoesNotHaveJustOneIPlugin);
                                    continue;
                                }

                                var pluginType = pluginTypes[0];
                                var plugin     = (IPlugin)Activator.CreateInstance(pluginType);
                                plugin.PluginFolder = subFolder;

                                var snapshot = Provider.ClassFactoryTakeSnapshot();
                                try {
                                    plugin.RegisterImplementations(Factory.Singleton);
                                    LoadedPlugins.Add(plugin);
                                } catch {
                                    Provider.ClassFactoryRestoreSnapshot(snapshot);
                                    throw;
                                }
                            } catch (Exception ex) {
                                var exceptionMessage = FormatException(ex);
                                Debug.WriteLine(String.Format("PluginManager.LoadPlugins caught exception: {0}", exceptionMessage));
                                log.WriteLine("Caught exception loading plugin {0}: {1}", dllFileName, exceptionMessage);

                                IgnoredPlugins.Add(dllFileName, String.Format(Strings.PluginCannotBeLoaded, ex.Message));
                            }
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        private bool CompareManifestVersions(string manifestVersion, Version applicationVersion, string dllFileName, bool isMinimum)
        {
            bool result = false;

            try {
                int comparison = VersionComparer.Compare(manifestVersion, applicationVersion);
                result = isMinimum ? comparison <= 0 : comparison >= 0;
                if (!result)
                {
                    IgnoredPlugins.Add(dllFileName, isMinimum ? String.Format(Strings.PluginMinimumVersionNotMet, manifestVersion) : String.Format(Strings.PluginMaximumVersionNotMet, manifestVersion));
                }
            } catch {
                IgnoredPlugins.Add(dllFileName, isMinimum ? Strings.PluginMinumumVersionUnparseable : Strings.PluginMaximumVersionUnparseable);
            }

            return(result);
        }