From 488e8aab0029993db2e4b5c3a5ac991f7258c067 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Wed, 4 Sep 2024 15:47:17 +0800 Subject: [PATCH] Improvement check updates --- v2rayN/ServiceLib/Common/DownloaderHelper.cs | 2 +- v2rayN/ServiceLib/Handler/DownloadHandler.cs | 4 +- v2rayN/ServiceLib/Handler/UpdateHandler.cs | 137 +++++++----------- .../ViewModels/CheckUpdateViewModel.cs | 80 +++++----- v2rayN/v2rayN/Views/MainWindow.xaml.cs | 5 +- 5 files changed, 96 insertions(+), 132 deletions(-) diff --git a/v2rayN/ServiceLib/Common/DownloaderHelper.cs b/v2rayN/ServiceLib/Common/DownloaderHelper.cs index b08286e7..ed5867c3 100644 --- a/v2rayN/ServiceLib/Common/DownloaderHelper.cs +++ b/v2rayN/ServiceLib/Common/DownloaderHelper.cs @@ -176,7 +176,7 @@ namespace ServiceLib.Common }; using var cts = new CancellationTokenSource(); - await downloader.DownloadFileTaskAsync(url, fileName, cts.Token).WaitAsync(TimeSpan.FromSeconds(timeout), cts.Token); + await downloader.DownloadFileTaskAsync(url, fileName, cts.Token); downloadOpt = null; } diff --git a/v2rayN/ServiceLib/Handler/DownloadHandler.cs b/v2rayN/ServiceLib/Handler/DownloadHandler.cs index 1674232b..d2811a4e 100644 --- a/v2rayN/ServiceLib/Handler/DownloadHandler.cs +++ b/v2rayN/ServiceLib/Handler/DownloadHandler.cs @@ -58,7 +58,7 @@ namespace ServiceLib.Handler return 0; } - public async Task DownloadFileAsync(string url, bool blProxy, int downloadTimeout) + public async Task DownloadFileAsync(string url, string fileName, bool blProxy, int downloadTimeout) { try { @@ -74,7 +74,7 @@ namespace ServiceLib.Handler var webProxy = GetWebProxy(blProxy); await DownloaderHelper.Instance.DownloadFileAsync(webProxy, url, - Utils.GetTempPath(Utils.GetDownloadFileName(url)), + fileName, progress, downloadTimeout); } diff --git a/v2rayN/ServiceLib/Handler/UpdateHandler.cs b/v2rayN/ServiceLib/Handler/UpdateHandler.cs index 15c49e9e..d025ea00 100644 --- a/v2rayN/ServiceLib/Handler/UpdateHandler.cs +++ b/v2rayN/ServiceLib/Handler/UpdateHandler.cs @@ -9,10 +9,9 @@ namespace ServiceLib.Handler { private Action _updateFunc; private Config _config; + private int _timeout = 30; - public event EventHandler AbsoluteCompleted; - - public class ResultEventArgs : EventArgs + private class ResultEventArgs { public bool Success; public string Msg; @@ -26,11 +25,12 @@ namespace ServiceLib.Handler } } - public void CheckUpdateGuiN(Config config, Action update, bool preRelease) + public async Task CheckUpdateGuiN(Config config, Action update, bool preRelease) { _config = config; _updateFunc = update; var url = string.Empty; + var fileName = string.Empty; DownloadHandler downloadHandle = new(); downloadHandle.UpdateCompleted += (sender2, args) => @@ -38,9 +38,7 @@ namespace ServiceLib.Handler if (args.Success) { _updateFunc(false, ResUI.MsgDownloadV2rayCoreSuccessfully); - string fileName = Utils.GetTempPath(Utils.GetDownloadFileName(url)); - fileName = Utils.UrlEncode(fileName); - _updateFunc(true, fileName); + _updateFunc(true, Utils.UrlEncode(fileName)); } else { @@ -50,36 +48,31 @@ namespace ServiceLib.Handler downloadHandle.Error += (sender2, args) => { _updateFunc(false, args.GetException().Message); - _updateFunc(false, ""); }; - AbsoluteCompleted += (sender2, args) => - { - if (args.Success) - { - _updateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN)); - _updateFunc(false, args.Msg); - url = args.Url; - AskToDownload(downloadHandle, url, true).ContinueWith(task => - { - _updateFunc(false, ""); - }); - } - else - { - _updateFunc(false, args.Msg); - _updateFunc(false, ""); - } - }; _updateFunc(false, string.Format(ResUI.MsgStartUpdating, ECoreType.v2rayN)); - CheckUpdateAsync(downloadHandle, ECoreType.v2rayN, preRelease); + var args = await CheckUpdateAsync(downloadHandle, ECoreType.v2rayN, preRelease); + if (args.Success) + { + _updateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN)); + _updateFunc(false, args.Msg); + + url = args.Url; + fileName = Utils.GetTempPath(Utils.GetGUID()); + await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout); + } + else + { + _updateFunc(false, args.Msg); + } } - public async void CheckUpdateCore(ECoreType type, Config config, Action update, bool preRelease) + public async Task CheckUpdateCore(ECoreType type, Config config, Action update, bool preRelease) { _config = config; _updateFunc = update; var url = string.Empty; + var fileName = string.Empty; DownloadHandler downloadHandle = new(); downloadHandle.UpdateCompleted += (sender2, args) => @@ -91,7 +84,7 @@ namespace ServiceLib.Handler try { - _updateFunc(true, url); + _updateFunc(true, fileName); } catch (Exception ex) { @@ -105,31 +98,27 @@ namespace ServiceLib.Handler }; downloadHandle.Error += (sender2, args) => { - _updateFunc(false, args.GetException().Message); - _updateFunc(false, ""); + _updateFunc(false, args.GetException().Message); }; - AbsoluteCompleted += (sender2, args) => + _updateFunc(false, string.Format(ResUI.MsgStartUpdating, type)); + var args = await CheckUpdateAsync(downloadHandle, type, preRelease); + if (args.Success) { - if (args.Success) - { - _updateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, type)); - _updateFunc(false, args.Msg); + _updateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, type)); + _updateFunc(false, args.Msg); - url = args.Url; - AskToDownload(downloadHandle, url, true).ContinueWith(task => - { - _updateFunc(false, ""); - }); - } - else + url = args.Url; + fileName = Utils.GetTempPath(Utils.GetGUID()); + await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout); + } + else + { + if (!args.Msg.IsNullOrEmpty()) { _updateFunc(false, args.Msg); - _updateFunc(false, ""); } - }; - _updateFunc(false, string.Format(ResUI.MsgStartUpdating, type)); - CheckUpdateAsync(downloadHandle, type, preRelease); + } } public void UpdateSubscriptionProcess(Config config, string subId, bool blProxy, Action update) @@ -265,14 +254,11 @@ namespace ServiceLib.Handler }); } - public void UpdateGeoFileAll(Config config, Action update) + public async Task UpdateGeoFileAll(Config config, Action update) { - Task.Run(async () => - { - await UpdateGeoFile("geosite", _config, update); - await UpdateGeoFile("geoip", _config, update); - _updateFunc(true, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, "geo")); - }); + await UpdateGeoFile("geosite", _config, update); + await UpdateGeoFile("geoip", _config, update); + _updateFunc(true, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, "geo")); } public void RunAvailabilityCheck(Action update) @@ -287,28 +273,28 @@ namespace ServiceLib.Handler #region private - private async void CheckUpdateAsync(DownloadHandler downloadHandle, ECoreType type, bool preRelease) + private async Task CheckUpdateAsync(DownloadHandler downloadHandle, ECoreType type, bool preRelease) { try { var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(type); - string url = coreInfo.coreReleaseApiUrl; + var url = coreInfo?.coreReleaseApiUrl; var result = await downloadHandle.DownloadStringAsync(url, true, Global.AppName); if (!Utils.IsNullOrEmpty(result)) { - ResponseHandler(type, result, preRelease); + return await ParseDownloadUrl(type, result, preRelease); } else { - Logging.SaveLog("StatusCode error: " + url); - return; + return new ResultEventArgs(false, ""); } } catch (Exception ex) { Logging.SaveLog(ex.Message, ex); _updateFunc(false, ex.Message); + return new ResultEventArgs(false, ex.Message); } } @@ -380,7 +366,7 @@ namespace ServiceLib.Handler } } - private void ResponseHandler(ECoreType type, string gitHubReleaseApi, bool preRelease) + private async Task ParseDownloadUrl(ECoreType type, string gitHubReleaseApi, bool preRelease) { try { @@ -434,16 +420,16 @@ namespace ServiceLib.Handler if (curVersion >= version && version != new SemanticVersion(0, 0, 0)) { - AbsoluteCompleted?.Invoke(this, new ResultEventArgs(false, message)); - return; + return new ResultEventArgs(false, message); } - AbsoluteCompleted?.Invoke(this, new ResultEventArgs(true, body, url)); + return new ResultEventArgs(true, body, url); } catch (Exception ex) { Logging.SaveLog(ex.Message, ex); _updateFunc(false, ex.Message); + return new ResultEventArgs(false, ex.Message); } } @@ -472,31 +458,12 @@ namespace ServiceLib.Handler return null; } - private async Task AskToDownload(DownloadHandler downloadHandle, string url, bool blAsk) - { - //bool blDownload = false; - //if (blAsk) - //{ - // if (UI.ShowYesNo(string.Format(ResUI.DownloadYesNo, url)) == MessageBoxResult.Yes) - // { - // blDownload = true; - // } - //} - //else - //{ - // blDownload = true; - //} - //if (blDownload) - //{ - await downloadHandle.DownloadFileAsync(url, true, 60); - //} - } - private async Task UpdateGeoFile(string geoName, Config config, Action update) { _config = config; _updateFunc = update; var url = string.Format(Global.GeoUrl, geoName); + var fileName = Utils.GetTempPath(Utils.GetGUID()); DownloadHandler downloadHandle = new(); downloadHandle.UpdateCompleted += (sender2, args) => @@ -507,7 +474,6 @@ namespace ServiceLib.Handler try { - string fileName = Utils.GetTempPath(Utils.GetDownloadFileName(url)); if (File.Exists(fileName)) { string targetPath = Utils.GetBinPath($"{geoName}.dat"); @@ -531,7 +497,8 @@ namespace ServiceLib.Handler { _updateFunc(false, args.GetException().Message); }; - await AskToDownload(downloadHandle, url, false); + + await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout); } #endregion private diff --git a/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs b/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs index 882b6684..b1e3b798 100644 --- a/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs @@ -28,9 +28,13 @@ namespace ServiceLib.ViewModels RefreshSubItems(); - CheckUpdateCmd = ReactiveCommand.Create(() => + CheckUpdateCmd = ReactiveCommand.CreateFromTask(async () => { - CheckUpdate(); + await CheckUpdate() + .ContinueWith(t => + { + UpdateFinished(); + }); }); EnableCheckPreReleaseUpdate = _config.guiItem.checkPreReleaseUpdate; IsCheckUpdate = true; @@ -77,9 +81,11 @@ namespace ServiceLib.ViewModels }); } - private void CheckUpdate() + private async Task CheckUpdate() { _lstUpdated.Clear(); + _lstUpdated = _checkUpdateItem.Where(x => x.isSelected == true) + .Select(x => new CheckUpdateItem() { coreType = x.coreType }).ToList(); for (int k = _checkUpdateItem.Count - 1; k >= 0; k--) { @@ -87,23 +93,22 @@ namespace ServiceLib.ViewModels if (item.isSelected == true) { IsCheckUpdate = false; - _lstUpdated.Add(new CheckUpdateItem() { coreType = item.coreType }); UpdateView(item.coreType, "..."); if (item.coreType == _geo) { - CheckUpdateGeo(); + await CheckUpdateGeo(); } else if (item.coreType == ECoreType.v2rayN.ToString()) { - CheckUpdateN(EnableCheckPreReleaseUpdate); + await CheckUpdateN(EnableCheckPreReleaseUpdate); } else if (item.coreType == ECoreType.mihomo.ToString()) { - CheckUpdateCore(item, false); + await CheckUpdateCore(item, false); } else { - CheckUpdateCore(item, EnableCheckPreReleaseUpdate); + await CheckUpdateCore(item, EnableCheckPreReleaseUpdate); } } } @@ -123,7 +128,7 @@ namespace ServiceLib.ViewModels } } - private void CheckUpdateGeo() + private async Task CheckUpdateGeo() { void _updateUI(bool success, string msg) { @@ -131,13 +136,16 @@ namespace ServiceLib.ViewModels if (success) { UpdatedPlusPlus(_geo, ""); - UpdateFinished(); } } - (new UpdateHandler()).UpdateGeoFileAll(_config, _updateUI); + await (new UpdateHandler()).UpdateGeoFileAll(_config, _updateUI) + .ContinueWith(t => + { + UpdatedPlusPlus(_geo, ""); + }); } - private void CheckUpdateN(bool preRelease) + private async Task CheckUpdateN(bool preRelease) { //Check for standalone windows .Net version if (Utils.IsWindows() @@ -151,54 +159,38 @@ namespace ServiceLib.ViewModels void _updateUI(bool success, string msg) { + UpdateView(ECoreType.v2rayN.ToString(), msg); if (success) { UpdateView(ECoreType.v2rayN.ToString(), ResUI.OperationSuccess); UpdatedPlusPlus(ECoreType.v2rayN.ToString(), msg); - UpdateFinished(); - } - else - { - if (msg.IsNullOrEmpty()) - { - UpdatedPlusPlus(ECoreType.v2rayN.ToString(), ""); - UpdateFinished(); - } - else - { - UpdateView(ECoreType.v2rayN.ToString(), msg); - } } } - (new UpdateHandler()).CheckUpdateGuiN(_config, _updateUI, preRelease); + await (new UpdateHandler()).CheckUpdateGuiN(_config, _updateUI, preRelease) + .ContinueWith(t => + { + UpdatedPlusPlus(ECoreType.v2rayN.ToString(), ""); + }); } - private void CheckUpdateCore(CheckUpdateItem item, bool preRelease) + private async Task CheckUpdateCore(CheckUpdateItem item, bool preRelease) { void _updateUI(bool success, string msg) { + UpdateView(item.coreType, msg); if (success) { UpdateView(item.coreType, ResUI.MsgUpdateV2rayCoreSuccessfullyMore); UpdatedPlusPlus(item.coreType, msg); - UpdateFinished(); - } - else - { - if (msg.IsNullOrEmpty()) - { - UpdatedPlusPlus(item.coreType, ""); - UpdateFinished(); - } - else - { - UpdateView(item.coreType, msg); - } } } var type = (ECoreType)Enum.Parse(typeof(ECoreType), item.coreType); - (new UpdateHandler()).CheckUpdateCore(type, _config, _updateUI, preRelease); + await (new UpdateHandler()).CheckUpdateCore(type, _config, _updateUI, preRelease) + .ContinueWith(t => + { + UpdatedPlusPlus(item.coreType, ""); + }); } private void UpdateFinished() @@ -206,13 +198,15 @@ namespace ServiceLib.ViewModels if (_lstUpdated.Count > 0 && _lstUpdated.Count(x => x.isFinished == true) == _lstUpdated.Count) { _updateView?.Invoke(EViewAction.DispatcherCheckUpdateFinished, false); - + Task.Delay(1000); UpgradeCore(); if (_lstUpdated.Any(x => x.coreType == ECoreType.v2rayN.ToString() && x.isFinished == true)) { + Task.Delay(1000); UpgradeN(); } + Task.Delay(1000); _updateView?.Invoke(EViewAction.DispatcherCheckUpdateFinished, true); } } @@ -270,7 +264,7 @@ namespace ServiceLib.ViewModels continue; } - var fileName = Utils.GetTempPath(Utils.GetDownloadFileName(item.fileName)); + var fileName = item.fileName; if (!File.Exists(fileName)) { continue; diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml.cs b/v2rayN/v2rayN/Views/MainWindow.xaml.cs index 5aa397c8..8d826d92 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/MainWindow.xaml.cs @@ -293,7 +293,10 @@ namespace v2rayN.Views break; case EViewAction.Shutdown: - Application.Current.Shutdown(); + Application.Current?.Dispatcher.Invoke((() => + { + Application.Current.Shutdown(); + }), DispatcherPriority.Normal); break; case EViewAction.ScanScreenTask: