MainForm.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using Oreo.VersionUpdate.Commons;
  2. using Oreo.VersionUpdate.Helpers;
  3. using Oreo.VersionUpdate.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. using Y.Utils.DataUtils.Collections;
  16. using Y.Utils.DataUtils.JsonUtils;
  17. using Y.Utils.IOUtils.FileUtils;
  18. using Y.Utils.IOUtils.PathUtils;
  19. using Y.Utils.IOUtils.TxtUtils;
  20. using Y.Utils.NetUtils.FTPUtils;
  21. using Y.Utils.WindowsUtils.ProcessUtils;
  22. namespace Oreo.VersionUpdate.Views
  23. {
  24. public partial class MainForm : Form
  25. {
  26. const int DETAIL_SHOWTIME = 1000;
  27. string VersionNumber = "";
  28. string VersionDesc = "";
  29. string DownTemp = "DownTemp";
  30. string BackupTemp = "BackupTemp";
  31. public MainForm()
  32. {
  33. InitializeComponent();
  34. }
  35. private void MainForm_Load(object sender, EventArgs e)
  36. {
  37. StartUpdate();
  38. }
  39. private void StartUpdate()
  40. {
  41. Task.Factory.StartNew(() =>
  42. {
  43. R.Log.i("读取要更新的插件列表");
  44. List<PluginModel> pluginList = DataHelper.GetPluginList();
  45. if (ListTool.HasElements(pluginList))
  46. {
  47. R.Log.i("共读取到 " + pluginList.Count + " 个插件");
  48. pluginList.ForEach(x =>
  49. {
  50. R.Log.i("请求 " + x.Name + " / " + x.Version + " 插件的更新");
  51. VersionModel pluginNewVersion = DataHelper.GetPluginNewVersion(x);
  52. if (pluginNewVersion != null)
  53. {
  54. R.Log.i("准备更新 " + x.Name);
  55. BeforeUpdate(pluginNewVersion);//更新前操作
  56. bool flag = Update(pluginNewVersion);
  57. AfterUpdate(pluginNewVersion);//更新后操作
  58. if (flag)
  59. {
  60. R.Log.i(x.Name + " 更新成功,当前版本" + pluginNewVersion.VersionNumber);
  61. }
  62. else
  63. {
  64. R.Log.w(x.Name + " 更新失败");
  65. }
  66. }
  67. else
  68. {
  69. R.Log.i("更新配置请求失败,跳过当前更新");
  70. }
  71. });
  72. }
  73. else
  74. {
  75. R.Log.w("更新插件列表为空");
  76. }
  77. R.Log.i("本地插件更新操作结束");
  78. });
  79. }
  80. #region 更新操作
  81. /// <summary>
  82. /// 更新的完整流程
  83. /// </summary>
  84. /// <param name="vm"></param>
  85. /// <returns></returns>
  86. private bool Update(VersionModel vm)
  87. {
  88. VersionNumber = vm.VersionNumber;
  89. VersionDesc = vm.VersionDesc;
  90. DownTemp = Guid.NewGuid().ToString();
  91. BackupTemp = Guid.NewGuid().ToString();
  92. if (DirTool.Create(R.Paths.Temp + DownTemp))
  93. {
  94. R.Log.i("创建临时下载目录 " + R.Paths.Temp + DownTemp);
  95. R.Log.i("创建临时备份目录 " + R.Paths.Temp + BackupTemp);
  96. R.Log.i("将版本信息显示到 UI");
  97. UILoadVersion(vm);
  98. if (UpdateDownload(vm))
  99. {
  100. R.Log.i("文件已全部下载成功");
  101. if (UpdateInsteadAndBackup(vm))
  102. {
  103. R.Log.i("文件已替换,准备执行清理任务");
  104. FileHelper.Clean(vm);
  105. R.Log.i("准备更新配置信息");
  106. DataHelper.UpdatePluginConfig(vm);
  107. DataHelper.UpdateWhatsnew(vm);
  108. UIUpdateDetail("添加新版本特性说明 Whatsnew.txt");
  109. return true;
  110. }
  111. else
  112. {
  113. UpdateRollback(vm);
  114. R.Log.w("文件替换失败,当前更新失败,准备回滚备份的文件");
  115. }
  116. }
  117. else
  118. {
  119. R.Log.w("文件下载失败,当前更新失败");
  120. }
  121. }
  122. else
  123. {
  124. R.Log.i("创建临时存放文件目录 " + R.Paths.Temp + DownTemp + " 失败,中止更新");
  125. }
  126. return false;
  127. }
  128. /// <summary>
  129. /// 更新前操作(启动或关闭进程)
  130. /// </summary>
  131. /// <param name="vm"></param>
  132. private void BeforeUpdate(VersionModel vm)
  133. {
  134. ProcessTool.Kills(vm.BeforeUpdateKillProcess);
  135. ProcessTool.Starts(vm.BeforeUpdateStartProcess);
  136. }
  137. /// <summary>
  138. /// 更新后操作(启动或关闭进程)
  139. /// </summary>
  140. /// <param name="vm"></param>
  141. private void AfterUpdate(VersionModel vm)
  142. {
  143. ProcessTool.Kills(vm.AfterUpdateKillProcess);
  144. ProcessTool.Starts(vm.AfterUpdateStartProcess);
  145. UIUpdateDetail("当前更新完成");
  146. }
  147. /// <summary>
  148. /// 下载要更新的文件
  149. /// </summary>
  150. /// <param name="vm"></param>
  151. /// <returns></returns>
  152. private bool UpdateDownload(VersionModel vm)
  153. {
  154. FileCodeTool fcode = new FileCodeTool();
  155. var downFile = vm.FileList.Where(x => x.IsClean == false);
  156. if (vm != null && ListTool.HasElements(downFile))
  157. {
  158. foreach (var file in vm.FileList)
  159. {
  160. R.Log.v("当前处理文件:" + file.ServerFile);
  161. string serverFile = DirTool.Combine(vm.ServerPath, file.ServerFile);
  162. string tempFile = DirTool.Combine(R.Paths.Temp, DownTemp, file.ServerFile);//下载到目标位置(带文件名)
  163. string localFile = DirTool.IsDriver(file.LocalFile) ? file.LocalFile : DirTool.Combine(R.Paths.ProjectRoot, file.LocalFile);//旧文件位置
  164. if (fcode.GetMD5(localFile) != file.FileMD5)
  165. {
  166. UIUpdateDetail("准备下载:" + Path.GetFileName(file.ServerFile));
  167. R.Log.v("MD5码不相同,准备下载");
  168. FtpHelper ftp = new FtpHelper(R.Settings.FTP.Address, R.Settings.FTP.Account, R.Settings.FTP.Password);
  169. if (!ftp.Download(serverFile, tempFile))
  170. if (!ftp.Download(serverFile, tempFile))
  171. if (!ftp.Download(serverFile, tempFile))
  172. {
  173. R.Log.v("更新文件无法被下载,请检查网络重试");
  174. return false;
  175. }
  176. }
  177. else
  178. {
  179. UIUpdateDetail("文件已存在:" + Path.GetFileName(file.ServerFile));
  180. }
  181. }
  182. return true;
  183. }
  184. return false;
  185. }
  186. /// <summary>
  187. /// 备份并替换文件
  188. /// </summary>
  189. /// <param name="vm"></param>
  190. /// <returns></returns>
  191. private bool UpdateInsteadAndBackup(VersionModel vm)
  192. {
  193. var insteadFile = vm.FileList.Where(x => x.IsClean == false);
  194. foreach (var file in insteadFile)
  195. {
  196. string tempDown = DirTool.Combine(R.Paths.Temp, DownTemp, file.ServerFile);//下载到目标位置(带文件名)
  197. string tempBack = DirTool.Combine(R.Paths.Temp, BackupTemp, file.ServerFile);//备份到目标位置(带文件名)
  198. string localFile = DirTool.IsDriver(file.LocalFile) ? file.LocalFile : DirTool.Combine(R.Paths.ProjectRoot, file.LocalFile);//旧文件位置
  199. //备份文件
  200. if (File.Exists(localFile) && File.Exists(tempDown))
  201. {
  202. try
  203. {
  204. DirTool.Create(DirTool.GetFilePath(tempBack));
  205. File.Copy(localFile, tempBack, true);
  206. UIUpdateDetail("正在备份:" + tempBack);
  207. }
  208. catch (Exception e) { }
  209. }
  210. //替换文件
  211. if (File.Exists(tempDown))
  212. {
  213. try
  214. {
  215. DirTool.Create(DirTool.GetFilePath(localFile));
  216. File.Copy(tempDown, localFile, true);
  217. UIUpdateDetail("正在更新:" + file.LocalFile);
  218. }
  219. catch (Exception e)
  220. {
  221. return false;
  222. }
  223. }
  224. }
  225. return true;
  226. }
  227. /// <summary>
  228. /// 更新回滚
  229. /// </summary>
  230. /// <param name="vm"></param>
  231. private void UpdateRollback(VersionModel vm)
  232. {
  233. var backFile = vm.FileList.Where(x => x.IsClean == false);
  234. foreach (var file in backFile)
  235. {
  236. string tempBack = DirTool.Combine(R.Paths.Temp, BackupTemp, file.ServerFile);//备份到目标位置(带文件名)
  237. string localFile = DirTool.IsDriver(file.LocalFile) ? file.LocalFile : DirTool.Combine(R.Paths.ProjectRoot, file.LocalFile);//旧文件位置
  238. //还原备份文件
  239. if (File.Exists(tempBack))
  240. {
  241. try
  242. {
  243. DirTool.Create(DirTool.GetFilePath(localFile));
  244. File.Copy(tempBack, localFile, true);
  245. UIUpdateDetail("正在还原备份文件:" + file.LocalFile);
  246. }
  247. catch (Exception e) { }
  248. }
  249. }
  250. }
  251. #endregion
  252. #region UI操作
  253. /// <summary>
  254. /// 显示更新详情
  255. /// </summary>
  256. /// <param name="s"></param>
  257. void UIUpdateDetail(string s)
  258. {
  259. Invoke(new Action(() =>
  260. {
  261. LbUpdateDetail.Text = s;
  262. }));
  263. Thread.Sleep(DETAIL_SHOWTIME);
  264. }
  265. /// <summary>
  266. /// 读取当前要更新的信息
  267. /// </summary>
  268. /// <param name="vm"></param>
  269. void UILoadVersion(VersionModel vm)
  270. {
  271. Invoke(new Action(() =>
  272. {
  273. LbCodeName.Text = vm.CodeName;
  274. LbPluginName.Text = vm.PluginName;
  275. LbUpdateDetail.Text = "配置已加载,准备更新……";
  276. }));
  277. }
  278. /// <summary>
  279. /// 退出程序
  280. /// </summary>
  281. void UIClose()
  282. {
  283. Invoke(new Action(() =>
  284. {
  285. Close();
  286. }));
  287. }
  288. #endregion
  289. #region 控件事件
  290. /// <summary>
  291. /// 点击版本号 显示版本特性
  292. /// </summary>
  293. /// <param name="sender"></param>
  294. /// <param name="e"></param>
  295. private void LbVersionNumber_Click(object sender, EventArgs e)
  296. {
  297. MessageBox.Show(VersionDesc, VersionNumber + " 新特性");
  298. }
  299. #endregion
  300. }
  301. }