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 { plugins {
java java
`java-library` `java-library`
@ -6,14 +8,14 @@ plugins {
} }
group = "dev.frogmc" group = "dev.frogmc"
version = "1.0.0-SNAPSHOT" version = "0.0.1-alpha.1"
repositories { repositories {
mavenCentral() mavenCentral()
} }
dependencies { 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:9.7")
implementation("org.ow2.asm:asm-commons:9.7") implementation("org.ow2.asm:asm-commons:9.7")
} }
@ -23,6 +25,14 @@ java {
targetCompatibility = JavaVersion.VERSION_21 targetCompatibility = JavaVersion.VERSION_21
} }
tasks.jar {
manifest {
attributes("Implementation-Version" to version,
"Implementation-Name" to project.name,
"Implementation-Date" to Date())
}
}
publishing { publishing {
publications { publications {
create<MavenPublication>("mavenJava") { create<MavenPublication>("mavenJava") {

View file

@ -1,9 +1,8 @@
package dev.frogmc.thyroxine; package dev.frogmc.thyroxine;
import com.google.gson.Gson; import com.electronwill.nightconfig.json.JsonParser;
import com.google.gson.GsonBuilder;
public final class Constants { public final class Constants {
public static final String VERSION_MANIFEST = "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json"; 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; package dev.frogmc.thyroxine;
import com.google.gson.JsonObject;
import lombok.experimental.UtilityClass;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI; import java.net.URI;
@ -12,26 +9,29 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; 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 @UtilityClass
public class HttpHelper { public class HttpHelper {
private static final Map<String, String> requestCache = new HashMap<>(); private static final Map<String, String> requestCache = new HashMap<>();
public static Optional<JsonObject> getJson(String url) { public static Optional<UnmodifiableConfig> getJson(String url) {
return getString(url).map(s -> GSON.fromJson(s, JsonObject.class)); return getString(url).map(s -> JSON_PARSER.parse(s).unmodifiable());
} }
public static Optional<String> getString(String url) { public static Optional<String> getString(String url) {
return Optional.ofNullable(requestCache.computeIfAbsent(url, s -> { return Optional.ofNullable(requestCache.computeIfAbsent(url, s -> {
try (InputStream in = URI.create(url).parseServerAuthority().toURL().openStream()) { try (InputStream in = URI.create(url).parseServerAuthority().toURL().openStream()) {
return new String(in.readAllBytes(), StandardCharsets.UTF_8); return new String(in.readAllBytes(), StandardCharsets.UTF_8);
} catch (IOException | URISyntaxException e) { } catch (IOException | URISyntaxException e) {
e.printStackTrace(); e.printStackTrace();
// TODO // TODO
} }
return null; return null;
})); }));
} }
} }

View file

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

View file

@ -14,8 +14,10 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; 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 dev.frogmc.thyroxine.api.data.Parchment;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
@ -48,7 +50,7 @@ public class ParchmentProvider {
return getParchment(gameVersion, parchmentVer, cacheDir, false); 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(); long time = System.currentTimeMillis();
System.out.println("Loading Parchment mappings.."); System.out.println("Loading Parchment mappings..");
var cachePath = cacheDir.resolve("parchment-" + gameVersion + "-" + parchmentVer + ".zip"); var cachePath = cacheDir.resolve("parchment-" + gameVersion + "-" + parchmentVer + ".zip");
@ -80,7 +82,26 @@ public class ParchmentProvider {
Parchment parchment; Parchment parchment;
try (FileSystem fs = FileSystems.newFileSystem(cachePath)) { try (FileSystem fs = FileSystems.newFileSystem(cachePath)) {
var mappings = Files.readString(fs.getPath("parchment.json")); 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); System.out.printf("Finished loading (%.2fs)\n", (System.currentTimeMillis() - time) / 1000F);