diff --git a/src/main/java/net/fabricmc/loom/util/srg/InnerClassRemapper.java b/src/main/java/net/fabricmc/loom/util/srg/InnerClassRemapper.java index 7d57f6fd..33c18701 100644 --- a/src/main/java/net/fabricmc/loom/util/srg/InnerClassRemapper.java +++ b/src/main/java/net/fabricmc/loom/util/srg/InnerClassRemapper.java @@ -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 map = buildInnerClassRemap(fromJar, mappingsWithSrg, from, to); return sink -> { - for (Map.Entry entry : map.entrySet()) { - sink.acceptClass(entry.getKey(), entry.getValue()); - } + remapInnerClass(fromJar, mappingsWithSrg, from, to, sink::acceptClass); }; } - private static Map buildInnerClassRemap(Path fromJar, TinyTree mappingsWithSrg, String from, String to) throws IOException { - Map remapInnerClasses = new HashMap<>(); - + private static void remapInnerClass(Path fromJar, TinyTree mappingsWithSrg, String from, String to, BiConsumer action) { try (InputStream inputStream = Files.newInputStream(fromJar)) { - Set availableClasses = mappingsWithSrg.getClasses().stream() - .map(classDef -> classDef.getName(from)) - .collect(Collectors.toSet()); + Map availableClasses = mappingsWithSrg.getClasses().stream() + .collect(Collectors.groupingBy(classDef -> classDef.getName(from), + Collectors.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; } }