diff --git a/src/main/java/org/quiltmc/launchermeta/version/v1/Arguments.java b/src/main/java/org/quiltmc/launchermeta/version/v1/Arguments.java index f7d15ef..ecc5853 100644 --- a/src/main/java/org/quiltmc/launchermeta/version/v1/Arguments.java +++ b/src/main/java/org/quiltmc/launchermeta/version/v1/Arguments.java @@ -20,11 +20,15 @@ import java.util.List; import java.util.Objects; +import com.google.gson.JsonArray; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; import com.google.gson.reflect.TypeToken; public class Arguments { @@ -95,5 +99,32 @@ public Argument deserialize(JsonElement json, Type typeOfT, JsonDeserializationC return new Argument(value, rules); } } + + public static class Serializer implements JsonSerializer { + @Override + public JsonElement serialize(Argument argument, Type type, JsonSerializationContext context) { + if (argument.value.size() == 1 && argument.rules.isEmpty()) { + return new JsonPrimitive(argument.value.get(0)); + } + + JsonObject json = new JsonObject(); + + if (argument.value.size() == 1) { + json.add("value", new JsonPrimitive(argument.value.get(0))); + } else { + JsonArray array = new JsonArray(argument.value.size()); + argument.value.forEach(array::add); + json.add("value", array); + } + + if (!argument.rules.isEmpty()) { + JsonArray array = new JsonArray(argument.rules.size()); + argument.rules.stream().map(rule -> context.serialize(rule, new TypeToken(){}.getType())).forEach(array::add); + json.add("rules", array); + } + + return json; + } + } } } diff --git a/src/main/java/org/quiltmc/launchermeta/version/v1/Rule.java b/src/main/java/org/quiltmc/launchermeta/version/v1/Rule.java index f6851b8..66a5c6e 100644 --- a/src/main/java/org/quiltmc/launchermeta/version/v1/Rule.java +++ b/src/main/java/org/quiltmc/launchermeta/version/v1/Rule.java @@ -98,9 +98,29 @@ public static class Features { @Nullable private Boolean hasCustomResolution; - public Features(@Nullable Boolean isDemoUser, @Nullable Boolean hasCustomResolution) { + @SerializedName("has_quick_plays_support") + @Nullable + private Boolean hasQuickPlaysSupport; + + @SerializedName("is_quick_play_singleplayer") + @Nullable + private Boolean isQuickPlaySinglePlayer; + + @SerializedName("is_quick_play_multiplayer") + @Nullable + private Boolean isQuickPlayMultiPlayer; + + @SerializedName("is_quick_play_realms") + @Nullable + private Boolean isQuickPlayRealms; + + public Features(@Nullable Boolean isDemoUser, @Nullable Boolean hasCustomResolution, @Nullable Boolean hasQuickPlaysSupport, @Nullable Boolean isQuickPlaySinglePlayer, @Nullable Boolean isQuickPlayMultiPlayer, @Nullable Boolean isQuickPlayRealms) { this.isDemoUser = isDemoUser; this.hasCustomResolution = hasCustomResolution; + this.hasQuickPlaysSupport = hasQuickPlaysSupport; + this.isQuickPlaySinglePlayer = isQuickPlaySinglePlayer; + this.isQuickPlayMultiPlayer = isQuickPlayMultiPlayer; + this.isQuickPlayRealms = isQuickPlayRealms; } public Optional getDemoUser() { @@ -111,6 +131,22 @@ public Optional getHasCustomResolution() { return Optional.ofNullable(hasCustomResolution); } + public Optional getHasQuickPlaysSupport() { + return Optional.ofNullable(hasQuickPlaysSupport); + } + + public Optional getIsQuickPlaySinglePlayer() { + return Optional.ofNullable(isQuickPlaySinglePlayer); + } + + public Optional getIsQuickPlayMultiPlayer() { + return Optional.ofNullable(isQuickPlayMultiPlayer); + } + + public Optional getIsQuickPlayRealms() { + return Optional.ofNullable(isQuickPlayRealms); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/org/quiltmc/launchermeta/version/v1/Version.java b/src/main/java/org/quiltmc/launchermeta/version/v1/Version.java index c93d6d1..b977e3a 100644 --- a/src/main/java/org/quiltmc/launchermeta/version/v1/Version.java +++ b/src/main/java/org/quiltmc/launchermeta/version/v1/Version.java @@ -28,6 +28,7 @@ public class Version { public static final Gson GSON = new GsonBuilder() .registerTypeAdapter(Arguments.Argument.class, new Arguments.Argument.Parser()) + .registerTypeAdapter(Arguments.Argument.class, new Arguments.Argument.Serializer()) .create(); @Nullable diff --git a/src/test/java/org/quiltmc/launchermeta/version/v1/VersionTest.java b/src/test/java/org/quiltmc/launchermeta/version/v1/VersionTest.java index d4a0e1b..daecbe3 100644 --- a/src/test/java/org/quiltmc/launchermeta/version/v1/VersionTest.java +++ b/src/test/java/org/quiltmc/launchermeta/version/v1/VersionTest.java @@ -18,6 +18,8 @@ import java.io.IOException; import com.google.gson.JsonElement; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.quiltmc.launchermeta.TestUtil; import org.quiltmc.launchermeta.version_manifest.VersionManifest; @@ -25,17 +27,24 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; public class VersionTest { - private static final String VERSION_URL = "https://launchermeta.mojang.com/v1/packages/f2affa3247f2471d3334b199d1915ce582914464/21w42a.json"; - private static final String MANIFEST_URL = "https://launchermeta.mojang.com/mc/game/version_manifest.json"; + private static final String VERSION_URL = "https://piston-meta.mojang.com/v1/packages/3ecc58bbbc2b680be6742747089cbbf3272526f9/1.20.6-rc1.json"; + private static final String MANIFEST_URL = "https://launchermeta.mojang.com/mc/game/version_manifest_v2.json"; @Test - public void testParseFullJson() throws IOException { + public void testParseDoesNotThrow() throws IOException { JsonElement json = TestUtil.getJsonFromURL(VERSION_URL); assertDoesNotThrow(() -> { Version version = Version.fromJson(json); }); } + @Disabled("Manual verification passes with 1.20.6-rc1, but the order of elements is different") + @Test + public void testParseFullJson() throws IOException { + JsonElement json = TestUtil.getJsonFromURL(VERSION_URL); + Assertions.assertEquals(json, Version.GSON.toJsonTree(Version.fromJson(json))); + } + @Test public void assertNoMethodReturnsAreNull() throws IOException { VersionManifest.fromJson(TestUtil.getJsonFromURL(MANIFEST_URL)) @@ -52,4 +61,21 @@ public void assertNoMethodReturnsAreNull() throws IOException { }) .forEach(TestUtil.checkNoMethodsReturnNull(Version.class)); } + + @Disabled("Manual verification passes with 1.20.6-rc1, but the order of elements is different") + @Test + public void assertCreatesSameJson() throws IOException { + VersionManifest.fromJson(TestUtil.getJsonFromURL(MANIFEST_URL)) + .getVersions() + .parallelStream() + .forEach(version -> { + JsonElement json = null; + try { + json = TestUtil.getJsonFromURL(version.getUrl()); + } catch (IOException e) { + e.printStackTrace(); + } + Assertions.assertEquals(Version.GSON.toJsonTree(Version.fromJson(json)), json); + }); + } } \ No newline at end of file