Improved CheckUpdate

pull/5876/head
2dust 2024-10-21 20:31:19 +08:00
parent 4fc71fb77e
commit 28aa954f8c
2 changed files with 57 additions and 34 deletions

View File

@ -95,6 +95,7 @@ namespace ServiceLib.Services
} }
else else
{ {
Error?.Invoke(this, new ErrorEventArgs(new Exception("StatusCode error: " + response.StatusCode)));
Logging.SaveLog("StatusCode error: " + url); Logging.SaveLog("StatusCode error: " + url);
return null; return null;
} }

View File

@ -35,19 +35,19 @@ namespace ServiceLib.Services
}; };
_updateFunc?.Invoke(false, string.Format(ResUI.MsgStartUpdating, ECoreType.v2rayN)); _updateFunc?.Invoke(false, string.Format(ResUI.MsgStartUpdating, ECoreType.v2rayN));
var args = await CheckUpdateAsync(downloadHandle, ECoreType.v2rayN, preRelease); var result = await CheckUpdateAsync(downloadHandle, ECoreType.v2rayN, preRelease);
if (args.Success) if (result.Success)
{ {
_updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN)); _updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN));
_updateFunc?.Invoke(false, args.Msg); _updateFunc?.Invoke(false, result.Msg);
url = args.Data?.ToString(); url = result.Data?.ToString();
fileName = Utils.GetTempPath(Utils.GetGuid()); fileName = Utils.GetTempPath(Utils.GetGuid());
await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout); await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout);
} }
else else
{ {
_updateFunc?.Invoke(false, args.Msg); _updateFunc?.Invoke(false, result.Msg);
} }
} }
@ -86,22 +86,22 @@ namespace ServiceLib.Services
}; };
_updateFunc?.Invoke(false, string.Format(ResUI.MsgStartUpdating, type)); _updateFunc?.Invoke(false, string.Format(ResUI.MsgStartUpdating, type));
var args = await CheckUpdateAsync(downloadHandle, type, preRelease); var result = await CheckUpdateAsync(downloadHandle, type, preRelease);
if (args.Success) if (result.Success)
{ {
_updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, type)); _updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, type));
_updateFunc?.Invoke(false, args.Msg); _updateFunc?.Invoke(false, result.Msg);
url = args.Data?.ToString(); url = result.Data?.ToString();
var ext = url.Contains(".tar.gz") ? ".tar.gz" : Path.GetExtension(url); var ext = url.Contains(".tar.gz") ? ".tar.gz" : Path.GetExtension(url);
fileName = Utils.GetTempPath(Utils.GetGuid() + ext); fileName = Utils.GetTempPath(Utils.GetGuid() + ext);
await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout); await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout);
} }
else else
{ {
if (!args.Msg.IsNullOrEmpty()) if (!result.Msg.IsNullOrEmpty())
{ {
_updateFunc?.Invoke(false, args.Msg); _updateFunc?.Invoke(false, result.Msg);
} }
} }
} }
@ -250,24 +250,18 @@ namespace ServiceLib.Services
updateFunc?.Invoke(false, string.Format(ResUI.TestMeOutput, time)); updateFunc?.Invoke(false, string.Format(ResUI.TestMeOutput, time));
} }
#region private #region CheckUpdate private
private async Task<RetResult> CheckUpdateAsync(DownloadService downloadHandle, ECoreType type, bool preRelease) private async Task<RetResult> CheckUpdateAsync(DownloadService downloadHandle, ECoreType type, bool preRelease)
{ {
try try
{ {
var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(type); var result = await GetRemoteVersion(downloadHandle, type, preRelease);
var url = coreInfo?.ReleaseApiUrl; if (!result.Success || result.Data is null)
var result = await downloadHandle.TryDownloadString(url, true, Global.AppName);
if (Utils.IsNotEmpty(result))
{ {
return await ParseDownloadUrl(type, result, preRelease); return result;
}
else
{
return new RetResult(false, "");
} }
return await ParseDownloadUrl(type, (SemanticVersion)result.Data);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -277,9 +271,38 @@ namespace ServiceLib.Services
} }
} }
/// <summary> private async Task<RetResult> GetRemoteVersion(DownloadService downloadHandle, ECoreType type, bool preRelease)
/// 获取Core版本 {
/// </summary> var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(type);
var tagName = string.Empty;
if (preRelease)
{
var url = coreInfo?.ReleaseApiUrl;
var result = await downloadHandle.TryDownloadString(url, true, Global.AppName);
if (Utils.IsNullOrEmpty(result))
{
return new RetResult(false, "");
}
var gitHubReleases = JsonUtils.Deserialize<List<GitHubRelease>>(result);
var gitHubRelease = preRelease ? gitHubReleases?.First() : gitHubReleases?.First(r => r.Prerelease == false);
tagName = gitHubRelease?.TagName;
//var body = gitHubRelease?.Body;
}
else
{
var url = Path.Combine(coreInfo.Url, "latest");
var lastUrl = await downloadHandle.UrlRedirectAsync(url, true);
if (lastUrl == null)
{
return new RetResult(false, "");
}
tagName = lastUrl?.Split("/tag/").LastOrDefault();
}
return new RetResult(true, "", new SemanticVersion(tagName));
}
private async Task<SemanticVersion> GetCoreVersion(ECoreType type) private async Task<SemanticVersion> GetCoreVersion(ECoreType type)
{ {
try try
@ -333,15 +356,10 @@ namespace ServiceLib.Services
} }
} }
private async Task<RetResult> ParseDownloadUrl(ECoreType type, string gitHubReleaseApi, bool preRelease) private async Task<RetResult> ParseDownloadUrl(ECoreType type, SemanticVersion version)
{ {
try try
{ {
var gitHubReleases = JsonUtils.Deserialize<List<GitHubRelease>>(gitHubReleaseApi);
var gitHubRelease = preRelease ? gitHubReleases?.First() : gitHubReleases?.First(r => r.Prerelease == false);
var version = new SemanticVersion(gitHubRelease?.TagName);
var body = gitHubRelease?.Body;
var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(type); var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(type);
SemanticVersion curVersion; SemanticVersion curVersion;
string message; string message;
@ -387,7 +405,7 @@ namespace ServiceLib.Services
return new RetResult(false, message); return new RetResult(false, message);
} }
return new RetResult(true, body, url); return new RetResult(true, "", url);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -405,7 +423,7 @@ namespace ServiceLib.Services
if (coreInfo?.CoreType == ECoreType.v2rayN if (coreInfo?.CoreType == ECoreType.v2rayN
&& File.Exists(Path.Combine(Utils.StartupPath(), "wpfgfx_cor3.dll")) && File.Exists(Path.Combine(Utils.StartupPath(), "wpfgfx_cor3.dll"))
&& File.Exists(Path.Combine(Utils.StartupPath(), "D3DCompiler_47_cor3.dll")) && File.Exists(Path.Combine(Utils.StartupPath(), "D3DCompiler_47_cor3.dll"))
) )
{ {
return coreInfo?.DownloadUrlWin64?.Replace(".zip", "-SelfContained.zip"); return coreInfo?.DownloadUrlWin64?.Replace(".zip", "-SelfContained.zip");
} }
@ -430,6 +448,10 @@ namespace ServiceLib.Services
return null; return null;
} }
#endregion CheckUpdate private
#region Geo private
private async Task UpdateGeoFile(string geoName, Config config, Action<bool, string> updateFunc) private async Task UpdateGeoFile(string geoName, Config config, Action<bool, string> updateFunc)
{ {
_config = config; _config = config;
@ -544,6 +566,6 @@ namespace ServiceLib.Services
await downloadHandle.DownloadFileAsync(url, tmpFileName, true, _timeout); await downloadHandle.DownloadFileAsync(url, tmpFileName, true, _timeout);
} }
#endregion private #endregion Geo private
} }
} }