From 702a3d0e62f32c6d8cadbc095f6ee81e71e1feeb Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Mon, 17 Jun 2024 15:01:36 +0800 Subject: [PATCH] refactor launchgame resources --- .../Service/Game/Scheme/LaunchScheme.cs | 4 +- .../Control/LaunchGameResourceExpander.xaml | 54 +++---------------- .../Snap.Hutao/View/Page/LaunchGamePage.xaml | 12 ++--- .../ViewModel/Game/LaunchGameViewModel.cs | 22 ++++---- .../HoyoPlay/Connect/HoyoPlayClient.cs | 6 +-- .../HoyoPlay/Connect/Package/GamePackages.cs | 10 ++++ .../HoyoPlay/Connect/Package/Package.cs | 6 +++ .../HoyoPlay/Connect/PackageSegment.cs | 20 +++++-- 8 files changed, 62 insertions(+), 72 deletions(-) create mode 100644 src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/Package/GamePackages.cs diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/Scheme/LaunchScheme.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/Scheme/LaunchScheme.cs index 0c98a2fe..1e96ccb9 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Game/Scheme/LaunchScheme.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/Scheme/LaunchScheme.cs @@ -45,9 +45,9 @@ internal class LaunchScheme : IEquatable /// public int LauncherId { get; private protected set; } - public string HoyoPlayLauncherId { get; private protected set; } + public string HoyoPlayLauncherId { get; private protected set; } = default!; - public string HoyoPlayGameId { get; private protected set; } + public string HoyoPlayGameId { get; private protected set; } = default!; /// /// API Key diff --git a/src/Snap.Hutao/Snap.Hutao/View/Control/LaunchGameResourceExpander.xaml b/src/Snap.Hutao/Snap.Hutao/View/Control/LaunchGameResourceExpander.xaml index b937a820..01c448f7 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Control/LaunchGameResourceExpander.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/Control/LaunchGameResourceExpander.xaml @@ -6,12 +6,13 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:shcm="using:Snap.Hutao.Control.Markup" - xmlns:shwhshlr="using:Snap.Hutao.Web.Hoyolab.SdkStatic.Hk4e.Launcher.Resource" + xmlns:shwhhpc="using:Snap.Hutao.Web.Hoyolab.HoyoPlay.Connect" + xmlns:shwhhpcp="using:Snap.Hutao.Web.Hoyolab.HoyoPlay.Connect.Package" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" - d:DataContext="{d:DesignInstance shwhshlr:Snap.Hutao.Web.Hoyolab.SdkStatic.Hk4e.Launcher.Resource.Package}" + d:DataContext="{d:DesignInstance shwhhpcp:Package}" IsExpanded="True" - ItemsSource="{Binding VoicePacks}" + ItemsSource="{Binding AllPackages}" mc:Ignorable="d"> @@ -24,7 +25,7 @@ - + + Text="{Binding Size, Converter={StaticResource FileSizeToFriendlyStringConverter}}"/> + Text="{Binding DecompressedSize, Converter={StaticResource FileSizeToFriendlyStringConverter}}"/> - - - - - - - - - - - - - - - - diff --git a/src/Snap.Hutao/Snap.Hutao/View/Page/LaunchGamePage.xaml b/src/Snap.Hutao/Snap.Hutao/View/Page/LaunchGamePage.xaml index ac7d718c..307c3cac 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Page/LaunchGamePage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/Page/LaunchGamePage.xaml @@ -366,13 +366,13 @@ - + @@ -382,22 +382,22 @@ + ItemsSource="{Binding GamePackage.PreDownload.Patches, Mode=OneWay}"/> + ItemsSource="{Binding GamePackage.Main.Patches, Mode=OneWay}"/> - + diff --git a/src/Snap.Hutao/Snap.Hutao/ViewModel/Game/LaunchGameViewModel.cs b/src/Snap.Hutao/Snap.Hutao/ViewModel/Game/LaunchGameViewModel.cs index 79af1cd2..745a2202 100644 --- a/src/Snap.Hutao/Snap.Hutao/ViewModel/Game/LaunchGameViewModel.cs +++ b/src/Snap.Hutao/Snap.Hutao/ViewModel/Game/LaunchGameViewModel.cs @@ -15,8 +15,8 @@ using Snap.Hutao.Service.Game.PathAbstraction; using Snap.Hutao.Service.Game.Scheme; using Snap.Hutao.Service.Notification; using Snap.Hutao.Service.User; -using Snap.Hutao.Web.Hoyolab.SdkStatic.Hk4e.Launcher; -using Snap.Hutao.Web.Hoyolab.SdkStatic.Hk4e.Launcher.Resource; +using Snap.Hutao.Web.Hoyolab.HoyoPlay.Connect; +using Snap.Hutao.Web.Hoyolab.HoyoPlay.Connect.Package; using System.Collections.Immutable; using System.IO; @@ -39,7 +39,6 @@ internal sealed partial class LaunchGameViewModel : Abstraction.ViewModel, IView private readonly IGameLocatorFactory gameLocatorFactory; private readonly LaunchGameShared launchGameShared; private readonly IInfoBarService infoBarService; - private readonly ResourceClient resourceClient; private readonly RuntimeOptions runtimeOptions; private readonly LaunchOptions launchOptions; private readonly IUserService userService; @@ -47,11 +46,12 @@ internal sealed partial class LaunchGameViewModel : Abstraction.ViewModel, IView private readonly IGameServiceFacade gameService; private readonly IMemoryCache memoryCache; private readonly AppOptions appOptions; + private readonly HoyoPlayClient hoyoPlayClient; private LaunchScheme? selectedScheme; private AdvancedCollectionView? gameAccountsView; private GameAccount? selectedGameAccount; - private GameResource? gameResource; + private GamePackage? gamePackage; private bool gamePathSelectedAndValid; private ImmutableList gamePathEntries; private GamePathEntry? selectedGamePathEntry; @@ -69,7 +69,7 @@ internal sealed partial class LaunchGameViewModel : Abstraction.ViewModel, IView public List KnownSchemes { get; } = KnownLaunchSchemes.Get(); - [AlsoAsyncSets(nameof(GameResource), nameof(GameAccountsView))] + [AlsoAsyncSets(nameof(GameAccountsView), nameof(GamePackage))] public LaunchScheme? SelectedScheme { get => selectedScheme; @@ -80,7 +80,7 @@ internal sealed partial class LaunchGameViewModel : Abstraction.ViewModel, IView public GameAccount? SelectedGameAccount { get => selectedGameAccount; set => SetProperty(ref selectedGameAccount, value); } - public GameResource? GameResource { get => gameResource; set => SetProperty(ref gameResource, value); } + public GamePackage? GamePackage { get => gamePackage; set => SetProperty(ref gamePackage, value); } public bool GamePathSelectedAndValid { @@ -301,10 +301,10 @@ internal sealed partial class LaunchGameViewModel : Abstraction.ViewModel, IView SelectedGameAccount = default; await UpdateGameAccountsViewAsync().ConfigureAwait(false); - UpdateGameResourceAsync(value).SafeForget(); + UpdateGamePackageAsync(value).SafeForget(); } - async ValueTask UpdateGameResourceAsync(LaunchScheme? scheme) + async ValueTask UpdateGamePackageAsync(LaunchScheme? scheme) { if (scheme is null) { @@ -312,14 +312,14 @@ internal sealed partial class LaunchGameViewModel : Abstraction.ViewModel, IView } await taskContext.SwitchToBackgroundAsync(); - Web.Response.Response response = await resourceClient - .GetResourceAsync(scheme) + Web.Response.Response response = await hoyoPlayClient + .GetPackagesAsync(scheme) .ConfigureAwait(false); if (response.IsOk()) { await taskContext.SwitchToMainThreadAsync(); - GameResource = response.Data; + GamePackage = response.Data.Packages.Single(); } } diff --git a/src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/HoyoPlayClient.cs b/src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/HoyoPlayClient.cs index cd9738e5..18f31518 100644 --- a/src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/HoyoPlayClient.cs +++ b/src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/HoyoPlayClient.cs @@ -21,7 +21,7 @@ internal sealed partial class HoyoPlayClient private readonly HttpClient httpClient; private readonly ILogger logger; - public async ValueTask> GetPackagesAsync(LaunchScheme scheme, CancellationToken token = default) + public async ValueTask> GetPackagesAsync(LaunchScheme scheme, CancellationToken token = default) { string url = scheme.IsOversea ? ApiOsEndpoints.SgHoyoPlayConnectGamePackages(scheme) @@ -31,8 +31,8 @@ internal sealed partial class HoyoPlayClient .SetRequestUri(url) .Get(); - Response? resp = await builder - .SendAsync>(httpClient, logger, token) + Response? resp = await builder + .SendAsync>(httpClient, logger, token) .ConfigureAwait(false); return Response.Response.DefaultIfNull(resp); diff --git a/src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/Package/GamePackages.cs b/src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/Package/GamePackages.cs new file mode 100644 index 00000000..1fa71867 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/Package/GamePackages.cs @@ -0,0 +1,10 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +namespace Snap.Hutao.Web.Hoyolab.HoyoPlay.Connect.Package; + +internal sealed class GamePackages +{ + [JsonPropertyName("game_packages")] + public List Packages { get; set; } = default!; +} diff --git a/src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/Package/Package.cs b/src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/Package/Package.cs index 6cf3592c..e70dac60 100644 --- a/src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/Package/Package.cs +++ b/src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/Package/Package.cs @@ -16,4 +16,10 @@ internal sealed class Package [JsonPropertyName("res_list_url")] public string ResListUrl { get; set; } = default!; + + [JsonIgnore] + public List AllPackages + { + get => [.. GamePackages, .. AudioPackages]; + } } diff --git a/src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/PackageSegment.cs b/src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/PackageSegment.cs index 42a828c6..5bc6be1f 100644 --- a/src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/PackageSegment.cs +++ b/src/Snap.Hutao/Snap.Hutao/Web/Hoyolab/HoyoPlay/Connect/PackageSegment.cs @@ -1,9 +1,12 @@ // Copyright (c) DGP Studio. All rights reserved. // Licensed under the MIT license. +using Snap.Hutao.Core.IO.DataTransfer; +using Snap.Hutao.Service.Notification; + namespace Snap.Hutao.Web.Hoyolab.HoyoPlay.Connect; -internal class PackageSegment +internal partial class PackageSegment { [JsonPropertyName("url")] public string Url { get; set; } = default!; @@ -12,8 +15,19 @@ internal class PackageSegment public string Md5 { get; set; } = default!; [JsonPropertyName("size")] - public string Size { get; set; } = default!; + public long Size { get; set; } = default!; [JsonPropertyName("decompressed_size")] - public string DecompressedSize { get; set; } = default!; + public long DecompressedSize { get; set; } = default!; + + [JsonIgnore] + public string DisplayName { get => System.IO.Path.GetFileName(Url); } + + [Command("CopyPathCommand")] + private void CopyPathToClipboard() + { + IServiceProvider serviceProvider = Ioc.Default; + serviceProvider.GetRequiredService().SetText(Url); + serviceProvider.GetRequiredService().Success(SH.WebGameResourcePathCopySucceed); + } }