Esempio n. 1
0
        } = 3000;                                                      // Milliseconds

        #endregion


        #region PUBLIC

        public static void AddNewPlugin(IPlugin newPlugin)
        {
            if (newPlugin != null &&
                LoadedPlugins.FindAll(elem => elem.Config.Name == newPlugin.Config.Name).Count <= 0)
            {
                LoadedPlugins.Add(newPlugin);
                LoadedPlugins.Sort((s1, s2) => s1.CompareTo(s2));
            }
        }
Esempio n. 2
0
        public void LoadPlugins()
        {
            Console.WriteLine("Loading plugins...");
            Console.WriteLine();
            var pluginFiles = Directory.GetFiles(PluginFolder);
            var pluginTypes = new List <Type>();

            for (int i = 0; i < pluginFiles.Length; i++)
            {
                try
                {
                    if (Path.GetExtension(pluginFiles[i]) != ".dll")
                    {
                        continue;
                    }

                    Console.WriteLine("Loading {0}...", pluginFiles[i]);
                    var assembly = Assembly.LoadFrom(pluginFiles[i]);
                    var types    = assembly.GetExportedTypes();
                    for (int j = 0; j < types.Length; j++)
                    {
                        if (m_PluginType.IsAssignableFrom(types[j]) && !types[j].IsAbstract)
                        {
                            var plugin = Activator.CreateInstance(types[j]) as Plugin;
                            Console.WriteLine("\tLoading {0}...", plugin.GetType().Name);
                            Console.WriteLine("\t\tName: {0}\n\t\tDescription: {1}\n\t\tAuthor: {2}", plugin.Name, plugin.Description, plugin.Author);

                            plugin.Client = m_Client;
                            LoadedPlugins.Add(plugin);
                        }
                    }
                    Console.WriteLine();
                    LoadedPluginAssemblies.Add(assembly);
                }
                catch (PluginException)
                {
                    //TODO: Add logs.
                    //TODO: Remove plugin if the plugin was loaded.
                    Console.WriteLine("Error in plugin {0} constructor", pluginFiles[i]);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error while loading {0}: \n\t{1}", pluginFiles[i], ex.Message);
                }
            }

            for (int i = 0; i < LoadedPlugins.Count; i++)
            {
                var pluginTasks = new PluginTasks(LoadedPlugins[i]);
                m_PluginTasks.Add(pluginTasks);
                pluginTasks.DoLoad();
            }
        }
 public bool ImportPlugin(string pluginPath)
 {
     if (LoadedPlugins.Any(p => p.FullName == pluginPath))
     {
         return(true);
     }
     else
     {
         var plugin = new FileInfo(pluginPath);
         LoadedPlugins.Add(plugin);
         return(PluginManager.AddPlugin(plugin));
     }
 }
Esempio n. 4
0
 public Task <bool> ImportPlugin(string pluginPath)
 {
     if (LoadedPlugins.Any(p => p.FullName == pluginPath))
     {
         return(Task.FromResult(true));
     }
     else
     {
         var plugin = new FileInfo(pluginPath);
         LoadedPlugins.Add(plugin);
         return(Task.FromResult(PluginManager.AddPlugin(plugin)));
     }
 }
Esempio n. 5
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. 6
0
        private void LoadPlugin(IPlugin plugin)
        {
            var savePlugin = $"{plugin.Name}_{plugin.Author}";

            if (LoadedPlugins.Contains(plugin))
            {
                LoadedPlugins.Remove(plugin);
                Options.Values.LoadedPlugins.Remove(savePlugin);
                Options.Values.Save();
                plugin.Unload();
                return;
            }
            LoadedPlugins.Add(plugin);
            Options.Values.LoadedPlugins.Add(savePlugin);
            Options.Values.Save();
            plugin.Load();
        }
Esempio n. 7
0
        public void LoadPlugin(Assembly plugin, bool start = false)
        {
            if (plugin == null)
            {
                return;
            }
            var types = plugin.GetTypes();

            foreach (var pin in from type in types where !type.IsInterface && !type.IsAbstract where type.HasAbstract(typeof(Plugin)) select(Plugin) Activator.CreateInstance(type))
            {
                Logger.Info(string.Format("Loading Plugin - {0}(V -> {1})", pin.Name, pin.Version), "Plugin Engine");
                if (start)
                {
                    pin.Start();
                }
                LoadedPlugins.Add(pin);
                break;
            }
        }
Esempio n. 8
0
        public bool LoadPlugins()
        {
            LoadedPlugins.Clear();
            foreach (string name in m_PluginNames)
            {
                IPlugin plugin = null;

                try
                {
                    plugin = (IPlugin)Activator.CreateInstance(Type.GetType(name));
                }
                catch (Exception)
                {
                    return(false);
                }

                if (plugin != null)
                {
                    LoadedPlugins.Add(plugin);
                }
            }

            return(true);
        }
Esempio n. 9
0
 public void RegisterPlugin(Plugin plugin)
 {
     LoadedPlugins.Add(plugin.Name(), plugin);
 }
Esempio n. 10
0
        /// <summary>
        /// Gets the plugins.
        /// </summary>
        /// <param name="host">The host.</param>
        /// <returns></returns>
        public List <IPlugin> GetPlugins(IPluginHost host)
        {
            string         localPath = System.IO.Path.GetDirectoryName(this.GetType( ).Assembly.Location);
            List <IPlugin> plugins   = new List <IPlugin> ( );
            var            merged    = Paths.Union(Settings.Instance.PluginSettings.DefaultPlaths);

            foreach (string pathName in merged)
            {
                string tpathName = System.IO.Path.IsPathRooted(pathName) ? pathName : System.IO.Path.Combine(localPath, pathName);

                System.IO.DirectoryInfo path = new System.IO.DirectoryInfo(tpathName);
                foreach (var item in path.GetFiles("*.dll"))
                {
                    try {
                        Assembly    tasm  = AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(item.FullName));
                        List <Type> types = tasm.GetTypes( ).ToList <Type> ( );
                        types.Sort(new Comparison <Type> (delegate(Type x, Type y) {
                            return(x.Name.CompareTo(y.Name));
                        }));
                        foreach (var type in types)
                        {
                            if (type.GetInterface(typeof(IPlugin).FullName) != null && !type.IsInterface)
                            {
                                try {
                                    ConstructorInfo phCtor = type.GetConstructor(new Type[] { typeof(IPluginHost) });
                                    IPlugin         plugin = null;

                                    PluginInfo pi = GetPluginInfo(type);

                                    if (pi == null)
                                    {
                                        pi                 = new PluginInfo( );
                                        pi.Enabled         = true;
                                        pi.ID              = string.Format(CultureInfo.InvariantCulture, "{0}, {1}", type.FullName, tasm.GetName( ).Name);
                                        pi.ExecuteOnLoad   = false;
                                        pi.ExecuteOnUnload = false;
                                        pi.Name            = type.Name;
                                        this.Plugins.Add(pi);
                                    }

                                    if (pi.Enabled)
                                    {
                                        if (!plugins.Contains(plugin))
                                        {
                                            if (phCtor != null)
                                            {
                                                plugin = Activator.CreateInstance(type, host) as IPlugin;
                                            }
                                            else
                                            {
                                                plugin = Activator.CreateInstance(type) as IPlugin;
                                            }
                                            LoadedPlugins.Add(plugin);
                                            plugins.Add(plugin);
                                        }
                                    }
                                } catch (Exception ex) {
                                    this.LogError(string.Format(CultureInfo.InvariantCulture, "Unable to load type {0}: {1}", type.FullName), ex);
                                }
                            }
                        }
                    } catch (Exception ex) {
                        this.LogError(string.Format(CultureInfo.InvariantCulture, "Unable to load {0}", item), ex);
                    }
                }
            }

            return(plugins);
        }