123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- using System;
- using System.IO;
- using System.Net;
- using System.Threading;
- using System.Windows.Forms;
- using Update.Config;
- using Update.Core.Entities;
- using Update.Core.Events;
- using Update.Net.Events;
- using ErrorEventArgs = Update.Core.Events.ErrorEventArgs;
- namespace Update.Core
- {
- partial class Updater
- {
- /// <summary>
- /// 异步检查开始
- /// </summary>
- protected virtual void ClientCheckAsync()
- {
- DisposeAvaliables();
- HostConfig.RefreshVersion();
- if (!File.Exists(HostConfig.ExecutablePath))
- {
- OnError(new ErrorEventArgs("要更新的程序不存在。"));
- return;
- }
- OnNotify(new NotifyEventArgs("正在下载更新信息。"));
- OnCheckStarted(new CheckStartedEventArgs());
- try
- {
- string url = HttpUtils.GetDownloadUrl(Const.Key);
- m_Client.DownloadStringAsync(new Uri(url));
- }
- catch (Exception e)
- {
- return;
- }
- }
- /// <summary>
- /// 异步检查完成
- /// </summary>
- /// <param name="e">结果</param>
- protected virtual void ClientCheckCompleted(DownloadStringCompletedEventArgs e)
- {
- //用户取消
- if (e.Cancelled)
- {
- OnNotify(new NotifyEventArgs("已取消更新。"));
- return;
- }
- //出错
- if (e.Error != null)
- {
- OnError(new ErrorEventArgs("下载更新信息失败:{0}。", e.Error.Message.TrimEnd(PERIOD)));
- return;
- }
- //解析
- Packages packages;
- try
- {
- packages = new Packages(e.Result);
- }
- catch (Exception exp)
- {
- OnError(new ErrorEventArgs("解析更新信息失败:{0}。", exp.Message.TrimEnd(PERIOD)));
- return;
- }
- //可用更新
- PackageCollection avaliables = packages.GetAvailables(HostConfig.CurrentVersion);
- //MessageBox.Show(e.Result+HostConfig.CurrentVersion);
- bool uptodate = avaliables.Count < 1;
- OnNotify(new NotifyEventArgs(uptodate ? "已是最新版本。" : "发现新版本。"));
- CheckCompletedEventArgs ce = new CheckCompletedEventArgs(uptodate);
- OnCheckCompleted(ce);
- if (uptodate || ce.Handled)
- {
- return;
- }
- m_Avaliables = avaliables.GetEnumerator();
- //开始更新
- OnUpdateStarted(new UpdateStartedEventArgs(avaliables));
- ClientKillAsync();
- }
- /// <summary>
- /// 异步结束进程开始
- /// </summary>
- protected virtual void ClientKillAsync()
- {
- //结束进程
- OnNotify(new NotifyEventArgs("正在结束占用进程。"));
- m_Client?.KillProcessAsync(HostConfig.ExecutableDirectory);
- }
- /// <summary>
- /// 异步结束进程完成
- /// </summary>
- /// <param name="e">结果</param>
- protected virtual void ClientKillCompleted(KillProcessCompletedEventArgs e)
- {
- //用户取消
- if (e.Cancelled)
- {
- OnNotify(new NotifyEventArgs("已取消更新。"));
- return;
- }
- //出错
- if (e.Error != null)
- {
- OnError(new ErrorEventArgs("结束占用进程失败:{0}。", e.Error.Message.TrimEnd(PERIOD)));
- return;
- }
- //开始下载
- ClientDownloadAsync();
- }
- /// <summary>
- /// 异步下载开始
- /// </summary>
- protected virtual void ClientDownloadAsync()
- {
- //验证
- if (m_Avaliables == null)
- {
- OnError(new ErrorEventArgs("必须先检查更新。"));
- return;
- }
- //下一个
- if (!m_Avaliables.MoveNext())
- {
- OnNotify(new NotifyEventArgs("更新完成。"));
- OnUpdateCompleted(new UpdateCompletedEventArgs());
- return;
- }
- IPackage package = m_Avaliables.Current;
- OnNotify(new NotifyEventArgs("正在下载 {0}。", package.FileName));
- try
- {
- string url = HttpUtils.GetDownloadUrl(package.FileName);
- m_Client.DownloadDataAsync(new Uri(url), package);
- }
- catch (Exception e)
- {
- return;
- }
- }
- /// <summary>
- /// 异步下载完成
- /// </summary>
- /// <param name="e">结果</param>
- protected virtual void ClientDownloadCompleted(DownloadDataCompletedEventArgs e)
- {
- //验证
- IPackage package = e.UserState as IPackage;
- if (package == null)
- {
- OnError(new ErrorEventArgs("无效的下载操作。"));
- return;
- }
- //用户取消
- if (e.Cancelled)
- {
- OnNotify(new NotifyEventArgs("已取消更新。"));
- return;
- }
- //出错
- if (e.Error != null)
- {
- OnError(new ErrorEventArgs("下载 {0} 失败:{1}。", package.FileName, e.Error.Message.TrimEnd(PERIOD)));
- return;
- }
- ClientDecompressAsync(e.Result, package);
- }
- /// <summary>
- /// 异步解压开始
- /// </summary>
- /// <param name="data">要解压的数据</param>
- /// <param name="package">更新包</param>
- protected virtual void ClientDecompressAsync(byte[] data, IPackage package)
- {
- //由于MBI的Exe的文件结构特殊,所以解压地址取上一级文件
- string decompressPath = (new DirectoryInfo(HostConfig.ExecutableDirectory)).Parent?.FullName;
- //string decompressPath = HostConfig.ExecutableDirectory;
- //解压
- OnNotify(new NotifyEventArgs("正在解压 {0}。", package.FileName));
- m_Client.DecompressDataAsync(data, PACKAGE_DELETE, HostConfig.ExecutableName, decompressPath, package);
- }
- /// <summary>
- /// 异步解压完成
- /// </summary>
- /// <param name="e">结果</param>
- protected virtual void ClientDecompressCompleted(DecompressDataCompletedEventArgs e)
- {
- //验证
- IPackage package = e.UserState as IPackage;
- if (package == null)
- {
- OnError(new ErrorEventArgs("无效的解压操作。"));
- return;
- }
- //用户取消
- if (e.Cancelled)
- {
- OnNotify(new NotifyEventArgs("已取消更新。"));
- return;
- }
- //出错
- if (e.Error != null)
- {
- OnError(new ErrorEventArgs("解压 {0} 失败:{1}。", package.FileName, e.Error.Message.TrimEnd(PERIOD)));
- return;
- }
- //继续下一个
- ClientDownloadAsync();
- }
- /// <summary>
- /// 检查版本是否过期
- /// </summary>
- /// <returns></returns>
- public bool IsClientOverdue()
- {
- //解析
- Packages packages;
- try
- {
- string url = HostConfig.DownLoadURL;
- var result = m_Client.DownloadString(new Uri(url));
- packages = new Packages(result);
- }
- catch (Exception exp)
- {
- return false;
- }
- //可用更新
- return packages.HasAvailable(HostConfig.CurrentVersion);
- }
- }
- }
|