Updater.Methods.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. using Update.Config;
  7. using Update.Core.Entities;
  8. using Update.Core.Events;
  9. using Update.Net.Events;
  10. using ErrorEventArgs = Update.Core.Events.ErrorEventArgs;
  11. namespace Update.Core
  12. {
  13. partial class Updater
  14. {
  15. /// <summary>
  16. /// 异步检查开始
  17. /// </summary>
  18. protected virtual void ClientCheckAsync()
  19. {
  20. DisposeAvaliables();
  21. HostConfig.RefreshVersion();
  22. if (!File.Exists(HostConfig.ExecutablePath))
  23. {
  24. OnError(new ErrorEventArgs("要更新的程序不存在。"));
  25. return;
  26. }
  27. OnNotify(new NotifyEventArgs("正在下载更新信息。"));
  28. OnCheckStarted(new CheckStartedEventArgs());
  29. try
  30. {
  31. string url = HttpUtils.GetDownloadUrl(Const.Key);
  32. m_Client.DownloadStringAsync(new Uri(url));
  33. }
  34. catch (Exception e)
  35. {
  36. return;
  37. }
  38. }
  39. /// <summary>
  40. /// 异步检查完成
  41. /// </summary>
  42. /// <param name="e">结果</param>
  43. protected virtual void ClientCheckCompleted(DownloadStringCompletedEventArgs e)
  44. {
  45. //用户取消
  46. if (e.Cancelled)
  47. {
  48. OnNotify(new NotifyEventArgs("已取消更新。"));
  49. return;
  50. }
  51. //出错
  52. if (e.Error != null)
  53. {
  54. OnError(new ErrorEventArgs("下载更新信息失败:{0}。", e.Error.Message.TrimEnd(PERIOD)));
  55. return;
  56. }
  57. //解析
  58. Packages packages;
  59. try
  60. {
  61. packages = new Packages(e.Result);
  62. }
  63. catch (Exception exp)
  64. {
  65. OnError(new ErrorEventArgs("解析更新信息失败:{0}。", exp.Message.TrimEnd(PERIOD)));
  66. return;
  67. }
  68. //可用更新
  69. PackageCollection avaliables = packages.GetAvailables(HostConfig.CurrentVersion);
  70. //MessageBox.Show(e.Result+HostConfig.CurrentVersion);
  71. bool uptodate = avaliables.Count < 1;
  72. OnNotify(new NotifyEventArgs(uptodate ? "已是最新版本。" : "发现新版本。"));
  73. CheckCompletedEventArgs ce = new CheckCompletedEventArgs(uptodate);
  74. OnCheckCompleted(ce);
  75. if (uptodate || ce.Handled)
  76. {
  77. return;
  78. }
  79. m_Avaliables = avaliables.GetEnumerator();
  80. //开始更新
  81. OnUpdateStarted(new UpdateStartedEventArgs(avaliables));
  82. ClientKillAsync();
  83. }
  84. /// <summary>
  85. /// 异步结束进程开始
  86. /// </summary>
  87. protected virtual void ClientKillAsync()
  88. {
  89. //结束进程
  90. OnNotify(new NotifyEventArgs("正在结束占用进程。"));
  91. m_Client?.KillProcessAsync(HostConfig.ExecutableDirectory);
  92. }
  93. /// <summary>
  94. /// 异步结束进程完成
  95. /// </summary>
  96. /// <param name="e">结果</param>
  97. protected virtual void ClientKillCompleted(KillProcessCompletedEventArgs e)
  98. {
  99. //用户取消
  100. if (e.Cancelled)
  101. {
  102. OnNotify(new NotifyEventArgs("已取消更新。"));
  103. return;
  104. }
  105. //出错
  106. if (e.Error != null)
  107. {
  108. OnError(new ErrorEventArgs("结束占用进程失败:{0}。", e.Error.Message.TrimEnd(PERIOD)));
  109. return;
  110. }
  111. //开始下载
  112. ClientDownloadAsync();
  113. }
  114. /// <summary>
  115. /// 异步下载开始
  116. /// </summary>
  117. protected virtual void ClientDownloadAsync()
  118. {
  119. //验证
  120. if (m_Avaliables == null)
  121. {
  122. OnError(new ErrorEventArgs("必须先检查更新。"));
  123. return;
  124. }
  125. //下一个
  126. if (!m_Avaliables.MoveNext())
  127. {
  128. OnNotify(new NotifyEventArgs("更新完成。"));
  129. OnUpdateCompleted(new UpdateCompletedEventArgs());
  130. return;
  131. }
  132. IPackage package = m_Avaliables.Current;
  133. OnNotify(new NotifyEventArgs("正在下载 {0}。", package.FileName));
  134. try
  135. {
  136. string url = HttpUtils.GetDownloadUrl(package.FileName);
  137. m_Client.DownloadDataAsync(new Uri(url), package);
  138. }
  139. catch (Exception e)
  140. {
  141. return;
  142. }
  143. }
  144. /// <summary>
  145. /// 异步下载完成
  146. /// </summary>
  147. /// <param name="e">结果</param>
  148. protected virtual void ClientDownloadCompleted(DownloadDataCompletedEventArgs e)
  149. {
  150. //验证
  151. IPackage package = e.UserState as IPackage;
  152. if (package == null)
  153. {
  154. OnError(new ErrorEventArgs("无效的下载操作。"));
  155. return;
  156. }
  157. //用户取消
  158. if (e.Cancelled)
  159. {
  160. OnNotify(new NotifyEventArgs("已取消更新。"));
  161. return;
  162. }
  163. //出错
  164. if (e.Error != null)
  165. {
  166. OnError(new ErrorEventArgs("下载 {0} 失败:{1}。", package.FileName, e.Error.Message.TrimEnd(PERIOD)));
  167. return;
  168. }
  169. ClientDecompressAsync(e.Result, package);
  170. }
  171. /// <summary>
  172. /// 异步解压开始
  173. /// </summary>
  174. /// <param name="data">要解压的数据</param>
  175. /// <param name="package">更新包</param>
  176. protected virtual void ClientDecompressAsync(byte[] data, IPackage package)
  177. {
  178. //由于MBI的Exe的文件结构特殊,所以解压地址取上一级文件
  179. string decompressPath = (new DirectoryInfo(HostConfig.ExecutableDirectory)).Parent?.FullName;
  180. //string decompressPath = HostConfig.ExecutableDirectory;
  181. //解压
  182. OnNotify(new NotifyEventArgs("正在解压 {0}。", package.FileName));
  183. m_Client.DecompressDataAsync(data, PACKAGE_DELETE, HostConfig.ExecutableName, decompressPath, package);
  184. }
  185. /// <summary>
  186. /// 异步解压完成
  187. /// </summary>
  188. /// <param name="e">结果</param>
  189. protected virtual void ClientDecompressCompleted(DecompressDataCompletedEventArgs e)
  190. {
  191. //验证
  192. IPackage package = e.UserState as IPackage;
  193. if (package == null)
  194. {
  195. OnError(new ErrorEventArgs("无效的解压操作。"));
  196. return;
  197. }
  198. //用户取消
  199. if (e.Cancelled)
  200. {
  201. OnNotify(new NotifyEventArgs("已取消更新。"));
  202. return;
  203. }
  204. //出错
  205. if (e.Error != null)
  206. {
  207. OnError(new ErrorEventArgs("解压 {0} 失败:{1}。", package.FileName, e.Error.Message.TrimEnd(PERIOD)));
  208. return;
  209. }
  210. //继续下一个
  211. ClientDownloadAsync();
  212. }
  213. /// <summary>
  214. /// 检查版本是否过期
  215. /// </summary>
  216. /// <returns></returns>
  217. public bool IsClientOverdue()
  218. {
  219. //解析
  220. Packages packages;
  221. try
  222. {
  223. string url = HostConfig.DownLoadURL;
  224. var result = m_Client.DownloadString(new Uri(url));
  225. packages = new Packages(result);
  226. }
  227. catch (Exception exp)
  228. {
  229. return false;
  230. }
  231. //可用更新
  232. return packages.HasAvailable(HostConfig.CurrentVersion);
  233. }
  234. }
  235. }