/// <summary>
        /// Installs the plugin associated with the ID
        /// </summary>
        /// <param name="ID">The identifier.</param>
        /// <returns>Returns true if it is installed successfully, false otherwise</returns>
        public bool InstallPlugin(string ID)
        {
            Contract.Requires <ArgumentNullException>(!string.IsNullOrEmpty(ID), "ID");
            var User = HttpContext.Current.Chain(x => x.User).Chain(x => x.Identity).Chain(x => x.Name, "");

            Utilities.IO.Log.Get().LogMessage("Plugin {0} is being installed by {1}", MessageType.Debug, ID, User);
            var TempPlugin = PluginList.Get(ID);

            if (TempPlugin != null)
            {
                UninstallPlugin(ID);
            }
            foreach (IPackageRepository Repo in PackageRepositories)
            {
                var Package = Repo.FindPackage(ID);
                if (Package != null)
                {
                    new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/packages/" + Package.Id + "." + Package.Version.ToString() + "/lib").Create();
                    new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/packages/" + Package.Id + "." + Package.Version.ToString() + "/content").Create();
                    new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/packages/" + Package.Id + "." + Package.Version.ToString() + "/tools").Create();
                    var Manager = new PackageManager(Repo,
                                                     new DefaultPackagePathResolver(Repo.Source),
                                                     new PhysicalFileSystem(new DirectoryInfo(HttpContext.Current != null ?
                                                                                              HttpContext.Current.Server.MapPath("~/App_Data/packages") :
                                                                                              "./App_Data/packages").FullName));
                    Manager.InstallPackage(Package, false, true);
                    PluginList.Add(new Plugin(Package));
                    Package.DependencySets.ForEach(x => x.Dependencies.ForEach(y => InstallPlugin(y.Id)));
                    PluginList.Save();
                    break;
                }
            }
            Utilities.IO.Log.Get().LogMessage("Plugin {0} has been installed by {1}", MessageType.Debug, ID, User);
            return(true);
        }
Exemple #2
0
        private void AttachPlugins(PluginList plugins, Assembly pluginAssembly)
        {
            try
            {
                List <Type> availableTypes = new List <Type>();
                availableTypes.AddRange(pluginAssembly.GetTypes());

                // Get the object that implements the IPluginForm interface
                // and PluginAttribute
                List <Type> pluginForms = availableTypes.FindAll(delegate(Type t)
                {
                    List <Type> interfaceTypes = new List <Type>(t.GetInterfaces());
                    Object[] arr = t.GetCustomAttributes(typeof(MyPluginAttribute), true);

                    return(!(arr == null || arr.Length == 0) && interfaceTypes.Contains(typeof(IMyPlugin)));
                });

                foreach (Type pluginForm in pluginForms)
                {
                    if (typeof(IMyPlugin).IsAssignableFrom(pluginForm))
                    {
                        try
                        {
                            IMyPlugin gsaPlugin = Activator.CreateInstance(pluginForm) as IMyPlugin;
                            if (gsaPlugin != null)
                            {
                                plugins.Add(gsaPlugin);
                            }
                        }
                        catch (MyAppAccessDeniedException ex)
                        {
                            Logger.Instance.WriteLog(ex);
                            // user does not permission to load this
                        }
                        catch (Exception ex)
                        {
                            MyAppAccessDeniedException accessDeniedException = ex.InnerException as MyAppAccessDeniedException;
                            // suppress MyAppAccessDeniedException
                            if (accessDeniedException == null)
                            {
#if DEBUG
                                MessageBox.Show(ex.StackTrace, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif
                                Logger.Instance.WriteLog(accessDeniedException);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.WriteLog(ex.Message);
            }
        }
Exemple #3
0
 private void WrapPlugins()
 {
     Plugins = new PluginList();
     foreach (var rawPlugin in RawPlugins)
     {
         var newPlugin = new Plugin(rawPlugin)
         {
             Settings = FindPluginSettings(rawPlugin)
         };
         Plugins.Add(newPlugin);
     }
 }
        private List <IPluginContainer> LoadPlugins()
        {
            var ChangedList = new List <IPluginContainer>();

            string[] pluginNames = null;
            try
            {
                pluginNames = Directory.GetFiles(PluginDir, "*.dll");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(null);
            }
            foreach (string pluginName in pluginNames)
            {
                var pluginAssembly = Assembly.LoadFrom(pluginName);
                try
                {
                    foreach (var type in pluginAssembly.GetTypes())
                    {
                        if (type.GetInterfaces().Contains(typeof(IPluginContainer)))
                        {
                            var  plugin   = Activator.CreateInstance(type) as IPluginContainer;
                            bool isUnique = true;
                            foreach (var item in PluginList)
                            {
                                if (item.ClassName == plugin.ClassName)
                                {
                                    isUnique = false;
                                    break;
                                }
                            }
                            if (isUnique)
                            {
                                PluginList.Add(plugin);
                                ChangedList.Add(plugin);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return(null);
                }
            }
            return(ChangedList);
        }
        /// <summary>
        /// 加载插件
        /// </summary>
        /// <param name="pluginFile">插件文件名</param>
        /// <returns>成功=true,失败=false</returns>
        public static bool LoadPlugin(string pluginFile)
        {
            var assembly = Assembly.LoadFrom($@"{Program.PluginPath}\{pluginFile}");

            foreach (var type in assembly.GetExportedTypes())
            {
                if (type.GetInterfaces().Contains(typeof(IPlugin)))
                {
                    IPlugin plugin = (IPlugin)Activator.CreateInstance(type);
                    PluginList.Add(plugin);
                    for (var i = 0; i < plugin.ModeTypeList.Length; i++)
                    {
                        ModeList.Add(new ModeInfoModel()
                        {
                            ModeName = plugin.ModeNameList[i],
                            ModeType = plugin.ModeTypeList[i],
                        });
                    }
                    return(true);
                }
            }
            return(false);
        }