From a436a621aed5f95548ac913896033239f86b08e4 Mon Sep 17 00:00:00 2001 From: Jonas Herzig Date: Fri, 26 Apr 2019 10:32:48 +0200 Subject: Fix class name mapping for (even partially) swapped names E.g. with mappings: A to B B to C this A a; B b; should map to B a C b; but didn't. --- .../com/replaymod/gradle/remap/Transformer.java | 76 ++++++++++------------ 1 file changed, 34 insertions(+), 42 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/replaymod/gradle/remap/Transformer.java b/src/main/java/com/replaymod/gradle/remap/Transformer.java index d6ce2f2..4428058 100644 --- a/src/main/java/com/replaymod/gradle/remap/Transformer.java +++ b/src/main/java/com/replaymod/gradle/remap/Transformer.java @@ -13,9 +13,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicBoolean; @@ -30,7 +28,7 @@ class Transformer { if (args[0].isEmpty()) { mappings = new HashMap<>(); } else { - mappings = readMappings(Collections.singletonList(new File(args[0])), args[1].equals("true")); + mappings = readMappings(new File(args[0]), args[1].equals("true")); } Transformer transformer = new Transformer(mappings); @@ -205,48 +203,42 @@ class Transformer { public Map methods = new HashMap<>(); } - public static Map readMappings(List mappingFiles, boolean invert) throws IOException { + public static Map readMappings(File mappingFile, boolean invert) throws IOException { Map mappings = new HashMap<>(); - for (File mappingFile : mappingFiles) { - Map currentMappings = new HashMap<>(mappings); - int lineNumber = 0; - for (String line : Files.readAllLines(mappingFile.toPath(), StandardCharsets.UTF_8)) { - lineNumber++; - if (line.trim().startsWith("#") || line.trim().isEmpty()) continue; - - String[] parts = line.split(" "); - if (parts.length < 2 || line.contains(";")) { - throw new IllegalArgumentException("Failed to parse line " + lineNumber + " in " + mappingFile.getPath() + "."); - } + int lineNumber = 0; + for (String line : Files.readAllLines(mappingFile.toPath(), StandardCharsets.UTF_8)) { + lineNumber++; + if (line.trim().startsWith("#") || line.trim().isEmpty()) continue; + + String[] parts = line.split(" "); + if (parts.length < 2 || line.contains(";")) { + throw new IllegalArgumentException("Failed to parse line " + lineNumber + " in " + mappingFile.getPath() + "."); + } - Mapping mapping = currentMappings.get(parts[0]); - if (mapping == null) { - mapping = new Mapping(); - mapping.oldName = mapping.newName = parts[0]; - currentMappings.put(mapping.oldName, mapping); - mappings.put(mapping.newName, mapping); - } + Mapping mapping = mappings.get(parts[0]); + if (mapping == null) { + mapping = new Mapping(); + mapping.oldName = mapping.newName = parts[0]; + mappings.put(mapping.oldName, mapping); + } - if (parts.length == 2) { - // Class mapping - mappings.remove(mapping.newName); - mapping.newName = parts[1]; - mappings.put(mapping.newName, mapping); - } else if (parts[1].endsWith("()")) { - // Method mapping - String name = parts[1].substring(0, parts[1].length() - 2); - String newName = parts[2].substring(0, parts[2].length() - 2); - String oldName = mapping.methods.remove(name); - if (oldName == null) oldName = name; - mapping.methods.put(oldName, newName); - } else { - // Field mapping - String name = parts[1]; - String newName = parts[2]; - String oldName = mapping.fields.remove(name); - if (oldName == null) oldName = name; - mapping.fields.put(oldName, newName); - } + if (parts.length == 2) { + // Class mapping + mapping.newName = parts[1]; + } else if (parts[1].endsWith("()")) { + // Method mapping + String name = parts[1].substring(0, parts[1].length() - 2); + String newName = parts[2].substring(0, parts[2].length() - 2); + String oldName = mapping.methods.remove(name); + if (oldName == null) oldName = name; + mapping.methods.put(oldName, newName); + } else { + // Field mapping + String name = parts[1]; + String newName = parts[2]; + String oldName = mapping.fields.remove(name); + if (oldName == null) oldName = name; + mapping.fields.put(oldName, newName); } } if (invert) { -- cgit