From c761d8b7ad21392fac95cd9dfe784ec9f19419f9 Mon Sep 17 00:00:00 2001 From: DismissedLight <1686188646@qq.com> Date: Mon, 22 Jul 2024 22:36:31 +0800 Subject: [PATCH] update source selectable --- .../Snap.Hutao/Resource/Localization/SH.resx | 2 +- .../Unlocker/Island/IslandGameFpsUnlocker.cs | 1 - .../Update/HutaoSelectedMirrorInformation.cs | 15 +++ .../Service/Update/IUpdateService.cs | 2 +- .../Service/Update/UpdateService.cs | 101 +++++++++--------- .../UI/Shell/NotifyIconController.cs | 1 - .../UI/Windowing/XamlWindowController.cs | 1 - .../UpdatePackageDownloadConfirmDialog.xaml | 33 ++---- ...UpdatePackageDownloadConfirmDialog.xaml.cs | 11 ++ .../Snap.Hutao/ViewModel/TestViewModel.cs | 5 - .../Snap.Hutao/ViewModel/TitleViewModel.cs | 20 +++- .../Snap.Hutao/Web/ApiOsEndpoints.cs | 1 + .../Web/Hutao/HutaoPackageInformation.cs | 2 +- 13 files changed, 102 insertions(+), 93 deletions(-) create mode 100644 src/Snap.Hutao/Snap.Hutao/Service/Update/HutaoSelectedMirrorInformation.cs diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx index 85960509..d0d94703 100644 --- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx +++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx @@ -1440,7 +1440,7 @@ 如果自动更新下载过慢,可以手动下载并安装 - 下载链接 + 下载源 更新 diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/Island/IslandGameFpsUnlocker.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/Island/IslandGameFpsUnlocker.cs index d9bdf6c7..5bef368f 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/Island/IslandGameFpsUnlocker.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/Island/IslandGameFpsUnlocker.cs @@ -8,7 +8,6 @@ using System.Diagnostics; using System.IO; using System.IO.MemoryMappedFiles; using System.Runtime.InteropServices; -using Windows.Storage; using static Snap.Hutao.Win32.ConstValues; using static Snap.Hutao.Win32.Kernel32; using static Snap.Hutao.Win32.Macros; diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Update/HutaoSelectedMirrorInformation.cs b/src/Snap.Hutao/Snap.Hutao/Service/Update/HutaoSelectedMirrorInformation.cs new file mode 100644 index 00000000..8ccba38d --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Service/Update/HutaoSelectedMirrorInformation.cs @@ -0,0 +1,15 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using Snap.Hutao.Web.Hutao; + +namespace Snap.Hutao.Service.Update; + +internal sealed class HutaoSelectedMirrorInformation +{ + public required Version Version { get; set; } + + public required string Validation { get; set; } + + public required HutaoPackageMirror Mirror { get; set; } +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Update/IUpdateService.cs b/src/Snap.Hutao/Snap.Hutao/Service/Update/IUpdateService.cs index f6de3c3a..0de52ce9 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Update/IUpdateService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Update/IUpdateService.cs @@ -9,7 +9,7 @@ internal interface IUpdateService { ValueTask CheckUpdateAsync(IProgress progress, CancellationToken token = default); - ValueTask DownloadUpdateAsync(CheckUpdateResult checkUpdateResult, IProgress progress, CancellationToken token = default); + ValueTask DownloadUpdateAsync(HutaoSelectedMirrorInformation mirrorInformation, IProgress progress, CancellationToken token = default); LaunchUpdaterResult LaunchUpdater(); } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Update/UpdateService.cs b/src/Snap.Hutao/Snap.Hutao/Service/Update/UpdateService.cs index 5f68301d..ddd3c43b 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Update/UpdateService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Update/UpdateService.cs @@ -82,10 +82,9 @@ internal sealed partial class UpdateService : IUpdateService } } - public ValueTask DownloadUpdateAsync(CheckUpdateResult checkUpdateResult, IProgress progress, CancellationToken token = default) + public ValueTask DownloadUpdateAsync(HutaoSelectedMirrorInformation mirrorInformation, IProgress progress, CancellationToken token = default) { - ArgumentNullException.ThrowIfNull(checkUpdateResult.PackageInformation); - return DownloadUpdatePackageAsync(checkUpdateResult.PackageInformation, GetUpdatePackagePath(), progress, token); + return DownloadUpdatePackageAsync(mirrorInformation, GetUpdatePackagePath(), progress, token); } public LaunchUpdaterResult LaunchUpdater() @@ -131,70 +130,68 @@ internal sealed partial class UpdateService : IUpdateService return runtimeOptions.GetDataFolderUpdateCacheFolderFile("Snap.Hutao.msix"); } - private async ValueTask DownloadUpdatePackageAsync(HutaoPackageInformation versionInformation, string filePath, IProgress progress, CancellationToken token = default) + private async ValueTask DownloadUpdatePackageAsync(HutaoSelectedMirrorInformation mirrorInformation, string filePath, IProgress progress, CancellationToken token = default) { - string version = versionInformation.Version.ToString(); - using IServiceScope scope = serviceProvider.CreateScope(); using HttpClient httpClient = scope.ServiceProvider.GetRequiredService(); - foreach (HutaoPackageMirror mirror in versionInformation.Mirrors) + HutaoPackageMirror mirror = mirrorInformation.Mirror; + string version = mirrorInformation.Version.ToString(); + + try { - try + using HttpResponseMessage responseMessage = await httpClient.GetAsync(mirror.Url, HttpCompletionOption.ResponseHeadersRead, token).ConfigureAwait(false); + long totalBytes = responseMessage.Content.Headers.ContentLength ?? 0; + using Stream webStream = await responseMessage.Content.ReadAsStreamAsync(token).ConfigureAwait(false); + + switch (mirror.MirrorType) { - using HttpResponseMessage responseMessage = await httpClient.GetAsync(mirror.Url, HttpCompletionOption.ResponseHeadersRead, token).ConfigureAwait(false); - long totalBytes = responseMessage.Content.Headers.ContentLength ?? 0; - using Stream webStream = await responseMessage.Content.ReadAsStreamAsync(token).ConfigureAwait(false); + case HutaoPackageMirrorType.Direct: + using (FileStream fileStream = File.Create(filePath)) + { + StreamCopyWorker worker = new(webStream, fileStream, bytesRead => new UpdateStatus(version, bytesRead, totalBytes)); + await worker.CopyAsync(progress).ConfigureAwait(false); + } - switch (mirror.MirrorType) - { - case HutaoPackageMirrorType.Direct: - using (FileStream fileStream = File.Create(filePath)) + break; + case HutaoPackageMirrorType.Archive: + using (TempFileStream tempFileStream = new(FileMode.Create, FileAccess.ReadWrite)) + { + StreamCopyWorker worker = new(webStream, tempFileStream, bytesRead => new UpdateStatus(version, bytesRead, totalBytes)); + await worker.CopyAsync(progress).ConfigureAwait(false); + + using ZipArchive archive = new(tempFileStream); + foreach (ZipArchiveEntry entry in archive.Entries) { - StreamCopyWorker worker = new(webStream, fileStream, bytesRead => new UpdateStatus(version, bytesRead, totalBytes)); - await worker.CopyAsync(progress).ConfigureAwait(false); - } - - break; - case HutaoPackageMirrorType.Archive: - using (TempFileStream tempFileStream = new(FileMode.Create, FileAccess.ReadWrite)) - { - StreamCopyWorker worker = new(webStream, tempFileStream, bytesRead => new UpdateStatus(version, bytesRead, totalBytes)); - await worker.CopyAsync(progress).ConfigureAwait(false); - - using ZipArchive archive = new(tempFileStream); - foreach (ZipArchiveEntry entry in archive.Entries) + if (!entry.FullName.EndsWith(".msix", StringComparison.OrdinalIgnoreCase)) { - if (!entry.FullName.EndsWith(".msix", StringComparison.OrdinalIgnoreCase)) - { - continue; - } - - using Stream entryStream = entry.Open(); - using FileStream fileStream = File.Create(filePath); - await entryStream.CopyToAsync(fileStream, token).ConfigureAwait(false); - break; + continue; } + + using Stream entryStream = entry.Open(); + using FileStream fileStream = File.Create(filePath); + await entryStream.CopyToAsync(fileStream, token).ConfigureAwait(false); + break; } + } - break; - } - - if (!File.Exists(filePath)) - { - return false; - } - - string? remoteHash = versionInformation.Validation; - ArgumentNullException.ThrowIfNull(remoteHash); - if (await CheckUpdateCacheSHA256Async(filePath, remoteHash, token).ConfigureAwait(false)) - { - return true; - } + break; } - catch + + if (!File.Exists(filePath)) { + return false; } + + string? remoteHash = mirrorInformation.Validation; + ArgumentNullException.ThrowIfNull(remoteHash); + if (await CheckUpdateCacheSHA256Async(filePath, remoteHash, token).ConfigureAwait(false)) + { + return true; + } + } + catch + { } return false; diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Shell/NotifyIconController.cs b/src/Snap.Hutao/Snap.Hutao/UI/Shell/NotifyIconController.cs index 7c7eb43f..de7d29bf 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Shell/NotifyIconController.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Shell/NotifyIconController.cs @@ -6,7 +6,6 @@ using Snap.Hutao.Core.ExceptionService; using Snap.Hutao.Core.Graphics; using Snap.Hutao.Win32.Foundation; using Snap.Hutao.Win32.UI.WindowsAndMessaging; -using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Security.Cryptography; diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Windowing/XamlWindowController.cs b/src/Snap.Hutao/Snap.Hutao/UI/Windowing/XamlWindowController.cs index b4d5c89c..1e6ad563 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Windowing/XamlWindowController.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Windowing/XamlWindowController.cs @@ -22,7 +22,6 @@ using Snap.Hutao.UI.Xaml.Media.Backdrop; using Snap.Hutao.Win32.Foundation; using Snap.Hutao.Win32.Graphics.Dwm; using Snap.Hutao.Win32.UI.WindowsAndMessaging; -using System.IO; using Windows.Foundation; using Windows.Graphics; using Windows.UI; diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Dialog/UpdatePackageDownloadConfirmDialog.xaml b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Dialog/UpdatePackageDownloadConfirmDialog.xaml index 48ebf001..4dda6257 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Dialog/UpdatePackageDownloadConfirmDialog.xaml +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Dialog/UpdatePackageDownloadConfirmDialog.xaml @@ -15,12 +15,6 @@ Style="{ThemeResource DefaultContentDialogStyle}" mc:Ignorable="d"> - - 0 - 0 - 0 - - @@ -31,28 +25,15 @@ - - + ItemsSource="{x:Bind Mirrors}" + SelectedItem="{x:Bind SelectedItem}"> + - - - - - - - + - - + + \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Dialog/UpdatePackageDownloadConfirmDialog.xaml.cs b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Dialog/UpdatePackageDownloadConfirmDialog.xaml.cs index 4a68a249..fdd8e874 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Dialog/UpdatePackageDownloadConfirmDialog.xaml.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Dialog/UpdatePackageDownloadConfirmDialog.xaml.cs @@ -7,10 +7,21 @@ using Snap.Hutao.Web.Hutao; namespace Snap.Hutao.UI.Xaml.View.Dialog; [DependencyProperty("Mirrors", typeof(List))] +[DependencyProperty("SelectedItem", typeof(HutaoPackageMirror))] internal sealed partial class UpdatePackageDownloadConfirmDialog : ContentDialog { public UpdatePackageDownloadConfirmDialog() { InitializeComponent(); } + + public async ValueTask> GetSelectedMirrorAsync() + { + if (await ShowAsync() is ContentDialogResult.Primary) + { + return new(true, SelectedItem); + } + + return new(false, default!); + } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/ViewModel/TestViewModel.cs b/src/Snap.Hutao/Snap.Hutao/ViewModel/TestViewModel.cs index cb8587a5..c7871b9d 100644 --- a/src/Snap.Hutao/Snap.Hutao/ViewModel/TestViewModel.cs +++ b/src/Snap.Hutao/Snap.Hutao/ViewModel/TestViewModel.cs @@ -2,7 +2,6 @@ // Licensed under the MIT license. using Microsoft.Extensions.Caching.Memory; -using Microsoft.Graphics.Canvas.Effects; using Microsoft.UI.Xaml.Controls; using Snap.Hutao.Core.Caching; using Snap.Hutao.Core.ExceptionService; @@ -21,10 +20,6 @@ using System.IO; namespace Snap.Hutao.ViewModel; -/// -/// 测试视图模型 -/// -[HighQuality] [ConstructorGenerated] [Injection(InjectAs.Scoped)] internal sealed partial class TestViewModel : Abstraction.ViewModel diff --git a/src/Snap.Hutao/Snap.Hutao/ViewModel/TitleViewModel.cs b/src/Snap.Hutao/Snap.Hutao/ViewModel/TitleViewModel.cs index a3499a49..ed49d379 100644 --- a/src/Snap.Hutao/Snap.Hutao/ViewModel/TitleViewModel.cs +++ b/src/Snap.Hutao/Snap.Hutao/ViewModel/TitleViewModel.cs @@ -15,6 +15,7 @@ using Snap.Hutao.UI.Xaml.Behavior.Action; using Snap.Hutao.UI.Xaml.Control; using Snap.Hutao.UI.Xaml.View.Dialog; using Snap.Hutao.UI.Xaml.View.Window.WebView2; +using Snap.Hutao.Web.Hutao; using System.Globalization; using System.Text; @@ -95,11 +96,22 @@ internal sealed partial class TitleViewModel : Abstraction.ViewModel dialog.Title = SH.FormatViewTitileUpdatePackageDownloadTitle(UpdateStatus?.Version); dialog.Mirrors = checkUpdateResult.PackageInformation?.Mirrors; + dialog.SelectedItem = dialog.Mirrors?.FirstOrDefault(); - if (await dialog.ShowAsync() is ContentDialogResult.Primary) + (bool isOk, HutaoPackageMirror mirror) = await dialog.GetSelectedMirrorAsync().ConfigureAwait(false); + + if (isOk) { + ArgumentNullException.ThrowIfNull(checkUpdateResult.PackageInformation); + HutaoSelectedMirrorInformation mirrorInformation = new() + { + Mirror = mirror, + Validation = checkUpdateResult.PackageInformation.Validation, + Version = checkUpdateResult.PackageInformation.Version, + }; + // This method will set CheckUpdateResult.Kind to NeedInstall if download success - if (!await DownloadPackageAsync(progress, checkUpdateResult).ConfigureAwait(false)) + if (!await DownloadPackageAsync(progress, mirrorInformation, checkUpdateResult).ConfigureAwait(false)) { infoBarService.Warning(SH.ViewTitileUpdatePackageDownloadFailedMessage); return; @@ -139,12 +151,12 @@ internal sealed partial class TitleViewModel : Abstraction.ViewModel UpdateStatus = null; } - private async ValueTask DownloadPackageAsync(IProgress progress, CheckUpdateResult checkUpdateResult) + private async ValueTask DownloadPackageAsync(IProgress progress, HutaoSelectedMirrorInformation mirrorInformation, CheckUpdateResult checkUpdateResult) { bool downloadSuccess = true; try { - if (await updateService.DownloadUpdateAsync(checkUpdateResult, progress).ConfigureAwait(false)) + if (await updateService.DownloadUpdateAsync(mirrorInformation, progress).ConfigureAwait(false)) { checkUpdateResult.Kind = CheckUpdateResultKind.NeedInstall; } diff --git a/src/Snap.Hutao/Snap.Hutao/Web/ApiOsEndpoints.cs b/src/Snap.Hutao/Snap.Hutao/Web/ApiOsEndpoints.cs index 074d171a..62f72b58 100644 --- a/src/Snap.Hutao/Snap.Hutao/Web/ApiOsEndpoints.cs +++ b/src/Snap.Hutao/Snap.Hutao/Web/ApiOsEndpoints.cs @@ -97,6 +97,7 @@ internal static class ApiOsEndpoints public const string CalculateWeaponList = $"{SgPublicApi}/event/calculateos/weapon/list"; public const string CalculateCompute = $"{SgPublicApi}/event/calculateos/compute"; public const string CalculateBatchCompute = $"{SgPublicApi}/event/calculateos/batch_compute"; + public static string CalculateSyncAvatarDetail(in AvatarId avatarId, in PlayerUid uid) { return $"{SgPublicApi}/event/calculateos/sync/avatar/detail?avatar_id={avatarId.Value}&uid={uid.Value}®ion={uid.Region}"; diff --git a/src/Snap.Hutao/Snap.Hutao/Web/Hutao/HutaoPackageInformation.cs b/src/Snap.Hutao/Snap.Hutao/Web/Hutao/HutaoPackageInformation.cs index e118cc10..529e3725 100644 --- a/src/Snap.Hutao/Snap.Hutao/Web/Hutao/HutaoPackageInformation.cs +++ b/src/Snap.Hutao/Snap.Hutao/Web/Hutao/HutaoPackageInformation.cs @@ -9,7 +9,7 @@ internal sealed class HutaoPackageInformation public Version Version { get; set; } = default!; [JsonPropertyName("validation")] - public string? Validation { get; set; } = default!; + public string Validation { get; set; } = default!; [JsonPropertyName("mirrors")] public List Mirrors { get; set; } = default!;