diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java | 124 |
1 files changed, 53 insertions, 71 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java index ba1b258..b29990f 100644 --- a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java +++ b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java | |||
| @@ -14,9 +14,7 @@ package cuchaz.enigma.mapping; | |||
| 14 | import com.google.common.base.Charsets; | 14 | import com.google.common.base.Charsets; |
| 15 | 15 | ||
| 16 | import java.io.*; | 16 | import java.io.*; |
| 17 | import java.util.ArrayList; | 17 | import java.util.*; |
| 18 | import java.util.Collections; | ||
| 19 | import java.util.List; | ||
| 20 | 18 | ||
| 21 | public class MappingsEnigmaWriter { | 19 | public class MappingsEnigmaWriter { |
| 22 | 20 | ||
| @@ -33,83 +31,67 @@ public class MappingsEnigmaWriter { | |||
| 33 | if (!target.exists() && !target.mkdirs()) | 31 | if (!target.exists() && !target.mkdirs()) |
| 34 | throw new IOException("Cannot create mapping directory!"); | 32 | throw new IOException("Cannot create mapping directory!"); |
| 35 | 33 | ||
| 34 | Mappings previousState = mappings.getPreviousState(); | ||
| 36 | for (ClassMapping classMapping : sorted(mappings.classes())) { | 35 | for (ClassMapping classMapping : sorted(mappings.classes())) { |
| 37 | if (!classMapping.isDirty()) | 36 | if (!classMapping.isDirty()) { |
| 38 | continue; | 37 | continue; |
| 39 | this.deletePreviousClassMapping(target, classMapping); | ||
| 40 | File obFile = new File(target, classMapping.getObfFullName() + ".mapping"); | ||
| 41 | File result; | ||
| 42 | if (classMapping.getDeobfName() == null) | ||
| 43 | result = obFile; | ||
| 44 | else { | ||
| 45 | // Make sure that old version of the file doesn't exist | ||
| 46 | if (obFile.exists()) | ||
| 47 | obFile.delete(); | ||
| 48 | result = new File(target, classMapping.getDeobfName() + ".mapping"); | ||
| 49 | } | 38 | } |
| 50 | 39 | ||
| 51 | if (!result.getParentFile().exists()) | 40 | if (previousState != null) { |
| 52 | result.getParentFile().mkdirs(); | 41 | ClassMapping previousClass = previousState.classesByObf.get(classMapping.getObfFullName()); |
| 42 | File previousFile; | ||
| 43 | if (previousClass != null) { | ||
| 44 | previousFile = new File(target, previousClass.getSaveName() + ".mapping"); | ||
| 45 | } else { | ||
| 46 | previousFile = new File(target, classMapping.getObfFullName() + ".mapping"); | ||
| 47 | } | ||
| 48 | if (previousFile.exists() && !previousFile.delete()) { | ||
| 49 | System.err.println("Failed to delete old class mapping " + previousFile.getName()); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | File result = new File(target, classMapping.getSaveName() + ".mapping"); | ||
| 54 | |||
| 55 | File packageFile = result.getParentFile(); | ||
| 56 | if (!packageFile.exists()) { | ||
| 57 | packageFile.mkdirs(); | ||
| 58 | } | ||
| 53 | result.createNewFile(); | 59 | result.createNewFile(); |
| 54 | PrintWriter outputWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(result), Charsets.UTF_8)); | 60 | |
| 55 | write(outputWriter, classMapping, 0); | 61 | try (PrintWriter outputWriter = new PrintWriter(new BufferedWriter(new FileWriter(result)))) { |
| 56 | outputWriter.close(); | 62 | write(outputWriter, classMapping, 0); |
| 63 | } | ||
| 57 | } | 64 | } |
| 58 | 65 | ||
| 59 | // Remove dropped mappings | 66 | // Remove dropped mappings |
| 60 | if (mappings.getPreviousState() != null) { | 67 | if (previousState != null) { |
| 61 | List<ClassMapping> droppedClassMappings = new ArrayList<>(mappings.getPreviousState().classes()); | 68 | Set<ClassMapping> droppedClassMappings = new HashSet<>(previousState.classes()); |
| 62 | List<ClassMapping> classMappings = new ArrayList<>(mappings.classes()); | 69 | droppedClassMappings.removeAll(mappings.classes()); |
| 63 | droppedClassMappings.removeAll(classMappings); | 70 | for (ClassMapping droppedMapping : droppedClassMappings) { |
| 64 | for (ClassMapping classMapping : droppedClassMappings) { | 71 | File result = new File(target, droppedMapping.getSaveName() + ".mapping"); |
| 65 | File obFile = new File(target, classMapping.getObfFullName() + ".mapping"); | 72 | if (!result.exists()) { |
| 66 | File result; | 73 | continue; |
| 67 | if (classMapping.getDeobfName() == null) | 74 | } |
| 68 | result = obFile; | 75 | if (!result.delete()) { |
| 69 | else { | 76 | System.err.println("Failed to delete dropped class mapping " + result.getName()); |
| 70 | // Make sure that old version of the file doesn't exist | ||
| 71 | if (obFile.exists()) | ||
| 72 | obFile.delete(); | ||
| 73 | result = new File(target, classMapping.getDeobfName() + ".mapping"); | ||
| 74 | } | 77 | } |
| 75 | if (result.exists()) | ||
| 76 | result.delete(); | ||
| 77 | } | 78 | } |
| 78 | } | 79 | } |
| 79 | } | 80 | } |
| 80 | 81 | ||
| 81 | private void deletePreviousClassMapping(File target, ClassMapping classMapping) { | ||
| 82 | File prevFile = null; | ||
| 83 | // Deob rename | ||
| 84 | if (classMapping.getDeobfName() != null && classMapping.getPreviousDeobfName() != null && !classMapping.getPreviousDeobfName().equals(classMapping.getDeobfName())) { | ||
| 85 | prevFile = new File(target, classMapping.getPreviousDeobfName() + ".mapping"); | ||
| 86 | } | ||
| 87 | // Deob to ob rename | ||
| 88 | else if (classMapping.getDeobfName() == null && classMapping.getPreviousDeobfName() != null) { | ||
| 89 | prevFile = new File(target, classMapping.getPreviousDeobfName() + ".mapping"); | ||
| 90 | } | ||
| 91 | // Ob to Deob rename | ||
| 92 | else if (classMapping.getDeobfName() != null && classMapping.getPreviousDeobfName() == null) { | ||
| 93 | prevFile = new File(target, classMapping.getObfFullName() + ".mapping"); | ||
| 94 | } | ||
| 95 | |||
| 96 | if (prevFile != null && prevFile.exists()) | ||
| 97 | prevFile.delete(); | ||
| 98 | } | ||
| 99 | |||
| 100 | public void write(PrintWriter out, Mappings mappings) throws IOException { | 82 | public void write(PrintWriter out, Mappings mappings) throws IOException { |
| 101 | for (ClassMapping classMapping : sorted(mappings.classes())) { | 83 | for (ClassMapping classMapping : sorted(mappings.classes())) { |
| 102 | write(out, classMapping, 0); | 84 | write(out, classMapping, 0); |
| 103 | } | 85 | } |
| 104 | } | 86 | } |
| 105 | 87 | ||
| 106 | private void write(PrintWriter out, ClassMapping classMapping, int depth) throws IOException { | 88 | protected void write(PrintWriter out, ClassMapping classMapping, int depth) throws IOException { |
| 107 | if (classMapping.getDeobfName() == null) { | 89 | if (classMapping.getDeobfName() == null) { |
| 108 | out.format("%sCLASS %s%s\n", getIndent(depth), classMapping.getObfFullName(), | 90 | out.format("%sCLASS %s%s\n", getIndent(depth), classMapping.getObfFullName(), |
| 109 | classMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : classMapping.getModifier().getFormattedName()); | 91 | classMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : classMapping.getModifier().getFormattedName()); |
| 110 | } else { | 92 | } else { |
| 111 | out.format("%sCLASS %s %s%s\n", getIndent(depth), classMapping.getObfFullName(), classMapping.getDeobfName(), | 93 | out.format("%sCLASS %s %s%s\n", getIndent(depth), classMapping.getObfFullName(), classMapping.getDeobfName(), |
| 112 | classMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : classMapping.getModifier().getFormattedName()); | 94 | classMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : classMapping.getModifier().getFormattedName()); |
| 113 | } | 95 | } |
| 114 | 96 | ||
| 115 | for (ClassMapping innerClassMapping : sorted(classMapping.innerClasses())) { | 97 | for (ClassMapping innerClassMapping : sorted(classMapping.innerClasses())) { |
| @@ -127,32 +109,32 @@ public class MappingsEnigmaWriter { | |||
| 127 | 109 | ||
| 128 | private void write(PrintWriter out, FieldMapping fieldMapping, int depth) { | 110 | private void write(PrintWriter out, FieldMapping fieldMapping, int depth) { |
| 129 | if (fieldMapping.getDeobfName() == null) | 111 | if (fieldMapping.getDeobfName() == null) |
| 130 | out.format("%sFIELD %s %s%s\n", getIndent(depth), fieldMapping.getObfName(), fieldMapping.getObfType().toString(), | 112 | out.format("%sFIELD %s %s%s\n", getIndent(depth), fieldMapping.getObfName(), fieldMapping.getObfDesc().toString(), |
| 131 | fieldMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : fieldMapping.getModifier().getFormattedName()); | 113 | fieldMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : fieldMapping.getModifier().getFormattedName()); |
| 132 | else | 114 | else |
| 133 | out.format("%sFIELD %s %s %s%s\n", getIndent(depth), fieldMapping.getObfName(), fieldMapping.getDeobfName(), fieldMapping.getObfType().toString(), | 115 | out.format("%sFIELD %s %s %s%s\n", getIndent(depth), fieldMapping.getObfName(), fieldMapping.getDeobfName(), fieldMapping.getObfDesc().toString(), |
| 134 | fieldMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : fieldMapping.getModifier().getFormattedName()); | 116 | fieldMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : fieldMapping.getModifier().getFormattedName()); |
| 135 | } | 117 | } |
| 136 | 118 | ||
| 137 | private void write(PrintWriter out, MethodMapping methodMapping, int depth) throws IOException { | 119 | private void write(PrintWriter out, MethodMapping methodMapping, int depth) throws IOException { |
| 138 | if (methodMapping.getDeobfName() == null) { | 120 | if (methodMapping.isObfuscated()) { |
| 139 | out.format("%sMETHOD %s %s%s\n", getIndent(depth), methodMapping.getObfName(), methodMapping.getObfSignature(), | 121 | out.format("%sMETHOD %s %s%s\n", getIndent(depth), methodMapping.getObfName(), methodMapping.getObfDesc(), |
| 140 | methodMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : methodMapping.getModifier().getFormattedName()); | 122 | methodMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : methodMapping.getModifier().getFormattedName()); |
| 141 | } else { | 123 | } else { |
| 142 | out.format("%sMETHOD %s %s %s%s\n", getIndent(depth), methodMapping.getObfName(), methodMapping.getDeobfName(), methodMapping.getObfSignature(), | 124 | out.format("%sMETHOD %s %s %s%s\n", getIndent(depth), methodMapping.getObfName(), methodMapping.getDeobfName(), methodMapping.getObfDesc(), |
| 143 | methodMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : methodMapping.getModifier().getFormattedName()); | 125 | methodMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : methodMapping.getModifier().getFormattedName()); |
| 144 | } | 126 | } |
| 145 | 127 | ||
| 146 | for (ArgumentMapping argumentMapping : sorted(methodMapping.arguments())) { | 128 | for (LocalVariableMapping localVariableMapping : sorted(methodMapping.arguments())) { |
| 147 | write(out, argumentMapping, depth + 1); | 129 | write(out, localVariableMapping, depth + 1); |
| 148 | } | 130 | } |
| 149 | } | 131 | } |
| 150 | 132 | ||
| 151 | private void write(PrintWriter out, ArgumentMapping argumentMapping, int depth) { | 133 | private void write(PrintWriter out, LocalVariableMapping localVariableMapping, int depth) { |
| 152 | out.format("%sARG %d %s\n", getIndent(depth), argumentMapping.getIndex(), argumentMapping.getName()); | 134 | out.format("%sARG %d %s\n", getIndent(depth), localVariableMapping.getIndex(), localVariableMapping.getName()); |
| 153 | } | 135 | } |
| 154 | 136 | ||
| 155 | private <T extends Comparable<T>> List<T> sorted(Iterable<T> classes) { | 137 | protected <T extends Comparable<T>> List<T> sorted(Iterable<T> classes) { |
| 156 | List<T> out = new ArrayList<>(); | 138 | List<T> out = new ArrayList<>(); |
| 157 | for (T t : classes) { | 139 | for (T t : classes) { |
| 158 | out.add(t); | 140 | out.add(t); |