fix line number remapping, add option to disable mixin refmap remapping (unnecessary in loader 0.4.0+, but needs testting)

dev/0.11
Adrian Siekierka 2019-04-23 22:50:28 +02:00
parent b4beaed968
commit b5b3138e24
5 changed files with 53 additions and 30 deletions

View File

@ -46,6 +46,7 @@ public class LoomGradleExtension {
public String refmapName; public String refmapName;
public String loaderLaunchMethod; public String loaderLaunchMethod;
public boolean remapMod = true; public boolean remapMod = true;
public boolean remapDependencyMixinRefMaps = true;
public boolean autoGenIDERuns = true; public boolean autoGenIDERuns = true;
public boolean extractJars = false; public boolean extractJars = false;

View File

@ -25,15 +25,20 @@
package net.fabricmc.loom; package net.fabricmc.loom;
import net.fabricmc.loom.providers.MappingsProvider; import net.fabricmc.loom.providers.MappingsProvider;
import net.fabricmc.loom.providers.MinecraftAssetsProvider;
import net.fabricmc.loom.providers.MinecraftLibraryProvider; import net.fabricmc.loom.providers.MinecraftLibraryProvider;
import net.fabricmc.loom.task.*; import net.fabricmc.loom.task.*;
import net.fabricmc.loom.task.fernflower.FernFlowerTask; import net.fabricmc.loom.task.fernflower.FernFlowerTask;
import net.fabricmc.loom.util.LineNumberRemapper; import net.fabricmc.loom.util.LineNumberRemapper;
import net.fabricmc.loom.util.progress.ProgressLogger;
import net.fabricmc.stitch.util.StitchUtil;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.tasks.TaskContainer; import org.gradle.api.tasks.TaskContainer;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale; import java.util.Locale;
public class LoomGradlePlugin extends AbstractPlugin { public class LoomGradlePlugin extends AbstractPlugin {
@ -71,8 +76,8 @@ public class LoomGradlePlugin extends AbstractPlugin {
MinecraftLibraryProvider libraryProvider = extension.getMinecraftProvider().libraryProvider; MinecraftLibraryProvider libraryProvider = extension.getMinecraftProvider().libraryProvider;
MappingsProvider mappingsProvider = extension.getMappingsProvider(); MappingsProvider mappingsProvider = extension.getMappingsProvider();
File mappedJar = mappingsProvider.mappedProvider.getMappedJar(); File mappedJar = mappingsProvider.mappedProvider.getMappedJar();
File sourcesJar = getMappedByproduct(project, "-sources-tmp.jar"); File linemappedJarTmp = getMappedByproduct(project, "-linemapped.jar.tmp");
File sourcesFinalJar = getMappedByproduct(project, "-sources.jar"); File sourcesJar = getMappedByproduct(project, "-sources.jar");
File linemapFile = getMappedByproduct(project, "-sources.lmap"); File linemapFile = getMappedByproduct(project, "-sources.lmap");
task.setInput(mappedJar); task.setInput(mappedJar);
@ -80,23 +85,33 @@ public class LoomGradlePlugin extends AbstractPlugin {
task.setLineMapFile(linemapFile); task.setLineMapFile(linemapFile);
task.setLibraries(libraryProvider.getLibraries()); task.setLibraries(libraryProvider.getLibraries());
if (sourcesJar.exists()) {
sourcesJar.delete();
}
task.doLast((tt) -> { task.doLast((tt) -> {
project.getLogger().lifecycle(":readjusting line numbers"); project.getLogger().lifecycle(":adjusting line numbers");
LineNumberRemapper remapper = new LineNumberRemapper(); LineNumberRemapper remapper = new LineNumberRemapper();
remapper.readMappings(linemapFile); remapper.readMappings(linemapFile);
try { ProgressLogger progressLogger = ProgressLogger.getProgressFactory(project, FernFlowerTask.class.getName());
remapper.process(sourcesJar.toPath(), sourcesFinalJar.toPath()); progressLogger.start("Adjusting line numbers", "linemap");
try (StitchUtil.FileSystemDelegate inFs = StitchUtil.getJarFileSystem(mappedJar, true);
StitchUtil.FileSystemDelegate outFs = StitchUtil.getJarFileSystem(linemappedJarTmp, true)) {
remapper.process(progressLogger, inFs.get().getPath("/"), outFs.get().getPath("/"));
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
if (sourcesJar.exists()) { progressLogger.completed();
sourcesJar.delete();
Path mappedJarPath = mappedJar.toPath();
Path linemappedJarTmpPath = linemappedJarTmp.toPath();
if (Files.exists(linemappedJarTmpPath)) {
try {
Files.deleteIfExists(mappedJarPath);
Files.move(linemappedJarTmpPath, mappedJarPath);
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
}); });
}); });

View File

@ -74,7 +74,7 @@ public class ThreadSafeResultSaver implements IResultSaver, IFabricResultSaver {
try { try {
lineMapWriter = new PrintWriter(new FileWriter(lineMapFile.get())); lineMapWriter = new PrintWriter(new FileWriter(lineMapFile.get()));
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Unable to create LineMap file.", e); throw new RuntimeException("Unable to create line mapping file: " + lineMapFile.get(), e);
} }
} }
} }
@ -100,16 +100,15 @@ public class ThreadSafeResultSaver implements IResultSaver, IFabricResultSaver {
} }
if (mapping != null && lineMapWriter != null) { if (mapping != null && lineMapWriter != null) {
int maxLine = 0; int maxLine = 0;
int maxLineDist = 0; int maxLineDest = 0;
// 2 loops here is meh, perhaps merge with a buffer? StringBuilder builder = new StringBuilder();
for (int i = 0; i < mapping.length; i += 2) { for (int i = 0; i < mapping.length; i += 2) {
maxLine = Math.max(maxLine, mapping[i]); maxLine = Math.max(maxLine, mapping[i]);
maxLineDist = Math.max(maxLineDist, mapping[i + 1]); maxLineDest = Math.max(maxLineDest, mapping[i + 1]);
} builder.append("\t").append(mapping[i]).append("\t").append(mapping[i + 1]).append("\n");
lineMapWriter.println(qualifiedName + "\t" + maxLine + "\t" + maxLineDist);
for (int i = 0; i < mapping.length; i += 2) {
lineMapWriter.println("\t" + mapping[i] + "\t" + mapping[i + 1]);
} }
lineMapWriter.println(qualifiedName + "\t" + maxLine + "\t" + maxLineDest);
lineMapWriter.println(builder.toString());
} }
}); });
} }

View File

@ -24,6 +24,8 @@
package net.fabricmc.loom.util; package net.fabricmc.loom.util;
import net.fabricmc.loom.util.progress.ProgressLogger;
import org.gradle.api.logging.Logger;
import org.objectweb.asm.*; import org.objectweb.asm.*;
import java.io.*; import java.io.*;
@ -56,9 +58,9 @@ public class LineNumberRemapper {
if (line.charAt(0) != '\t') { if (line.charAt(0) != '\t') {
clazz = lineMap.computeIfAbsent(segs[0], RClass::new); clazz = lineMap.computeIfAbsent(segs[0], RClass::new);
clazz.maxLine = Integer.parseInt(segs[1]); clazz.maxLine = Integer.parseInt(segs[1]);
clazz.maxLineDist = Integer.parseInt(segs[2]); clazz.maxLineDest = Integer.parseInt(segs[2]);
} else { } else {
clazz.lineMap.put(Integer.parseInt(segs[0]), (Integer) Integer.parseInt(segs[1])); clazz.lineMap.put(Integer.parseInt(segs[0]), Integer.parseInt(segs[1]));
} }
i++; i++;
} }
@ -70,7 +72,7 @@ public class LineNumberRemapper {
} }
} }
public void process(Path input, Path output) throws IOException { public void process(ProgressLogger logger, Path input, Path output) throws IOException {
Files.walkFileTree(input, new SimpleFileVisitor<Path>() { Files.walkFileTree(input, new SimpleFileVisitor<Path>() {
@Override @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
@ -86,7 +88,11 @@ public class LineNumberRemapper {
Files.delete(dst); Files.delete(dst);
} }
String idx = rel.substring(0, rel.length() - 6); String idx = rel.substring(0, rel.length() - 6);
int dollarPos = idx.indexOf('$');//This makes the assumption that only Java classes are to be remapped. if (logger != null) {
logger.progress("Remapping " + idx);
}
int dollarPos = idx.indexOf('$'); //This makes the assumption that only Java classes are to be remapped.
if (dollarPos >= 0) { if (dollarPos >= 0) {
idx = idx.substring(0, dollarPos); idx = idx.substring(0, dollarPos);
} }
@ -127,13 +133,13 @@ public class LineNumberRemapper {
if (tLine <= 0) { if (tLine <= 0) {
super.visitLineNumber(line, start); super.visitLineNumber(line, start);
} else if (tLine >= rClass.maxLine) { } else if (tLine >= rClass.maxLine) {
super.visitLineNumber(rClass.maxLineDist, start); super.visitLineNumber(rClass.maxLineDest, start);
} else { } else {
Integer matchedLine = null; Integer matchedLine = null;
while (tLine <= rClass.maxLine && ((matchedLine = rClass.lineMap.get(tLine)) != null)) { while (tLine <= rClass.maxLine && ((matchedLine = rClass.lineMap.get(tLine)) == null)) {
tLine++; tLine++;
} }
super.visitLineNumber(matchedLine != null ? matchedLine : rClass.maxLineDist, start); super.visitLineNumber(matchedLine != null ? matchedLine : rClass.maxLineDest, start);
} }
} }
}; };
@ -144,7 +150,7 @@ public class LineNumberRemapper {
private final String name; private final String name;
private int maxLine; private int maxLine;
private int maxLineDist; private int maxLineDest;
private Map<Integer, Integer> lineMap = new HashMap<>(); private Map<Integer, Integer> lineMap = new HashMap<>();
private RClass(String name) { private RClass(String name) {

View File

@ -182,9 +182,11 @@ public class ModProcessor {
throw new RuntimeException("Failed to remap JAR to " + toM + " file not found: " + output.getAbsolutePath()); throw new RuntimeException("Failed to remap JAR to " + toM + " file not found: " + output.getAbsolutePath());
} }
if (MixinRefmapHelper.transformRefmaps(remapper, output)) { if (extension.remapDependencyMixinRefMaps) {
project.getLogger().lifecycle(":remapping " + input.getName() + " (Mixin reference maps)"); if (MixinRefmapHelper.transformRefmaps(remapper, output)) {
remapper.finish(); project.getLogger().lifecycle(":remapping " + input.getName() + " (Mixin reference maps)");
remapper.finish();
}
} }
} }