mirror of
https://github.com/2dust/v2rayN.git
synced 2025-11-27 02:02:52 +00:00
Refactor update result handling and model
This commit is contained in:
parent
34fc4de0c2
commit
3ff7299aca
3 changed files with 40 additions and 17 deletions
21
v2rayN/ServiceLib/Models/UpdateResult.cs
Normal file
21
v2rayN/ServiceLib/Models/UpdateResult.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
namespace ServiceLib.Models;
|
||||||
|
|
||||||
|
public class UpdateResult
|
||||||
|
{
|
||||||
|
public bool Success { get; set; }
|
||||||
|
public string? Msg { get; set; }
|
||||||
|
public SemanticVersion? Version { get; set; }
|
||||||
|
public string? Url { get; set; }
|
||||||
|
|
||||||
|
public UpdateResult(bool success, string? msg)
|
||||||
|
{
|
||||||
|
Success = success;
|
||||||
|
Msg = msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UpdateResult(bool success, SemanticVersion? version)
|
||||||
|
{
|
||||||
|
Success = success;
|
||||||
|
Version = version;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,7 +7,7 @@ namespace ServiceLib.Services;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class DownloadService
|
public class DownloadService
|
||||||
{
|
{
|
||||||
public event EventHandler<RetResult>? UpdateCompleted;
|
public event EventHandler<UpdateResult>? UpdateCompleted;
|
||||||
|
|
||||||
public event ErrorEventHandler? Error;
|
public event ErrorEventHandler? Error;
|
||||||
|
|
||||||
|
|
@ -40,10 +40,10 @@ public class DownloadService
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
UpdateCompleted?.Invoke(this, new RetResult(false, $"{ResUI.Downloading} {url}"));
|
UpdateCompleted?.Invoke(this, new UpdateResult(false, $"{ResUI.Downloading} {url}"));
|
||||||
|
|
||||||
var progress = new Progress<double>();
|
var progress = new Progress<double>();
|
||||||
progress.ProgressChanged += (sender, value) => UpdateCompleted?.Invoke(this, new RetResult(value > 100, $"...{value}%"));
|
progress.ProgressChanged += (sender, value) => UpdateCompleted?.Invoke(this, new UpdateResult(value > 100, $"...{value}%"));
|
||||||
|
|
||||||
var webProxy = await GetWebProxy(blProxy);
|
var webProxy = await GetWebProxy(blProxy);
|
||||||
await DownloaderHelper.Instance.DownloadFileAsync(webProxy,
|
await DownloaderHelper.Instance.DownloadFileAsync(webProxy,
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ public class UpdateService(Config config, Func<bool, string, Task> updateFunc)
|
||||||
await UpdateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN));
|
await UpdateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN));
|
||||||
await UpdateFunc(false, result.Msg);
|
await UpdateFunc(false, result.Msg);
|
||||||
|
|
||||||
url = result.Data?.ToString();
|
url = result.Url.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);
|
||||||
}
|
}
|
||||||
|
|
@ -86,7 +86,7 @@ public class UpdateService(Config config, Func<bool, string, Task> updateFunc)
|
||||||
await UpdateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, type));
|
await UpdateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, type));
|
||||||
await UpdateFunc(false, result.Msg);
|
await UpdateFunc(false, result.Msg);
|
||||||
|
|
||||||
url = result.Data?.ToString();
|
url = result.Url.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);
|
||||||
|
|
@ -110,26 +110,26 @@ public class UpdateService(Config config, Func<bool, string, Task> updateFunc)
|
||||||
|
|
||||||
#region CheckUpdate private
|
#region CheckUpdate private
|
||||||
|
|
||||||
private async Task<RetResult> CheckUpdateAsync(DownloadService downloadHandle, ECoreType type, bool preRelease)
|
private async Task<UpdateResult> CheckUpdateAsync(DownloadService downloadHandle, ECoreType type, bool preRelease)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var result = await GetRemoteVersion(downloadHandle, type, preRelease);
|
var result = await GetRemoteVersion(downloadHandle, type, preRelease);
|
||||||
if (!result.Success || result.Data is null)
|
if (!result.Success || result.Version is null)
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
return await ParseDownloadUrl(type, (SemanticVersion)result.Data);
|
return await ParseDownloadUrl(type, result);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logging.SaveLog(_tag, ex);
|
Logging.SaveLog(_tag, ex);
|
||||||
await UpdateFunc(false, ex.Message);
|
await UpdateFunc(false, ex.Message);
|
||||||
return new RetResult(false, ex.Message);
|
return new UpdateResult(false, ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<RetResult> GetRemoteVersion(DownloadService downloadHandle, ECoreType type, bool preRelease)
|
private async Task<UpdateResult> GetRemoteVersion(DownloadService downloadHandle, ECoreType type, bool preRelease)
|
||||||
{
|
{
|
||||||
var coreInfo = CoreInfoManager.Instance.GetCoreInfo(type);
|
var coreInfo = CoreInfoManager.Instance.GetCoreInfo(type);
|
||||||
var tagName = string.Empty;
|
var tagName = string.Empty;
|
||||||
|
|
@ -139,7 +139,7 @@ public class UpdateService(Config config, Func<bool, string, Task> updateFunc)
|
||||||
var result = await downloadHandle.TryDownloadString(url, true, Global.AppName);
|
var result = await downloadHandle.TryDownloadString(url, true, Global.AppName);
|
||||||
if (result.IsNullOrEmpty())
|
if (result.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
return new RetResult(false, "");
|
return new UpdateResult(false, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
var gitHubReleases = JsonUtils.Deserialize<List<GitHubRelease>>(result);
|
var gitHubReleases = JsonUtils.Deserialize<List<GitHubRelease>>(result);
|
||||||
|
|
@ -153,12 +153,12 @@ public class UpdateService(Config config, Func<bool, string, Task> updateFunc)
|
||||||
var lastUrl = await downloadHandle.UrlRedirectAsync(url, true);
|
var lastUrl = await downloadHandle.UrlRedirectAsync(url, true);
|
||||||
if (lastUrl == null)
|
if (lastUrl == null)
|
||||||
{
|
{
|
||||||
return new RetResult(false, "");
|
return new UpdateResult(false, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
tagName = lastUrl?.Split("/tag/").LastOrDefault();
|
tagName = lastUrl?.Split("/tag/").LastOrDefault();
|
||||||
}
|
}
|
||||||
return new RetResult(true, "", new SemanticVersion(tagName));
|
return new UpdateResult(true, new SemanticVersion(tagName));
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<SemanticVersion> GetCoreVersion(ECoreType type)
|
private async Task<SemanticVersion> GetCoreVersion(ECoreType type)
|
||||||
|
|
@ -213,10 +213,11 @@ public class UpdateService(Config config, Func<bool, string, Task> updateFunc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<RetResult> ParseDownloadUrl(ECoreType type, SemanticVersion version)
|
private async Task<UpdateResult> ParseDownloadUrl(ECoreType type, UpdateResult result)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var version = result.Version ?? new SemanticVersion(0, 0, 0);
|
||||||
var coreInfo = CoreInfoManager.Instance.GetCoreInfo(type);
|
var coreInfo = CoreInfoManager.Instance.GetCoreInfo(type);
|
||||||
var coreUrl = await GetUrlFromCore(coreInfo) ?? string.Empty;
|
var coreUrl = await GetUrlFromCore(coreInfo) ?? string.Empty;
|
||||||
SemanticVersion curVersion;
|
SemanticVersion curVersion;
|
||||||
|
|
@ -260,16 +261,17 @@ public class UpdateService(Config config, Func<bool, string, Task> updateFunc)
|
||||||
|
|
||||||
if (curVersion >= version && version != new SemanticVersion(0, 0, 0))
|
if (curVersion >= version && version != new SemanticVersion(0, 0, 0))
|
||||||
{
|
{
|
||||||
return new RetResult(false, message);
|
return new UpdateResult(false, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new RetResult(true, "", url);
|
result.Url = url;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logging.SaveLog(_tag, ex);
|
Logging.SaveLog(_tag, ex);
|
||||||
await UpdateFunc(false, ex.Message);
|
await UpdateFunc(false, ex.Message);
|
||||||
return new RetResult(false, ex.Message);
|
return new UpdateResult(false, ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue