Signed-off-by: shedaniel <daniel@shedaniel.me>
This commit is contained in:
shedaniel
2021-09-24 00:36:09 +08:00
parent 5bf22dd50d
commit 9033d1e781
12 changed files with 216 additions and 93 deletions

View File

@@ -148,7 +148,7 @@ public class ModProcessor {
Stopwatch stopwatch = Stopwatch.createStarted();
project.getLogger().lifecycle(":remapping " + remapList.size() + " mods (TinyRemapper, " + fromM + " -> " + toM + ")");
MemoryMappingTree mappings = extension.isForge() ? mappingsProvider.getMappingsWithSrg() : mappingsProvider.getMappings();
MemoryMappingTree mappings = (fromM.equals("srg") || toM.equals("srg")) && extension.isForge() ? mappingsProvider.getMappingsWithSrg() : mappingsProvider.getMappings();
LoggerFilter.replaceSystemOut();
TinyRemapper remapper = TinyRemapper.newRemapper()
.logger(project.getLogger()::lifecycle)

View File

@@ -114,7 +114,7 @@ public class FieldMigratedMappingsProvider extends MappingsProviderImpl {
if (getExtension().shouldGenerateSrgTiny()) {
if (Files.notExists(rawTinyMappingsWithSrg) || isRefreshDeps()) {
// Merge tiny mappings with srg
SrgMerger.mergeSrg(getProject().getLogger(), this::getMojmapSrgFileIfPossible, getRawSrgFile(), rawTinyMappings, rawTinyMappingsWithSrg, true);
SrgMerger.mergeSrg(getProject().getLogger(), null, getRawSrgFile(), rawTinyMappings, rawTinyMappingsWithSrg, true);
}
}

View File

@@ -96,6 +96,7 @@ import net.fabricmc.loom.util.TinyRemapperHelper;
import net.fabricmc.loom.util.function.FsPathConsumer;
import net.fabricmc.loom.util.srg.InnerClassRemapper;
import net.fabricmc.loom.util.srg.SpecialSourceExecutor;
import net.fabricmc.loom.util.srg.SrgMerger;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
public class MinecraftPatchedProvider extends DependencyProvider {
@@ -308,7 +309,14 @@ public class MinecraftPatchedProvider extends DependencyProvider {
private TinyRemapper buildRemapper(Path input) throws IOException {
Path[] libraries = TinyRemapperHelper.getMinecraftDependencies(getProject());
MemoryMappingTree mappingsWithSrg = getExtension().getMappingsProvider().getMappingsWithSrg();
MemoryMappingTree mappingsWithSrg;
if (getExtension().isForgeAndOfficial()) {
mappingsWithSrg = SrgMerger.mergeSrg(getProject().getLogger(), getExtension().getMappingsProvider()::getMojmapSrgFileIfPossible, getExtension().getSrgProvider().getMergedMojangTrimmed(), getExtension().getMappingsProvider().tinyMappings, true);
} else {
mappingsWithSrg = getExtension().getMappingsProvider().getMappingsWithSrg();
}
TinyRemapper remapper = TinyRemapper.newRemapper()
.logger(getProject().getLogger()::lifecycle)
.logUnknownInvokeDynamic(false)
@@ -355,7 +363,7 @@ public class MinecraftPatchedProvider extends DependencyProvider {
private Path getToSrgMappings() throws IOException {
if (getExtension().getSrgProvider().isTsrgV2()) {
return getExtension().getSrgProvider().getMergedMojang();
return getExtension().getSrgProvider().getMergedMojangRaw();
} else {
return getExtension().getMcpConfigProvider().getMappings();
}
@@ -483,6 +491,7 @@ public class MinecraftPatchedProvider extends DependencyProvider {
List<File> toDelete = new ArrayList<>();
String atDependency = Constants.Dependencies.ACCESS_TRANSFORMERS + Constants.Dependencies.Versions.ACCESS_TRANSFORMERS;
FileCollection classpath = DependencyDownloader.download(getProject(), atDependency);
Stopwatch stopwatch = Stopwatch.createStarted();
logger.lifecycle(":access transforming minecraft");
@@ -537,6 +546,8 @@ public class MinecraftPatchedProvider extends DependencyProvider {
for (File file : toDelete) {
file.delete();
}
logger.lifecycle(":access transformed minecraft in " + stopwatch.stop());
}
public enum Environment {

View File

@@ -28,6 +28,7 @@ import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.net.URI;
@@ -38,13 +39,20 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.output.NullOutputStream;
import org.gradle.api.Project;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.logging.configuration.ShowStacktrace;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.LoomGradlePlugin;
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
import net.fabricmc.loom.configuration.DependencyProvider;
import net.fabricmc.loom.configuration.providers.mappings.GradleMappingContext;
@@ -55,14 +63,18 @@ import net.fabricmc.loom.util.srg.Tsrg2Utils;
import net.fabricmc.loom.util.srg.Tsrg2Writer;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.MappingVisitor;
import net.fabricmc.mappingio.adapter.ForwardingMappingVisitor;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
public class SrgProvider extends DependencyProvider {
private Path srg;
private Boolean isTsrgV2;
private Path mergedMojangRaw;
private Path mergedMojang;
private Path mergedMojangTrimmed;
private static Path mojmapTsrg;
private static Path mojmapTsrg2;
public SrgProvider(Project project) {
super(project);
@@ -85,7 +97,18 @@ public class SrgProvider extends DependencyProvider {
}
if (isTsrgV2) {
if (!Files.exists(mergedMojang) || !Files.exists(mergedMojangTrimmed) || isRefreshDeps()) {
if (!Files.exists(mergedMojangRaw) || !Files.exists(mergedMojang) || !Files.exists(mergedMojangTrimmed) || isRefreshDeps()) {
Stopwatch stopwatch = Stopwatch.createStarted();
getProject().getLogger().lifecycle(":merging mappings (InstallerTools, srg + mojmap)");
PrintStream out = System.out;
PrintStream err = System.err;
if (getProject().getGradle().getStartParameter().getLogLevel().compareTo(LogLevel.LIFECYCLE) >= 0) {
System.setOut(new PrintStream(NullOutputStream.NULL_OUTPUT_STREAM));
System.setErr(new PrintStream(NullOutputStream.NULL_OUTPUT_STREAM));
}
Files.deleteIfExists(mergedMojangRaw);
Files.deleteIfExists(mergedMojang);
net.minecraftforge.installertools.ConsoleTool.main(new String[] {
"--task",
@@ -93,14 +116,15 @@ public class SrgProvider extends DependencyProvider {
"--left",
getSrg().toAbsolutePath().toString(),
"--right",
getMojmapTsrg(getProject(), getExtension()).toAbsolutePath().toString(),
getMojmapTsrg2(getProject(), getExtension()).toAbsolutePath().toString(),
"--classes",
"--output",
mergedMojang.toAbsolutePath().toString()
mergedMojangRaw.toAbsolutePath().toString()
});
MemoryMappingTree tree = new MemoryMappingTree();
MappingReader.read(new StringReader(FileUtils.readFileToString(mergedMojang.toFile(), StandardCharsets.UTF_8)), tree);
MappingReader.read(new StringReader(FileUtils.readFileToString(mergedMojangRaw.toFile(), StandardCharsets.UTF_8)), new FieldDescWrappingVisitor(tree));
Files.writeString(mergedMojang, Tsrg2Writer.serialize(tree), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
for (MappingTree.ClassMapping classDef : tree.getClasses()) {
for (MappingTree.MethodMapping methodDef : classDef.getMethods()) {
@@ -109,13 +133,60 @@ public class SrgProvider extends DependencyProvider {
}
Files.writeString(mergedMojangTrimmed, Tsrg2Writer.serialize(tree), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
if (getProject().getGradle().getStartParameter().getLogLevel().compareTo(LogLevel.LIFECYCLE) >= 0) {
System.setOut(out);
System.setErr(err);
}
getProject().getLogger().lifecycle(":merged mappings (InstallerTools, srg + mojmap) in " + stopwatch.stop());
}
}
}
// Read mojmap and apply field descs to the tsrg2
private class FieldDescWrappingVisitor extends ForwardingMappingVisitor {
private final Map<FieldKey, String> fieldDescMap = new HashMap<>();
private String lastClass;
protected FieldDescWrappingVisitor(MappingVisitor next) throws IOException {
super(next);
MemoryMappingTree mojmap = new MemoryMappingTree();
MappingReader.read(getMojmapTsrg2(getProject(), getExtension()), mojmap);
for (MappingTree.ClassMapping classMapping : mojmap.getClasses()) {
for (MappingTree.FieldMapping fieldMapping : classMapping.getFields()) {
fieldDescMap.put(new FieldKey(classMapping.getSrcName(), fieldMapping.getSrcName()), fieldMapping.getSrcDesc());
}
}
}
@Override
public boolean visitClass(String srcName) throws IOException {
if (super.visitClass(srcName)) {
this.lastClass = srcName;
return true;
} else {
return false;
}
}
@Override
public boolean visitField(String srcName, String srcDesc) throws IOException {
if (srcDesc == null) {
srcDesc = fieldDescMap.get(new FieldKey(lastClass, srcName));
}
return super.visitField(srcName, srcDesc);
}
private record FieldKey(String owner, String name) {}
}
private void init(String version) {
File dir = getMinecraftProvider().dir("srg/" + version);
srg = new File(dir, "srg.tsrg").toPath();
mergedMojangRaw = new File(dir, "srg-mojmap-merged-raw.tsrg").toPath();
mergedMojang = new File(dir, "srg-mojmap-merged.tsrg").toPath();
mergedMojangTrimmed = new File(dir, "srg-mojmap-merged-trimmed.tsrg").toPath();
}
@@ -124,6 +195,12 @@ public class SrgProvider extends DependencyProvider {
return srg;
}
public Path getMergedMojangRaw() {
if (!isTsrgV2()) throw new IllegalStateException("May not access merged mojmap srg if not on modern Minecraft!");
return mergedMojangRaw;
}
public Path getMergedMojang() {
if (!isTsrgV2()) throw new IllegalStateException("May not access merged mojmap srg if not on modern Minecraft!");
@@ -141,30 +218,34 @@ public class SrgProvider extends DependencyProvider {
}
public static Path getMojmapTsrg(Project project, LoomGradleExtension extension) throws IOException {
Path path = extension.getMinecraftProvider().dir("forge").toPath().resolve("mojmap.tsrg");
if (mojmapTsrg != null) return mojmapTsrg;
if (Files.notExists(path)) {
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
mojmapTsrg = extension.getMinecraftProvider().dir("forge").toPath().resolve("mojmap.tsrg");
if (Files.notExists(mojmapTsrg) || LoomGradlePlugin.refreshDeps) {
try (BufferedWriter writer = Files.newBufferedWriter(mojmapTsrg, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
Tsrg2Utils.writeTsrg(visitor -> visitMojmap(visitor, project),
MappingsNamespace.NAMED.toString(), false, writer);
}
}
return path;
return mojmapTsrg;
}
public static Path getMojmapTsrg2(Project project, LoomGradleExtension extension) throws IOException {
Path path = extension.getMinecraftProvider().dir("forge").toPath().resolve("mojmap.tsrg2");
if (mojmapTsrg2 != null) return mojmapTsrg2;
if (Files.notExists(path)) {
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
mojmapTsrg2 = extension.getMinecraftProvider().dir("forge").toPath().resolve("mojmap.tsrg2");
if (Files.notExists(mojmapTsrg2) || LoomGradlePlugin.refreshDeps) {
try (BufferedWriter writer = Files.newBufferedWriter(mojmapTsrg2, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
MemoryMappingTree tree = new MemoryMappingTree();
visitMojmap(tree, project);
writer.write(Tsrg2Writer.serialize(tree));
}
}
return path;
return mojmapTsrg2;
}
private static void visitMojmap(MappingVisitor visitor, Project project) {

View File

@@ -156,7 +156,7 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings
if (getExtension().shouldGenerateSrgTiny()) {
if (Files.notExists(tinyMappingsWithSrg) || isRefreshDeps()) {
// Merge tiny mappings with srg
SrgMerger.mergeSrg(getProject().getLogger(), this::getMojmapSrgFileIfPossible, getRawSrgFile(), tinyMappings, tinyMappingsWithSrg, true);
SrgMerger.mergeSrg(getProject().getLogger(), null, getRawSrgFile(), tinyMappings, tinyMappingsWithSrg, true);
}
mappingTreeWithSrg = readMappings(tinyMappingsWithSrg);
@@ -241,7 +241,7 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings
return extension.getSrgProvider().getSrg();
}
protected Path getMojmapSrgFileIfPossible() {
public Path getMojmapSrgFileIfPossible() {
try {
LoomGradleExtension extension = getExtension();
return SrgProvider.getMojmapTsrg2(getProject(), extension);

View File

@@ -299,10 +299,10 @@ public class MinecraftMappedProvider extends DependencyProvider {
public Set<IMappingProvider> getMappings(@Nullable Set<String> fromClassNames, String fromM, String toM, Mutable<MemoryMappingTree> mappings) throws IOException {
Set<IMappingProvider> providers = new HashSet<>();
mappings.setValue(getExtension().isForge() ? getExtension().getMappingsProvider().getMappingsWithSrg() : getExtension().getMappingsProvider().getMappings());
mappings.setValue((true || fromM.equals("srg") || toM.equals("srg")) && getExtension().isForge() ? getExtension().getMappingsProvider().getMappingsWithSrg() : getExtension().getMappingsProvider().getMappings());
providers.add(TinyRemapperHelper.create(mappings.getValue(), fromM, toM, true));
if (getExtension().isForge()) {
if (getExtension().isForge() && (true || fromM.equals("srg") || toM.equals("srg"))) {
if (fromClassNames != null) {
providers.add(InnerClassRemapper.of(fromClassNames, getExtension().getMappingsProvider().getMappingsWithSrg(), fromM, toM));
}

View File

@@ -165,7 +165,7 @@ public class RemapJarTask extends Jar {
}
private ReferenceRemapper createReferenceRemapper(LoomGradleExtension extension, String from, String to) throws IOException {
MappingTree mappings = extension.shouldGenerateSrgTiny() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings();
MappingTree mappings = (from.equals("srg") || to.equals("srg")) && extension.shouldGenerateSrgTiny() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings();
return new SimpleReferenceRemapper(new SimpleReferenceRemapper.Remapper() {
@Override
@@ -248,7 +248,7 @@ public class RemapJarTask extends Jar {
if (isMainRemapTask) {
jarRemapper.addToClasspath(getRemapClasspath());
jarRemapper.addMappings(TinyRemapperHelper.create(extension.shouldGenerateSrgTiny() ? mappingsProvider.getMappingsWithSrg() : mappingsProvider.getMappings(), fromM, toM, false));
jarRemapper.addMappings(TinyRemapperHelper.create((fromM.equals("srg") || toM.equals("srg")) && extension.shouldGenerateSrgTiny() ? mappingsProvider.getMappingsWithSrg() : mappingsProvider.getMappings(), fromM, toM, false));
}
for (File mixinMapFile : extension.getAllMixinMappings()) {
@@ -403,7 +403,7 @@ public class RemapJarTask extends Jar {
}
private IMappingProvider remapToSrg(LoomGradleExtension extension, IMappingProvider parent, String from, String to) throws IOException {
MappingTree mappings = extension.shouldGenerateSrgTiny() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings();
MappingTree mappings = (from.equals("srg") || to.equals("srg")) && extension.shouldGenerateSrgTiny() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings();
return sink -> {
parent.load(new IMappingProvider.MappingAcceptor() {
@@ -503,7 +503,7 @@ public class RemapJarTask extends Jar {
}
LoomGradleExtension extension = LoomGradleExtension.get(getProject());
MappingTree mappings = extension.shouldGenerateSrgTiny() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings();
MappingTree mappings = (fromM.get().equals("srg") || toM.get().equals("srg")) && extension.shouldGenerateSrgTiny() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings();
try (TinyMappingsReader reader = new TinyMappingsReader(mappings, fromM.get(), toM.get())) {
MappingSet mappingSet = reader.read();

View File

@@ -184,7 +184,7 @@ public class SourceRemapper {
MappingSet mappings = extension.getOrCreateSrcMappingCache(id, () -> {
try {
MemoryMappingTree m = extension.shouldGenerateSrgTiny() ? mappingsProvider.getMappingsWithSrg() : mappingsProvider.getMappings();
MemoryMappingTree m = (from.equals("srg") || to.equals("srg")) && extension.shouldGenerateSrgTiny() ? mappingsProvider.getMappingsWithSrg() : mappingsProvider.getMappings();
project.getLogger().info(":loading " + from + " -> " + to + " source mappings");
return new TinyMappingsReader(m, from, to).read();
} catch (Exception e) {

View File

@@ -71,7 +71,7 @@ public final class TinyRemapperHelper {
TinyRemapper remapper = _getTinyRemapper(project, fixRecords).getKey();
remapper.replaceMappings(ImmutableSet.of(
TinyRemapperHelper.create(extension.isForge() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings(), fromM, toM, true),
TinyRemapperHelper.create((fromM.equals("srg") || toM.equals("srg")) && extension.isForge() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings(), fromM, toM, true),
out -> TinyRemapperHelper.JSR_TO_JETBRAINS.forEach(out::acceptClass)
));
return remapper;

View File

@@ -103,6 +103,7 @@ public class SpecialSourceExecutor {
Path output = extension.getFiles().getProjectBuildCache().toPath().resolve(officialJar.getFileName().toString().substring(0, officialJar.getFileName().toString().length() - 4) + "-srg-output.jar");
Files.deleteIfExists(output);
stopwatch = Stopwatch.createStarted();
if (specialSource) {
String[] args = new String[] {
@@ -134,6 +135,8 @@ public class SpecialSourceExecutor {
spec.setErrorOutput(NullOutputStream.NULL_OUTPUT_STREAM);
}
}).rethrowFailure().assertNormalExitValue();
project.getLogger().lifecycle(":remapped minecraft (SpecialSource, " + side + ", official -> srg) in " + stopwatch.stop());
} else {
List<String> args = new ArrayList<>(Arrays.asList(
"--jar-in",
@@ -172,6 +175,8 @@ public class SpecialSourceExecutor {
spec.setErrorOutput(NullOutputStream.NULL_OUTPUT_STREAM);
}
}).rethrowFailure().assertNormalExitValue();
project.getLogger().lifecycle(":remapped minecraft (Vignette, " + side + ", official -> mojang) in " + stopwatch.stop());
}
Files.deleteIfExists(stripped);

View File

@@ -1,7 +1,7 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2020-2021 FabricMC
* Copyright (c) 2016, 2017, 2018 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -35,7 +35,6 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -46,8 +45,8 @@ import org.gradle.api.logging.Logger;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.loom.util.function.CollectionUtil;
import net.fabricmc.mappingio.FlatMappingVisitor;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.adapter.MappingDstNsReorder;
import net.fabricmc.mappingio.adapter.RegularAsFlatMappingVisitor;
import net.fabricmc.mappingio.format.Tiny2Writer;
import net.fabricmc.mappingio.format.TsrgReader;
@@ -73,9 +72,18 @@ public final class SrgMerger {
* or if an element mentioned in the SRG file does not have tiny mappings
*/
public static void mergeSrg(Logger logger, Supplier<Path> mojmap, Path srg, Path tiny, Path out, boolean lenient) throws IOException, MappingException {
MemoryMappingTree tree = mergeSrg(logger, mojmap, srg, tiny, lenient);
try (Tiny2Writer writer = new Tiny2Writer(Files.newBufferedWriter(out), false)) {
tree.accept(writer);
} catch (IOException e) {
e.printStackTrace();
}
}
public static MemoryMappingTree mergeSrg(Logger logger, Supplier<Path> mojmap, Path srg, Path tiny, boolean lenient) throws IOException, MappingException {
Map<String, List<MappingTreeView.MemberMappingView>> addRegardlessSrgs = new HashMap<>();
MemoryMappingTree arr = readSrg(srg, mojmap, addRegardlessSrgs);
addRegardlessSrgs.clear();
MemoryMappingTree foss = new MemoryMappingTree();
try (BufferedReader reader = Files.newBufferedReader(tiny)) {
@@ -87,19 +95,14 @@ public final class SrgMerger {
}
MemoryMappingTree output = new MemoryMappingTree();
output.visitNamespaces(foss.getSrcNamespace(), Stream.concat(foss.getDstNamespaces().stream(), Stream.of("srg")).collect(Collectors.toList()));
output.visitNamespaces(foss.getSrcNamespace(), Stream.concat(Stream.of("srg"), foss.getDstNamespaces().stream()).collect(Collectors.toList()));
RegularAsFlatMappingVisitor flatMappingVisitor = new RegularAsFlatMappingVisitor(output);
for (MappingTree.ClassMapping klass : arr.getClasses()) {
classToTiny(logger, addRegardlessSrgs, klass, foss, flatMappingVisitor, output, lenient);
classToTiny(logger, addRegardlessSrgs, foss, klass, output, flatMappingVisitor, lenient);
}
try (Tiny2Writer writer = new Tiny2Writer(Files.newBufferedWriter(out), false)) {
MappingDstNsReorder reorder = new MappingDstNsReorder(writer, Stream.concat(Stream.of("srg"), foss.getDstNamespaces().stream()).collect(Collectors.toList()));
output.accept(reorder);
} catch (IOException e) {
e.printStackTrace();
}
return output;
}
private static MemoryMappingTree readSrg(Path srg, Supplier<Path> mojmap, Map<String, List<MappingTreeView.MemberMappingView>> addRegardlessSrgs)
@@ -107,20 +110,17 @@ public final class SrgMerger {
try (BufferedReader reader = Files.newBufferedReader(srg)) {
String content = IOUtils.toString(reader);
if (content.startsWith("tsrg2")) {
return readTsrg2(content, mojmap, addRegardlessSrgs);
} else {
MemoryMappingTree tsrg = new MemoryMappingTree();
TsrgReader.read(new StringReader(content), tsrg);
return tsrg;
if (content.startsWith("tsrg2") && mojmap != null) {
addRegardlessSrgs(mojmap, addRegardlessSrgs);
}
MemoryMappingTree tsrg = new MemoryMappingTree();
TsrgReader.read(new StringReader(content), tsrg);
return tsrg;
}
}
private static MemoryMappingTree readTsrg2(String content, Supplier<Path> mojmap, Map<String, List<MappingTreeView.MemberMappingView>> addRegardlessSrgs)
throws IOException {
MemoryMappingTree tsrg2 = new MemoryMappingTree();
TsrgReader.read(new StringReader(content), tsrg2);
private static void addRegardlessSrgs(Supplier<Path> mojmap, Map<String, List<MappingTreeView.MemberMappingView>> addRegardlessSrgs) throws IOException {
MemoryMappingTree mojmapTree = readTsrg2ToTinyTree(mojmap.get());
for (MappingTree.ClassMapping classDef : mojmapTree.getClasses()) {
@@ -138,8 +138,6 @@ public final class SrgMerger {
}
}
}
return tsrg2;
}
private static MemoryMappingTree readTsrg2ToTinyTree(Path path) throws IOException {
@@ -152,14 +150,13 @@ public final class SrgMerger {
return tree;
}
private static void classToTiny(Logger logger, Map<String, List<MappingTreeView.MemberMappingView>> addRegardlessSrgs, MappingTree.ClassMapping klass, MemoryMappingTree foss, RegularAsFlatMappingVisitor flatOutput, MemoryMappingTree output, boolean lenient)
private static void classToTiny(Logger logger, Map<String, List<MappingTreeView.MemberMappingView>> addRegardlessSrgs, MappingTree foss, MappingTree.ClassMapping klass, MappingTree output, FlatMappingVisitor flatOutput, boolean lenient)
throws IOException {
String obf = klass.getSrcName();
String srg = klass.getDstName(0);
MappingTree.ClassMapping fossClass = foss.getClass(obf);
int srgId = output.getNamespaceId("srg");
MappingTree.ClassMapping classDef = foss.getClass(obf);
if (fossClass == null) {
if (classDef == null) {
if (lenient) {
return;
} else {
@@ -167,63 +164,92 @@ public final class SrgMerger {
}
}
flatOutput.visitClass(obf, output.getDstNamespaces().stream().map(ns ->
ns.equals("srg") ? srg : fossClass.getName(ns)).toArray(String[]::new));
List<String> classNames = CollectionUtil.map(
output.getDstNamespaces(),
namespace -> "srg".equals(namespace) ? srg : classDef.getName(namespace)
);
flatOutput.visitClass(obf, classNames.toArray(new String[0]));
for (MappingTree.MethodMapping method : klass.getMethods()) {
MappingTree.MethodMapping fossMethod = CollectionUtil.find(
fossClass.getMethods(),
m -> m.getSrcName().equals(method.getSrcName()) && m.getSrcDesc().equals(method.getSrcDesc())
MappingTree.MethodMapping def = CollectionUtil.find(
classDef.getMethods(),
m -> m.getName("official").equals(method.getSrcName()) && m.getDesc("official").equals(method.getSrcDesc())
).orElse(null);
if (fossMethod == null) {
if (tryMatchRegardlessSrgs(addRegardlessSrgs, obf, method)) {
flatOutput.visitMethod(obf, method.getSrcName(), method.getSrcDesc(), output.getDstNamespaces().stream().map(ns ->
ns.equals("srg") ? method.getDstName(0) : method.getSrcName()).toArray(String[]::new));
continue;
}
if (def == null) {
if (tryMatchRegardlessSrgsMethod(addRegardlessSrgs, obf, output, flatOutput, method)) continue;
if (!lenient) {
throw new MappingException("Missing method: " + method.getSrcName() + " (srg: " + method.getDstName(0) + ")");
}
logger.debug("Missing method: " + method.getSrcName() + method.getSrcDesc() + " (srg: " + method.getDstName(0) + ") " + fossClass.getMethods().size() + " methods in the original class");
continue;
}
flatOutput.visitMethod(obf, fossMethod.getSrcName(), fossMethod.getSrcDesc(), output.getDstNamespaces().stream().map(ns ->
ns.equals("srg") ? method.getDstName(0) : fossMethod.getName(ns)).toArray(String[]::new));
List<String> methodNames = CollectionUtil.map(
output.getDstNamespaces(),
namespace -> "srg".equals(namespace) ? method.getDstName(0) : def.getName(namespace)
);
for (MappingTree.MethodArgMapping arg : fossMethod.getArgs()) {
flatOutput.visitMethodArg(obf, fossMethod.getSrcName(), fossMethod.getSrcDesc(), arg.getArgPosition(),
arg.getLvIndex(), arg.getSrcName(), output.getDstNamespaces().stream().map(ns ->
ns.equals("srg") ? arg.getName("named") : arg.getName(ns)).toArray(String[]::new));
}
flatOutput.visitMethod(obf, def.getName("official"), def.getDesc("official"), methodNames.toArray(new String[0]));
}
for (MappingTree.FieldMapping field : klass.getFields()) {
MappingTree.FieldMapping fossField = CollectionUtil.find(
fossClass.getFields(),
f -> f.getSrcName().equals(field.getSrcName())
MappingTree.FieldMapping def = CollectionUtil.find(
classDef.getFields(),
f -> f.getName("official").equals(field.getSrcName())
).orElse(nullOrThrow(lenient, () -> new MappingException("Missing field: " + field.getSrcName() + " (srg: " + field.getDstName(0) + ")")));
if (fossField == null) {
logger.debug("Missing field: " + field.getSrcName() + " (srg: " + field.getDstName(0) + ") " + fossClass.getFields().size() + " fields in the original class");
if (def == null) {
if (tryMatchRegardlessSrgsField(addRegardlessSrgs, obf, output, flatOutput, field)) continue;
continue;
}
flatOutput.visitField(obf, fossField.getSrcName(), fossField.getSrcDesc(), output.getDstNamespaces().stream().map(ns ->
ns.equals("srg") ? field.getDstName(0) : fossField.getName(ns)).toArray(String[]::new));
List<String> fieldNames = CollectionUtil.map(
output.getDstNamespaces(),
namespace -> "srg".equals(namespace) ? field.getDstName(0) : def.getName(namespace)
);
flatOutput.visitField(obf, def.getName("official"), def.getDesc("official"), fieldNames.toArray(new String[0]));
}
}
private static boolean tryMatchRegardlessSrgs(Map<String, List<MappingTreeView.MemberMappingView>> addRegardlessSrgs, String obf, MappingTree.MethodMapping method) {
private static boolean tryMatchRegardlessSrgsMethod(Map<String, List<MappingTreeView.MemberMappingView>> addRegardlessSrgs, String obf,
MappingTree output, FlatMappingVisitor flatOutput, MappingTree.MethodMapping method) throws IOException {
List<MappingTreeView.MemberMappingView> mutableDescriptoredList = addRegardlessSrgs.get(obf);
if (!Objects.equals(method.getDstName(0), method.getSrcName())) {
if (!method.getDstName(0).equals(method.getSrcName())) {
for (MappingTreeView.MemberMappingView descriptored : MoreObjects.firstNonNull(mutableDescriptoredList, Collections.<MappingTreeView.MemberMappingView>emptyList())) {
if (descriptored instanceof MappingTreeView.MethodMappingView && descriptored.getSrcName().equals(method.getSrcName()) && descriptored.getSrcDesc().equals(method.getSrcDesc())) {
if (descriptored instanceof MappingTree.MethodMapping && descriptored.getSrcName().equals(method.getSrcName()) && descriptored.getSrcDesc().equals(method.getSrcDesc())) {
List<String> methodNames = CollectionUtil.map(
output.getDstNamespaces(),
namespace -> "srg".equals(namespace) ? method.getDstName(0) : method.getSrcName()
);
flatOutput.visitMethod(obf, method.getSrcName(), method.getSrcDesc(), methodNames.toArray(new String[0]));
return true;
}
}
}
return false;
}
private static boolean tryMatchRegardlessSrgsField(Map<String, List<MappingTreeView.MemberMappingView>> addRegardlessSrgs, String obf,
MappingTree output, FlatMappingVisitor flatOutput, MappingTree.FieldMapping field) throws IOException {
List<MappingTreeView.MemberMappingView> mutableDescriptoredList = addRegardlessSrgs.get(obf);
if (!field.getDstName(0).equals(field.getSrcName())) {
for (MappingTreeView.MemberMappingView descriptored : MoreObjects.firstNonNull(mutableDescriptoredList, Collections.<MappingTreeView.MemberMappingView>emptyList())) {
if (descriptored instanceof MappingTree.FieldMapping && descriptored.getSrcName().equals(field.getSrcName())) {
List<String> fieldNames = CollectionUtil.map(
output.getDstNamespaces(),
namespace -> "srg".equals(namespace) ? field.getDstName(0) : field.getSrcName()
);
flatOutput.visitField(obf, field.getSrcName(), field.getSrcDesc(), fieldNames.toArray(new String[0]));
return true;
}
}
@@ -240,4 +266,4 @@ public final class SrgMerger {
throw exception.get();
}
}
}
}

View File

@@ -48,39 +48,39 @@ public class Tsrg2Writer {
}
private static void writeClass(List<String> namespaces, MappingTree.ClassMapping def, StringBuilder builder) {
writeMapped(false, namespaces, def, builder);
writeMapped(null, namespaces, def, builder);
for (MappingTree.MethodMapping method : def.getMethods()) {
writeMethod(namespaces, method, builder);
}
for (MappingTree.FieldMapping field : def.getFields()) {
writeMapped(true, namespaces, field, builder);
writeMapped('\t', namespaces, field, builder);
}
}
private static void writeMethod(List<String> namespaces, MappingTree.MethodMapping def, StringBuilder builder) {
writeMapped(true, namespaces, def, builder);
writeMapped('\t', namespaces, def, builder);
for (MappingTree.MethodArgMapping arg : def.getArgs()) {
builder.append("\t\t").append(arg.getLvIndex());
writeMapped(true, namespaces, arg, builder);
writeMapped(' ', namespaces, arg, builder);
}
}
private static void writeField(List<String> namespaces, MappingTree.FieldMapping def, StringBuilder builder) {
writeMapped(true, namespaces, def, builder);
writeMapped('\t', namespaces, def, builder);
}
private static void writeMapped(boolean needFirst, List<String> namespaces, MappingTreeView.ElementMappingView mapped, StringBuilder builder) {
private static void writeMapped(Character first, List<String> namespaces, MappingTreeView.ElementMappingView mapped, StringBuilder builder) {
String[] names = namespaces.stream().map(mapped::getName).toArray(String[]::new);
for (int i = 0; i < names.length; ++i) {
String name = names[i];
if (i == 0) {
if (needFirst) {
builder.append('\t');
if (first != null) {
builder.append(first);
}
} else {
builder.append(' ');