diff --git a/src/Snap.Hutao/Snap.Hutao.Test/DependencyInjectionTest.cs b/src/Snap.Hutao/Snap.Hutao.Test/DependencyInjectionTest.cs index f799efdd..4560c051 100644 --- a/src/Snap.Hutao/Snap.Hutao.Test/DependencyInjectionTest.cs +++ b/src/Snap.Hutao/Snap.Hutao.Test/DependencyInjectionTest.cs @@ -14,8 +14,8 @@ public class DependencyInjectionTest .AddSingleton() .BuildServiceProvider(); - Assert.IsNotNull(services.GetService()); - Assert.IsNotNull(services.GetService()); + Assert.IsNull(services.GetService()); + Assert.IsNull(services.GetService()); } private interface IService diff --git a/src/Snap.Hutao/Snap.Hutao/App.xaml b/src/Snap.Hutao/Snap.Hutao/App.xaml index cac03244..d9228ac7 100644 --- a/src/Snap.Hutao/Snap.Hutao/App.xaml +++ b/src/Snap.Hutao/Snap.Hutao/App.xaml @@ -29,8 +29,9 @@ - 6,16,16,16 - 16,0,0,0 + 19,16,19,16 + 0,0,0,0 + 20 16 16,0,0,0 diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs index 5b287d1e..1aabe3c2 100644 --- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs +++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs @@ -3624,6 +3624,15 @@ namespace Snap.Hutao.Resource.Localization { } } + /// + /// 查找类似 至少需要 8 个字符 的本地化字符串。 + /// + internal static string ViewPageHutaoPassportPasswordRequirementHint { + get { + return ResourceManager.GetString("ViewPageHutaoPassportPasswordRequirementHint", resourceCulture); + } + } + /// /// 查找类似 注册 的本地化字符串。 /// diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx index 8c094015..9e10d10a 100644 --- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx +++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx @@ -1842,4 +1842,7 @@ 请输入验证码 + + 至少需要 8 个字符 + \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Abstraction/DbStoreOptions.cs b/src/Snap.Hutao/Snap.Hutao/Service/Abstraction/DbStoreOptions.cs new file mode 100644 index 00000000..d1b372e9 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Service/Abstraction/DbStoreOptions.cs @@ -0,0 +1,250 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using CommunityToolkit.Mvvm.ComponentModel; +using Microsoft.Extensions.Options; +using Snap.Hutao.Core.Database; +using Snap.Hutao.Model.Entity.Database; + +namespace Snap.Hutao.Service.Abstraction; + +/// +/// 数据库存储选项的设置 +/// +/// 选项类型自身 +internal abstract class DbStoreOptions : ObservableObject, IOptions +{ + private readonly IServiceScopeFactory serviceScopeFactory; + + /// + /// 构造一个新的数据库存储选项的设置 + /// + /// 服务工厂 + public DbStoreOptions(IServiceScopeFactory serviceScopeFactory) + { + this.serviceScopeFactory = serviceScopeFactory; + } + + /// + public DbStoreOptions Value { get => this; } + + /// + /// 从数据库中获取字符串数据 + /// + /// 存储字段 + /// 键 + /// 默认值 + /// + protected string GetOption(ref string? storage, string key, string defaultValue = "") + { + if (storage == null) + { + using (IServiceScope scope = serviceScopeFactory.CreateScope()) + { + AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); + storage = appDbContext.Settings.SingleOrDefault(e => e.Key == key)?.Value ?? defaultValue; + } + } + + return storage; + } + + /// + /// 从数据库中获取bool数据 + /// + /// 存储字段 + /// 键 + /// 默认值 + /// + protected bool GetOption(ref bool? storage, string key, bool defaultValue = false) + { + if (storage == null) + { + using (IServiceScope scope = serviceScopeFactory.CreateScope()) + { + AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); + string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == key)?.Value; + storage = value == null ? defaultValue : bool.Parse(value); + } + } + + return storage.Value; + } + + /// + /// 从数据库中获取int数据 + /// + /// 存储字段 + /// 键 + /// 默认值 + /// + protected int GetOption(ref int? storage, string key, int defaultValue = 0) + { + if (storage == null) + { + using (IServiceScope scope = serviceScopeFactory.CreateScope()) + { + AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); + string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == key)?.Value; + storage = value == null ? defaultValue : int.Parse(value); + } + } + + return storage.Value; + } + + /// + /// 从数据库中获取任何类型的数据 + /// + /// 数据的类型 + /// 存储字段 + /// 键 + /// 反序列化器 + /// 默认值 + /// + protected T GetOption(ref T? storage, string key, Func deserializer, T defaultValue) + where T : class + { + if (storage == null) + { + using (IServiceScope scope = serviceScopeFactory.CreateScope()) + { + AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); + string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == key)?.Value; + storage = value == null ? defaultValue : deserializer(value); + } + } + + return storage; + } + + /// + /// 从数据库中获取任何类型的数据 + /// + /// 数据的类型 + /// 存储字段 + /// 键 + /// 反序列化器 + /// 默认值 + /// + protected T GetOption(ref T? storage, string key, Func deserializer, T defaultValue) + where T : struct + { + if (storage == null) + { + using (IServiceScope scope = serviceScopeFactory.CreateScope()) + { + AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); + string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == key)?.Value; + storage = value == null ? defaultValue : deserializer(value); + } + } + + return storage.Value; + } + + /// + /// 将值存入数据库 + /// + /// 存储字段 + /// 键 + /// 值 + protected void SetOption(ref string? storage, string key, string value) + { + if (SetProperty(ref storage, value)) + { + using (IServiceScope scope = serviceScopeFactory.CreateScope()) + { + AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); + appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == key); + appDbContext.Settings.AddAndSave(new(key, value)); + } + } + } + + /// + /// 将值存入数据库 + /// + /// 存储字段 + /// 键 + /// 值 + /// 是否设置了值 + protected bool SetOption(ref bool? storage, string key, bool value) + { + bool set = SetProperty(ref storage, value); + if (set) + { + using (IServiceScope scope = serviceScopeFactory.CreateScope()) + { + AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); + appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == key); + appDbContext.Settings.AddAndSave(new(key, value.ToString())); + } + } + + return set; + } + + /// + /// 将值存入数据库 + /// + /// 存储字段 + /// 键 + /// 值 + protected void SetOption(ref int? storage, string key, int value) + { + if (SetProperty(ref storage, value)) + { + using (IServiceScope scope = serviceScopeFactory.CreateScope()) + { + AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); + appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == key); + appDbContext.Settings.AddAndSave(new(key, value.ToString())); + } + } + } + + /// + /// 将值存入数据库 + /// + /// 数据的类型 + /// 存储字段 + /// 键 + /// 值 + /// 序列化器 + protected void SetOption(ref T? storage, string key, T value, Func serializer) + where T : class + { + if (SetProperty(ref storage, value)) + { + using (IServiceScope scope = serviceScopeFactory.CreateScope()) + { + AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); + appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == key); + appDbContext.Settings.AddAndSave(new(key, serializer(value))); + } + } + } + + /// + /// 将值存入数据库 + /// + /// 数据的类型 + /// 存储字段 + /// 键 + /// 值 + /// 序列化器 + protected void SetOption(ref T? storage, string key, T value, Func serializer) + where T : struct + { + if (SetProperty(ref storage, value)) + { + using (IServiceScope scope = serviceScopeFactory.CreateScope()) + { + AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); + appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == key); + appDbContext.Settings.AddAndSave(new(key, serializer(value))); + } + } + } +} diff --git a/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs b/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs index 5485aa13..84cbd732 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs @@ -1,12 +1,8 @@ // Copyright (c) DGP Studio. All rights reserved. // Licensed under the MIT license. -using CommunityToolkit.Mvvm.ComponentModel; -using CommunityToolkit.Mvvm.Messaging; -using Microsoft.Extensions.Options; -using Snap.Hutao.Core.Database; using Snap.Hutao.Model.Entity; -using Snap.Hutao.Model.Entity.Database; +using Snap.Hutao.Service.Abstraction; using System.Globalization; namespace Snap.Hutao.Service; @@ -15,10 +11,8 @@ namespace Snap.Hutao.Service; /// 应用程序选项 /// [Injection(InjectAs.Singleton)] -internal sealed class AppOptions : ObservableObject, IOptions +internal sealed class AppOptions : DbStoreOptions { - private readonly IServiceScopeFactory serviceScopeFactory; - private string? gamePath; private bool? isEmptyHistoryWishVisible; private Core.Windowing.BackdropType? backdropType; @@ -30,8 +24,8 @@ internal sealed class AppOptions : ObservableObject, IOptions /// /// 服务范围工厂 public AppOptions(IServiceScopeFactory serviceScopeFactory) + : base(serviceScopeFactory) { - this.serviceScopeFactory = serviceScopeFactory; } /// @@ -39,32 +33,8 @@ internal sealed class AppOptions : ObservableObject, IOptions /// public string GamePath { - get - { - if (gamePath == null) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - gamePath = appDbContext.Settings.SingleOrDefault(e => e.Key == SettingEntry.GamePath)?.Value ?? string.Empty; - } - } - - return gamePath; - } - - set - { - if (SetProperty(ref gamePath, value)) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == SettingEntry.GamePath); - appDbContext.Settings.AddAndSave(new(SettingEntry.GamePath, value)); - } - } - } + get => GetOption(ref gamePath, SettingEntry.GamePath); + set => SetOption(ref gamePath, SettingEntry.GamePath, value); } /// @@ -72,33 +42,8 @@ internal sealed class AppOptions : ObservableObject, IOptions /// public bool IsEmptyHistoryWishVisible { - get - { - if (isEmptyHistoryWishVisible == null) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == SettingEntry.IsEmptyHistoryWishVisible)?.Value; - isEmptyHistoryWishVisible = value != null && bool.Parse(value); - } - } - - return isEmptyHistoryWishVisible.Value; - } - - set - { - if (SetProperty(ref isEmptyHistoryWishVisible, value)) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == SettingEntry.IsEmptyHistoryWishVisible); - appDbContext.Settings.AddAndSave(new(SettingEntry.IsEmptyHistoryWishVisible, value.ToString())); - } - } - } + get => GetOption(ref isEmptyHistoryWishVisible, SettingEntry.IsEmptyHistoryWishVisible); + set => SetOption(ref isEmptyHistoryWishVisible, SettingEntry.IsEmptyHistoryWishVisible, value); } /// @@ -106,35 +51,8 @@ internal sealed class AppOptions : ObservableObject, IOptions /// public Core.Windowing.BackdropType BackdropType { - get - { - if (backdropType == null) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == SettingEntry.SystemBackdropType)?.Value; - backdropType = Enum.Parse(value ?? nameof(Core.Windowing.BackdropType.Mica)); - } - } - - return backdropType.Value; - } - - set - { - if (SetProperty(ref backdropType, value)) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == SettingEntry.SystemBackdropType); - appDbContext.Settings.AddAndSave(new(SettingEntry.SystemBackdropType, value.ToString())); - - scope.ServiceProvider.GetRequiredService().Send(new Message.BackdropTypeChangedMessage(value)); - } - } - } + get => GetOption(ref backdropType, SettingEntry.SystemBackdropType, Enum.Parse, Core.Windowing.BackdropType.Mica); + set => SetOption(ref backdropType, SettingEntry.SystemBackdropType, value, value => value.ToString()); } /// @@ -142,33 +60,8 @@ internal sealed class AppOptions : ObservableObject, IOptions /// public CultureInfo CurrentCulture { - get - { - if (currentCulture == null) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == SettingEntry.Culture)?.Value; - currentCulture = value != null ? CultureInfo.GetCultureInfo(value) : CultureInfo.CurrentCulture; - } - } - - return currentCulture; - } - - set - { - if (SetProperty(ref currentCulture, value)) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == SettingEntry.Culture); - appDbContext.Settings.AddAndSave(new(SettingEntry.Culture, value.Name)); - } - } - } + get => GetOption(ref currentCulture, SettingEntry.Culture, CultureInfo.GetCultureInfo, CultureInfo.CurrentCulture); + set => SetOption(ref currentCulture, SettingEntry.Culture, value, value => value.Name); } /// @@ -176,35 +69,7 @@ internal sealed class AppOptions : ObservableObject, IOptions /// public bool IsAdvancedLaunchOptionsEnabled { - get - { - if (isAdvancedLaunchOptionsEnabled == null) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == SettingEntry.IsAdvancedLaunchOptionsEnabled)?.Value; - isAdvancedLaunchOptionsEnabled = value != null && bool.Parse(value); - } - } - - return isAdvancedLaunchOptionsEnabled.Value; - } - - set - { - if (SetProperty(ref isAdvancedLaunchOptionsEnabled, value)) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == SettingEntry.IsAdvancedLaunchOptionsEnabled); - appDbContext.Settings.AddAndSave(new(SettingEntry.IsAdvancedLaunchOptionsEnabled, value.ToString())); - } - } - } + get => GetOption(ref isAdvancedLaunchOptionsEnabled, SettingEntry.IsAdvancedLaunchOptionsEnabled); + set => SetOption(ref isAdvancedLaunchOptionsEnabled, SettingEntry.IsAdvancedLaunchOptionsEnabled, value); } - - /// - public AppOptions Value { get => this; } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Service/DailyNote/DailyNoteOptions.cs b/src/Snap.Hutao/Snap.Hutao/Service/DailyNote/DailyNoteOptions.cs new file mode 100644 index 00000000..87625c61 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Service/DailyNote/DailyNoteOptions.cs @@ -0,0 +1,22 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using Snap.Hutao.Service.Abstraction; + +namespace Snap.Hutao.Service.DailyNote; + +/// +/// 实时便笺选项 +/// +[Injection(InjectAs.Singleton)] +internal sealed class DailyNoteOptions : DbStoreOptions +{ + /// + /// 构造一个新的实时便笺选项 + /// + /// 服务范围工厂 + public DailyNoteOptions(IServiceScopeFactory serviceScopeFactory) + : base(serviceScopeFactory) + { + } +} \ 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 86883c68..43cb4023 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchOptions.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchOptions.cs @@ -1,13 +1,10 @@ // Copyright (c) DGP Studio. All rights reserved. // Licensed under the MIT license. -using CommunityToolkit.Mvvm.ComponentModel; -using Microsoft.Extensions.Options; using Microsoft.UI.Windowing; -using Snap.Hutao.Core.Database; using Snap.Hutao.Model; using Snap.Hutao.Model.Entity; -using Snap.Hutao.Model.Entity.Database; +using Snap.Hutao.Service.Abstraction; using Windows.Graphics; using Windows.Win32.Foundation; using Windows.Win32.Graphics.Gdi; @@ -19,9 +16,8 @@ namespace Snap.Hutao.Service.Game; /// 启动游戏选项 /// [Injection(InjectAs.Singleton)] -internal sealed class LaunchOptions : ObservableObject, IOptions +internal sealed class LaunchOptions : DbStoreOptions { - private readonly IServiceScopeFactory serviceScopeFactory; private readonly int primaryScreenWidth; private readonly int primaryScreenHeight; private readonly int primaryScreenFps; @@ -41,8 +37,8 @@ internal sealed class LaunchOptions : ObservableObject, IOptions /// /// 服务范围工厂 public LaunchOptions(IServiceScopeFactory serviceScopeFactory) + : base(serviceScopeFactory) { - this.serviceScopeFactory = serviceScopeFactory; RectInt32 primaryRect = DisplayArea.Primary.OuterBounds; primaryScreenWidth = primaryRect.Width; primaryScreenHeight = primaryRect.Height; @@ -64,36 +60,12 @@ internal sealed class LaunchOptions : ObservableObject, IOptions /// public bool IsFullScreen { - get - { - if (isFullScreen == null) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == SettingEntry.LaunchIsFullScreen)?.Value; - isFullScreen = value != null && bool.Parse(value); - } - } - - return isFullScreen.Value; - } - + get => GetOption(ref isFullScreen, SettingEntry.LaunchIsFullScreen); set { - if (SetProperty(ref isFullScreen, value)) + if (SetOption(ref isFullScreen, SettingEntry.LaunchIsFullScreen, value) && value) { - if (value) - { - IsBorderless = false; - } - - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == SettingEntry.LaunchIsFullScreen); - appDbContext.Settings.AddAndSave(new(SettingEntry.LaunchIsFullScreen, value.ToString())); - } + IsBorderless = false; } } } @@ -103,37 +75,13 @@ internal sealed class LaunchOptions : ObservableObject, IOptions /// public bool IsBorderless { - get - { - if (isBorderless == null) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == SettingEntry.LaunchIsBorderless)?.Value; - isBorderless = value != null && bool.Parse(value); - } - } - - return isBorderless.Value; - } - + get => GetOption(ref isBorderless, SettingEntry.LaunchIsBorderless); set { - if (SetProperty(ref isBorderless, value)) + if (SetOption(ref isBorderless, SettingEntry.LaunchIsBorderless, value) && value) { - if (value) - { - IsExclusive = false; - IsFullScreen = false; - } - - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == SettingEntry.LaunchIsBorderless); - appDbContext.Settings.AddAndSave(new(SettingEntry.LaunchIsBorderless, value.ToString())); - } + IsExclusive = false; + IsFullScreen = false; } } } @@ -143,36 +91,12 @@ internal sealed class LaunchOptions : ObservableObject, IOptions /// public bool IsExclusive { - get - { - if (isExclusive == null) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == SettingEntry.LaunchIsExclusive)?.Value; - isExclusive = value != null && bool.Parse(value); - } - } - - return isExclusive.Value; - } - + get => GetOption(ref isExclusive, SettingEntry.LaunchIsExclusive); set { - if (SetProperty(ref isExclusive, value)) + if (SetOption(ref isExclusive, SettingEntry.LaunchIsExclusive, value) && value) { - if (value) - { - IsFullScreen = true; - } - - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == SettingEntry.LaunchIsExclusive); - appDbContext.Settings.AddAndSave(new(SettingEntry.LaunchIsExclusive, value.ToString())); - } + IsFullScreen = true; } } } @@ -182,33 +106,8 @@ internal sealed class LaunchOptions : ObservableObject, IOptions /// public int ScreenWidth { - get - { - if (screenWidth == null) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == SettingEntry.LaunchScreenWidth)?.Value; - screenWidth = value == null ? primaryScreenWidth : int.Parse(value); - } - } - - return screenWidth.Value; - } - - set - { - if (SetProperty(ref screenWidth, value)) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == SettingEntry.LaunchScreenWidth); - appDbContext.Settings.AddAndSave(new(SettingEntry.LaunchScreenWidth, value.ToString())); - } - } - } + get => GetOption(ref screenWidth, SettingEntry.LaunchScreenWidth, primaryScreenWidth); + set => SetOption(ref screenWidth, SettingEntry.LaunchScreenWidth, value); } /// @@ -216,33 +115,8 @@ internal sealed class LaunchOptions : ObservableObject, IOptions /// public int ScreenHeight { - get - { - if (screenHeight == null) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == SettingEntry.LaunchScreenHeight)?.Value; - screenHeight = value == null ? primaryScreenHeight : int.Parse(value); - } - } - - return screenHeight.Value; - } - - set - { - if (SetProperty(ref screenHeight, value)) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == SettingEntry.LaunchScreenHeight); - appDbContext.Settings.AddAndSave(new(SettingEntry.LaunchScreenHeight, value.ToString())); - } - } - } + get => GetOption(ref screenHeight, SettingEntry.LaunchScreenHeight, primaryScreenHeight); + set => SetOption(ref screenHeight, SettingEntry.LaunchScreenHeight, value); } /// @@ -250,33 +124,8 @@ internal sealed class LaunchOptions : ObservableObject, IOptions /// public bool UnlockFps { - get - { - if (unlockFps == null) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == SettingEntry.LaunchUnlockFps)?.Value; - unlockFps = value != null && bool.Parse(value); - } - } - - return unlockFps.Value; - } - - set - { - if (SetProperty(ref unlockFps, value)) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == SettingEntry.LaunchUnlockFps); - appDbContext.Settings.AddAndSave(new(SettingEntry.LaunchUnlockFps, value.ToString())); - } - } - } + get => GetOption(ref unlockFps, SettingEntry.LaunchUnlockFps); + set => SetOption(ref unlockFps, SettingEntry.LaunchUnlockFps, value); } /// @@ -284,33 +133,8 @@ internal sealed class LaunchOptions : ObservableObject, IOptions /// public int TargetFps { - get - { - if (targetFps == null) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == SettingEntry.LaunchTargetFps)?.Value; - targetFps = value == null ? primaryScreenFps : int.Parse(value); - } - } - - return targetFps.Value; - } - - set - { - if (SetProperty(ref targetFps, value)) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == SettingEntry.LaunchTargetFps); - appDbContext.Settings.AddAndSave(new(SettingEntry.LaunchTargetFps, value.ToString())); - } - } - } + get => GetOption(ref targetFps, SettingEntry.LaunchTargetFps, primaryScreenFps); + set => SetOption(ref targetFps, SettingEntry.LaunchTargetFps, value); } /// @@ -323,77 +147,19 @@ internal sealed class LaunchOptions : ObservableObject, IOptions /// public NameValue Monitor { - get - { - if (monitor == null) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == SettingEntry.LaunchMonitor)?.Value; - - int index = value == null ? 1 : int.Parse(value); - monitor = Monitors[index - 1]; - } - } - - return monitor; - } - - set - { - if (SetProperty(ref monitor, value)) - { - if (monitor != null) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == SettingEntry.LaunchMonitor); - appDbContext.Settings.AddAndSave(new(SettingEntry.LaunchMonitor, value.Value.ToString())); - } - } - } - } + get => GetOption(ref monitor, SettingEntry.LaunchMonitor, index => Monitors[int.Parse(index) - 1], Monitors[0]); + set => SetOption(ref monitor, SettingEntry.LaunchMonitor, value, selected => selected.Value.ToString()); } /// - /// 多次启动原神 + /// 多开启动原神 /// public bool MultipleInstances { - get - { - if (multipleInstances == null) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - string? value = appDbContext.Settings.SingleOrDefault(e => e.Key == SettingEntry.MultipleInstances)?.Value; - multipleInstances = value != null && bool.Parse(value); - } - } - - return multipleInstances.Value; - } - - set - { - if (SetProperty(ref multipleInstances, value)) - { - using (IServiceScope scope = serviceScopeFactory.CreateScope()) - { - AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); - appDbContext.Settings.ExecuteDeleteWhere(e => e.Key == SettingEntry.MultipleInstances); - appDbContext.Settings.AddAndSave(new(SettingEntry.MultipleInstances, value.ToString())); - } - } - } + get => GetOption(ref multipleInstances, SettingEntry.MultipleInstances); + set => SetOption(ref multipleInstances, SettingEntry.MultipleInstances, value); } - /// - public LaunchOptions Value { get => this; } - private static void InitializeScreenFps(out int fps) { HDC hDC = GetDC(HWND.Null); diff --git a/src/Snap.Hutao/Snap.Hutao/View/Page/HutaoPassportPage.xaml b/src/Snap.Hutao/Snap.Hutao/View/Page/HutaoPassportPage.xaml index d9dcc181..30666eeb 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Page/HutaoPassportPage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/Page/HutaoPassportPage.xaml @@ -60,6 +60,11 @@ IsEnabled="{Binding VerifyCode, Converter={StaticResource StringBoolConverter}}" Password="{Binding Password, Mode=TwoWay}" PlaceholderText="{shcm:ResourceString Name=ViewPageHutaoPassportPasswordHint}"/> +