From 3ff7299aca2cb52a735dc3884c55e70dc79ee201 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sun, 23 Nov 2025 14:06:34 +0800 Subject: [PATCH] Refactor update result handling and model --- v2rayN/ServiceLib/Models/UpdateResult.cs | 21 +++++++++++++ v2rayN/ServiceLib/Services/DownloadService.cs | 6 ++-- v2rayN/ServiceLib/Services/UpdateService.cs | 30 ++++++++++--------- 3 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 v2rayN/ServiceLib/Models/UpdateResult.cs diff --git a/v2rayN/ServiceLib/Models/UpdateResult.cs b/v2rayN/ServiceLib/Models/UpdateResult.cs new file mode 100644 index 00000000..d8f18dd4 --- /dev/null +++ b/v2rayN/ServiceLib/Models/UpdateResult.cs @@ -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; + } +} diff --git a/v2rayN/ServiceLib/Services/DownloadService.cs b/v2rayN/ServiceLib/Services/DownloadService.cs index fe0da3d6..77d3a7c1 100644 --- a/v2rayN/ServiceLib/Services/DownloadService.cs +++ b/v2rayN/ServiceLib/Services/DownloadService.cs @@ -7,7 +7,7 @@ namespace ServiceLib.Services; /// public class DownloadService { - public event EventHandler? UpdateCompleted; + public event EventHandler? UpdateCompleted; public event ErrorEventHandler? Error; @@ -40,10 +40,10 @@ public class DownloadService { try { - UpdateCompleted?.Invoke(this, new RetResult(false, $"{ResUI.Downloading} {url}")); + UpdateCompleted?.Invoke(this, new UpdateResult(false, $"{ResUI.Downloading} {url}")); var progress = new Progress(); - 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); await DownloaderHelper.Instance.DownloadFileAsync(webProxy, diff --git a/v2rayN/ServiceLib/Services/UpdateService.cs b/v2rayN/ServiceLib/Services/UpdateService.cs index a1d72655..88272bf1 100644 --- a/v2rayN/ServiceLib/Services/UpdateService.cs +++ b/v2rayN/ServiceLib/Services/UpdateService.cs @@ -37,7 +37,7 @@ public class UpdateService(Config config, Func updateFunc) await UpdateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN)); await UpdateFunc(false, result.Msg); - url = result.Data?.ToString(); + url = result.Url.ToString(); fileName = Utils.GetTempPath(Utils.GetGuid()); await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout); } @@ -86,7 +86,7 @@ public class UpdateService(Config config, Func updateFunc) await UpdateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, type)); await UpdateFunc(false, result.Msg); - url = result.Data?.ToString(); + url = result.Url.ToString(); var ext = url.Contains(".tar.gz") ? ".tar.gz" : Path.GetExtension(url); fileName = Utils.GetTempPath(Utils.GetGuid() + ext); await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout); @@ -110,26 +110,26 @@ public class UpdateService(Config config, Func updateFunc) #region CheckUpdate private - private async Task CheckUpdateAsync(DownloadService downloadHandle, ECoreType type, bool preRelease) + private async Task CheckUpdateAsync(DownloadService downloadHandle, ECoreType type, bool preRelease) { try { var result = await GetRemoteVersion(downloadHandle, type, preRelease); - if (!result.Success || result.Data is null) + if (!result.Success || result.Version is null) { return result; } - return await ParseDownloadUrl(type, (SemanticVersion)result.Data); + return await ParseDownloadUrl(type, result); } catch (Exception ex) { Logging.SaveLog(_tag, ex); await UpdateFunc(false, ex.Message); - return new RetResult(false, ex.Message); + return new UpdateResult(false, ex.Message); } } - private async Task GetRemoteVersion(DownloadService downloadHandle, ECoreType type, bool preRelease) + private async Task GetRemoteVersion(DownloadService downloadHandle, ECoreType type, bool preRelease) { var coreInfo = CoreInfoManager.Instance.GetCoreInfo(type); var tagName = string.Empty; @@ -139,7 +139,7 @@ public class UpdateService(Config config, Func updateFunc) var result = await downloadHandle.TryDownloadString(url, true, Global.AppName); if (result.IsNullOrEmpty()) { - return new RetResult(false, ""); + return new UpdateResult(false, ""); } var gitHubReleases = JsonUtils.Deserialize>(result); @@ -153,12 +153,12 @@ public class UpdateService(Config config, Func updateFunc) var lastUrl = await downloadHandle.UrlRedirectAsync(url, true); if (lastUrl == null) { - return new RetResult(false, ""); + return new UpdateResult(false, ""); } tagName = lastUrl?.Split("/tag/").LastOrDefault(); } - return new RetResult(true, "", new SemanticVersion(tagName)); + return new UpdateResult(true, new SemanticVersion(tagName)); } private async Task GetCoreVersion(ECoreType type) @@ -213,10 +213,11 @@ public class UpdateService(Config config, Func updateFunc) } } - private async Task ParseDownloadUrl(ECoreType type, SemanticVersion version) + private async Task ParseDownloadUrl(ECoreType type, UpdateResult result) { try { + var version = result.Version ?? new SemanticVersion(0, 0, 0); var coreInfo = CoreInfoManager.Instance.GetCoreInfo(type); var coreUrl = await GetUrlFromCore(coreInfo) ?? string.Empty; SemanticVersion curVersion; @@ -260,16 +261,17 @@ public class UpdateService(Config config, Func updateFunc) 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) { Logging.SaveLog(_tag, ex); await UpdateFunc(false, ex.Message); - return new RetResult(false, ex.Message); + return new UpdateResult(false, ex.Message); } }