diff --git a/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs b/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs index 106a7d5693..1f28a96240 100644 --- a/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs +++ b/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs @@ -10,6 +10,7 @@ internal sealed partial class SettingEntry { public const string GamePath = "GamePath"; public const string GamePathEntries = "GamePathEntries"; + public const string AspectRatios = "AspectRatios"; public const string Culture = "Culture"; public const string FirstDayOfWeek = "FirstDayOfWeek"; diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/AspectRatio.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/AspectRatio.cs new file mode 100644 index 0000000000..a5596397ca --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/AspectRatio.cs @@ -0,0 +1,49 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +namespace Snap.Hutao.Service.Game; + +internal sealed class AspectRatio : IEquatable +{ + public AspectRatio(double width, double height) + { + Width = width; + Height = height; + } + + [JsonPropertyName("width")] + public double Width { get; } + + [JsonPropertyName("height")] + public double Height { get; } + + public override string ToString() + { + return Width + ":" + Height; + } + + public override int GetHashCode() + { + return HashCode.Combine(Width, Height); + } + + public bool Equals(AspectRatio? other) + { + if (other is null) + { + return false; + } + + if (ReferenceEquals(this, other)) + { + return true; + } + + return Width.Equals(other.Width) && Height.Equals(other.Height); + } + + public override bool Equals(object? obj) + { + return ReferenceEquals(this, obj) || (obj is AspectRatio other && Equals(other)); + } +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchOptions.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchOptions.cs index af9cfb7999..61ac0ddbcc 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchOptions.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchOptions.cs @@ -2,7 +2,6 @@ // Licensed under the MIT license. using CommunityToolkit.Mvvm.Messaging; -using CommunityToolkit.WinUI.Controls; using JetBrains.Annotations; using Microsoft.UI.Windowing; using Snap.Hutao.Model; @@ -369,14 +368,11 @@ public bool HookingMickyWonderPartner2 } [UsedImplicitly] - public ImmutableArray AspectRatios { get; } = - [ - new(3840, 2160), - new(2560, 1600), - new(2560, 1440), - new(2410, 1080), - new(1920, 1080), - ]; + public ImmutableArray AspectRatios + { + get => GetOption(ref fields.AspectRatios, SettingEntry.AspectRatios, raw => JsonSerializer.Deserialize>(raw), []); + set => SetOption(ref fields.AspectRatios, SettingEntry.AspectRatios, value, static v => JsonSerializer.Serialize(v)); + } [UsedImplicitly] public AspectRatio? SelectedAspectRatio @@ -445,6 +441,7 @@ private static ImmutableArray> InitializeMonitors() private struct Fields { public ImmutableArray? GamePathEntries; + public ImmutableArray? AspectRatios; public bool? UsingHoyolabAccount; public bool? AreCommandLineArgumentsEnabled; diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchOptionsExtension.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchOptionsExtension.cs new file mode 100644 index 0000000000..12453b5154 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchOptionsExtension.cs @@ -0,0 +1,27 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using System.Collections.Immutable; + +namespace Snap.Hutao.Service.Game; + +internal static class LaunchOptionsExtension +{ + public static ImmutableArray SaveAspectRatio(this LaunchOptions options, AspectRatio aspectRatio) + { + if (options.AspectRatios.Contains(aspectRatio)) + { + return options.AspectRatios; + } + + options.AspectRatios = options.AspectRatios.Add(aspectRatio); + return options.AspectRatios; + } + + public static ImmutableArray RemoveAspectRatio(this LaunchOptions options, AspectRatio aspectRatio) + { + options.SelectedAspectRatio = default; + options.AspectRatios = options.AspectRatios.Remove(aspectRatio); + return options.AspectRatios; + } +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/Launching/Handler/LaunchExecutionGameProcessInitializationHandler.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/Launching/Handler/LaunchExecutionGameProcessInitializationHandler.cs index 0980144112..ce6a71c442 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Game/Launching/Handler/LaunchExecutionGameProcessInitializationHandler.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/Launching/Handler/LaunchExecutionGameProcessInitializationHandler.cs @@ -40,6 +40,11 @@ private static System.Diagnostics.Process InitializeGameProcess(LaunchExecutionC .AppendIf(launchOptions.UsingCloudThirdPartyMobile, "-platform_type CLOUD_THIRD_PARTY_MOBILE") .AppendIf(launchOptions.UsingHoyolabAccount && !string.IsNullOrEmpty(context.AuthTicket), "login_auth_ticket", context.AuthTicket, CommandLineArgumentSeparator.Equal) .ToString(); + + context.TaskContext.InvokeOnMainThread(() => + { + launchOptions.SaveAspectRatio(new(launchOptions.ScreenWidth, launchOptions.ScreenHeight)); + }); } context.Logger.LogInformation("Command Line Arguments: {commandLine}", commandLine); diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/LaunchGamePage.xaml b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/LaunchGamePage.xaml index 316f978de7..b64576abda 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/LaunchGamePage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/LaunchGamePage.xaml @@ -101,6 +101,32 @@ ToolTipService.ToolTip="{shuxm:ResourceString Name=ViewPageLaunchGameSwitchAccountRemoveToolTip}"/> + + + + + + + + + + + +