Пример #1
0
        public void LoadPlugins(MainForm mainForm, ICaptureHost captureHost)
        {
            // Copy ContextMenu
            mainMenu = mainForm.MainMenu;

            List <string> pluginFiles = new List <string>();

            if (IniConfig.IsPortable && Directory.Exists(pafPath))
            {
                foreach (string pluginFile in Directory.GetFiles(pafPath, "*.gsp", SearchOption.AllDirectories))
                {
                    pluginFiles.Add(pluginFile);
                }
            }
            else
            {
                if (Directory.Exists(pluginPath))
                {
                    foreach (string pluginFile in Directory.GetFiles(pluginPath, "*.gsp", SearchOption.AllDirectories))
                    {
                        pluginFiles.Add(pluginFile);
                    }
                }

                if (Directory.Exists(applicationPath))
                {
                    foreach (string pluginFile in Directory.GetFiles(applicationPath, "*.gsp", SearchOption.AllDirectories))
                    {
                        pluginFiles.Add(pluginFile);
                    }
                }
            }

            Dictionary <string, PluginAttribute> tmpAttributes = new Dictionary <string, PluginAttribute>();
            Dictionary <string, Assembly>        tmpAssemblies = new Dictionary <string, Assembly>();

            // Loop over the list of available files and get the Plugin Attributes
            foreach (string pluginFile in pluginFiles)
            {
                LOG.DebugFormat("Checking the following file for plugins: {0}", pluginFile);
                try {
                    Assembly          assembly         = Assembly.LoadFile(pluginFile);
                    PluginAttribute[] pluginAttributes = assembly.GetCustomAttributes(typeof(PluginAttribute), false) as PluginAttribute[];
                    if (pluginAttributes.Length > 0)
                    {
                        PluginAttribute pluginAttribute = pluginAttributes[0];

                        AssemblyProductAttribute[] assemblyProductAttributes = assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false) as AssemblyProductAttribute[];
                        if (assemblyProductAttributes.Length > 0)
                        {
                            pluginAttribute.Name = assemblyProductAttributes[0].Product;
                        }
                        else
                        {
                            continue;
                        }
                        pluginAttribute.Version = assembly.GetName().Version.ToString();
                        pluginAttribute.DllFile = pluginFile;

                        // check if this plugin is already available
                        PluginAttribute checkPluginAttribute = null;
                        try {
                            checkPluginAttribute = tmpAttributes[pluginAttribute.Name];
                        } catch {
                        }

                        if (checkPluginAttribute != null)
                        {
                            LOG.WarnFormat("Duplicate plugin {0} found", pluginAttribute.Name);
                            if (isNewer(pluginAttribute.Version, checkPluginAttribute.Version))
                            {
                                // Found is newer
                                tmpAttributes[pluginAttribute.Name] = pluginAttribute;
                                tmpAssemblies[pluginAttribute.Name] = assembly;
                                LOG.InfoFormat("Loading the newer plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
                            }
                            else
                            {
                                LOG.InfoFormat("Skipping (as the duplicate is newer or same version) the plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
                            }
                            continue;
                        }
                        else
                        {
                            if (conf.ExcludePlugins != null && conf.ExcludePlugins.Contains(pluginAttribute.Name))
                            {
                                LOG.WarnFormat("Exclude list: {0}", conf.ExcludePlugins.ToArray());
                                LOG.WarnFormat("Skipping the excluded plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
                                continue;
                            }
                            if (conf.IncludePlugins != null && conf.IncludePlugins.Count > 0 && !conf.IncludePlugins.Contains(pluginAttribute.Name))
                            {
                                // Whitelist is set
                                LOG.WarnFormat("Include list: {0}", conf.IncludePlugins.ToArray());
                                LOG.WarnFormat("Skipping the not included plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
                                continue;
                            }
                            LOG.InfoFormat("Loading the plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
                            tmpAttributes[pluginAttribute.Name] = pluginAttribute;
                            tmpAssemblies[pluginAttribute.Name] = assembly;
                        }
                    }
                    else
                    {
                        LOG.ErrorFormat("Can't find the needed Plugin Attribute ({0}) in the assembly of the file \"{1}\", skipping this file.", typeof(PluginAttribute), pluginFile);
                    }
                } catch (Exception e) {
                    LOG.Warn("Can't load file: " + pluginFile, e);
                }
            }
            foreach (string pluginName in tmpAttributes.Keys)
            {
                try {
                    PluginAttribute pluginAttribute = tmpAttributes[pluginName];
                    Assembly        assembly        = tmpAssemblies[pluginName];
                    Type            entryType       = assembly.GetType(pluginAttribute.EntryType);
                    if (entryType == null)
                    {
                        LOG.ErrorFormat("Can't find the in the PluginAttribute referenced type {0} in \"{1}\"", pluginAttribute.EntryType, pluginAttribute.DllFile);
                        continue;
                    }
                    try {
                        IGreenshotPlugin plugin = (IGreenshotPlugin)Activator.CreateInstance(entryType);
                        if (plugin != null)
                        {
                            if (plugin.Initialize(this, captureHost, pluginAttribute))
                            {
                                plugins.Add(pluginAttribute, plugin);
                            }
                            else
                            {
                                LOG.InfoFormat("Plugin {0} not initialized!", pluginAttribute.Name);
                            }
                        }
                        else
                        {
                            LOG.ErrorFormat("Can't create an instance of the in the PluginAttribute referenced type {0} from \"{1}\"", pluginAttribute.EntryType, pluginAttribute.DllFile);
                        }
                    } catch (Exception e) {
                        LOG.Error("Can't load Plugin: " + pluginAttribute.Name, e);
                    }
                } catch (Exception e) {
                    LOG.Error("Can't load Plugin: " + pluginName, e);
                }
            }
        }
Пример #2
0
 public RemoteWorker(ICaptureHost captureHost)
 {
     _captureHost = captureHost;
     _captureHost.Start();
 }
Пример #3
0
        public void LoadPlugins(MainForm mainForm, ICaptureHost captureHost)
        {
            // Copy ContextMenu
            mainMenu = mainForm.MainMenu;

            List<string> pluginFiles = new List<string>();

            if (IniConfig.IsPortable && Directory.Exists(pafPath)) {
                foreach(string pluginFile in Directory.GetFiles(pafPath, "*.gsp", SearchOption.AllDirectories)) {
                    pluginFiles.Add(pluginFile);
                }
            } else {
                if (Directory.Exists(pluginPath)) {
                    foreach(string pluginFile in Directory.GetFiles(pluginPath, "*.gsp", SearchOption.AllDirectories)) {
                        pluginFiles.Add(pluginFile);
                    }
                }

                if (Directory.Exists(applicationPath)) {
                    foreach(string pluginFile in Directory.GetFiles(applicationPath, "*.gsp", SearchOption.AllDirectories)) {
                        pluginFiles.Add(pluginFile);
                    }
                }
            }

            Dictionary<string, PluginAttribute> tmpAttributes = new Dictionary<string, PluginAttribute>();
            Dictionary<string, Assembly> tmpAssemblies = new Dictionary<string, Assembly>();
            // Loop over the list of available files and get the Plugin Attributes
            foreach (string pluginFile in pluginFiles) {
                LOG.DebugFormat("Checking the following file for plugins: {0}", pluginFile);
                try {
                    Assembly assembly = Assembly.LoadFile(pluginFile);
                    PluginAttribute[] pluginAttributes = assembly.GetCustomAttributes(typeof(PluginAttribute), false) as PluginAttribute[];
                    if (pluginAttributes.Length > 0) {
                        PluginAttribute pluginAttribute = pluginAttributes[0];

                        AssemblyProductAttribute[] assemblyProductAttributes = assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false) as AssemblyProductAttribute[];
                        if (assemblyProductAttributes.Length > 0) {
                            pluginAttribute.Name = assemblyProductAttributes[0].Product;
                        } else {
                            continue;
                        }
                        pluginAttribute.Version = assembly.GetName().Version.ToString();
                        pluginAttribute.DllFile = pluginFile;

                        // check if this plugin is already available
                        PluginAttribute checkPluginAttribute = null;
                        try {
                            checkPluginAttribute = tmpAttributes[pluginAttribute.Name];
                        } catch {

                        }

                        if (checkPluginAttribute != null) {
                            LOG.WarnFormat("Duplicate plugin {0} found", pluginAttribute.Name);
                            if (isNewer(pluginAttribute.Version, checkPluginAttribute.Version)) {
                                // Found is newer
                                tmpAttributes[pluginAttribute.Name] = pluginAttribute;
                                tmpAssemblies[pluginAttribute.Name] = assembly;
                                LOG.InfoFormat("Loading the newer plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
                            } else {
                                LOG.InfoFormat("Skipping (as the duplicate is newer or same version) the plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
                            }
                            continue;
                        } else {
                            if (conf.ExcludePlugins != null && conf.ExcludePlugins.Contains(pluginAttribute.Name)) {
                                LOG.WarnFormat("Exclude list: {0}", conf.ExcludePlugins.ToArray());
                                LOG.WarnFormat("Skipping the excluded plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
                                continue;
                            }
                            if (conf.IncludePlugins != null && conf.IncludePlugins.Count > 0 && !conf.IncludePlugins.Contains(pluginAttribute.Name)) {
                                // Whitelist is set
                                LOG.WarnFormat("Include list: {0}", conf.IncludePlugins.ToArray());
                                LOG.WarnFormat("Skipping the not included plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
                                continue;
                            }
                            LOG.InfoFormat("Loading the plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
                            tmpAttributes[pluginAttribute.Name] = pluginAttribute;
                            tmpAssemblies[pluginAttribute.Name] = assembly;
                        }
                    } else {
                        LOG.ErrorFormat("Can't find the needed Plugin Attribute ({0}) in the assembly of the file \"{1}\", skipping this file.", typeof(PluginAttribute), pluginFile);
                    }
                } catch (Exception e) {
                    LOG.Warn("Can't load file: " + pluginFile, e);
                }
            }
            foreach(string pluginName in tmpAttributes.Keys) {
                try {
                    PluginAttribute pluginAttribute = tmpAttributes[pluginName];
                    Assembly assembly = tmpAssemblies[pluginName];
                    Type entryType = assembly.GetType(pluginAttribute.EntryType);
                    if (entryType == null) {
                        LOG.ErrorFormat("Can't find the in the PluginAttribute referenced type {0} in \"{1}\"", pluginAttribute.EntryType, pluginAttribute.DllFile);
                        continue;
                    }
                    try {
                        IGreenshotPlugin plugin = (IGreenshotPlugin)Activator.CreateInstance(entryType);
                        if (plugin != null) {
                            if (plugin.Initialize(this, captureHost, pluginAttribute)) {
                                plugins.Add(pluginAttribute, plugin);
                            } else {
                                LOG.InfoFormat("Plugin {0} not initialized!", pluginAttribute.Name);
                            }
                        } else {
                            LOG.ErrorFormat("Can't create an instance of the in the PluginAttribute referenced type {0} from \"{1}\"", pluginAttribute.EntryType, pluginAttribute.DllFile);
                        }
                    } catch(Exception e) {
                        LOG.Error("Can't load Plugin: " + pluginAttribute.Name, e);
                    }
                } catch(Exception e) {
                    LOG.Error("Can't load Plugin: " + pluginName, e);
                }
            }
        }
Пример #4
0
 public virtual bool Initialize(IGreenshotPluginHost pluginHost, ICaptureHost captureHost, PluginAttribute myAttributes)
 {
     this.host = pluginHost;
     this.captureHost = captureHost;
     FogbugzPlugin.FogbugzPluginAttribute = myAttributes;
     this.host.OnImageEditorOpen += new OnImageEditorOpenHandler(this.ImageEditorOpened);
     this.config = IniConfig.GetIniSection<FogBugzConfiguration>();
     return true;
 }