From fb0491dc57e1a1c8655876003891e2b26af7dfcb Mon Sep 17 00:00:00 2001 From: Lightczx <1686188646@qq.com> Date: Tue, 2 Jan 2024 17:10:45 +0800 Subject: [PATCH] get object form registry --- .../GameRegistryContentTest.cs | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Snap.Hutao/Snap.Hutao.Test/IncomingFeature/GameRegistryContentTest.cs b/src/Snap.Hutao/Snap.Hutao.Test/IncomingFeature/GameRegistryContentTest.cs index d04e9749..449b6c2e 100644 --- a/src/Snap.Hutao/Snap.Hutao.Test/IncomingFeature/GameRegistryContentTest.cs +++ b/src/Snap.Hutao/Snap.Hutao.Test/IncomingFeature/GameRegistryContentTest.cs @@ -11,6 +11,12 @@ namespace Snap.Hutao.Test.IncomingFeature; [TestClass] public class GameRegistryContentTest { + private static readonly JsonSerializerOptions RegistryContentSerializerOptions = new() + { + WriteIndented = true, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + }; + [TestMethod] [SupportedOSPlatform("windows")] public void GetRegistryContent() @@ -33,28 +39,29 @@ public class GameRegistryContentTest data[valueName] = gameKey.GetValueKind(valueName) switch { RegistryValueKind.DWord => (int)gameKey.GetValue(valueName)!, - RegistryValueKind.Binary => GetString((byte[])gameKey.GetValue(valueName)!), + RegistryValueKind.Binary => GetStringOrObject((byte[])gameKey.GetValue(valueName)!), _ => throw new NotImplementedException() }; } - JsonSerializerOptions options = new() - { - WriteIndented = true, - Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - }; - Console.WriteLine($"Subkey: {subkey}"); - Console.WriteLine(JsonSerializer.Serialize(data, options)); + Console.WriteLine(JsonSerializer.Serialize(data, RegistryContentSerializerOptions)); } } - private static unsafe string GetString(byte[] bytes) + private static unsafe object GetStringOrObject(byte[] bytes) { fixed (byte* pByte = bytes) { ReadOnlySpan span = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(pByte); - return Encoding.UTF8.GetString(span); + string temp = Encoding.UTF8.GetString(span); + + if (temp.AsSpan()[0] is '{' or '[') + { + return JsonSerializer.Deserialize(temp); + } + + return temp; } } } \ No newline at end of file