Skip to content

Commit

Permalink
添加账户Json转换器
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSpring114 committed Dec 23, 2023
1 parent 2140cc8 commit 67a2e65
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion MinecraftLaunch.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -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()) {
Expand Down
44 changes: 44 additions & 0 deletions MinecraftLaunch/Components/Converter/AccountJsonConverter.cs
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();
}
}
}
2 changes: 1 addition & 1 deletion MinecraftLaunch/MinecraftLaunch.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>3.0.0-preview4</Version>
<Version>3.0.0-preview5</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions MinecraftLaunch/Utilities/JsonConverterUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ private static JsonSerializerOptions Get() {
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};

options.Converters.Add(new AccountJsonConverter());
options.Converters.Add(new PlatformEnumToStringJsonConverter());

return options;
Expand Down

0 comments on commit 67a2e65

Please sign in to comment.