From 67a2e65f87df1c8c1a4505ff3bc43485a20dfec2 Mon Sep 17 00:00:00 2001 From: YangSpring114 Date: Sat, 23 Dec 2023 14:07:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B4=A6=E6=88=B7Json?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MinecraftLaunch.Test/Program.cs | 2 +- .../Converter/AccountJsonConverter.cs | 44 +++++++++++++++++++ MinecraftLaunch/MinecraftLaunch.csproj | 2 +- .../Utilities/JsonConverterUtil.cs | 1 + 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 MinecraftLaunch/Components/Converter/AccountJsonConverter.cs diff --git a/MinecraftLaunch.Test/Program.cs b/MinecraftLaunch.Test/Program.cs index 41a676c..61f617b 100644 --- a/MinecraftLaunch.Test/Program.cs +++ b/MinecraftLaunch.Test/Program.cs @@ -1,8 +1,8 @@ // -using MinecraftLaunch.Components.Authenticator; using MinecraftLaunch.Components.Fetcher; using MinecraftLaunch.Components.Launcher; using MinecraftLaunch.Components.Resolver; +using MinecraftLaunch.Components.Authenticator; var resolver = new GameResolver("C:\\Users\\w\\Desktop\\temp\\.minecraft"); Launcher launcher = new(resolver, new(new OfflineAuthenticator("Yang114").Authenticate()) { diff --git a/MinecraftLaunch/Components/Converter/AccountJsonConverter.cs b/MinecraftLaunch/Components/Converter/AccountJsonConverter.cs new file mode 100644 index 0000000..8b9d976 --- /dev/null +++ b/MinecraftLaunch/Components/Converter/AccountJsonConverter.cs @@ -0,0 +1,44 @@ +using System.Text.Json.Serialization; +using System.Text.Json; +using MinecraftLaunch.Classes.Models.Auth; +using MinecraftLaunch.Classes.Enums; + +namespace MinecraftLaunch.Components.Converter { + public class AccountJsonConverter : JsonConverter { + public override bool CanConvert(Type typeToConvert) { + return typeToConvert == typeof(Account); + } + + public override Account Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { + using var jsonDoc = JsonDocument.ParseValue(ref reader); + var root = jsonDoc.RootElement; + var accountType = (AccountType)root.GetProperty("Type").GetInt32(); + + return accountType switch { + AccountType.Offline => new OfflineAccount { + AccessToken = root.GetProperty("AccessToken").GetString(), + Name = root.GetProperty("Name").GetString(), + Uuid = root.GetProperty("Uuid").GetGuid() + }, + AccountType.Microsoft => new MicrosoftAccount { + AccessToken = root.GetProperty("AccessToken").GetString()!, + Name = root.GetProperty("Name").GetString(), + Uuid = root.GetProperty("Uuid").GetGuid(), + RefreshToken = root.GetProperty("RefreshToken").GetString() + }, + AccountType.Yggdrasil => new YggdrasilAccount { + AccessToken = root.GetProperty("AccessToken").GetString(), + ClientToken = root.GetProperty("ClientToken").GetString(), + Name = root.GetProperty("Name").GetString(), + Uuid = root.GetProperty("Uuid").GetGuid(), + YggdrasilServerUrl = root.GetProperty("YggdrasilServerUrl").GetString() + }, + _ => default! + }; + } + + public override void Write(Utf8JsonWriter writer, Account value, JsonSerializerOptions options) { + throw new NotImplementedException(); + } + } +} diff --git a/MinecraftLaunch/MinecraftLaunch.csproj b/MinecraftLaunch/MinecraftLaunch.csproj index 943a5ca..8a41f2c 100644 --- a/MinecraftLaunch/MinecraftLaunch.csproj +++ b/MinecraftLaunch/MinecraftLaunch.csproj @@ -1,6 +1,6 @@  - 3.0.0-preview4 + 3.0.0-preview5 diff --git a/MinecraftLaunch/Utilities/JsonConverterUtil.cs b/MinecraftLaunch/Utilities/JsonConverterUtil.cs index b50184e..be84f31 100644 --- a/MinecraftLaunch/Utilities/JsonConverterUtil.cs +++ b/MinecraftLaunch/Utilities/JsonConverterUtil.cs @@ -11,6 +11,7 @@ private static JsonSerializerOptions Get() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, }; + options.Converters.Add(new AccountJsonConverter()); options.Converters.Add(new PlatformEnumToStringJsonConverter()); return options;