From 7bfea0e0906a9084ddafbe71ba097c06305c19b3 Mon Sep 17 00:00:00 2001 From: DismissedLight <1686188646@qq.com> Date: Mon, 1 Jan 2024 23:20:59 +0800 Subject: [PATCH] Create GameRegistryContentTest.cs --- .../GameRegistryContentTest.cs | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/Snap.Hutao/Snap.Hutao.Test/IncomingFeature/GameRegistryContentTest.cs diff --git a/src/Snap.Hutao/Snap.Hutao.Test/IncomingFeature/GameRegistryContentTest.cs b/src/Snap.Hutao/Snap.Hutao.Test/IncomingFeature/GameRegistryContentTest.cs new file mode 100644 index 00000000..8cf6486b --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao.Test/IncomingFeature/GameRegistryContentTest.cs @@ -0,0 +1,52 @@ +using Microsoft.Win32; +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; +using System.Text; +using System.Text.Json; + +namespace Snap.Hutao.Test.IncomingFeature; + +[TestClass] +public class GameRegistryContentTest +{ + [TestMethod] + [SupportedOSPlatform("windows")] + public void GetRegistryContent() + { + using (RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64)) + { + RegistryKey? gameKey = key.OpenSubKey(@"Software\miHoYo\原神"); + Assert.IsNotNull(gameKey); + + Dictionary data = []; + foreach (string valueName in gameKey.GetValueNames()) + { + data[valueName] = gameKey.GetValueKind(valueName) switch + { + RegistryValueKind.DWord => (int)gameKey.GetValue(valueName)!, + RegistryValueKind.Binary => GetString((byte[])gameKey.GetValue(valueName)!), + _ => throw new NotImplementedException() + }; + } + + JsonSerializerOptions options = new() + { + WriteIndented = true, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + }; + + Console.WriteLine(JsonSerializer.Serialize(data, options)); + } + } + + private static unsafe string GetString(byte[] bytes) + { + fixed (byte* pByte = bytes) + { + ReadOnlySpan span = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(pByte); + return Encoding.UTF8.GetString(span); + } + } +} \ No newline at end of file