From 93077104b8a7876778f2b0f6ea6f3efe5619dfd2 Mon Sep 17 00:00:00 2001 From: DismissedLight <1686188646@qq.com> Date: Sun, 24 Dec 2023 13:52:06 +0800 Subject: [PATCH] direct set registry value --- .../Model/Entity/SettingEntry.Constant.cs | 3 -- .../Snap.Hutao/Resource/Localization/SH.resx | 2 +- .../Game/Account/GameAccountService.cs | 8 +--- .../Service/Game/Account/RegistryInterop.cs | 46 ++----------------- .../Snap.Hutao/View/Page/SettingPage.xaml | 29 ++++++------ .../ViewModel/Game/LaunchGameViewModel.cs | 2 - .../ViewModel/Game/LaunchGameViewModelSlim.cs | 7 +++ 7 files changed, 28 insertions(+), 69 deletions(-) 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 29249345..37cbdbb3 100644 --- a/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs +++ b/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs @@ -15,9 +15,6 @@ internal sealed partial class SettingEntry public const string GamePathEntries = "GamePathEntries"; - /// - /// PowerShell 路径 - /// public const string PowerShellPath = "PowerShellPath"; /// diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx index b81f3242..1b8cf38a 100644 --- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx +++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx @@ -2436,7 +2436,7 @@ 设置游戏路径时,请选择游戏本体(YuanShen.exe 或 GenshinImpact.exe)而不是启动器(launcher.exe) - 胡桃使用 PowerShell 更改注册表中的信息以修改游戏内账号 + 在创建的快捷方式中启动胡桃需要使用 PowerShell PowerShell 路径 diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/Account/GameAccountService.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/Account/GameAccountService.cs index e3ef96f9..704bb64f 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Game/Account/GameAccountService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/Account/GameAccountService.cs @@ -16,7 +16,6 @@ internal sealed partial class GameAccountService : IGameAccountService private readonly IContentDialogFactory contentDialogFactory; private readonly IGameDbService gameDbService; private readonly ITaskContext taskContext; - private readonly AppOptions appOptions; private ObservableCollection? gameAccounts; @@ -90,12 +89,7 @@ internal sealed partial class GameAccountService : IGameAccountService public bool SetGameAccount(GameAccount account) { - if (string.IsNullOrEmpty(appOptions.PowerShellPath)) - { - ThrowHelper.RuntimeEnvironment(SH.ServiceGameRegisteryInteropPowershellNotFound, default!); - } - - return RegistryInterop.Set(account, appOptions.PowerShellPath); + return RegistryInterop.Set(account); } public void AttachGameAccountToUid(GameAccount gameAccount, string uid) diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/Account/RegistryInterop.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/Account/RegistryInterop.cs index bc145fe9..773b8e64 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Game/Account/RegistryInterop.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/Account/RegistryInterop.cs @@ -20,48 +20,16 @@ internal static class RegistryInterop private const string GenshinKey = $@"HKEY_CURRENT_USER\{GenshinPath}"; private const string SdkChineseKey = "MIHOYOSDK_ADL_PROD_CN_h3123967166"; - /// - /// 设置键值 - /// 需要支持 - /// https://learn.microsoft.com/zh-cn/windows/win32/fileio/maximum-file-path-limitation - /// - /// 账户 - /// PowerShell 路径 - /// 账号是否设置 - public static bool Set(GameAccount? account, string powerShellPath) + public static bool Set(GameAccount? account) { if (account is not null) { // 存回注册表的字节需要 '\0' 结尾 - Encoding.UTF8.GetByteCount(account.MihoyoSDK); - byte[] tempBytes = Encoding.UTF8.GetBytes(account.MihoyoSDK); - byte[] target = new byte[tempBytes.Length + 1]; - tempBytes.CopyTo(target, 0); + byte[] target = [.. Encoding.UTF8.GetBytes(account.MihoyoSDK), 0]; + Registry.SetValue(GenshinKey, SdkChineseKey, target); - string base64 = Convert.ToBase64String(target); - string path = $"HKCU:{GenshinPath}"; - string command = $""" - -Command "$value = [Convert]::FromBase64String('{base64}'); Set-ItemProperty -Path '{path}' -Name '{SdkChineseKey}' -Value $value -Force;" - """; - - ProcessStartInfo startInfo = new() - { - Arguments = command, - WorkingDirectory = Path.GetDirectoryName(powerShellPath), - CreateNoWindow = true, - FileName = powerShellPath, - }; - - try - { - System.Diagnostics.Process.Start(startInfo)?.WaitForExit(); - } - catch (Win32Exception ex) - { - ThrowHelper.RuntimeEnvironment(SH.ServiceGameRegisteryInteropLongPathsDisabled, ex); - } - - if (Get() == account.MihoyoSDK) + string? get = Get(); + if (get == account.MihoyoSDK) { return true; } @@ -70,10 +38,6 @@ internal static class RegistryInterop return false; } - /// - /// 在注册表中获取账号信息 - /// - /// 当前注册表中的信息 public static unsafe string? Get() { object? sdk = Registry.GetValue(GenshinKey, SdkChineseKey, Array.Empty()); diff --git a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml index 6de22392..c554478c 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml @@ -189,7 +189,20 @@ Header="{shcm:ResourceString Name=ViewPageSettingCreateDesktopShortcutHeader}" HeaderIcon="{shcm:FontIcon Glyph=}" IsClickEnabled="True"/> - + + + + + + + + - - - - - - - -