Improvement check updates

pull/5701/head
2dust 2024-09-04 15:47:17 +08:00
parent fc43a9a726
commit 488e8aab00
5 changed files with 100 additions and 136 deletions

View File

@ -176,7 +176,7 @@ namespace ServiceLib.Common
}; };
using var cts = new CancellationTokenSource(); 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; downloadOpt = null;
} }

View File

@ -58,7 +58,7 @@ namespace ServiceLib.Handler
return 0; 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 try
{ {
@ -74,7 +74,7 @@ namespace ServiceLib.Handler
var webProxy = GetWebProxy(blProxy); var webProxy = GetWebProxy(blProxy);
await DownloaderHelper.Instance.DownloadFileAsync(webProxy, await DownloaderHelper.Instance.DownloadFileAsync(webProxy,
url, url,
Utils.GetTempPath(Utils.GetDownloadFileName(url)), fileName,
progress, progress,
downloadTimeout); downloadTimeout);
} }

View File

@ -9,10 +9,9 @@ namespace ServiceLib.Handler
{ {
private Action<bool, string> _updateFunc; private Action<bool, string> _updateFunc;
private Config _config; private Config _config;
private int _timeout = 30;
public event EventHandler<ResultEventArgs> AbsoluteCompleted; private class ResultEventArgs
public class ResultEventArgs : EventArgs
{ {
public bool Success; public bool Success;
public string Msg; public string Msg;
@ -26,11 +25,12 @@ namespace ServiceLib.Handler
} }
} }
public void CheckUpdateGuiN(Config config, Action<bool, string> update, bool preRelease) public async Task CheckUpdateGuiN(Config config, Action<bool, string> update, bool preRelease)
{ {
_config = config; _config = config;
_updateFunc = update; _updateFunc = update;
var url = string.Empty; var url = string.Empty;
var fileName = string.Empty;
DownloadHandler downloadHandle = new(); DownloadHandler downloadHandle = new();
downloadHandle.UpdateCompleted += (sender2, args) => downloadHandle.UpdateCompleted += (sender2, args) =>
@ -38,9 +38,7 @@ namespace ServiceLib.Handler
if (args.Success) if (args.Success)
{ {
_updateFunc(false, ResUI.MsgDownloadV2rayCoreSuccessfully); _updateFunc(false, ResUI.MsgDownloadV2rayCoreSuccessfully);
string fileName = Utils.GetTempPath(Utils.GetDownloadFileName(url)); _updateFunc(true, Utils.UrlEncode(fileName));
fileName = Utils.UrlEncode(fileName);
_updateFunc(true, fileName);
} }
else else
{ {
@ -50,36 +48,31 @@ namespace ServiceLib.Handler
downloadHandle.Error += (sender2, args) => downloadHandle.Error += (sender2, args) =>
{ {
_updateFunc(false, args.GetException().Message); _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)); _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<bool, string> update, bool preRelease) public async Task CheckUpdateCore(ECoreType type, Config config, Action<bool, string> update, bool preRelease)
{ {
_config = config; _config = config;
_updateFunc = update; _updateFunc = update;
var url = string.Empty; var url = string.Empty;
var fileName = string.Empty;
DownloadHandler downloadHandle = new(); DownloadHandler downloadHandle = new();
downloadHandle.UpdateCompleted += (sender2, args) => downloadHandle.UpdateCompleted += (sender2, args) =>
@ -91,7 +84,7 @@ namespace ServiceLib.Handler
try try
{ {
_updateFunc(true, url); _updateFunc(true, fileName);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -105,31 +98,27 @@ namespace ServiceLib.Handler
}; };
downloadHandle.Error += (sender2, args) => downloadHandle.Error += (sender2, args) =>
{ {
_updateFunc(false, args.GetException().Message); _updateFunc(false, args.GetException().Message);
_updateFunc(false, "");
}; };
AbsoluteCompleted += (sender2, args) =>
{
if (args.Success)
{
_updateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, type));
_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, type)); _updateFunc(false, string.Format(ResUI.MsgStartUpdating, type));
CheckUpdateAsync(downloadHandle, type, preRelease); var args = await CheckUpdateAsync(downloadHandle, type, preRelease);
if (args.Success)
{
_updateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, type));
_updateFunc(false, args.Msg);
url = args.Url;
fileName = Utils.GetTempPath(Utils.GetGUID());
await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout);
}
else
{
if (!args.Msg.IsNullOrEmpty())
{
_updateFunc(false, args.Msg);
}
}
} }
public void UpdateSubscriptionProcess(Config config, string subId, bool blProxy, Action<bool, string> update) public void UpdateSubscriptionProcess(Config config, string subId, bool blProxy, Action<bool, string> update)
@ -265,14 +254,11 @@ namespace ServiceLib.Handler
}); });
} }
public void UpdateGeoFileAll(Config config, Action<bool, string> update) public async Task UpdateGeoFileAll(Config config, Action<bool, string> update)
{ {
Task.Run(async () => await UpdateGeoFile("geosite", _config, update);
{ await UpdateGeoFile("geoip", _config, update);
await UpdateGeoFile("geosite", _config, update); _updateFunc(true, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, "geo"));
await UpdateGeoFile("geoip", _config, update);
_updateFunc(true, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, "geo"));
});
} }
public void RunAvailabilityCheck(Action<bool, string> update) public void RunAvailabilityCheck(Action<bool, string> update)
@ -287,28 +273,28 @@ namespace ServiceLib.Handler
#region private #region private
private async void CheckUpdateAsync(DownloadHandler downloadHandle, ECoreType type, bool preRelease) private async Task<ResultEventArgs> CheckUpdateAsync(DownloadHandler downloadHandle, ECoreType type, bool preRelease)
{ {
try try
{ {
var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(type); var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(type);
string url = coreInfo.coreReleaseApiUrl; var url = coreInfo?.coreReleaseApiUrl;
var result = await downloadHandle.DownloadStringAsync(url, true, Global.AppName); var result = await downloadHandle.DownloadStringAsync(url, true, Global.AppName);
if (!Utils.IsNullOrEmpty(result)) if (!Utils.IsNullOrEmpty(result))
{ {
ResponseHandler(type, result, preRelease); return await ParseDownloadUrl(type, result, preRelease);
} }
else else
{ {
Logging.SaveLog("StatusCode error: " + url); return new ResultEventArgs(false, "");
return;
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Logging.SaveLog(ex.Message, ex); Logging.SaveLog(ex.Message, ex);
_updateFunc(false, ex.Message); _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<ResultEventArgs> ParseDownloadUrl(ECoreType type, string gitHubReleaseApi, bool preRelease)
{ {
try try
{ {
@ -434,16 +420,16 @@ namespace ServiceLib.Handler
if (curVersion >= version && version != new SemanticVersion(0, 0, 0)) if (curVersion >= version && version != new SemanticVersion(0, 0, 0))
{ {
AbsoluteCompleted?.Invoke(this, new ResultEventArgs(false, message)); return new ResultEventArgs(false, message);
return;
} }
AbsoluteCompleted?.Invoke(this, new ResultEventArgs(true, body, url)); return new ResultEventArgs(true, body, url);
} }
catch (Exception ex) catch (Exception ex)
{ {
Logging.SaveLog(ex.Message, ex); Logging.SaveLog(ex.Message, ex);
_updateFunc(false, ex.Message); _updateFunc(false, ex.Message);
return new ResultEventArgs(false, ex.Message);
} }
} }
@ -472,31 +458,12 @@ namespace ServiceLib.Handler
return null; 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<bool, string> update) private async Task UpdateGeoFile(string geoName, Config config, Action<bool, string> update)
{ {
_config = config; _config = config;
_updateFunc = update; _updateFunc = update;
var url = string.Format(Global.GeoUrl, geoName); var url = string.Format(Global.GeoUrl, geoName);
var fileName = Utils.GetTempPath(Utils.GetGUID());
DownloadHandler downloadHandle = new(); DownloadHandler downloadHandle = new();
downloadHandle.UpdateCompleted += (sender2, args) => downloadHandle.UpdateCompleted += (sender2, args) =>
@ -507,7 +474,6 @@ namespace ServiceLib.Handler
try try
{ {
string fileName = Utils.GetTempPath(Utils.GetDownloadFileName(url));
if (File.Exists(fileName)) if (File.Exists(fileName))
{ {
string targetPath = Utils.GetBinPath($"{geoName}.dat"); string targetPath = Utils.GetBinPath($"{geoName}.dat");
@ -531,7 +497,8 @@ namespace ServiceLib.Handler
{ {
_updateFunc(false, args.GetException().Message); _updateFunc(false, args.GetException().Message);
}; };
await AskToDownload(downloadHandle, url, false);
await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout);
} }
#endregion private #endregion private

View File

@ -28,9 +28,13 @@ namespace ServiceLib.ViewModels
RefreshSubItems(); RefreshSubItems();
CheckUpdateCmd = ReactiveCommand.Create(() => CheckUpdateCmd = ReactiveCommand.CreateFromTask(async () =>
{ {
CheckUpdate(); await CheckUpdate()
.ContinueWith(t =>
{
UpdateFinished();
});
}); });
EnableCheckPreReleaseUpdate = _config.guiItem.checkPreReleaseUpdate; EnableCheckPreReleaseUpdate = _config.guiItem.checkPreReleaseUpdate;
IsCheckUpdate = true; IsCheckUpdate = true;
@ -77,9 +81,11 @@ namespace ServiceLib.ViewModels
}); });
} }
private void CheckUpdate() private async Task CheckUpdate()
{ {
_lstUpdated.Clear(); _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--) for (int k = _checkUpdateItem.Count - 1; k >= 0; k--)
{ {
@ -87,23 +93,22 @@ namespace ServiceLib.ViewModels
if (item.isSelected == true) if (item.isSelected == true)
{ {
IsCheckUpdate = false; IsCheckUpdate = false;
_lstUpdated.Add(new CheckUpdateItem() { coreType = item.coreType });
UpdateView(item.coreType, "..."); UpdateView(item.coreType, "...");
if (item.coreType == _geo) if (item.coreType == _geo)
{ {
CheckUpdateGeo(); await CheckUpdateGeo();
} }
else if (item.coreType == ECoreType.v2rayN.ToString()) else if (item.coreType == ECoreType.v2rayN.ToString())
{ {
CheckUpdateN(EnableCheckPreReleaseUpdate); await CheckUpdateN(EnableCheckPreReleaseUpdate);
} }
else if (item.coreType == ECoreType.mihomo.ToString()) else if (item.coreType == ECoreType.mihomo.ToString())
{ {
CheckUpdateCore(item, false); await CheckUpdateCore(item, false);
} }
else 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) void _updateUI(bool success, string msg)
{ {
@ -131,13 +136,16 @@ namespace ServiceLib.ViewModels
if (success) if (success)
{ {
UpdatedPlusPlus(_geo, ""); 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 //Check for standalone windows .Net version
if (Utils.IsWindows() if (Utils.IsWindows()
@ -151,54 +159,38 @@ namespace ServiceLib.ViewModels
void _updateUI(bool success, string msg) void _updateUI(bool success, string msg)
{ {
UpdateView(ECoreType.v2rayN.ToString(), msg);
if (success) if (success)
{ {
UpdateView(ECoreType.v2rayN.ToString(), ResUI.OperationSuccess); UpdateView(ECoreType.v2rayN.ToString(), ResUI.OperationSuccess);
UpdatedPlusPlus(ECoreType.v2rayN.ToString(), msg); 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) void _updateUI(bool success, string msg)
{ {
UpdateView(item.coreType, msg);
if (success) if (success)
{ {
UpdateView(item.coreType, ResUI.MsgUpdateV2rayCoreSuccessfullyMore); UpdateView(item.coreType, ResUI.MsgUpdateV2rayCoreSuccessfullyMore);
UpdatedPlusPlus(item.coreType, msg); 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); 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() private void UpdateFinished()
@ -206,13 +198,15 @@ namespace ServiceLib.ViewModels
if (_lstUpdated.Count > 0 && _lstUpdated.Count(x => x.isFinished == true) == _lstUpdated.Count) if (_lstUpdated.Count > 0 && _lstUpdated.Count(x => x.isFinished == true) == _lstUpdated.Count)
{ {
_updateView?.Invoke(EViewAction.DispatcherCheckUpdateFinished, false); _updateView?.Invoke(EViewAction.DispatcherCheckUpdateFinished, false);
Task.Delay(1000);
UpgradeCore(); UpgradeCore();
if (_lstUpdated.Any(x => x.coreType == ECoreType.v2rayN.ToString() && x.isFinished == true)) if (_lstUpdated.Any(x => x.coreType == ECoreType.v2rayN.ToString() && x.isFinished == true))
{ {
Task.Delay(1000);
UpgradeN(); UpgradeN();
} }
Task.Delay(1000);
_updateView?.Invoke(EViewAction.DispatcherCheckUpdateFinished, true); _updateView?.Invoke(EViewAction.DispatcherCheckUpdateFinished, true);
} }
} }
@ -270,7 +264,7 @@ namespace ServiceLib.ViewModels
continue; continue;
} }
var fileName = Utils.GetTempPath(Utils.GetDownloadFileName(item.fileName)); var fileName = item.fileName;
if (!File.Exists(fileName)) if (!File.Exists(fileName))
{ {
continue; continue;

View File

@ -293,7 +293,10 @@ namespace v2rayN.Views
break; break;
case EViewAction.Shutdown: case EViewAction.Shutdown:
Application.Current.Shutdown(); Application.Current?.Dispatcher.Invoke((() =>
{
Application.Current.Shutdown();
}), DispatcherPriority.Normal);
break; break;
case EViewAction.ScanScreenTask: case EViewAction.ScanScreenTask: