MainForm.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using Oreo.VersionUpdate.Commons;
  2. using Oreo.VersionUpdate.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using Y.Utils.DataUtils.Collections;
  15. using Y.Utils.DataUtils.JsonUtils;
  16. using Y.Utils.IOUtils.FileUtils;
  17. using Y.Utils.IOUtils.PathUtils;
  18. using Y.Utils.IOUtils.TxtUtils;
  19. using Y.Utils.NetUtils.FTPUtils;
  20. using Y.Utils.WindowsUtils.ProcessUtils;
  21. namespace Oreo.VersionUpdate.Views
  22. {
  23. public partial class MainForm : Form
  24. {
  25. const int DETAIL_SHOWTIME = 1000;
  26. string VersionNumber = "";
  27. string VersionDesc = "";
  28. string TempFile = "Temp";
  29. public MainForm()
  30. {
  31. InitializeComponent();
  32. }
  33. private void MainForm_Load(object sender, EventArgs e)
  34. {
  35. StartUpdate();
  36. }
  37. private void StartUpdate()
  38. {
  39. Task.Factory.StartNew(() =>
  40. {
  41. //读取标准更新最新版本号 获取需要标准更新的配置
  42. StartUpdateStandard();
  43. //使用本地插件名称和版本号 获取需要更新插件的配置
  44. StartUpdatePlugin();
  45. });
  46. }
  47. private void StartUpdateStandard()
  48. {
  49. R.Log.i("读取本地标准更新版本号");
  50. string versionNumber = GetStandardVersionNumber();
  51. R.Log.i("读取当前标准版本 " + versionNumber + ",准备请求服务器新版本");
  52. VersionModel standardNewVersion = GetStandardNewVersion(versionNumber);
  53. if (standardNewVersion != null)
  54. {
  55. R.Log.i("有新的更新,版本号 " + standardNewVersion.VersionNumber);
  56. bool flag = Update(standardNewVersion);
  57. if (flag)
  58. {
  59. R.Log.i(versionNumber + " 更新成功,当前版本" + standardNewVersion.VersionNumber);
  60. }
  61. else
  62. {
  63. R.Log.w(versionNumber + " 更新失败");
  64. }
  65. }
  66. else
  67. {
  68. R.Log.i("当前标准版本为最新,不需要更新");
  69. }
  70. }
  71. private void StartUpdatePlugin()
  72. {
  73. R.Log.i("读取本地插件列表(名称、版本号)");
  74. List<PluginModel> pluginList = GetPluginList();
  75. if (ListTool.HasElements(pluginList))
  76. {
  77. R.Log.i("共读取到 " + pluginList.Count + " 个插件");
  78. pluginList.ForEach(x =>
  79. {
  80. R.Log.i("请求 " + x.Name + " / " + x.Version + " 插件的更新");
  81. VersionModel pluginNewVersion = GetPluginNewVersion(x);
  82. if (pluginNewVersion != null)
  83. {
  84. R.Log.i(x.Name + " / " + x.Version + " 插件有新版本 " + pluginNewVersion.VersionNumber + " 需要更新,准备更新");
  85. bool flag = Update(pluginNewVersion);
  86. if (flag)
  87. {
  88. R.Log.i(x.Name + " 更新成功,当前版本" + pluginNewVersion.VersionNumber);
  89. }
  90. else
  91. {
  92. R.Log.w(x.Name + " 更新失败");
  93. }
  94. }
  95. else
  96. {
  97. R.Log.i("当前插件版本为最新版本,不需要更新");
  98. }
  99. });
  100. }
  101. else
  102. {
  103. R.Log.w("本地插件列表为空");
  104. }
  105. R.Log.i("本地插件更新操作结束");
  106. }
  107. #region 读取本地数据操作
  108. private List<PluginModel> GetPluginList()
  109. {
  110. List<PluginModel> rs = null;
  111. return rs;
  112. }
  113. private string GetStandardVersionNumber()
  114. {
  115. string rs = R.Settings.Version.Number;
  116. return rs;
  117. }
  118. #endregion
  119. #region 读取网络数据操作
  120. private VersionModel GetStandardNewVersion(string vn)
  121. {
  122. VersionModel rs = JsonTool.ToObjFromFile<VersionModel>(@"D:\CoCo\GitHub\Fork\Fork.Net\Oreo.VersionBuilder\bin\Debug\VersionFile\0527112916.version");
  123. return rs;
  124. }
  125. private VersionModel GetPluginNewVersion(PluginModel pm)
  126. {
  127. VersionModel rs = JsonTool.ToObjFromFile<VersionModel>(@"D:\CoCo\GitHub\Fork\Fork.Net\Oreo.VersionBuilder\bin\Debug\VersionFile\0527112916.version");
  128. return rs;
  129. }
  130. #endregion
  131. #region 更新操作
  132. private bool Update(VersionModel vm)
  133. {
  134. VersionNumber = vm.VersionNumber;
  135. VersionDesc = vm.VersionDesc;
  136. TempFile = Guid.NewGuid().ToString();
  137. if (DirTool.Create(R.Paths.Temp + TempFile))
  138. {
  139. R.Log.i("创建临时存放文件目录 " + R.Paths.Temp + TempFile);
  140. R.Log.i("将版本信息显示到 UI");
  141. UILoadVersion(vm);
  142. UpdateBefore(vm);
  143. if (UpdateDownload(vm))
  144. {
  145. R.Log.i("文件已下载成功");
  146. if (UpdateInstead(vm))
  147. {
  148. R.Log.i("文件已替换,准备执行清理任务");
  149. UpdateClean(vm);
  150. R.Log.i("配置文件信息更新完毕,准备写入新版本配置和描述信息");
  151. UpdateConfig(vm);
  152. UpdateWhatsnew(vm);
  153. UpdateAfter(vm);
  154. return true;
  155. }
  156. else
  157. {
  158. R.Log.w("文件替换失败,当前更新失败");
  159. }
  160. }
  161. else
  162. {
  163. R.Log.w("文件下载失败,当前更新失败");
  164. }
  165. }
  166. else
  167. {
  168. R.Log.i("创建临时存放文件目录 " + R.Paths.Temp + TempFile + " 失败,中止更新");
  169. }
  170. return false;
  171. }
  172. private void UpdateBefore(VersionModel vm)
  173. {
  174. if (ListTool.HasElements(vm.BeforeUpdateKillProcess))
  175. {
  176. foreach (var p in vm.BeforeUpdateKillProcess)
  177. {
  178. if (!string.IsNullOrWhiteSpace(p))
  179. ProcessTool.KillProcess(p);
  180. }
  181. }
  182. if (ListTool.HasElements(vm.BeforeUpdateStartProcess))
  183. {
  184. foreach (var p in vm.BeforeUpdateStartProcess)
  185. {
  186. if (!string.IsNullOrWhiteSpace(p))
  187. ProcessTool.StartProcess(p);
  188. }
  189. }
  190. }
  191. private bool UpdateDownload(VersionModel vm)
  192. {
  193. FileCodeTool fcode = new FileCodeTool();
  194. var downFile = vm.FileList.Where(x => x.IsClean == false);
  195. if (vm != null && ListTool.HasElements(downFile))
  196. {
  197. foreach (var file in vm.FileList)
  198. {
  199. R.Log.v("当前处理文件:" + file.ServerFile);
  200. string serverFile = DirTool.Combine(vm.ServerPath, file.ServerFile);
  201. string tempFile = DirTool.Combine(R.Paths.Temp, TempFile, file.ServerFile);//下载到目标位置(带文件名)
  202. string localFile = DirTool.IsDriver(file.LocalFile) ? file.LocalFile : DirTool.Combine(R.Paths.App, file.LocalFile);//旧文件位置
  203. if (fcode.GetMD5(localFile) != file.FileMD5)
  204. {
  205. UIUpdateDetail("准备下载:" + Path.GetFileName(file.ServerFile));
  206. R.Log.v("MD5码不相同,准备下载");
  207. FtpHelper ftp = new FtpHelper(R.Settings.FTP.Address, R.Settings.FTP.Account, R.Settings.FTP.Password);
  208. if (!ftp.Download(serverFile, tempFile))
  209. if (!ftp.Download(serverFile, tempFile))
  210. if (!ftp.Download(serverFile, tempFile))
  211. {
  212. R.Log.v("更新文件无法被下载,请检查网络重试");
  213. return false;
  214. }
  215. }
  216. else
  217. {
  218. UIUpdateDetail("文件已存在:" + Path.GetFileName(file.ServerFile));
  219. }
  220. }
  221. return true;
  222. }
  223. return false;
  224. }
  225. private bool UpdateInstead(VersionModel vm)
  226. {
  227. var insteadFile = vm.FileList.Where(x => x.IsClean == false);
  228. foreach (var file in insteadFile)
  229. {
  230. string tempFile = DirTool.Combine(R.Paths.Temp, TempFile, file.ServerFile);//下载到目标位置(带文件名)
  231. string localFile = DirTool.IsDriver(file.LocalFile) ? file.LocalFile : DirTool.Combine(R.Paths.App, file.LocalFile);//旧文件位置
  232. if (File.Exists(tempFile))
  233. {
  234. try
  235. {
  236. DirTool.Create(DirTool.GetFilePath(localFile));
  237. File.Copy(tempFile, localFile, true);
  238. UIUpdateDetail("正在更新:" + file.LocalFile);
  239. }
  240. catch (Exception e)
  241. {
  242. return false;
  243. }
  244. }
  245. }
  246. return true;
  247. }
  248. private void UpdateClean(VersionModel vm)
  249. {
  250. //清理临时文件夹
  251. if (Directory.Exists(R.Paths.Temp))
  252. {
  253. try { Directory.Delete(R.Paths.Temp, true); } catch { }
  254. UIUpdateDetail("正在清理临时文件夹:Temp");
  255. }
  256. //清理指定文件
  257. var cleanFile = vm.FileList.Where(x => x.IsClean == true);
  258. foreach (var file in cleanFile)
  259. {
  260. string fff = DirTool.IsDriver(file.LocalFile) ? file.LocalFile : R.Paths.App + file.LocalFile;
  261. if (File.Exists(fff))
  262. {
  263. try { File.Delete(fff); } catch { }
  264. UIUpdateDetail("正在清理指定位置文件:" + fff);
  265. }
  266. }
  267. }
  268. private void UpdateConfig(VersionModel vm)
  269. {
  270. if (string.IsNullOrWhiteSpace(vm.PluginName) || string.IsNullOrWhiteSpace(vm.PluginEntry))
  271. {
  272. //标准版本更改设置
  273. IniTool.WriteValue(R.Files.Settings, "Version", "Number", vm.VersionNumber);
  274. UIUpdateDetail("修改主版本号:" + vm.VersionNumber);
  275. }
  276. else
  277. {
  278. //插件版本更改设置
  279. UIUpdateDetail("修改插件版本号:" + vm.PluginName + " " + vm.VersionNumber);
  280. }
  281. }
  282. private void UpdateWhatsnew(VersionModel vm)
  283. {
  284. TxtTool.Append(R.Files.Whatsnew, string.Format("{0} {1} {2}",
  285. vm.CodeName, vm.VersionNumber, (vm.PluginName == "" ? "" : "For:" + vm.PluginName)));
  286. TxtTool.Append(R.Files.Whatsnew, vm.VersionDesc);
  287. TxtTool.Append(R.Files.Whatsnew, new string('=', 50));
  288. UIUpdateDetail("添加新版本特性说明 Whatsnew.txt");
  289. }
  290. private void UpdateAfter(VersionModel vm)
  291. {
  292. if (ListTool.HasElements(vm.AfterUpdateKillProcess))
  293. {
  294. foreach (var p in vm.AfterUpdateKillProcess)
  295. {
  296. if (!string.IsNullOrWhiteSpace(p))
  297. ProcessTool.KillProcess(p);
  298. }
  299. }
  300. if (ListTool.HasElements(vm.AfterUpdateStartProcess))
  301. {
  302. foreach (var p in vm.AfterUpdateStartProcess)
  303. {
  304. if (!string.IsNullOrWhiteSpace(p))
  305. ProcessTool.StartProcess(p);
  306. }
  307. }
  308. UIUpdateDetail("当前更新完成");
  309. }
  310. #endregion
  311. #region UI操作
  312. void UIUpdateDetail(string s)
  313. {
  314. Invoke(new Action(() =>
  315. {
  316. LbUpdateDetail.Text = s;
  317. }));
  318. Thread.Sleep(DETAIL_SHOWTIME);
  319. }
  320. void UILoadVersion(VersionModel vm)
  321. {
  322. Invoke(new Action(() =>
  323. {
  324. LbCodeName.Text = vm.CodeName;
  325. LbPluginName.Text = vm.PluginName;
  326. LbUpdateDetail.Text = "配置已加载,准备更新……";
  327. }));
  328. }
  329. void UIClose()
  330. {
  331. Invoke(new Action(() =>
  332. {
  333. Close();
  334. }));
  335. }
  336. #endregion
  337. #region 控件事件
  338. private void LbVersionNumber_Click(object sender, EventArgs e)
  339. {
  340. MessageBox.Show(VersionDesc, VersionNumber + " 新特性");
  341. }
  342. #endregion
  343. }
  344. }