|
|
@@ -25,74 +25,91 @@ namespace Y.Utils.UpdateUtils
|
|
|
public class AppUpdateTool
|
|
|
{
|
|
|
/// <summary>
|
|
|
+ /// 获取新版本
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="url"></param>
|
|
|
+ /// <param name="name"></param>
|
|
|
+ /// <param name="version"></param>
|
|
|
+ /// <param name="info"></param>
|
|
|
+ /// <returns>
|
|
|
+ /// -10;//请求版本失败
|
|
|
+ /// -20;//没有更新的版本
|
|
|
+ /// </returns>
|
|
|
+ public int GetNewVersion(string url, string name, Version version, out AppUpdateInfo info)
|
|
|
+ {
|
|
|
+ Stopwatch stopwatch = new Stopwatch();
|
|
|
+ stopwatch.Start();
|
|
|
+
|
|
|
+ info = HttpTool.Get<AppUpdateInfo>(string.Format("{0}?name={1}", url, name));
|
|
|
+ if (info != null)
|
|
|
+ {
|
|
|
+ Version newVersion = FormatVersion(info.Version);
|
|
|
+ if (newVersion != null && newVersion > version)
|
|
|
+ {
|
|
|
+ stopwatch.Stop();
|
|
|
+ return (int)stopwatch.Elapsed.TotalSeconds;//成功返回操作时长
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return -20;//没有更新的版本
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return -10;//请求版本失败
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
/// 更新
|
|
|
/// </summary>
|
|
|
- /// <param name="name">功能名称</param>
|
|
|
- /// <param name="version">当前版本号</param>
|
|
|
- /// <param name="url">请求新版本地址</param>
|
|
|
- /// <param name="path">文件下载位置</param>
|
|
|
+ /// <param name="info">新版本信息</param>
|
|
|
+ /// <param name="tempPath">文件下载位置</param>
|
|
|
/// <param name="dictionary">文件相对位置字典</param>
|
|
|
- /// <param name="downprogress">下载进度回调</param>
|
|
|
- /// <param name="downsender">下载进度事件数据</param>
|
|
|
- /// <param name="releaseprogress">释放进度回调</param>
|
|
|
- /// <param name="releasesender">释放进度事件数据</param>
|
|
|
+ /// <param name="downProgress">下载进度回调</param>
|
|
|
+ /// <param name="downSender">下载进度事件数据</param>
|
|
|
+ /// <param name="releaseProgress">释放进度回调</param>
|
|
|
+ /// <param name="releaseSender">释放进度事件数据</param>
|
|
|
/// <returns>
|
|
|
- /// -10000;//无最新版本,停止操作
|
|
|
- /// -20000;//请求服务器最新版本失败
|
|
|
- /// -30000;//新版本号格式不正确,解析失败
|
|
|
- /// -40000;//文件下载失败
|
|
|
- /// -50000;//文件释放失败
|
|
|
+ /// -10000;//没有新版本
|
|
|
+ /// -20000;//文件下载失败
|
|
|
+ /// -30000;//文件释放失败
|
|
|
/// </returns>
|
|
|
- public int Update(string name, Version version, string url, string path, Dictionary<string, string> dictionary,
|
|
|
- ProgressDelegate.ProgressHandler downprogress = null, object downsender = null,
|
|
|
- ProgressDelegate.ProgressHandler releaseprogress = null, object releasesender = null)
|
|
|
+ public int Update(AppUpdateInfo info, string tempPath, Dictionary<string, string> dictionary,
|
|
|
+ ProgressDelegate.ProgressHandler downProgress = null, object downSender = null,
|
|
|
+ ProgressDelegate.ProgressHandler releaseProgress = null, object releaseSender = null)
|
|
|
{
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
stopwatch.Start();
|
|
|
//请求最新版本信息
|
|
|
- AppUpdateInfo info = HttpTool.Get<AppUpdateInfo>(string.Format("{0}?name={1}", url, name));
|
|
|
if (info != null)
|
|
|
{
|
|
|
- Version newVersion = GerVersion(info.Version);
|
|
|
- if (newVersion == null) return -30000;//新版本号格式不正确,解析失败
|
|
|
-
|
|
|
- if (newVersion > version)
|
|
|
+ string file = DirTool.Combine(tempPath, info.Name + info.Version);
|
|
|
+ //准备更新(下载)
|
|
|
+ string downfile = Download(file, info, downProgress, downSender);
|
|
|
+ if (!string.IsNullOrWhiteSpace(downfile) && File.Exists(downfile))
|
|
|
{
|
|
|
- string file = DirTool.Combine(path, name + newVersion.ToString());
|
|
|
- //准备更新(下载)
|
|
|
- string downfile = Download(file, info, downprogress, downsender);
|
|
|
- if (!string.IsNullOrWhiteSpace(downfile) && File.Exists(downfile))
|
|
|
+ //格式化释放文件目录(相对路径转换为绝对路径)
|
|
|
+ string releasepath = AppDirTool.Get(info.ReleasePath, dictionary);
|
|
|
+ //释放文件
|
|
|
+ int unpackCode = 0;
|
|
|
+ if ((unpackCode = FilePackageTool.Unpack(downfile, releasepath, releaseProgress, releaseSender)) > 0)
|
|
|
{
|
|
|
- //格式化释放文件目录
|
|
|
- string releasepath = AppDirTool.Get(info.ReleasePath, dictionary);
|
|
|
- //释放文件
|
|
|
- int unpackCode = 0;
|
|
|
- if ((unpackCode = FilePackageTool.Unpack(downfile, releasepath, releaseprogress, releasesender)) > 0)
|
|
|
- {
|
|
|
- stopwatch.Stop();
|
|
|
- return (int)stopwatch.Elapsed.TotalSeconds;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return -50000 + unpackCode;//文件释放失败
|
|
|
- }
|
|
|
+ stopwatch.Stop();
|
|
|
+ return (int)stopwatch.Elapsed.TotalSeconds;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- return -40000;//文件下载失败
|
|
|
+ return -30000 + unpackCode;//文件释放失败
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- return -10000;//无最新版本,停止操作
|
|
|
+ return -20000;//文件下载失败
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- return -20000;//请求服务器最新版本失败
|
|
|
+ return -10000;//没有新版本
|
|
|
}
|
|
|
}
|
|
|
- private Version GerVersion(string s)
|
|
|
+ private Version FormatVersion(string s)
|
|
|
{
|
|
|
//解析最新版本号
|
|
|
Version version = null;
|