diff --git a/build.gradle b/build.gradle index 46796f2..757804b 100644 --- a/build.gradle +++ b/build.gradle @@ -19,6 +19,8 @@ base { apply from: "https://raw.githubusercontent.com/constellation-mc/artifacts/main/artifacts.groovy" constellationRepo(project, "dark-matter") +apply from: "gradle/mappings.gradle" + repositories { maven { url 'https://jitpack.io' } } diff --git a/gradle/mappings.gradle b/gradle/mappings.gradle new file mode 100644 index 0000000..5db793e --- /dev/null +++ b/gradle/mappings.gradle @@ -0,0 +1,65 @@ +buildscript { + repositories { + mavenCentral() + maven { + name = 'Fabric' + url = 'https://maven.fabricmc.net/' + } + } + + dependencies { + classpath ("net.fabricmc:mapping-io:${project.mappingio_version}") + classpath ('com.google.code.gson:gson:2.10.1') + } +} + + +import com.google.gson.JsonElement +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import net.fabricmc.mappingio.MappingReader +import net.fabricmc.mappingio.MappingWriter +import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch +import net.fabricmc.mappingio.format.MappingFormat +import net.fabricmc.mappingio.tree.MemoryMappingTree + +import java.nio.file.Files +import java.util.zip.DeflaterOutputStream + +ext.MANIFEST_URL = new URL("https://launchermeta.mojang.com/mc/game/version_manifest_v2.json") + +tasks.register("downloadCommanderMappings") { + File processed = new File("${project.layout.projectDirectory.getAsFile()}/.gradle/downloadCommanderMappings/${project.minecraft_version}/processed.bin") + if (!processed.exists()) { + Files.createDirectories(processed.toPath().getParent()) + + MemoryMappingTree tree = new MemoryMappingTree() + MappingReader.read(new InputStreamReader(new URL(getManifest().getAsJsonObject("downloads").getAsJsonObject("client_mappings").get("url").getAsString()).openStream()), new MappingSourceNsSwitch(tree, "target", true)) + tree.setSrcNamespace("official") + tree.setDstNamespaces(List.of("mojang")) + + OutputStream stream = Files.newOutputStream(processed.toPath()) + tree.accept(MappingWriter.create(new OutputStreamWriter(new DeflaterOutputStream(stream)), MappingFormat.TINY_FILE)) + } +} + +processResources { + mustRunAfter(tasks.downloadCommanderMappings) + from(new File("${project.layout.projectDirectory.getAsFile()}/.gradle/downloadCommanderMappings/${project.minecraft_version}/processed.bin")) { + rename { "commander/mappings/processed.bin" } + } +} + +JsonObject getManifest() { + var o = downloadObject(ext.MANIFEST_URL) + for (JsonElement versions : o.getAsJsonArray("versions")) { + if (project.minecraft_version == versions.getAsJsonObject().get("id").getAsString()) { + return downloadObject(new URL(versions.getAsJsonObject().get("url").getAsString())) + } + } + throw new IllegalStateException("Unknown version '%s'".formatted(project.minecraft_version)) +} + +static JsonObject downloadObject(URL url) { + return JsonParser.parseReader(new InputStreamReader(url.openStream())).getAsJsonObject() +} \ No newline at end of file diff --git a/src/main/java/me/melontini/commander/impl/Commander.java b/src/main/java/me/melontini/commander/impl/Commander.java index fedbae6..bb2709c 100644 --- a/src/main/java/me/melontini/commander/impl/Commander.java +++ b/src/main/java/me/melontini/commander/impl/Commander.java @@ -1,7 +1,5 @@ package me.melontini.commander.impl; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import lombok.Getter; import lombok.extern.log4j.Log4j2; import me.melontini.commander.api.expression.Arithmetica; @@ -15,7 +13,6 @@ import me.melontini.commander.impl.util.loot.ExpressionLootCondition; import me.melontini.commander.impl.util.mappings.AmbiguousRemapper; import me.melontini.commander.impl.util.mappings.MappingKeeper; -import me.melontini.commander.impl.util.mappings.MinecraftDownloader; import me.melontini.dark_matter.api.base.util.Exceptions; import me.melontini.dark_matter.api.base.util.PrependingLogger; import me.melontini.dark_matter.api.data.codecs.ExtraCodecs; @@ -32,7 +29,6 @@ import net.minecraft.util.Util; import java.io.IOException; -import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; @@ -48,7 +44,6 @@ public class Commander implements ModInitializer { public static final LootConditionType EXPRESSION_CONDITION = Registry.register(Registries.LOOT_CONDITION_TYPE, id("expression"), new LootConditionType(ExtraCodecs.toJsonSerializer(ExpressionLootCondition.CODEC))); public static final Path COMMANDER_PATH = FabricLoader.getInstance().getGameDir().resolve(".commander"); - public static final String MINECRAFT_VERSION = getVersion(); @Getter private static AmbiguousRemapper mappingKeeper; @@ -73,13 +68,11 @@ public void onInitialize() { EvalUtils.init(); try { - MinecraftDownloader.downloadMappings(); - CompletableFuture offMojmap = CompletableFuture.supplyAsync(MappingKeeper::loadOffMojmap, Util.getMainWorkerExecutor()); CompletableFuture offTarget = CompletableFuture.supplyAsync(MappingKeeper::loadOffTarget, Util.getMainWorkerExecutor()); mappingKeeper = new MappingKeeper(MappingKeeper.loadMojmapTarget(offMojmap.join(), offTarget.join())); } catch (Throwable t) { - log.error("Failed to download and prepare mappings! Data access remapping will not work!!!", t); + log.error("Failed to prepare mappings! Data access remapping will not work!!!", t); mappingKeeper = (cls, name) -> name;//Returning null will force it to traverse the hierarchy. } @@ -94,9 +87,4 @@ public void onInitialize() { DAMAGE_SOURCE, EXPLOSION_RADIUS, BLOCK_STATE, BLOCK_ENTITY); } - - private static String getVersion() { - JsonObject o = JsonParser.parseReader(new InputStreamReader(MinecraftDownloader.class.getResourceAsStream("/version.json"))).getAsJsonObject(); - return o.getAsJsonPrimitive("id").getAsString(); - } } diff --git a/src/main/java/me/melontini/commander/impl/util/mappings/MappingKeeper.java b/src/main/java/me/melontini/commander/impl/util/mappings/MappingKeeper.java index 84f8bd5..2097d89 100644 --- a/src/main/java/me/melontini/commander/impl/util/mappings/MappingKeeper.java +++ b/src/main/java/me/melontini/commander/impl/util/mappings/MappingKeeper.java @@ -2,7 +2,6 @@ import lombok.SneakyThrows; import lombok.extern.log4j.Log4j2; -import me.melontini.commander.impl.Commander; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.mappingio.MappingReader; import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch; @@ -11,8 +10,10 @@ import org.objectweb.asm.Type; import java.io.InputStreamReader; -import java.util.List; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Objects; +import java.util.zip.InflaterInputStream; @Log4j2 public record MappingKeeper(MemoryMappingTree mojmapTarget) implements AmbiguousRemapper { @@ -36,11 +37,10 @@ public static MemoryMappingTree loadMojmapTarget(MemoryMappingTree offMojmap, Me public static MemoryMappingTree loadOffMojmap() { if (NAMESPACE.equals("mojang")) return null; log.info("Loading official->mojmap mappings..."); + Path path = FabricLoader.getInstance().getModContainer("commander").orElseThrow().findPath("commander/mappings/processed.bin").orElseThrow(); var tree = new MemoryMappingTree(); - MappingReader.read(Commander.COMMANDER_PATH.resolve("mappings/client_mappings.txt"), new MappingSourceNsSwitch(tree, "target")); - tree.setSrcNamespace("official"); - tree.setDstNamespaces(List.of("mojang")); + MappingReader.read(new InputStreamReader(new InflaterInputStream(Files.newInputStream(path))), tree); return tree; } diff --git a/src/main/java/me/melontini/commander/impl/util/mappings/MinecraftDownloader.java b/src/main/java/me/melontini/commander/impl/util/mappings/MinecraftDownloader.java deleted file mode 100644 index d19decc..0000000 --- a/src/main/java/me/melontini/commander/impl/util/mappings/MinecraftDownloader.java +++ /dev/null @@ -1,61 +0,0 @@ -package me.melontini.commander.impl.util.mappings; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import lombok.SneakyThrows; -import lombok.experimental.ExtensionMethod; -import lombok.extern.log4j.Log4j2; -import me.melontini.commander.impl.Commander; - -import java.io.InputStreamReader; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; - -@ExtensionMethod(Files.class) -@Log4j2 -public final class MinecraftDownloader { - - private static final URL MANIFEST = url("https://launchermeta.mojang.com/mc/game/version_manifest_v2.json"); - - @SneakyThrows - static URL url(String s) { - return new URL(s); - } - - @SneakyThrows - public static void downloadMappings() { - Path mappings = Commander.COMMANDER_PATH.resolve("mappings/client_mappings.txt"); - if (mappings.exists()) return; - downloadIfNotExists(mappings, url(getManifest().getAsJsonObject("downloads") - .getAsJsonObject("client_mappings").get("url").getAsString())); - } - - @SneakyThrows - public static void downloadIfNotExists(Path path, URL url) { - if (!path.exists()) { - try (var stream = url.openStream()) { - path.getParent().createDirectories(); - - log.info("Downloading {}...", path.getFileName().toString()); - path.write(stream.readAllBytes()); - } - } - } - - public static JsonObject getManifest() { - var o = downloadObject(MANIFEST); - for (JsonElement versions : o.getAsJsonArray("versions")) { - if (Commander.MINECRAFT_VERSION.equals(versions.getAsJsonObject().get("id").getAsString())) { - return downloadObject(url(versions.getAsJsonObject().get("url").getAsString())); - } - } - throw new IllegalStateException("Unknown version '%s'".formatted(Commander.MINECRAFT_VERSION)); - } - - @SneakyThrows - private static JsonObject downloadObject(URL url) { - return JsonParser.parseReader(new InputStreamReader(url.openStream())).getAsJsonObject(); - } -}