This commit is contained in:
DismissedLight
2023-09-23 21:02:11 +08:00
parent 4fb997c8d0
commit 7d9612cf13

View File

@@ -6,6 +6,7 @@ using Snap.Hutao.Core.ExceptionService;
using Snap.Hutao.Model.Entity; using Snap.Hutao.Model.Entity;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
namespace Snap.Hutao.Service.Game; namespace Snap.Hutao.Service.Game;
@@ -31,8 +32,14 @@ internal static class RegistryInterop
{ {
if (account is not null) if (account is not null)
{ {
string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(account.MihoyoSDK)); // 存回注册表的字节需要 '\0' 结尾
string path = $"HKCU:{GenshinKey[@"HKEY_CURRENT_USER\".Length..]}"; Encoding.UTF8.GetByteCount(account.MihoyoSDK);
byte[] tempBytes = Encoding.UTF8.GetBytes(account.MihoyoSDK);
byte[] target = new byte[tempBytes.Length + 1];
tempBytes.CopyTo(target, 0);
string base64 = Convert.ToBase64String(target);
string path = $"HKCU:{GenshinPath}";
string command = $""" string command = $"""
$value = [Convert]::FromBase64String('{base64}'); $value = [Convert]::FromBase64String('{base64}');
Set-ItemProperty -Path '{path}' -Name '{SdkChineseKey}' -Value $value -Force; Set-ItemProperty -Path '{path}' -Name '{SdkChineseKey}' -Value $value -Force;
@@ -68,13 +75,18 @@ internal static class RegistryInterop
/// 在注册表中获取账号信息 /// 在注册表中获取账号信息
/// </summary> /// </summary>
/// <returns>当前注册表中的信息</returns> /// <returns>当前注册表中的信息</returns>
public static string? Get() public static unsafe string? Get()
{ {
object? sdk = Registry.GetValue(GenshinKey, SdkChineseKey, Array.Empty<byte>()); object? sdk = Registry.GetValue(GenshinKey, SdkChineseKey, Array.Empty<byte>());
if (sdk is byte[] bytes) if (sdk is byte[] bytes)
{ {
return Encoding.UTF8.GetString(bytes); fixed (byte* pByte = bytes)
{
// 从注册表获取的字节数组带有 '\0' 结尾,需要舍去
ReadOnlySpan<byte> span = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(pByte);
return Encoding.UTF8.GetString(span);
}
} }
return null; return null;