diff --git a/src/MaaWpfGui/Models/ResourceUpdater.cs b/src/MaaWpfGui/Models/ResourceUpdater.cs index a1fe3aa0bb..d3b6d09fe7 100644 --- a/src/MaaWpfGui/Models/ResourceUpdater.cs +++ b/src/MaaWpfGui/Models/ResourceUpdater.cs @@ -13,12 +13,15 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Net.Http; using System.Threading.Tasks; +using System.Windows; using MaaWpfGui.Constants; using MaaWpfGui.Helper; +using MaaWpfGui.ViewModels; using Stylet; namespace MaaWpfGui.Models @@ -186,6 +189,7 @@ namespace MaaWpfGui.Models return UpdateResult.NotModified; } + OutputDownloadProgress("Preparing update……"); var ret1 = await UpdateFilesWithIndex(baseUrl); if (ret1 == UpdateResult.Failed) @@ -217,6 +221,9 @@ namespace MaaWpfGui.Models { UpdateResult ret = UpdateResult.NotModified; + var maxCount = _maaSingleFiles.Count; + var count = 0; + // TODO: 加个文件存这些文件的 hash,如果 hash 没变就不下载了,只需要请求一次 foreach (var file in _maaSingleFiles) { @@ -226,9 +233,11 @@ namespace MaaWpfGui.Models if (sRet == UpdateResult.Failed) { + OutputDownloadProgress(LocalizationHelper.GetString("GameResourceFailed")); ret = UpdateResult.Failed; } + OutputDownloadProgress(2, ++count, maxCount); if (ret == UpdateResult.NotModified && sRet == UpdateResult.Success) { ret = UpdateResult.Success; @@ -256,6 +265,12 @@ namespace MaaWpfGui.Models var ret = UpdateResult.NotModified; var context = File.ReadAllText(indexPath); + var maxCount = context + .Split('\n') + .ToList() + .Where(file => !string.IsNullOrEmpty(file)) + .Count(file => !File.Exists(Path.Combine(Environment.CurrentDirectory, file))); + var count = 0; foreach (var file in context.Split('\n').ToList() .Where(file => !string.IsNullOrEmpty(file)) @@ -268,9 +283,11 @@ namespace MaaWpfGui.Models var sRet = await UpdateFileWithETag(baseUrl, file, file); if (sRet == UpdateResult.Failed) { + OutputDownloadProgress(LocalizationHelper.GetString("GameResourceFailed")); return UpdateResult.Failed; } + OutputDownloadProgress(1, ++count, maxCount); if (ret == UpdateResult.NotModified && sRet == UpdateResult.Success) { ret = UpdateResult.Success; @@ -324,5 +341,44 @@ namespace MaaWpfGui.Models return UpdateResult.Success; } + + private static ObservableCollection _logItemViewModels; + + private static void OutputDownloadProgress(int index, int count = 0, int maxCount = 1) + { + OutputDownloadProgress( + $"index {index}/2: {count}/{maxCount}({100 * count / maxCount}%)"); + } + + private static void OutputDownloadProgress(string output) + { + _logItemViewModels = Instances.TaskQueueViewModel.LogItemViewModels; + if (_logItemViewModels == null) + { + return; + } + + var log = new LogItemViewModel(LocalizationHelper.GetString("GameResourceUpdating") + "\n" + output, UiLogColor.Download); + + Application.Current.Dispatcher.Invoke(() => + { + if (_logItemViewModels.Count > 0 && _logItemViewModels[0].Color == UiLogColor.Download) + { + if (!string.IsNullOrEmpty(output)) + { + _logItemViewModels[0] = log; + } + else + { + _logItemViewModels.RemoveAt(0); + } + } + else if (!string.IsNullOrEmpty(output)) + { + _logItemViewModels.Clear(); + _logItemViewModels.Add(log); + } + }); + } } } diff --git a/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs b/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs index c7b0b25904..3217153d89 100644 --- a/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs @@ -473,8 +473,7 @@ namespace MaaWpfGui.ViewModels.UI { Process.Start(UpdateUrl); } - } - ); + }); _ = Execute.OnUIThreadAsync(() => { using var toast = new ToastNotification((otaFound ? LocalizationHelper.GetString("NewVersionFoundTitle") : LocalizationHelper.GetString("NewVersionFoundButNoPackageTitle")) + " : " + UpdateTag);