allow stand-alone remapper creation
All checks were successful
Publish to snapshot maven / build (push) Successful in 52s

This commit is contained in:
moehreag 2024-10-02 16:03:33 +02:00
parent e08ed69730
commit f5f4cb1d4f
2 changed files with 22 additions and 18 deletions

View file

@ -8,7 +8,7 @@ plugins {
} }
group = "dev.frogmc" group = "dev.frogmc"
version = "0.0.1-alpha.17" version = "0.0.1-alpha.18"
repositories { repositories {
mavenCentral() mavenCentral()

View file

@ -3,10 +3,7 @@ package dev.frogmc.thyroxine;
import java.io.IOException; import java.io.IOException;
import java.nio.file.*; import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@ -25,6 +22,7 @@ import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.commons.ClassRemapper; import org.objectweb.asm.commons.ClassRemapper;
import org.objectweb.asm.commons.Remapper;
public class Thyroxine { public class Thyroxine {
@ -32,8 +30,9 @@ public class Thyroxine {
public static void run(String minecraftVersion, Path inputJar, Path outputJar, boolean skipMetaInf, boolean renameParameters) throws IOException, InterruptedException { public static void run(String minecraftVersion, Path inputJar, Path outputJar, boolean skipMetaInf, boolean renameParameters) throws IOException, InterruptedException {
Path out = outputJar.toAbsolutePath(); Path out = outputJar.toAbsolutePath();
MappingBundle data = MojmapProvider.get(minecraftVersion, "client", MappingBundle data = MojmapProvider.get(minecraftVersion,
out.resolveSibling("client-" + minecraftVersion + ".txt")).reverse(); out.resolveSibling("client-" + minecraftVersion + ".txt"),
out.resolveSibling("server-" + minecraftVersion + ".txt")).reverse();
MappingBundle parchment = null; MappingBundle parchment = null;
if (renameParameters) { if (renameParameters) {
@ -81,17 +80,15 @@ public class Thyroxine {
FileSystem inFs = FileSystems.newFileSystem(inputJar); FileSystem inFs = FileSystems.newFileSystem(inputJar);
FileSystem outFs = FileSystems.newFileSystem(outputJar, Map.of("create", "true"))) { FileSystem outFs = FileSystems.newFileSystem(outputJar, Map.of("create", "true"))) {
List<Callable<Void>> tasks = new ArrayList<>(); List<Callable<Void>> tasks = new ArrayList<>();
FileSystem[] contexts = new FileSystem[context.length+1]; Map<Path, FileSystem> contexts = new HashMap<>(context.length + 1);
contexts[0] = inFs; contexts.put(inputJar, inFs);
for (int i = 0, contextLength = context.length; i < contextLength; i++) { for (Path p : context) {
Path path = context[i]; if (!contexts.containsKey(p)) {
contexts[i+1] = FileSystems.newFileSystem(path); contexts.put(p, FileSystems.newFileSystem(p));
}
} }
Map<String, List<String>> lazyParents = new ConcurrentHashMap<>(); Remapper mapper = createMapper(data, contexts.values());
MappingData reverseData = data.reverse();
Mapper mapper = new Mapper(data, className -> lazyParents.computeIfAbsent(className, ignored ->
computeInheritances(className, reverseData, contexts)));
Files.walkFileTree(inFs.getPath("/"), new SimpleFileVisitor<>() { Files.walkFileTree(inFs.getPath("/"), new SimpleFileVisitor<>() {
@ -143,7 +140,7 @@ public class Thyroxine {
exec.invokeAll(tasks); exec.invokeAll(tasks);
for (FileSystem fs : contexts) { for (FileSystem fs : contexts.values()) {
fs.close(); fs.close();
} }
@ -153,6 +150,13 @@ public class Thyroxine {
} }
} }
public static Remapper createMapper(MappingData data, Collection<FileSystem> contexts) {
Map<String, List<String>> lazyParents = new ConcurrentHashMap<>();
MappingData reverseData = data.reverse();
return new Mapper(data, className -> lazyParents.computeIfAbsent(className, ignored ->
computeInheritances(className, reverseData, contexts)));
}
public static MappingBundle parse(Path mappings) throws IOException { public static MappingBundle parse(Path mappings) throws IOException {
String data = Files.readString(mappings); String data = Files.readString(mappings);
String header = data.split("\n", 2)[0]; String header = data.split("\n", 2)[0];
@ -190,7 +194,7 @@ public class Thyroxine {
return false; return false;
} }
private static List<String> computeInheritances(String className, MappingData reverse, FileSystem... jars) { private static List<String> computeInheritances(String className, MappingData reverse, Collection<FileSystem> jars) {
try { try {
if (reverse.classes().containsKey(className)) { if (reverse.classes().containsKey(className)) {
className = reverse.classes().get(className); className = reverse.classes().get(className);