Exemple #1
0
        private static List <T> GetAllPluginInstances <T>() where T : class, IPluginBase
        {
            _logger.Trace("PluginsManager.GetAllPluginInstances()");

            List <T> list = new List <T>();

            // Get the global settings
            var settings = ProgramSettings.GetGlobalSettings();

            foreach (var pluginAssembly in settings.Assemblies.Where(p => p.IsValid))
            {
                foreach (var pluginType in pluginAssembly.Plugins.Where(t => t.Enabled))
                {
                    // If the type instance was already loaded, returns the instance
                    if (pluginType.IsLoaded)
                    {
                        T instance = pluginType.PluginInstance as T;
                        if (instance != null)
                        {
                            list.Add(instance);
                        }
                    }
                    else
                    {
                        if (!pluginAssembly.IsLoaded)
                        {
                            string pluginsDirectory = settings.DirectoriesSettings.PluginsDirectory;

                            string pluginLocation = Path.Combine(pluginsDirectory, pluginAssembly.File);

                            pluginAssembly.AssemblyInstance = Assembly.LoadFile(pluginLocation);
                            pluginAssembly.IsLoaded         = true;
                        }

                        Type type = pluginAssembly.AssemblyInstance.GetType(pluginType.Class);

                        var instance = Activator.CreateInstance(type) as T;
                        if (instance != null)
                        {
                            list.Add(instance);
                            pluginType.PluginInstance = instance;
                            pluginType.IsLoaded       = true;
                        }
                    }
                }
            }

            return(list);
        }
Exemple #2
0
        /// <summary>
        /// Gets the plugin instance from the global settins.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="assemblyGuid">The assembly file name.</param>
        /// <param name="pluginComponent">Type of the plugin.</param>
        /// <returns>Instance object of the plugin</returns>
        private static T GetPluginInstance <T>(string assemblyGuid, string pluginComponent) where T : class, IPluginBase
        {
            _logger.Trace("PluginsManager.GetPluginInstance()");

            // Get the global settings
            var settings = ProgramSettings.GetGlobalSettings();

            // Find the Assembly in the global settings
            var globalAssembly = settings.Assemblies.First(a => a.Guid.SequenceEqual(assemblyGuid));

            // Find the type in the assembly
            var globalAssemblyComponent = globalAssembly.Plugins.First(t => t.Class.Equals(pluginComponent, StringComparison.InvariantCultureIgnoreCase));

            // If the type instance was already loaded, returns the instance
            if (globalAssemblyComponent.IsLoaded)
            {
                return((T)globalAssemblyComponent.PluginInstance);
            }

            // If the assembly is not loaded we need to create an instance
            if (!globalAssembly.IsLoaded)
            {
                string pluginsDirectory = settings.DirectoriesSettings.PluginsDirectory;

                string pluginLocation = Path.Combine(pluginsDirectory, globalAssembly.File);

                if (File.Exists(pluginLocation))
                {
                    globalAssembly.AssemblyInstance = Assembly.LoadFile(pluginLocation);
                    globalAssembly.IsLoaded         = true;
                }
                else
                {
                    return(null);
                }
            }

            Type type = globalAssembly.AssemblyInstance.GetType(pluginComponent);

            var instance = Activator.CreateInstance(type);

            // Create the new instance of the plugin and save it in the global settings (for later use)
            globalAssemblyComponent.PluginInstance = (T)instance;
            globalAssemblyComponent.IsLoaded       = true;

            // returns the instance of the plugin
            return((T)globalAssemblyComponent.PluginInstance);
        }
Exemple #3
0
        /// <summary>
        /// Imports the plugin.
        /// </summary>
        /// <param name="pluginLocation">The plugin location.</param>
        /// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
        public static void ImportPlugin(string pluginLocation, bool overwrite = false)
        {
            _logger.Trace("PluginsManager.ImportPlugin('{0}')", pluginLocation);

            // Get current global settings
            var settings = ProgramSettings.GetGlobalSettings();

            string extension = Path.GetExtension(pluginLocation) ?? string.Empty;

            if (extension.EndsWith(".zip", StringComparison.InvariantCultureIgnoreCase))
            {
                string shortUniqueName = StringHelper.CreateUniqueName(Path.GetFileNameWithoutExtension(pluginLocation));

                string tempDirectory = settings.DirectoriesSettings.TempDirectory;

                string pluginTempDirectory = Path.Combine(tempDirectory, shortUniqueName);

                Uri pluginTempDirectoryUri = new Uri(tempDirectory, UriKind.Absolute);

                if (!Directory.Exists(pluginTempDirectory))
                {
                    Directory.CreateDirectory(pluginTempDirectory);
                }

                ZipFile.ExtractToDirectory(pluginLocation, pluginTempDirectory);

                foreach (string extractedPluginLocation in Directory.GetFiles(pluginTempDirectory, "*.dll", SearchOption.AllDirectories))
                {
                    Uri pluginLocationUri = new Uri(extractedPluginLocation, UriKind.Absolute);

                    byte[]   assemblyBytes = File.ReadAllBytes(extractedPluginLocation);
                    Assembly assembly      = Assembly.Load(assemblyBytes);

                    string relativePluginLocation = pluginTempDirectoryUri.MakeRelativeUri(pluginLocationUri).ToString();

                    // Check each assembly for plugins
                    if (CheckAssembly(relativePluginLocation, assembly, settings, false))
                    {
                        string pluginTargetDirectory = Path.Combine(settings.DirectoriesSettings.PluginsDirectory, shortUniqueName);

                        if (!Directory.Exists(pluginTargetDirectory))
                        {
                            Directory.Move(pluginTempDirectory, pluginTargetDirectory);
                        }
                    }
                }
            }
            else if (extension.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase))
            {
                byte[]   assemblyBytes = File.ReadAllBytes(pluginLocation);
                Assembly assembly      = Assembly.Load(assemblyBytes);

                // Check each assembly for plugins
                if (CheckAssembly(null, assembly, settings, false))
                {
                    string pluginTargetLocation = Path.Combine(settings.DirectoriesSettings.PluginsDirectory, Path.GetFileName(pluginLocation));

                    File.Copy(pluginLocation, pluginTargetLocation, overwrite);
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Checks the existing plugins in the global settings.
        /// </summary>
        public static void CheckExistingPlugins()
        {
            _logger.Trace("PluginsManager.CheckExistingPlugins()");

            // Get current global settings
            var settings = ProgramSettings.GetGlobalSettings();

            // Get DefaultPluginsLocation
            string pluginsDirectory = settings.DirectoriesSettings.PluginsDirectory;

            foreach (var pluginAssembly in settings.Assemblies)
            {
                try
                {
                    // Load the assembly from the plugin folder
                    string pluginLocation = Path.Combine(pluginsDirectory, pluginAssembly.File);

                    byte[]   assemblyBytes = File.ReadAllBytes(pluginLocation);
                    Assembly assembly      = Assembly.Load(assemblyBytes);

                    bool isValidPlugin = false;
                    foreach (Configuration.GlobalPlugin pluginComponent in pluginAssembly.Plugins)
                    {
                        try
                        {
                            Type type = assembly.GetType(pluginComponent.Class);

                            // Try to create an instance of the type and check
                            // if inherit from IPluginBase interface
                            object runnable = Activator.CreateInstance(type);
                            if (runnable is IPluginBase)
                            {
                                isValidPlugin = true;

                                if (type != null)
                                {
                                    pluginAssembly.Version = type.Assembly.GetName().Version.ToString();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(ex, ex.Message);
                            isValidPlugin = false;
                        }
                        pluginComponent.IsValid = isValidPlugin;
                        pluginComponent.Enabled = isValidPlugin && pluginComponent.Enabled;
                    } // -- fin foreach (PluginType pluginType in pluginAssembly.Types)
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, ex.Message);
                    pluginAssembly.IsValid = false;
                    pluginAssembly.Plugins.ForEach(t =>
                    {
                        t.Enabled = false;
                        t.IsValid = false;
                    });
                }
            }
        }