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;