feat: 游戏资源热更进度显示

#6666
This commit is contained in:
uye
2023-11-09 17:26:52 +08:00
parent 5fa3d8ab5c
commit b5fee2a67b
2 changed files with 57 additions and 2 deletions

View File

@@ -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<LogItemViewModel> _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);
}
});
}
}
}

View File

@@ -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);