Example #1
0
        /// <summary>
        /// 获取插件包的信息
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static IEnumerable <PluginPackAttribute> GetPluginPackInfo(string fileName)
        {
            var tempDir = String.Concat(AppDomain.CurrentDomain, PluginConfig.PLUGIN_TMP_DIRECTORY, "tmp/");

            if (Directory.Exists(tempDir))
            {
                Directory.Delete(tempDir, true);
            }
            Directory.CreateDirectory(tempDir).Create();

            var dir = new DirectoryInfo(tempDir);

            ZipUtility.UncompressFile(tempDir, fileName, false);

            Assembly ass;

            string[] multExt = PluginConfig.GetFilePartterns();
            foreach (String ext in multExt)
            {
                var files = dir.GetFiles(ext);
                foreach (FileInfo f in files)
                {
                    ass = Assembly.Load(File.ReadAllBytes(f.FullName));

                    var attbs = ass.GetCustomAttributes(typeof(PluginPackAttribute), false);
                    foreach (object attb in attbs)
                    {
                        if (attb is PluginPackAttribute)
                        {
                            yield return((PluginPackAttribute)attb);
                        }
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// 从目录中加载插件
        /// </summary>
        /// <param name="pluginDirectory"></param>
        private static void LoadPluginFromDirectory(string pluginDirectory)
        {
            bool loadResult = true;

            //多个后缀的情况下
            IList <String> files = new List <string>(10);

            string[] multExt = PluginConfig.GetFilePartterns();
            foreach (String ext in multExt)
            {
                String[] searchedExt = Directory.GetFiles(pluginDirectory, ext);
                foreach (String sext in searchedExt)
                {
                    files.Add(sext);
                }
            }

            if (Log != null)
            {
                Log.Println(String.Format("{0:yyyy-MM-dd HH:mm:ss}   [+]Plugin Loading"
                                          + "\r\n========================================\r\n"
                                          + "Directory:{1} \t Total DLL:{2}\r\n",
                                          DateTime.Now, pluginDirectory.Replace("\\", "/"),
                                          files.Count.ToString()));
            }
            foreach (string file in files)
            {
                if (!LoadPlugin(file))
                {
                    loadResult = false;
                }
            }

            if (Log != null)
            {
                Log.Println(String.Format("load complete!result:{0}", loadResult ? "Ok" : "Error"));
            }
        }
Example #3
0
        /// <summary>
        /// 安装/升级插件
        /// </summary>
        /// <param name="url"></param>
        /// <param name="handler"></param>
        /// <returns></returns>
        public static bool InstallPlugin(string url, PluginHandler <PluginPackAttribute> handler)
        {
            var    installResult = false;
            string appDirectory  = AppDomain.CurrentDomain.BaseDirectory;
            var    pluginPath    = String.Concat(
                appDirectory,
                PluginConfig.PLUGIN_DIRECTORY);

            var tempDir  = String.Concat(appDirectory, PluginConfig.PLUGIN_TMP_DIRECTORY, "plugin/");
            var fileName = tempDir + "dl_pack_" + String.Empty.RandomLetters(16) + ".zip";

            if (Directory.Exists(tempDir))
            {
                Directory.Delete(tempDir, true);
            }
            Directory.CreateDirectory(tempDir).Create();

            var dir = new DirectoryInfo(tempDir);

            var data = HttpClient.DownloadFile(url, null);

            using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                fs.Write(data, 0, data.Length);
                fs.Flush();
                fs.Dispose();
            }

            IList <PluginPackAttribute> pluginAttrs = new List <PluginPackAttribute>(GetPluginPackInfo(fileName));

            if (pluginAttrs.Count != 0)
            {
                ZipUtility.UncompressFile(pluginPath, fileName, false);

                string[] multExt = PluginConfig.GetFilePartterns();
                foreach (String ext in multExt)
                {
                    foreach (FileInfo file in dir.GetFiles(ext))
                    {
                        File.Delete(pluginPath + file.Name);
                        file.MoveTo(pluginPath + file.Name);
                    }
                }


                if (handler != null)
                {
                    var result = false;
                    foreach (PluginPackAttribute attr in pluginAttrs)
                    {
                        handler(attr, ref result);
                    }
                }

                installResult = true;
            }

            Directory.Delete(tempDir, true);

            return(installResult);
        }