diff --git a/v2rayN/ServiceLib/Manager/TaskManager.cs b/v2rayN/ServiceLib/Manager/TaskManager.cs index d9e5cd76..dfbd242d 100644 --- a/v2rayN/ServiceLib/Manager/TaskManager.cs +++ b/v2rayN/ServiceLib/Manager/TaskManager.cs @@ -92,9 +92,9 @@ public class TaskManager Logging.SaveLog("Execute update geo files"); var updateHandle = new UpdateService(); - await updateHandle.UpdateGeoFileAll(_config, (success, msg) => + await updateHandle.UpdateGeoFileAll(_config, async (success, msg) => { - _updateFunc?.Invoke(false, msg); + await _updateFunc?.Invoke(false, msg); }); } } diff --git a/v2rayN/ServiceLib/Services/UpdateService.cs b/v2rayN/ServiceLib/Services/UpdateService.cs index 95d51d4b..429c0a62 100644 --- a/v2rayN/ServiceLib/Services/UpdateService.cs +++ b/v2rayN/ServiceLib/Services/UpdateService.cs @@ -5,11 +5,11 @@ namespace ServiceLib.Services; public class UpdateService { - private Action? _updateFunc; + private Func? _updateFunc; private readonly int _timeout = 30; private static readonly string _tag = "UpdateService"; - public async Task CheckUpdateGuiN(Config config, Action updateFunc, bool preRelease) + public async Task CheckUpdateGuiN(Config config, Func updateFunc, bool preRelease) { _updateFunc = updateFunc; var url = string.Empty; @@ -20,25 +20,25 @@ public class UpdateService { if (args.Success) { - _updateFunc?.Invoke(false, ResUI.MsgDownloadV2rayCoreSuccessfully); - _updateFunc?.Invoke(true, Utils.UrlEncode(fileName)); + UpdateFunc(false, ResUI.MsgDownloadV2rayCoreSuccessfully); + UpdateFunc(true, Utils.UrlEncode(fileName)); } else { - _updateFunc?.Invoke(false, args.Msg); + UpdateFunc(false, args.Msg); } }; downloadHandle.Error += (sender2, args) => { - _updateFunc?.Invoke(false, args.GetException().Message); + UpdateFunc(false, args.GetException().Message); }; - _updateFunc?.Invoke(false, string.Format(ResUI.MsgStartUpdating, ECoreType.v2rayN)); + await UpdateFunc(false, string.Format(ResUI.MsgStartUpdating, ECoreType.v2rayN)); var result = await CheckUpdateAsync(downloadHandle, ECoreType.v2rayN, preRelease); if (result.Success) { - _updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN)); - _updateFunc?.Invoke(false, result.Msg); + await UpdateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN)); + await UpdateFunc(false, result.Msg); url = result.Data?.ToString(); fileName = Utils.GetTempPath(Utils.GetGuid()); @@ -46,11 +46,11 @@ public class UpdateService } else { - _updateFunc?.Invoke(false, result.Msg); + await UpdateFunc(false, result.Msg); } } - public async Task CheckUpdateCore(ECoreType type, Config config, Action updateFunc, bool preRelease) + public async Task CheckUpdateCore(ECoreType type, Config config, Func updateFunc, bool preRelease) { _updateFunc = updateFunc; var url = string.Empty; @@ -61,34 +61,34 @@ public class UpdateService { if (args.Success) { - _updateFunc?.Invoke(false, ResUI.MsgDownloadV2rayCoreSuccessfully); - _updateFunc?.Invoke(false, ResUI.MsgUnpacking); + UpdateFunc(false, ResUI.MsgDownloadV2rayCoreSuccessfully); + UpdateFunc(false, ResUI.MsgUnpacking); try { - _updateFunc?.Invoke(true, fileName); + UpdateFunc(true, fileName); } catch (Exception ex) { - _updateFunc?.Invoke(false, ex.Message); + UpdateFunc(false, ex.Message); } } else { - _updateFunc?.Invoke(false, args.Msg); + UpdateFunc(false, args.Msg); } }; downloadHandle.Error += (sender2, args) => { - _updateFunc?.Invoke(false, args.GetException().Message); + UpdateFunc(false, args.GetException().Message); }; - _updateFunc?.Invoke(false, string.Format(ResUI.MsgStartUpdating, type)); + await UpdateFunc(false, string.Format(ResUI.MsgStartUpdating, type)); var result = await CheckUpdateAsync(downloadHandle, type, preRelease); if (result.Success) { - _updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, type)); - _updateFunc?.Invoke(false, result.Msg); + await UpdateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, type)); + await UpdateFunc(false, result.Msg); url = result.Data?.ToString(); var ext = url.Contains(".tar.gz") ? ".tar.gz" : Path.GetExtension(url); @@ -99,17 +99,17 @@ public class UpdateService { if (!result.Msg.IsNullOrEmpty()) { - _updateFunc?.Invoke(false, result.Msg); + await UpdateFunc(false, result.Msg); } } } - public async Task UpdateGeoFileAll(Config config, Action updateFunc) + public async Task UpdateGeoFileAll(Config config, Func updateFunc) { await UpdateGeoFiles(config, updateFunc); await UpdateOtherFiles(config, updateFunc); await UpdateSrsFileAll(config, updateFunc); - _updateFunc?.Invoke(true, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, "geo")); + await UpdateFunc(true, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, "geo")); } #region CheckUpdate private @@ -128,7 +128,7 @@ public class UpdateService catch (Exception ex) { Logging.SaveLog(_tag, ex); - _updateFunc?.Invoke(false, ex.Message); + await UpdateFunc(false, ex.Message); return new RetResult(false, ex.Message); } } @@ -212,7 +212,7 @@ public class UpdateService catch (Exception ex) { Logging.SaveLog(_tag, ex); - _updateFunc?.Invoke(false, ex.Message); + await UpdateFunc(false, ex.Message); return new SemanticVersion(""); } } @@ -272,7 +272,7 @@ public class UpdateService catch (Exception ex) { Logging.SaveLog(_tag, ex); - _updateFunc?.Invoke(false, ex.Message); + await UpdateFunc(false, ex.Message); return new RetResult(false, ex.Message); } } @@ -333,7 +333,7 @@ public class UpdateService #region Geo private - private async Task UpdateGeoFiles(Config config, Action updateFunc) + private async Task UpdateGeoFiles(Config config, Func updateFunc) { _updateFunc = updateFunc; @@ -352,7 +352,7 @@ public class UpdateService } } - private async Task UpdateOtherFiles(Config config, Action updateFunc) + private async Task UpdateOtherFiles(Config config, Func updateFunc) { //If it is not in China area, no update is required if (config.ConstItem.GeoSourceUrl.IsNotEmpty()) @@ -371,7 +371,7 @@ public class UpdateService } } - private async Task UpdateSrsFileAll(Config config, Action updateFunc) + private async Task UpdateSrsFileAll(Config config, Func updateFunc) { _updateFunc = updateFunc; @@ -426,7 +426,7 @@ public class UpdateService } } - private async Task UpdateSrsFile(string type, string srsName, Config config, Action updateFunc) + private async Task UpdateSrsFile(string type, string srsName, Config config, Func updateFunc) { var srsUrl = string.IsNullOrEmpty(config.ConstItem.SrsSourceUrl) ? Global.SingboxRulesetUrl @@ -439,7 +439,7 @@ public class UpdateService await DownloadGeoFile(url, fileName, targetPath, updateFunc); } - private async Task DownloadGeoFile(string url, string fileName, string targetPath, Action updateFunc) + private async Task DownloadGeoFile(string url, string fileName, string targetPath, Func updateFunc) { var tmpFileName = Utils.GetTempPath(Utils.GetGuid()); @@ -448,7 +448,7 @@ public class UpdateService { if (args.Success) { - _updateFunc?.Invoke(false, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, fileName)); + UpdateFunc(false, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, fileName)); try { @@ -457,26 +457,31 @@ public class UpdateService File.Copy(tmpFileName, targetPath, true); File.Delete(tmpFileName); - //_updateFunc?.Invoke(true, ""); + //await UpdateFunc(true, ""); } } catch (Exception ex) { - _updateFunc?.Invoke(false, ex.Message); + UpdateFunc(false, ex.Message); } } else { - _updateFunc?.Invoke(false, args.Msg); + UpdateFunc(false, args.Msg); } }; downloadHandle.Error += (sender2, args) => { - _updateFunc?.Invoke(false, args.GetException().Message); + UpdateFunc(false, args.GetException().Message); }; await downloadHandle.DownloadFileAsync(url, tmpFileName, true, _timeout); } #endregion Geo private + + private async Task UpdateFunc(bool notify, string msg) + { + await _updateFunc?.Invoke(notify, msg); + } } diff --git a/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs b/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs index 642149f9..59a3b163 100644 --- a/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs @@ -89,9 +89,11 @@ public class CheckUpdateViewModel : MyReactiveObject { var item = _checkUpdateModel[k]; if (item.IsSelected != true) + { continue; + } - UpdateView(item.CoreType, "..."); + await UpdateView(item.CoreType, "..."); if (item.CoreType == _geo) { await CheckUpdateGeo(); @@ -129,9 +131,9 @@ public class CheckUpdateViewModel : MyReactiveObject private async Task CheckUpdateGeo() { - void _updateUI(bool success, string msg) + async Task _updateUI(bool success, string msg) { - UpdateView(_geo, msg); + await UpdateView(_geo, msg); if (success) { UpdatedPlusPlus(_geo, ""); @@ -146,12 +148,12 @@ public class CheckUpdateViewModel : MyReactiveObject private async Task CheckUpdateN(bool preRelease) { - void _updateUI(bool success, string msg) + async Task _updateUI(bool success, string msg) { - UpdateView(_v2rayN, msg); + await UpdateView(_v2rayN, msg); if (success) { - UpdateView(_v2rayN, ResUI.OperationSuccess); + await UpdateView(_v2rayN, ResUI.OperationSuccess); UpdatedPlusPlus(_v2rayN, msg); } } @@ -164,12 +166,12 @@ public class CheckUpdateViewModel : MyReactiveObject private async Task CheckUpdateCore(CheckUpdateModel model, bool preRelease) { - void _updateUI(bool success, string msg) + async Task _updateUI(bool success, string msg) { - UpdateView(model.CoreType, msg); + await UpdateView(model.CoreType, msg); if (success) { - UpdateView(model.CoreType, ResUI.MsgUpdateV2rayCoreSuccessfullyMore); + await UpdateView(model.CoreType, ResUI.MsgUpdateV2rayCoreSuccessfullyMore); UpdatedPlusPlus(model.CoreType, msg); } @@ -193,7 +195,7 @@ public class CheckUpdateViewModel : MyReactiveObject if (_lstUpdated.Any(x => x.CoreType == _v2rayN && x.IsFinished == true)) { await Task.Delay(1000); - UpgradeN(); + await UpgradeN(); } await Task.Delay(1000); _updateView?.Invoke(EViewAction.DispatcherCheckUpdateFinished, true); @@ -212,7 +214,7 @@ public class CheckUpdateViewModel : MyReactiveObject } } - private void UpgradeN() + private async Task UpgradeN() { try { @@ -223,14 +225,14 @@ public class CheckUpdateViewModel : MyReactiveObject } if (!Utils.UpgradeAppExists(out _)) { - UpdateView(_v2rayN, ResUI.UpgradeAppNotExistTip); + await UpdateView(_v2rayN, ResUI.UpgradeAppNotExistTip); return; } Locator.Current.GetService()?.UpgradeApp(fileName); } catch (Exception ex) { - UpdateView(_v2rayN, ex.Message); + await UpdateView(_v2rayN, ex.Message); } } @@ -281,7 +283,7 @@ public class CheckUpdateViewModel : MyReactiveObject } } - UpdateView(item.CoreType, ResUI.MsgUpdateV2rayCoreSuccessfully); + await UpdateView(item.CoreType, ResUI.MsgUpdateV2rayCoreSuccessfully); if (File.Exists(fileName)) { @@ -290,21 +292,24 @@ public class CheckUpdateViewModel : MyReactiveObject } } - private void UpdateView(string coreType, string msg) + private async Task UpdateView(string coreType, string msg) { var item = new CheckUpdateModel() { CoreType = coreType, Remarks = msg, }; - _updateView?.Invoke(EViewAction.DispatcherCheckUpdate, item); + await _updateView?.Invoke(EViewAction.DispatcherCheckUpdate, item); } public void UpdateViewResult(CheckUpdateModel model) { var found = _checkUpdateModel.FirstOrDefault(t => t.CoreType == model.CoreType); if (found == null) + { return; + } + var itemCopy = JsonUtils.DeepCopy(found); itemCopy.Remarks = model.Remarks; _checkUpdateModel.Replace(found, itemCopy); diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index 281eeda8..7f5d53a4 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -633,7 +633,7 @@ public class MainWindowViewModel : MyReactiveObject Locator.Current.GetService()?.RefreshRoutingsMenu(); await ConfigHandler.SaveConfig(_config); - //TODO await new UpdateService().UpdateGeoFileAll(_config, UpdateTaskHandler); + await new UpdateService().UpdateGeoFileAll(_config, UpdateTaskHandler); await Reload(); }