diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java | 70 |
1 files changed, 34 insertions, 36 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java index 6c57200..ba1b258 100644 --- a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java +++ b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java | |||
| @@ -4,10 +4,11 @@ | |||
| 4 | * are made available under the terms of the GNU Lesser General Public | 4 | * are made available under the terms of the GNU Lesser General Public |
| 5 | * License v3.0 which accompanies this distribution, and is available at | 5 | * License v3.0 which accompanies this distribution, and is available at |
| 6 | * http://www.gnu.org/licenses/lgpl.html | 6 | * http://www.gnu.org/licenses/lgpl.html |
| 7 | * | 7 | * |
| 8 | * Contributors: | 8 | * Contributors: |
| 9 | * Jeff Martin - initial API and implementation | 9 | * Jeff Martin - initial API and implementation |
| 10 | ******************************************************************************/ | 10 | ******************************************************************************/ |
| 11 | |||
| 11 | package cuchaz.enigma.mapping; | 12 | package cuchaz.enigma.mapping; |
| 12 | 13 | ||
| 13 | import com.google.common.base.Charsets; | 14 | import com.google.common.base.Charsets; |
| @@ -18,15 +19,13 @@ import java.util.Collections; | |||
| 18 | import java.util.List; | 19 | import java.util.List; |
| 19 | 20 | ||
| 20 | public class MappingsEnigmaWriter { | 21 | public class MappingsEnigmaWriter { |
| 21 | 22 | ||
| 22 | public void write(File out, Mappings mappings, boolean isDirectoryFormat) throws IOException { | 23 | public void write(File out, Mappings mappings, boolean isDirectoryFormat) throws IOException { |
| 23 | if (!isDirectoryFormat) | 24 | if (!isDirectoryFormat) { |
| 24 | { | ||
| 25 | PrintWriter outputWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(out), Charsets.UTF_8)); | 25 | PrintWriter outputWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(out), Charsets.UTF_8)); |
| 26 | write(outputWriter, mappings); | 26 | write(outputWriter, mappings); |
| 27 | outputWriter.close(); | 27 | outputWriter.close(); |
| 28 | } | 28 | } else |
| 29 | else | ||
| 30 | writeAsDirectory(out, mappings); | 29 | writeAsDirectory(out, mappings); |
| 31 | } | 30 | } |
| 32 | 31 | ||
| @@ -42,8 +41,7 @@ public class MappingsEnigmaWriter { | |||
| 42 | File result; | 41 | File result; |
| 43 | if (classMapping.getDeobfName() == null) | 42 | if (classMapping.getDeobfName() == null) |
| 44 | result = obFile; | 43 | result = obFile; |
| 45 | else | 44 | else { |
| 46 | { | ||
| 47 | // Make sure that old version of the file doesn't exist | 45 | // Make sure that old version of the file doesn't exist |
| 48 | if (obFile.exists()) | 46 | if (obFile.exists()) |
| 49 | obFile.delete(); | 47 | obFile.delete(); |
| @@ -59,19 +57,16 @@ public class MappingsEnigmaWriter { | |||
| 59 | } | 57 | } |
| 60 | 58 | ||
| 61 | // Remove dropped mappings | 59 | // Remove dropped mappings |
| 62 | if (mappings.getPreviousState() != null) | 60 | if (mappings.getPreviousState() != null) { |
| 63 | { | ||
| 64 | List<ClassMapping> droppedClassMappings = new ArrayList<>(mappings.getPreviousState().classes()); | 61 | List<ClassMapping> droppedClassMappings = new ArrayList<>(mappings.getPreviousState().classes()); |
| 65 | List<ClassMapping> classMappings = new ArrayList<>(mappings.classes()); | 62 | List<ClassMapping> classMappings = new ArrayList<>(mappings.classes()); |
| 66 | droppedClassMappings.removeAll(classMappings); | 63 | droppedClassMappings.removeAll(classMappings); |
| 67 | for (ClassMapping classMapping : droppedClassMappings) | 64 | for (ClassMapping classMapping : droppedClassMappings) { |
| 68 | { | ||
| 69 | File obFile = new File(target, classMapping.getObfFullName() + ".mapping"); | 65 | File obFile = new File(target, classMapping.getObfFullName() + ".mapping"); |
| 70 | File result; | 66 | File result; |
| 71 | if (classMapping.getDeobfName() == null) | 67 | if (classMapping.getDeobfName() == null) |
| 72 | result = obFile; | 68 | result = obFile; |
| 73 | else | 69 | else { |
| 74 | { | ||
| 75 | // Make sure that old version of the file doesn't exist | 70 | // Make sure that old version of the file doesn't exist |
| 76 | if (obFile.exists()) | 71 | if (obFile.exists()) |
| 77 | obFile.delete(); | 72 | obFile.delete(); |
| @@ -86,18 +81,15 @@ public class MappingsEnigmaWriter { | |||
| 86 | private void deletePreviousClassMapping(File target, ClassMapping classMapping) { | 81 | private void deletePreviousClassMapping(File target, ClassMapping classMapping) { |
| 87 | File prevFile = null; | 82 | File prevFile = null; |
| 88 | // Deob rename | 83 | // Deob rename |
| 89 | if (classMapping.getDeobfName() != null && classMapping.getPreviousDeobfName() != null && !classMapping.getPreviousDeobfName().equals(classMapping.getDeobfName())) | 84 | if (classMapping.getDeobfName() != null && classMapping.getPreviousDeobfName() != null && !classMapping.getPreviousDeobfName().equals(classMapping.getDeobfName())) { |
| 90 | { | ||
| 91 | prevFile = new File(target, classMapping.getPreviousDeobfName() + ".mapping"); | 85 | prevFile = new File(target, classMapping.getPreviousDeobfName() + ".mapping"); |
| 92 | } | 86 | } |
| 93 | // Deob to ob rename | 87 | // Deob to ob rename |
| 94 | else if (classMapping.getDeobfName() == null && classMapping.getPreviousDeobfName() != null) | 88 | else if (classMapping.getDeobfName() == null && classMapping.getPreviousDeobfName() != null) { |
| 95 | { | ||
| 96 | prevFile = new File(target, classMapping.getPreviousDeobfName() + ".mapping"); | 89 | prevFile = new File(target, classMapping.getPreviousDeobfName() + ".mapping"); |
| 97 | } | 90 | } |
| 98 | // Ob to Deob rename | 91 | // Ob to Deob rename |
| 99 | else if (classMapping.getDeobfName() != null && classMapping.getPreviousDeobfName() == null) | 92 | else if (classMapping.getDeobfName() != null && classMapping.getPreviousDeobfName() == null) { |
| 100 | { | ||
| 101 | prevFile = new File(target, classMapping.getObfFullName() + ".mapping"); | 93 | prevFile = new File(target, classMapping.getObfFullName() + ".mapping"); |
| 102 | } | 94 | } |
| 103 | 95 | ||
| @@ -110,50 +102,56 @@ public class MappingsEnigmaWriter { | |||
| 110 | write(out, classMapping, 0); | 102 | write(out, classMapping, 0); |
| 111 | } | 103 | } |
| 112 | } | 104 | } |
| 113 | 105 | ||
| 114 | private void write(PrintWriter out, ClassMapping classMapping, int depth) throws IOException { | 106 | private void write(PrintWriter out, ClassMapping classMapping, int depth) throws IOException { |
| 115 | if (classMapping.getDeobfName() == null) { | 107 | if (classMapping.getDeobfName() == null) { |
| 116 | out.format("%sCLASS %s%s\n", getIndent(depth), classMapping.getObfFullName(), classMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : classMapping.getModifier().getFormattedName()); | 108 | out.format("%sCLASS %s%s\n", getIndent(depth), classMapping.getObfFullName(), |
| 109 | classMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : classMapping.getModifier().getFormattedName()); | ||
| 117 | } else { | 110 | } else { |
| 118 | out.format("%sCLASS %s %s%s\n", getIndent(depth), classMapping.getObfFullName(), classMapping.getDeobfName(), classMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : classMapping.getModifier().getFormattedName()); | 111 | out.format("%sCLASS %s %s%s\n", getIndent(depth), classMapping.getObfFullName(), classMapping.getDeobfName(), |
| 112 | classMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : classMapping.getModifier().getFormattedName()); | ||
| 119 | } | 113 | } |
| 120 | 114 | ||
| 121 | for (ClassMapping innerClassMapping : sorted(classMapping.innerClasses())) { | 115 | for (ClassMapping innerClassMapping : sorted(classMapping.innerClasses())) { |
| 122 | write(out, innerClassMapping, depth + 1); | 116 | write(out, innerClassMapping, depth + 1); |
| 123 | } | 117 | } |
| 124 | 118 | ||
| 125 | for (FieldMapping fieldMapping : sorted(classMapping.fields())) { | 119 | for (FieldMapping fieldMapping : sorted(classMapping.fields())) { |
| 126 | write(out, fieldMapping, depth + 1); | 120 | write(out, fieldMapping, depth + 1); |
| 127 | } | 121 | } |
| 128 | 122 | ||
| 129 | for (MethodMapping methodMapping : sorted(classMapping.methods())) { | 123 | for (MethodMapping methodMapping : sorted(classMapping.methods())) { |
| 130 | write(out, methodMapping, depth + 1); | 124 | write(out, methodMapping, depth + 1); |
| 131 | } | 125 | } |
| 132 | } | 126 | } |
| 133 | 127 | ||
| 134 | private void write(PrintWriter out, FieldMapping fieldMapping, int depth) { | 128 | private void write(PrintWriter out, FieldMapping fieldMapping, int depth) { |
| 135 | if (fieldMapping.getDeobfName() == null) | 129 | if (fieldMapping.getDeobfName() == null) |
| 136 | out.format("%sFIELD %s %s%s\n", getIndent(depth), fieldMapping.getObfName(), fieldMapping.getObfType().toString(), fieldMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : fieldMapping.getModifier().getFormattedName()); | 130 | out.format("%sFIELD %s %s%s\n", getIndent(depth), fieldMapping.getObfName(), fieldMapping.getObfType().toString(), |
| 131 | fieldMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : fieldMapping.getModifier().getFormattedName()); | ||
| 137 | else | 132 | else |
| 138 | out.format("%sFIELD %s %s %s%s\n", getIndent(depth), fieldMapping.getObfName(), fieldMapping.getDeobfName(), fieldMapping.getObfType().toString(), fieldMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : fieldMapping.getModifier().getFormattedName()); | 133 | out.format("%sFIELD %s %s %s%s\n", getIndent(depth), fieldMapping.getObfName(), fieldMapping.getDeobfName(), fieldMapping.getObfType().toString(), |
| 134 | fieldMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : fieldMapping.getModifier().getFormattedName()); | ||
| 139 | } | 135 | } |
| 140 | 136 | ||
| 141 | private void write(PrintWriter out, MethodMapping methodMapping, int depth) throws IOException { | 137 | private void write(PrintWriter out, MethodMapping methodMapping, int depth) throws IOException { |
| 142 | if (methodMapping.getDeobfName() == null) { | 138 | if (methodMapping.getDeobfName() == null) { |
| 143 | out.format("%sMETHOD %s %s%s\n", getIndent(depth), methodMapping.getObfName(), methodMapping.getObfSignature(), methodMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" :methodMapping.getModifier().getFormattedName()); | 139 | out.format("%sMETHOD %s %s%s\n", getIndent(depth), methodMapping.getObfName(), methodMapping.getObfSignature(), |
| 140 | methodMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : methodMapping.getModifier().getFormattedName()); | ||
| 144 | } else { | 141 | } else { |
| 145 | out.format("%sMETHOD %s %s %s%s\n", getIndent(depth), methodMapping.getObfName(), methodMapping.getDeobfName(), methodMapping.getObfSignature(), methodMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : methodMapping.getModifier().getFormattedName()); | 142 | out.format("%sMETHOD %s %s %s%s\n", getIndent(depth), methodMapping.getObfName(), methodMapping.getDeobfName(), methodMapping.getObfSignature(), |
| 143 | methodMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : methodMapping.getModifier().getFormattedName()); | ||
| 146 | } | 144 | } |
| 147 | 145 | ||
| 148 | for (ArgumentMapping argumentMapping : sorted(methodMapping.arguments())) { | 146 | for (ArgumentMapping argumentMapping : sorted(methodMapping.arguments())) { |
| 149 | write(out, argumentMapping, depth + 1); | 147 | write(out, argumentMapping, depth + 1); |
| 150 | } | 148 | } |
| 151 | } | 149 | } |
| 152 | 150 | ||
| 153 | private void write(PrintWriter out, ArgumentMapping argumentMapping, int depth) { | 151 | private void write(PrintWriter out, ArgumentMapping argumentMapping, int depth) { |
| 154 | out.format("%sARG %d %s\n", getIndent(depth), argumentMapping.getIndex(), argumentMapping.getName()); | 152 | out.format("%sARG %d %s\n", getIndent(depth), argumentMapping.getIndex(), argumentMapping.getName()); |
| 155 | } | 153 | } |
| 156 | 154 | ||
| 157 | private <T extends Comparable<T>> List<T> sorted(Iterable<T> classes) { | 155 | private <T extends Comparable<T>> List<T> sorted(Iterable<T> classes) { |
| 158 | List<T> out = new ArrayList<>(); | 156 | List<T> out = new ArrayList<>(); |
| 159 | for (T t : classes) { | 157 | for (T t : classes) { |
| @@ -162,7 +160,7 @@ public class MappingsEnigmaWriter { | |||
| 162 | Collections.sort(out); | 160 | Collections.sort(out); |
| 163 | return out; | 161 | return out; |
| 164 | } | 162 | } |
| 165 | 163 | ||
| 166 | private String getIndent(int depth) { | 164 | private String getIndent(int depth) { |
| 167 | StringBuilder buf = new StringBuilder(); | 165 | StringBuilder buf = new StringBuilder(); |
| 168 | for (int i = 0; i < depth; i++) { | 166 | for (int i = 0; i < depth; i++) { |