From 091b79f7cf2d15d8baac881761cf67af4587caa2 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sun, 2 Nov 2025 21:04:23 +0800 Subject: [PATCH] Refactor UpdateService to use instance config and callbacks --- v2rayN/ServiceLib/Common/Utils.cs | 16 +++--- v2rayN/ServiceLib/Manager/TaskManager.cs | 5 +- v2rayN/ServiceLib/Services/UpdateService.cs | 57 +++++++++---------- .../ViewModels/CheckUpdateViewModel.cs | 6 +- .../ViewModels/MainWindowViewModel.cs | 2 +- .../Views/ProfilesSelectWindow.axaml.cs | 6 +- 6 files changed, 45 insertions(+), 47 deletions(-) diff --git a/v2rayN/ServiceLib/Common/Utils.cs b/v2rayN/ServiceLib/Common/Utils.cs index aedd3334..5fb14023 100644 --- a/v2rayN/ServiceLib/Common/Utils.cs +++ b/v2rayN/ServiceLib/Common/Utils.cs @@ -9,7 +9,7 @@ public class Utils { private static readonly string _tag = "Utils"; - #region 转换函数 + #region Conversion Functions /// /// Convert to comma-separated string @@ -462,9 +462,9 @@ public class Utils return (domain, port); } - #endregion 转换函数 + #endregion Conversion Functions - #region 数据检查 + #region Data Checks /// /// Determine if the input is a number @@ -586,9 +586,9 @@ public class Utils return false; } - #endregion 数据检查 + #endregion Data Checks - #region 测速 + #region Speed Test private static bool PortInUse(int port) { @@ -641,9 +641,9 @@ public class Utils return 59090; } - #endregion 测速 + #endregion Speed Test - #region 杂项 + #region Miscellaneous public static bool UpgradeAppExists(out string upgradeFileName) { @@ -793,7 +793,7 @@ public class Utils return null; } - #endregion 杂项 + #endregion Miscellaneous #region TempPath diff --git a/v2rayN/ServiceLib/Manager/TaskManager.cs b/v2rayN/ServiceLib/Manager/TaskManager.cs index 40da7ac7..862afe44 100644 --- a/v2rayN/ServiceLib/Manager/TaskManager.cs +++ b/v2rayN/ServiceLib/Manager/TaskManager.cs @@ -111,11 +111,10 @@ public class TaskManager { Logging.SaveLog("Execute update geo files"); - var updateHandle = new UpdateService(); - await updateHandle.UpdateGeoFileAll(_config, async (success, msg) => + await new UpdateService(_config, async (success, msg) => { await _updateFunc?.Invoke(false, msg); - }); + }).UpdateGeoFileAll(); } } } diff --git a/v2rayN/ServiceLib/Services/UpdateService.cs b/v2rayN/ServiceLib/Services/UpdateService.cs index ac72b01d..f14f0a70 100644 --- a/v2rayN/ServiceLib/Services/UpdateService.cs +++ b/v2rayN/ServiceLib/Services/UpdateService.cs @@ -2,13 +2,19 @@ namespace ServiceLib.Services; public class UpdateService { - private Func? _updateFunc; + private readonly Config? _config; + private readonly Func? _updateFunc; private readonly int _timeout = 30; private static readonly string _tag = "UpdateService"; - public async Task CheckUpdateGuiN(Config config, Func updateFunc, bool preRelease) + public UpdateService(Config config, Func updateFunc) { + _config = config; _updateFunc = updateFunc; + } + + public async Task CheckUpdateGuiN(bool preRelease) + { var url = string.Empty; var fileName = string.Empty; @@ -47,9 +53,8 @@ public class UpdateService } } - public async Task CheckUpdateCore(ECoreType type, Config config, Func updateFunc, bool preRelease) + public async Task CheckUpdateCore(ECoreType type, bool preRelease) { - _updateFunc = updateFunc; var url = string.Empty; var fileName = string.Empty; @@ -101,11 +106,11 @@ public class UpdateService } } - public async Task UpdateGeoFileAll(Config config, Func updateFunc) + public async Task UpdateGeoFileAll() { - await UpdateGeoFiles(config, updateFunc); - await UpdateOtherFiles(config, updateFunc); - await UpdateSrsFileAll(config, updateFunc); + await UpdateGeoFiles(); + await UpdateOtherFiles(); + await UpdateSrsFileAll(); await UpdateFunc(true, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, "geo")); } @@ -330,13 +335,11 @@ public class UpdateService #region Geo private - private async Task UpdateGeoFiles(Config config, Func updateFunc) + private async Task UpdateGeoFiles() { - _updateFunc = updateFunc; - - var geoUrl = string.IsNullOrEmpty(config?.ConstItem.GeoSourceUrl) + var geoUrl = string.IsNullOrEmpty(_config?.ConstItem.GeoSourceUrl) ? Global.GeoUrl - : config.ConstItem.GeoSourceUrl; + : _config.ConstItem.GeoSourceUrl; List files = ["geosite", "geoip"]; foreach (var geoName in files) @@ -345,33 +348,29 @@ public class UpdateService var targetPath = Utils.GetBinPath($"{fileName}"); var url = string.Format(geoUrl, geoName); - await DownloadGeoFile(url, fileName, targetPath, updateFunc); + await DownloadGeoFile(url, fileName, targetPath); } } - private async Task UpdateOtherFiles(Config config, Func updateFunc) + private async Task UpdateOtherFiles() { //If it is not in China area, no update is required - if (config.ConstItem.GeoSourceUrl.IsNotEmpty()) + if (_config.ConstItem.GeoSourceUrl.IsNotEmpty()) { return; } - _updateFunc = updateFunc; - foreach (var url in Global.OtherGeoUrls) { var fileName = Path.GetFileName(url); var targetPath = Utils.GetBinPath($"{fileName}"); - await DownloadGeoFile(url, fileName, targetPath, updateFunc); + await DownloadGeoFile(url, fileName, targetPath); } } - private async Task UpdateSrsFileAll(Config config, Func updateFunc) + private async Task UpdateSrsFileAll() { - _updateFunc = updateFunc; - var geoipFiles = new List(); var geoSiteFiles = new List(); @@ -414,29 +413,29 @@ public class UpdateService } foreach (var item in geoipFiles.Distinct()) { - await UpdateSrsFile("geoip", item, config, updateFunc); + await UpdateSrsFile("geoip", item); } foreach (var item in geoSiteFiles.Distinct()) { - await UpdateSrsFile("geosite", item, config, updateFunc); + await UpdateSrsFile("geosite", item); } } - private async Task UpdateSrsFile(string type, string srsName, Config config, Func updateFunc) + private async Task UpdateSrsFile(string type, string srsName) { - var srsUrl = string.IsNullOrEmpty(config.ConstItem.SrsSourceUrl) + var srsUrl = string.IsNullOrEmpty(_config.ConstItem.SrsSourceUrl) ? Global.SingboxRulesetUrl - : config.ConstItem.SrsSourceUrl; + : _config.ConstItem.SrsSourceUrl; var fileName = $"{type}-{srsName}.srs"; var targetPath = Path.Combine(Utils.GetBinPath("srss"), fileName); var url = string.Format(srsUrl, type, $"{type}-{srsName}", srsName); - await DownloadGeoFile(url, fileName, targetPath, updateFunc); + await DownloadGeoFile(url, fileName, targetPath); } - private async Task DownloadGeoFile(string url, string fileName, string targetPath, Func updateFunc) + private async Task DownloadGeoFile(string url, string fileName, string targetPath) { var tmpFileName = Utils.GetTempPath(Utils.GetGuid()); diff --git a/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs b/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs index f2b39361..d5a1b0c6 100644 --- a/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs @@ -148,7 +148,7 @@ public class CheckUpdateViewModel : MyReactiveObject UpdatedPlusPlus(_geo, ""); } } - await new UpdateService().UpdateGeoFileAll(_config, _updateUI) + await new UpdateService(_config, _updateUI).UpdateGeoFileAll() .ContinueWith(t => UpdatedPlusPlus(_geo, "")); } @@ -163,7 +163,7 @@ public class CheckUpdateViewModel : MyReactiveObject UpdatedPlusPlus(_v2rayN, msg); } } - await new UpdateService().CheckUpdateGuiN(_config, _updateUI, preRelease) + await new UpdateService(_config, _updateUI).CheckUpdateGuiN(preRelease) .ContinueWith(t => UpdatedPlusPlus(_v2rayN, "")); } @@ -180,7 +180,7 @@ public class CheckUpdateViewModel : MyReactiveObject } } var type = (ECoreType)Enum.Parse(typeof(ECoreType), model.CoreType); - await new UpdateService().CheckUpdateCore(type, _config, _updateUI, preRelease) + await new UpdateService(_config, _updateUI).CheckUpdateCore(type, preRelease) .ContinueWith(t => UpdatedPlusPlus(model.CoreType, "")); } diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index d26213fb..525916c8 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -596,7 +596,7 @@ public class MainWindowViewModel : MyReactiveObject AppEvents.RoutingsMenuRefreshRequested.Publish(); await ConfigHandler.SaveConfig(_config); - await new UpdateService().UpdateGeoFileAll(_config, UpdateTaskHandler); + await new UpdateService(_config, UpdateTaskHandler).UpdateGeoFileAll(); await Reload(); } diff --git a/v2rayN/v2rayN.Desktop/Views/ProfilesSelectWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/ProfilesSelectWindow.axaml.cs index b71b61b0..297c9ae0 100644 --- a/v2rayN/v2rayN.Desktop/Views/ProfilesSelectWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/ProfilesSelectWindow.axaml.cs @@ -90,7 +90,7 @@ public partial class ProfilesSelectWindow : WindowBase private void LstProfiles_DoubleTapped(object? sender, TappedEventArgs e) { - // 忽略表头区域的双击 + // Ignore double-taps on the column header area if (e.Source is Control src) { if (src.FindAncestorOfType() != null) @@ -99,7 +99,7 @@ public partial class ProfilesSelectWindow : WindowBase return; } - // 仅当在数据行或其子元素上双击时才触发选择 + // Only trigger selection when double-tapped on a data row or its child element if (src.FindAncestorOfType() != null) { ViewModel?.SelectFinish(); @@ -110,7 +110,7 @@ public partial class ProfilesSelectWindow : WindowBase private void LstProfiles_Sorting(object? sender, DataGridColumnEventArgs e) { - // 自定义排序,防止默认行为导致误触发 + // Custom sort to prevent unintended default behavior e.Handled = true; if (ViewModel != null && e.Column?.Tag?.ToString() != null) {