/// <summary> /// 获取插件包的信息 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static IEnumerable <PluginPackAttribute> GetPluginPackInfo(string fileName) { var tempDir = String.Concat(EnvUtil.GetBaseDirectory() + "/", PluginConfig.PLUGIN_TMP_DIRECTORY, "tmp/"); if (Directory.Exists(tempDir)) { Directory.Delete(tempDir, true); } Directory.CreateDirectory(tempDir).Create(); var dir = new DirectoryInfo(tempDir); ZipUtility.DecompressFile(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); } } } } }
/// <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 = EnvUtil.GetBaseDirectory(); 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.DecompressFile(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); }