Skip to content

Commit

Permalink
Ugh... I'm not sure if I want to do this.
Browse files Browse the repository at this point in the history
  • Loading branch information
melontini committed Apr 18, 2024
1 parent 7a315fe commit e0cf7f8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 79 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
}
Expand Down
65 changes: 65 additions & 0 deletions gradle/mappings.gradle
Original file line number Diff line number Diff line change
@@ -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()
}
14 changes: 1 addition & 13 deletions src/main/java/me/melontini/commander/impl/Commander.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -73,13 +68,11 @@ public void onInitialize() {
EvalUtils.init();

try {
MinecraftDownloader.downloadMappings();

CompletableFuture<MemoryMappingTree> offMojmap = CompletableFuture.supplyAsync(MappingKeeper::loadOffMojmap, Util.getMainWorkerExecutor());
CompletableFuture<MemoryMappingTree> 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.
}

Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
}

Expand Down

This file was deleted.

0 comments on commit e0cf7f8

Please sign in to comment.