migrate to nightconfig, bump version
All checks were successful
Publish to snapshot maven / build (push) Successful in 25s

This commit is contained in:
moehreag 2024-06-12 22:35:33 +02:00
parent 98b52d394f
commit 422fa6aa3a
5 changed files with 69 additions and 37 deletions

View file

@ -1,3 +1,5 @@
import java.util.Date
plugins {
java
`java-library`
@ -6,14 +8,14 @@ plugins {
}
group = "dev.frogmc"
version = "1.0.0-SNAPSHOT"
version = "0.0.1-alpha.1"
repositories {
mavenCentral()
}
dependencies {
implementation("com.google.code.gson:gson:2.10.1")
implementation("com.electronwill.night-config:json:3.7.2")
implementation("org.ow2.asm:asm:9.7")
implementation("org.ow2.asm:asm-commons:9.7")
}
@ -23,6 +25,14 @@ java {
targetCompatibility = JavaVersion.VERSION_21
}
tasks.jar {
manifest {
attributes("Implementation-Version" to version,
"Implementation-Name" to project.name,
"Implementation-Date" to Date())
}
}
publishing {
publications {
create<MavenPublication>("mavenJava") {

View file

@ -1,9 +1,8 @@
package dev.frogmc.thyroxine;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.electronwill.nightconfig.json.JsonParser;
public final class Constants {
public static final String VERSION_MANIFEST = "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json";
public static final Gson GSON = new GsonBuilder().create();
public static final JsonParser JSON_PARSER = new JsonParser();
}

View file

@ -1,8 +1,5 @@
package dev.frogmc.thyroxine;
import com.google.gson.JsonObject;
import lombok.experimental.UtilityClass;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
@ -12,15 +9,18 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static dev.frogmc.thyroxine.Constants.GSON;
import com.electronwill.nightconfig.core.UnmodifiableConfig;
import lombok.experimental.UtilityClass;
import static dev.frogmc.thyroxine.Constants.JSON_PARSER;
@UtilityClass
public class HttpHelper {
private static final Map<String, String> requestCache = new HashMap<>();
public static Optional<JsonObject> getJson(String url) {
return getString(url).map(s -> GSON.fromJson(s, JsonObject.class));
public static Optional<UnmodifiableConfig> getJson(String url) {
return getString(url).map(s -> JSON_PARSER.parse(s).unmodifiable());
}
public static Optional<String> getString(String url) {

View file

@ -1,6 +1,6 @@
package dev.frogmc.thyroxine.provider;
import com.google.gson.*;
import com.electronwill.nightconfig.core.UnmodifiableConfig;
import dev.frogmc.thyroxine.Constants;
import dev.frogmc.thyroxine.HttpHelper;
@ -8,10 +8,13 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class MojmapProvider {
@SuppressWarnings("unchecked")
public static Optional<String> get(String gameVersion, Path cacheFile) {
if (Files.exists(cacheFile)){
try {
@ -24,20 +27,19 @@ public class MojmapProvider {
return HttpHelper.getJson(Constants.VERSION_MANIFEST).flatMap(manifest -> {
String versionName;
if (gameVersion.startsWith("latest-")) {
versionName = manifest.get("latest").getAsJsonObject().get(gameVersion.split("-")[1]).getAsString();
versionName = (String) ((Map<?, ?>) manifest.get("latest")).get(gameVersion.split("-")[1]);
} else {
versionName = gameVersion;
}
System.out.println("Loading version: " + versionName);
for (JsonElement element : manifest.get("versions").getAsJsonArray()) {
JsonObject version = element.getAsJsonObject();
if (version.get("id").getAsString().equals(versionName)) {
for (Map<String, ?> version : (List<Map<String, ?>>) manifest.get("versions")) {
if (version.get("id").equals(versionName)) {
JsonObject versionManifest = HttpHelper.getJson(version.get("url").getAsString()).orElseThrow();
String mappingsUrl = versionManifest
.get("downloads").getAsJsonObject().get("client_mappings").getAsJsonObject().get("url").getAsString();
UnmodifiableConfig versionManifest = HttpHelper.getJson((String) version.get("url")).orElseThrow();
String mappingsUrl = (String) ((Map<?, ?>) ((Map<?, ?>) versionManifest
.get("downloads")).get("client_mappings")).get("url");
return HttpHelper.getString(mappingsUrl).map(s -> {
try {

View file

@ -14,8 +14,10 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import com.google.gson.Gson;
import com.electronwill.nightconfig.core.UnmodifiableConfig;
import dev.frogmc.thyroxine.Constants;
import dev.frogmc.thyroxine.api.data.Parchment;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
@ -48,7 +50,7 @@ public class ParchmentProvider {
return getParchment(gameVersion, parchmentVer, cacheDir, false);
}
private static Parchment getParchment(String gameVersion, String parchmentVer, Path cacheDir, boolean forceDownload) throws IOException {
public static Parchment getParchment(String gameVersion, String parchmentVer, Path cacheDir, boolean forceDownload) throws IOException {
long time = System.currentTimeMillis();
System.out.println("Loading Parchment mappings..");
var cachePath = cacheDir.resolve("parchment-" + gameVersion + "-" + parchmentVer + ".zip");
@ -80,7 +82,26 @@ public class ParchmentProvider {
Parchment parchment;
try (FileSystem fs = FileSystems.newFileSystem(cachePath)) {
var mappings = Files.readString(fs.getPath("parchment.json"));
parchment = new Gson().fromJson(mappings, Parchment.class);
UnmodifiableConfig c = Constants.JSON_PARSER.parse(mappings);
String version = c.get("version");
List<Parchment.Package> packages = c.get("packages");
List<Parchment.Class> classes = new ArrayList<>();
List<UnmodifiableConfig> classesList = Objects.requireNonNullElse(c.get("classes"), Collections.emptyList());
classesList.forEach(config -> {
List<Parchment.Method> methods = new ArrayList<>();
List<Parchment.Field> fields = new ArrayList<>();
List<UnmodifiableConfig> methodsList = Objects.requireNonNullElse(config.get("methods"), Collections.emptyList());
methodsList.forEach(uc -> {
List<Parchment.Parameter> parameters = new ArrayList<>();
List<UnmodifiableConfig> parametersList = Objects.requireNonNullElse(uc.get("parameters"), Collections.emptyList());
parametersList.forEach(puC -> parameters.add(new Parchment.Parameter(puC.get("index"), puC.get("name"))));
methods.add(new Parchment.Method(uc.get("name"), uc.get("descriptor"), uc.get("javadoc"), parameters));
});
classes.add(new Parchment.Class(config.get("name"), config.get("javadoc"), fields, methods));
});
parchment = new Parchment(version, packages, classes);
}
System.out.printf("Finished loading (%.2fs)\n", (System.currentTimeMillis() - time) / 1000F);