Refactor InnerClassRemapper

This commit is contained in:
shedaniel
2021-01-10 17:12:38 +08:00
parent 0793ac224e
commit 39a3a301cf

View File

@@ -1,5 +1,6 @@
package net.fabricmc.loom.util.srg;
import net.fabricmc.mapping.tree.ClassDef;
import net.fabricmc.mapping.tree.TinyTree;
import net.fabricmc.tinyremapper.IMappingProvider;
import org.zeroturnaround.zip.ZipUtil;
@@ -8,49 +9,43 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
public class InnerClassRemapper {
public static IMappingProvider of(Path fromJar, TinyTree mappingsWithSrg, String from, String to) throws IOException {
Map<String, String> map = buildInnerClassRemap(fromJar, mappingsWithSrg, from, to);
return sink -> {
for (Map.Entry<String, String> entry : map.entrySet()) {
sink.acceptClass(entry.getKey(), entry.getValue());
}
remapInnerClass(fromJar, mappingsWithSrg, from, to, sink::acceptClass);
};
}
private static Map<String, String> buildInnerClassRemap(Path fromJar, TinyTree mappingsWithSrg, String from, String to) throws IOException {
Map<String, String> remapInnerClasses = new HashMap<>();
private static void remapInnerClass(Path fromJar, TinyTree mappingsWithSrg, String from, String to, BiConsumer<String, String> action) {
try (InputStream inputStream = Files.newInputStream(fromJar)) {
Set<String> availableClasses = mappingsWithSrg.getClasses().stream()
.map(classDef -> classDef.getName(from))
.collect(Collectors.toSet());
Map<String, String> availableClasses = mappingsWithSrg.getClasses().stream()
.collect(Collectors.groupingBy(classDef -> classDef.getName(from),
Collectors.<ClassDef, String>reducing(
null,
classDef -> classDef.getName(to),
(first, last) -> last
))
);
ZipUtil.iterate(inputStream, (in, zipEntry) -> {
if (!zipEntry.isDirectory() && zipEntry.getName().contains("$") && zipEntry.getName().endsWith(".class")) {
String className = zipEntry.getName().substring(0, zipEntry.getName().length() - 6);
if (!availableClasses.contains(className)) {
if (!availableClasses.containsKey(className)) {
String parentName = className.substring(0, className.indexOf('$'));
String childName = className.substring(className.indexOf('$') + 1);
String remappedParentName = mappingsWithSrg.getClasses().stream()
.filter(classDef -> Objects.equals(classDef.getName(from), parentName))
.findFirst()
.map(classDef -> classDef.getName(to))
.orElse(parentName);
String remappedParentName = availableClasses.getOrDefault(parentName, parentName);
String remappedName = remappedParentName + "$" + childName;
if (!className.equals(remappedName)) {
remapInnerClasses.put(className, remappedName);
action.accept(className, remappedName);
}
}
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
return remapInnerClasses;
}
}