Skip to content

Commit

Permalink
Add some missing fields
Browse files Browse the repository at this point in the history
  • Loading branch information
OroArmor committed Apr 26, 2024
1 parent d5979f6 commit 36ba9da
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/main/java/org/quiltmc/launchermeta/version/v1/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -95,5 +99,32 @@ public Argument deserialize(JsonElement json, Type typeOfT, JsonDeserializationC
return new Argument(value, rules);
}
}

public static class Serializer implements JsonSerializer<Argument> {
@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<Rule>(){}.getType())).forEach(array::add);
json.add("rules", array);
}

return json;
}
}
}
}
38 changes: 37 additions & 1 deletion src/main/java/org/quiltmc/launchermeta/version/v1/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> getDemoUser() {
Expand All @@ -111,6 +131,22 @@ public Optional<Boolean> getHasCustomResolution() {
return Optional.ofNullable(hasCustomResolution);
}

public Optional<Boolean> getHasQuickPlaysSupport() {
return Optional.ofNullable(hasQuickPlaysSupport);
}

public Optional<Boolean> getIsQuickPlaySinglePlayer() {
return Optional.ofNullable(isQuickPlaySinglePlayer);
}

public Optional<Boolean> getIsQuickPlayMultiPlayer() {
return Optional.ofNullable(isQuickPlayMultiPlayer);
}

public Optional<Boolean> getIsQuickPlayRealms() {
return Optional.ofNullable(isQuickPlayRealms);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 29 additions & 3 deletions src/test/java/org/quiltmc/launchermeta/version/v1/VersionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,33 @@
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;

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))
Expand All @@ -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);
});
}
}

0 comments on commit 36ba9da

Please sign in to comment.