From ed09b6549ab8f6cccee7c720614910a763a16d88 Mon Sep 17 00:00:00 2001 From: sabihoshi Date: Fri, 7 May 2021 11:51:00 +0800 Subject: [PATCH] Retrieve the last resolution user has set --- GenshinLauncher/GenshinLauncher.csproj | 2 +- GenshinLauncher/Models/Resolution.cs | 37 ++++++++++++++++++- .../ViewModels/MainWindowViewModel.cs | 16 +++++++- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/GenshinLauncher/GenshinLauncher.csproj b/GenshinLauncher/GenshinLauncher.csproj index 1d16d63..984ccfe 100644 --- a/GenshinLauncher/GenshinLauncher.csproj +++ b/GenshinLauncher/GenshinLauncher.csproj @@ -13,7 +13,7 @@ https://github.com/sabihoshi/GenshinLauncher A Genshin Impact launcher with more options in Modern Fluent UI. GenshinImpactIcon.ico - 1.2.2 + 1.3.0 enable diff --git a/GenshinLauncher/Models/Resolution.cs b/GenshinLauncher/Models/Resolution.cs index 46b38b6..09e1fe8 100644 --- a/GenshinLauncher/Models/Resolution.cs +++ b/GenshinLauncher/Models/Resolution.cs @@ -1,8 +1,10 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Linq; namespace GenshinLauncher.Models { - public class Resolution + public class Resolution : IEquatable { public static List Presets = new() { @@ -32,6 +34,37 @@ public Resolution(int width, int height) public int Width { get; set; } + public bool Equals(Resolution? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Height == other.Height && Width == other.Width; + } + + public static Resolution GetResolution(int width, int height) + { + var other = new Resolution(width, height); + var resolution = Presets.FirstOrDefault(r => r.Equals(other)); + + if (resolution is null) + { + Presets.Add(other); + return other; + } + + return resolution; + } + public override string ToString() => $"{Width}x{Height}"; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != GetType()) return false; + return Equals((Resolution) obj); + } + + public override int GetHashCode() => HashCode.Combine(Height, Width); } } \ No newline at end of file diff --git a/GenshinLauncher/ViewModels/MainWindowViewModel.cs b/GenshinLauncher/ViewModels/MainWindowViewModel.cs index a2595ad..270b3a6 100644 --- a/GenshinLauncher/ViewModels/MainWindowViewModel.cs +++ b/GenshinLauncher/ViewModels/MainWindowViewModel.cs @@ -18,7 +18,7 @@ public class MainWindowViewModel : Screen public MainWindowViewModel() { Quality = new(); - Resolution = Resolution.Presets.Last(); + Resolution = GetLastResolution(); } public Command Client => Cli.Wrap(Location); @@ -90,6 +90,20 @@ public async Task SetLocation() if (!(success && set)) await LocationMissing(); } + private Resolution GetLastResolution() + { + var config = Registry.CurrentUser + .OpenSubKey(@"SOFTWARE\miHoYo\Genshin Impact", false); + + var w = config.GetValue(@"Screenmanager Resolution Width_h182942802"); + var h = config.GetValue(@"Screenmanager Resolution Height_h2627697771"); + + if (w is int width && h is int height) + return Resolution.GetResolution(width, height); + + return Resolution.Presets.Last(); + } + private bool TryGetLocation() { var locations = new[]