-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2140cc8
commit 67a2e65
Showing
4 changed files
with
47 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
MinecraftLaunch/Components/Converter/AccountJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Account> { | ||
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters