Skip to content

Commit

Permalink
修复一个Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSpring114 committed Dec 9, 2023
1 parent d4aeb2a commit 22fe97f
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 45 deletions.
18 changes: 9 additions & 9 deletions MinecraftLaunch.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using MinecraftLaunch.Components.Authenticator;
using MinecraftLaunch.Components.Checker;
using MinecraftLaunch.Components.Resolver;
using System.Diagnostics;

MicrosoftAuthenticator authenticator = new("9fd44410-8ed7-4eb3-a160-9f1cc62c824c");
string gameFolder = "C:\\Users\\w\\Desktop\\temp\\.minecraft";

var result = await authenticator.DeviceFlowAuthAsync(x => {
Console.WriteLine(x.UserCode);
});
GameResolver resolver = new(gameFolder);
var gameEntry = resolver.GetGameEntity("1.12.2");

if(result != null) {
var account = await authenticator.AuthenticateAsync();
Console.WriteLine(account.Name);
Console.WriteLine(account.Uuid);
}
ResourceChecker checker = new(gameEntry);
var result = await checker.CheckAsync();

Console.WriteLine(result);
Console.ReadKey();
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public async ValueTask<MicrosoftAccount> AuthenticateAsync() {
using var authenticateMinecraftPostRes = await $"https://api.minecraftservices.com/authentication/login_with_xbox"
.PostJsonAsync(authenticateMinecraftContent);

string access_token = JsonNode.Parse(await authenticateMinecraftPostRes.GetStringAsync())!["access_token"]!
.GetValue<string>();
string access_token = JsonNode.Parse(await authenticateMinecraftPostRes.GetStringAsync())
.GetString("access_token");

/*
* Check player's minecraft ownership (optional steps)
Expand Down Expand Up @@ -192,7 +192,7 @@ public async Task<OAuth2TokenResponse> DeviceFlowAuthAsync(Action<DeviceCodeResp
};
}

if (tempTokenResponse["token_type"]?.GetValue<string>() is "Bearer") {
if (tempTokenResponse.GetString("token_type") is "Bearer") {
_oAuth2TokenResponse = tokenResponse;
return tokenResponse;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using MinecraftLaunch.Classes.Enums;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace MinecraftLaunch.Components.Converter {
public class PlatformEnumToStringJsonConverter : JsonConverter<Platform> {
public override Platform Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
var platform = reader.GetString();
return platform switch {
"osx" => Platform.osx,
"linux" => Platform.linux,
"windows" => Platform.windows,
_ => Platform.windows,
};
}

public override void Write(Utf8JsonWriter writer, Platform value, JsonSerializerOptions options) {
writer.WriteStringValue(value.ToString());
}
}
}
17 changes: 9 additions & 8 deletions MinecraftLaunch/Components/Fetcher/CurseForgeFetcher.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using MinecraftLaunch.Classes.Interfaces;
using MinecraftLaunch.Classes.Models.Download;
using System.Text.Json;
using System.Text.Json;
using System.Text.Json.Nodes;
using MinecraftLaunch.Extensions;
using MinecraftLaunch.Classes.Interfaces;
using MinecraftLaunch.Classes.Models.Download;

namespace MinecraftLaunch.Components.Fetcher {
public class CurseForgeFetcher : IFetcher<IEnumerable<CurseForgeResourceEntry>> {
Expand All @@ -16,11 +17,11 @@ public ValueTask<IEnumerable<CurseForgeResourceEntry>> FetchAsync() {
private CurseForgeResourceEntry ResolveFromJsonNode(JsonNode node) {
var entry = node.Deserialize<CurseForgeResourceEntry>();

entry.WebLink = node["links"]?["websiteUrl"]?.GetValue<string>();
entry.IconUrl = node["logo"]?["url"]?.GetValue<string>();
entry.Authors = node["authors"]?.AsArray().Select(x => x["name"].GetValue<string>());
entry.ScreenshotUrls = node["screenshots"]?.AsArray().Select(x => x["url"].GetValue<string>());
entry.Categories = node["categories"]?.AsArray().Select(x => x["name"].GetValue<string>());
entry.IconUrl = node["logo"]?.GetString("url");
entry.WebLink = node["links"]?.GetString("websiteUrl");
entry.Authors = node?.GetEnumerable<string>("authors", "name");
entry.Categories = node?.GetEnumerable<string>("categories", "name");
entry.ScreenshotUrls = node?.GetEnumerable<string>("screenshots", "url");
entry.Files = entry.Files.Select(x => {
x.ModId = entry.Id;
return x;
Expand Down
5 changes: 3 additions & 2 deletions MinecraftLaunch/Components/Resolver/GameResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,11 @@ string GetVersion(GameEntry gameInfo, GameJsonEntry jsonEntity, string path) {
var clientVersion = json["clientVersion"]; // pcl合并核心版本号读取

if (patches != null) {
return patches[0]["version"].GetValue<string>();
return patches[0].GetString("version");
}

if (clientVersion != null) {
return clientVersion.GetValue<string>();
return clientVersion.GetString();
}
}

Expand Down
16 changes: 8 additions & 8 deletions MinecraftLaunch/Components/Resolver/LibrariesResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public LibraryEntry Resolve(JsonNode libNode) {
var jsonRules = libNode["rules"];
var jsonNatives = libNode["natives"];

if (jsonRules != null && !GetLibraryEnable(jsonRules.Deserialize<IEnumerable<RuleModel>>()!)) {
if (jsonRules != null && !GetLibraryEnable(jsonRules.Deserialize<IEnumerable<RuleModel>>(JsonConverterUtil
.DefaultJsonConverterOptions)!)) {
return null!;
}

Expand Down Expand Up @@ -74,9 +75,9 @@ private LibraryEntry GetJarEntry() {
if (jsonClient != null)
return new LibraryEntry {
Path = GameEntry.JarPath,
Size = jsonClient["size"]!.GetValue<int>(),
Url = jsonClient["url"]!.GetValue<string>(),
Checksum = jsonClient["sha1"]!.GetValue<string>()
Url = jsonClient.GetString("url"),
Size = jsonClient.GetInt32("size"),
Checksum = jsonClient.GetString("sha1")
};

return null;
Expand Down Expand Up @@ -199,7 +200,7 @@ private void GetLibraryChecksumAndUrl(ref LibraryEntry libraryEntry, JsonNode js
libraryEntry.Url = libraryJsonNode.Downloads.Classifiers[nativeName].Url;
}

if (jsonNode["name"]!.GetValue<string>().Contains("natives")) {
if (jsonNode.GetString("name").Contains("natives")) {
libraryEntry.Checksum = libraryJsonNode.Downloads.Artifact.Sha1;
libraryEntry.Size = libraryJsonNode.Downloads.Artifact.Size;
libraryEntry.Url = libraryJsonNode.Downloads.Artifact.Url;
Expand All @@ -216,15 +217,14 @@ private void GetLibraryChecksumAndUrl(ref LibraryEntry libraryEntry, JsonNode js
}

if (libraryEntry.Url == null && jsonNode["url"] != null) {
libraryEntry.Url = (jsonNode["url"]
.GetValue<string>() + libraryEntry.RelativePath)
libraryEntry.Url = (jsonNode.GetString("url") + libraryEntry.RelativePath)
.Replace('\\', '/');
}

if (libraryEntry.Checksum == null && jsonNode["checksums"] != null) {
libraryEntry.Checksum = jsonNode["checksums"]!
.AsArray()[0]!
.GetValue<string>();
.GetString();
}
}
}
30 changes: 15 additions & 15 deletions MinecraftLaunch/Components/Resolver/ModResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ private void OfLegacyForgeModEntry(string text, ref ModEntry entry) {
?? throw new InvalidDataException("Invalid mcmod.info");

try {
entry.DisplayName = jsonNode["name"].GetValue<string>();
entry.Version = jsonNode["version"]?.GetValue<string>();
entry.Description = jsonNode["description"]?.GetValue<string>();
entry.Authors = (jsonNode["authorList"] ?? jsonNode["authors"]).AsArray()
.Select(x => x.GetValue<string>())
entry.DisplayName = jsonNode.GetString("name");
entry.Version = jsonNode.GetString("version");
entry.Description = jsonNode.GetString("description");
entry.Authors = (jsonNode["authorList"] ?? jsonNode["authors"])
.GetEnumerable<string>()
.ToImmutableArray();
}
catch (Exception) {
Expand All @@ -92,11 +92,11 @@ private void OfFabricModEntry(ZipArchive zipArchive, ref ModEntry entry) {
try {
var jsonNode = JsonNode.Parse(reader.ReadToEnd());

entry.DisplayName = jsonNode["name"].GetValue<string>();
entry.Version = jsonNode["version"]?.GetValue<string>();
entry.Description = jsonNode["description"]?.GetValue<string>();
entry.Authors = jsonNode["authors"].AsArray()
.Select(x => x.GetValue<string>())
entry.DisplayName = jsonNode.GetString("name");
entry.Version = jsonNode.GetString("version");
entry.Description = jsonNode.GetString("description");
entry.Authors = jsonNode["authors"]
.GetEnumerable<string>()
.ToImmutableArray();
}
catch (Exception) {
Expand Down Expand Up @@ -136,11 +136,11 @@ private void OfQulitModEntry(ZipArchive zipArchive, ref ModEntry entry) {
var jsonNode = JsonNode.Parse(reader.ReadToEnd());
jsonNode = jsonNode["quilt_loader"]["metadata"];

entry.DisplayName = jsonNode["name"].GetValue<string>();
entry.Version = jsonNode["version"]?.GetValue<string>();
entry.Description = jsonNode["description"]?.GetValue<string>();
entry.Authors = jsonNode["authors"].AsArray()
.Select(x => x.GetValue<string>())
entry.DisplayName = jsonNode?.GetString("name");
entry.Version = jsonNode?.GetString("version");
entry.Description = jsonNode?.GetString("description");
entry.Authors = jsonNode["authors"]
.GetEnumerable<string>()
.ToImmutableArray();
}
catch (Exception) {
Expand Down
30 changes: 30 additions & 0 deletions MinecraftLaunch/Extensions/JsonExtension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Xml.Linq;

namespace MinecraftLaunch.Extensions {
public static class JsonExtension {
Expand All @@ -11,16 +12,45 @@ public static T AsJsonEntry<T>(this string json) {
return JsonSerializer.Deserialize<T>(json);
}

public static int GetInt32(this JsonNode node) {
return node.GetValue<int>();
}

public static int GetInt32(this JsonNode node, string name) {
return node[name].GetValue<int>();
}

public static bool GetBool(this JsonNode node) {
return node.GetValue<bool>();
}

public static bool GetBool(this JsonNode node, string name) {
return node[name].GetValue<bool>();
}

public static string GetString(this JsonNode node) {
return node.GetValue<string>();
}

public static string GetString(this JsonNode node, string name) {
return node[name].GetValue<string>();
}

public static IEnumerable<T> GetEnumerable<T>(this JsonNode node) {
return node.AsArray()
.Select(x => x.GetValue<T>());
}

public static IEnumerable<T> GetEnumerable<T>(this JsonNode node, string name) {
return node[name]
.AsArray()
.Select(x => x.GetValue<T>());
}

public static IEnumerable<T> GetEnumerable<T>(this JsonNode node, string name, string elementName) {
return node[name]
.AsArray()
.Select(x => x[elementName].GetValue<T>());
}
}
}
19 changes: 19 additions & 0 deletions MinecraftLaunch/Utilities/JsonConverterUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using MinecraftLaunch.Components.Converter;
using System.Text.Encodings.Web;
using System.Text.Json;

namespace MinecraftLaunch.Utilities {
public class JsonConverterUtil {
public static JsonSerializerOptions DefaultJsonConverterOptions => Get();

private static JsonSerializerOptions Get() {
var options = new JsonSerializerOptions() {
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};

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

return options;
}
}
}

0 comments on commit 22fe97f

Please sign in to comment.