diff options
| author | 2016-08-11 14:18:43 +0100 | |
|---|---|---|
| committer | 2016-08-11 14:18:43 +0100 | |
| commit | 6c90b5f41ec9ea8c512981e4dbe69d5d9769651f (patch) | |
| tree | 4b116a3801ea9405cace5b9f2a56b11c51c0b516 /src/main/java/cuchaz | |
| parent | Removed util (diff) | |
| download | enigma-6c90b5f41ec9ea8c512981e4dbe69d5d9769651f.tar.gz enigma-6c90b5f41ec9ea8c512981e4dbe69d5d9769651f.tar.xz enigma-6c90b5f41ec9ea8c512981e4dbe69d5d9769651f.zip | |
Allow exporting mappings as SRG or Enigma
Diffstat (limited to 'src/main/java/cuchaz')
5 files changed, 220 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/gui/Gui.java b/src/main/java/cuchaz/enigma/gui/Gui.java index d93aa9fc..5b0b599f 100644 --- a/src/main/java/cuchaz/enigma/gui/Gui.java +++ b/src/main/java/cuchaz/enigma/gui/Gui.java | |||
| @@ -319,6 +319,8 @@ public class Gui { | |||
| 319 | this.menuBar.openMappingsMenu.setEnabled(true); | 319 | this.menuBar.openMappingsMenu.setEnabled(true); |
| 320 | this.menuBar.saveMappingsMenu.setEnabled(false); | 320 | this.menuBar.saveMappingsMenu.setEnabled(false); |
| 321 | this.menuBar.saveMappingsAsMenu.setEnabled(true); | 321 | this.menuBar.saveMappingsAsMenu.setEnabled(true); |
| 322 | this.menuBar.saveMappingsOldMenu.setEnabled(true); | ||
| 323 | this.menuBar.saveMappingsSrgMenu.setEnabled(true); | ||
| 322 | this.menuBar.closeMappingsMenu.setEnabled(true); | 324 | this.menuBar.closeMappingsMenu.setEnabled(true); |
| 323 | this.menuBar.exportSourceMenu.setEnabled(true); | 325 | this.menuBar.exportSourceMenu.setEnabled(true); |
| 324 | this.menuBar.exportJarMenu.setEnabled(true); | 326 | this.menuBar.exportJarMenu.setEnabled(true); |
| @@ -340,6 +342,8 @@ public class Gui { | |||
| 340 | this.menuBar.openMappingsMenu.setEnabled(false); | 342 | this.menuBar.openMappingsMenu.setEnabled(false); |
| 341 | this.menuBar.saveMappingsMenu.setEnabled(false); | 343 | this.menuBar.saveMappingsMenu.setEnabled(false); |
| 342 | this.menuBar.saveMappingsAsMenu.setEnabled(false); | 344 | this.menuBar.saveMappingsAsMenu.setEnabled(false); |
| 345 | this.menuBar.saveMappingsOldMenu.setEnabled(false); | ||
| 346 | this.menuBar.saveMappingsSrgMenu.setEnabled(false); | ||
| 343 | this.menuBar.closeMappingsMenu.setEnabled(false); | 347 | this.menuBar.closeMappingsMenu.setEnabled(false); |
| 344 | this.menuBar.exportSourceMenu.setEnabled(false); | 348 | this.menuBar.exportSourceMenu.setEnabled(false); |
| 345 | this.menuBar.exportJarMenu.setEnabled(false); | 349 | this.menuBar.exportJarMenu.setEnabled(false); |
diff --git a/src/main/java/cuchaz/enigma/gui/GuiController.java b/src/main/java/cuchaz/enigma/gui/GuiController.java index c3015948..fe7d0978 100644 --- a/src/main/java/cuchaz/enigma/gui/GuiController.java +++ b/src/main/java/cuchaz/enigma/gui/GuiController.java | |||
| @@ -17,6 +17,7 @@ import com.strobel.decompiler.languages.java.ast.CompilationUnit; | |||
| 17 | 17 | ||
| 18 | import java.io.File; | 18 | import java.io.File; |
| 19 | import java.io.FileReader; | 19 | import java.io.FileReader; |
| 20 | import java.io.FileWriter; | ||
| 20 | import java.io.IOException; | 21 | import java.io.IOException; |
| 21 | import java.util.Collection; | 22 | import java.util.Collection; |
| 22 | import java.util.Deque; | 23 | import java.util.Deque; |
| @@ -87,6 +88,17 @@ public class GuiController { | |||
| 87 | this.isDirty = false; | 88 | this.isDirty = false; |
| 88 | } | 89 | } |
| 89 | 90 | ||
| 91 | public void saveOldMappings(File file) throws IOException { | ||
| 92 | FileWriter out = new FileWriter(file); | ||
| 93 | new MappingsOldWriter().write(out, this.deobfuscator.getMappings()); | ||
| 94 | this.isDirty = false; | ||
| 95 | } | ||
| 96 | |||
| 97 | public void saveSRGMappings(File file) throws IOException { | ||
| 98 | new MappingsSRGWriter().write(file, this.deobfuscator); | ||
| 99 | this.isDirty = false; | ||
| 100 | } | ||
| 101 | |||
| 90 | public void closeMappings() { | 102 | public void closeMappings() { |
| 91 | this.deobfuscator.setMappings(null); | 103 | this.deobfuscator.setMappings(null); |
| 92 | this.gui.setMappingsFile(null); | 104 | this.gui.setMappingsFile(null); |
diff --git a/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java b/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java index e8703340..e79476bb 100644 --- a/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java +++ b/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java | |||
| @@ -22,6 +22,8 @@ public class MenuBar extends JMenuBar { | |||
| 22 | 22 | ||
| 23 | public final JMenuItem saveMappingsMenu; | 23 | public final JMenuItem saveMappingsMenu; |
| 24 | public final JMenuItem saveMappingsAsMenu; | 24 | public final JMenuItem saveMappingsAsMenu; |
| 25 | public final JMenuItem saveMappingsOldMenu; | ||
| 26 | public final JMenuItem saveMappingsSrgMenu; | ||
| 25 | public final JMenuItem closeMappingsMenu; | 27 | public final JMenuItem closeMappingsMenu; |
| 26 | 28 | ||
| 27 | 29 | ||
| @@ -121,6 +123,38 @@ public class MenuBar extends JMenuBar { | |||
| 121 | this.saveMappingsAsMenu = item; | 123 | this.saveMappingsAsMenu = item; |
| 122 | } | 124 | } |
| 123 | { | 125 | { |
| 126 | JMenuItem item = new JMenuItem("Save Mappings as Enigma"); | ||
| 127 | menu.add(item); | ||
| 128 | item.addActionListener(event -> { | ||
| 129 | if (this.gui.mappingsFileChooser.showSaveDialog(this.gui.getFrame()) == JFileChooser.APPROVE_OPTION) { | ||
| 130 | try { | ||
| 131 | this.gui.getController().saveOldMappings(this.gui.mappingsFileChooser.getSelectedFile()); | ||
| 132 | this.saveMappingsMenu.setEnabled(true); | ||
| 133 | } catch (IOException ex) { | ||
| 134 | throw new Error(ex); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | }); | ||
| 138 | item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK)); | ||
| 139 | this.saveMappingsOldMenu = item; | ||
| 140 | } | ||
| 141 | { | ||
| 142 | JMenuItem item = new JMenuItem("Save Mappings as SRG"); | ||
| 143 | menu.add(item); | ||
| 144 | item.addActionListener(event -> { | ||
| 145 | if (this.gui.mappingsFileChooser.showSaveDialog(this.gui.getFrame()) == JFileChooser.APPROVE_OPTION) { | ||
| 146 | try { | ||
| 147 | this.gui.getController().saveSRGMappings(this.gui.mappingsFileChooser.getSelectedFile()); | ||
| 148 | this.saveMappingsMenu.setEnabled(true); | ||
| 149 | } catch (IOException ex) { | ||
| 150 | throw new Error(ex); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | }); | ||
| 154 | item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK)); | ||
| 155 | this.saveMappingsSrgMenu = item; | ||
| 156 | } | ||
| 157 | { | ||
| 124 | JMenuItem item = new JMenuItem("Close Mappings"); | 158 | JMenuItem item = new JMenuItem("Close Mappings"); |
| 125 | menu.add(item); | 159 | menu.add(item); |
| 126 | item.addActionListener(event -> this.gui.getController().closeMappings()); | 160 | item.addActionListener(event -> this.gui.getController().closeMappings()); |
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsOldWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsOldWriter.java new file mode 100644 index 00000000..8b766a31 --- /dev/null +++ b/src/main/java/cuchaz/enigma/mapping/MappingsOldWriter.java | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2015 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 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 | ||
| 6 | * http://www.gnu.org/licenses/lgpl.html | ||
| 7 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.mapping; | ||
| 12 | |||
| 13 | import java.io.IOException; | ||
| 14 | import java.io.PrintWriter; | ||
| 15 | import java.io.Writer; | ||
| 16 | import java.util.ArrayList; | ||
| 17 | import java.util.Collections; | ||
| 18 | import java.util.List; | ||
| 19 | |||
| 20 | public class MappingsOldWriter { | ||
| 21 | |||
| 22 | public void write(Writer out, Mappings mappings) throws IOException { | ||
| 23 | write(new PrintWriter(out), mappings); | ||
| 24 | } | ||
| 25 | |||
| 26 | public void write(PrintWriter out, Mappings mappings) throws IOException { | ||
| 27 | for (ClassMapping classMapping : sorted(mappings.classes())) { | ||
| 28 | write(out, classMapping, 0); | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | private void write(PrintWriter out, ClassMapping classMapping, int depth) throws IOException { | ||
| 33 | if (classMapping.getDeobfName() == null) { | ||
| 34 | out.format("%sCLASS %s\n", getIndent(depth), classMapping.getObfFullName()); | ||
| 35 | } else { | ||
| 36 | out.format("%sCLASS %s %s\n", getIndent(depth), classMapping.getObfFullName(), classMapping.getDeobfName()); | ||
| 37 | } | ||
| 38 | |||
| 39 | for (ClassMapping innerClassMapping : sorted(classMapping.innerClasses())) { | ||
| 40 | write(out, innerClassMapping, depth + 1); | ||
| 41 | } | ||
| 42 | |||
| 43 | for (FieldMapping fieldMapping : sorted(classMapping.fields())) { | ||
| 44 | write(out, fieldMapping, depth + 1); | ||
| 45 | } | ||
| 46 | |||
| 47 | for (MethodMapping methodMapping : sorted(classMapping.methods())) { | ||
| 48 | write(out, methodMapping, depth + 1); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | private void write(PrintWriter out, FieldMapping fieldMapping, int depth) throws IOException { | ||
| 53 | out.format("%sFIELD %s %s %s\n", getIndent(depth), fieldMapping.getObfName(), fieldMapping.getDeobfName(), fieldMapping.getObfType().toString()); | ||
| 54 | } | ||
| 55 | |||
| 56 | private void write(PrintWriter out, MethodMapping methodMapping, int depth) throws IOException { | ||
| 57 | if (methodMapping.getDeobfName() == null) { | ||
| 58 | out.format("%sMETHOD %s %s\n", getIndent(depth), methodMapping.getObfName(), methodMapping.getObfSignature()); | ||
| 59 | } else { | ||
| 60 | out.format("%sMETHOD %s %s %s\n", getIndent(depth), methodMapping.getObfName(), methodMapping.getDeobfName(), methodMapping.getObfSignature()); | ||
| 61 | } | ||
| 62 | |||
| 63 | for (ArgumentMapping argumentMapping : sorted(methodMapping.arguments())) { | ||
| 64 | write(out, argumentMapping, depth + 1); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | private void write(PrintWriter out, ArgumentMapping argumentMapping, int depth) throws IOException { | ||
| 69 | out.format("%sARG %d %s\n", getIndent(depth), argumentMapping.getIndex(), argumentMapping.getName()); | ||
| 70 | } | ||
| 71 | |||
| 72 | private <T extends Comparable<T>> List<T> sorted(Iterable<T> classes) { | ||
| 73 | List<T> out = new ArrayList<T>(); | ||
| 74 | for (T t : classes) { | ||
| 75 | out.add(t); | ||
| 76 | } | ||
| 77 | Collections.sort(out); | ||
| 78 | return out; | ||
| 79 | } | ||
| 80 | |||
| 81 | private String getIndent(int depth) { | ||
| 82 | StringBuilder buf = new StringBuilder(); | ||
| 83 | for (int i = 0; i < depth; i++) { | ||
| 84 | buf.append("\t"); | ||
| 85 | } | ||
| 86 | return buf.toString(); | ||
| 87 | } | ||
| 88 | } | ||
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsSRGWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsSRGWriter.java new file mode 100644 index 00000000..64da7091 --- /dev/null +++ b/src/main/java/cuchaz/enigma/mapping/MappingsSRGWriter.java | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | package cuchaz.enigma.mapping; | ||
| 2 | |||
| 3 | import cuchaz.enigma.Deobfuscator; | ||
| 4 | |||
| 5 | import java.io.File; | ||
| 6 | import java.io.FileWriter; | ||
| 7 | import java.io.IOException; | ||
| 8 | import java.util.ArrayList; | ||
| 9 | import java.util.Collections; | ||
| 10 | import java.util.List; | ||
| 11 | |||
| 12 | /** | ||
| 13 | * Created by Mark on 11/08/2016. | ||
| 14 | */ | ||
| 15 | public class MappingsSRGWriter { | ||
| 16 | |||
| 17 | public void write(File file, Deobfuscator deobfuscator) throws IOException { | ||
| 18 | if(file.exists()){ | ||
| 19 | file.delete(); | ||
| 20 | } | ||
| 21 | file.createNewFile(); | ||
| 22 | |||
| 23 | Mappings mappings = deobfuscator.getMappings(); | ||
| 24 | |||
| 25 | FileWriter writer = new FileWriter(file); | ||
| 26 | List<String> fieldMappings = new ArrayList<>(); | ||
| 27 | List<String> methodMappings = new ArrayList<>(); | ||
| 28 | for (ClassMapping classMapping : sorted(mappings.classes())) { | ||
| 29 | if(classMapping.getDeobfName() == null || classMapping.getObfSimpleName() == null || classMapping.getDeobfName() == null){ | ||
| 30 | continue; | ||
| 31 | } | ||
| 32 | writer.write("CL: " + classMapping.getObfSimpleName() + " " + classMapping.getDeobfName()); | ||
| 33 | writer.write(System.lineSeparator()); | ||
| 34 | for (ClassMapping innerClassMapping : sorted(classMapping.innerClasses())) { | ||
| 35 | if(innerClassMapping.getDeobfName() == null || innerClassMapping.getObfSimpleName() == null || innerClassMapping.getDeobfName() == null){ | ||
| 36 | continue; | ||
| 37 | } | ||
| 38 | String innerClassName = classMapping.getObfSimpleName() + "$" + innerClassMapping.getObfSimpleName().replace("none/", ""); | ||
| 39 | String innerDebofClassName = classMapping.getDeobfName() + "$" + innerClassMapping.getDeobfName().replace("none/", ""); | ||
| 40 | writer.write("CL: " + innerClassName + " " + classMapping.getDeobfName() + "$" + innerClassMapping.getDeobfName()); | ||
| 41 | writer.write(System.lineSeparator()); | ||
| 42 | for (FieldMapping fieldMapping : sorted(innerClassMapping.fields())) { | ||
| 43 | fieldMappings.add("FD: " + innerClassName + "/" + fieldMapping.getObfName() + " " + innerDebofClassName + "/" + fieldMapping.getDeobfName()); | ||
| 44 | } | ||
| 45 | |||
| 46 | for (MethodMapping methodMapping : sorted(innerClassMapping.methods())) { | ||
| 47 | methodMappings.add("MD: " + innerClassName + "/" + methodMapping.getObfName() + " " + methodMapping.getObfSignature().toString().replace("none/", "") + " " + innerDebofClassName + "/" + methodMapping.getDeobfName() + " " + deobfuscator.getTranslator(TranslationDirection.Deobfuscating).translateSignature(methodMapping.getObfSignature())); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | for (FieldMapping fieldMapping : sorted(classMapping.fields())) { | ||
| 52 | fieldMappings.add("FD: " + classMapping.getObfFullName().replace("none/", "") + "/" + fieldMapping.getObfName() + " " + classMapping.getDeobfName() + "/" + fieldMapping.getDeobfName()); | ||
| 53 | } | ||
| 54 | |||
| 55 | for (MethodMapping methodMapping : sorted(classMapping.methods())) { | ||
| 56 | methodMappings.add("MD: " + classMapping.getObfFullName().replace("none/", "") + "/" + methodMapping.getObfName() + " " + methodMapping.getObfSignature().toString().replace("none/", "") + " " + classMapping.getDeobfName() + "/" + methodMapping.getDeobfName() + " " + deobfuscator.getTranslator(TranslationDirection.Deobfuscating).translateSignature(methodMapping.getObfSignature())); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | for(String fd : fieldMappings){ | ||
| 60 | writer.write(fd); | ||
| 61 | writer.write(System.lineSeparator()); | ||
| 62 | } | ||
| 63 | |||
| 64 | for(String md : methodMappings){ | ||
| 65 | writer.write(md); | ||
| 66 | writer.write(System.lineSeparator()); | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | writer.close(); | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | private <T extends Comparable<T>> List<T> sorted(Iterable<T> classes) { | ||
| 75 | List<T> out = new ArrayList<T>(); | ||
| 76 | for (T t : classes) { | ||
| 77 | out.add(t); | ||
| 78 | } | ||
| 79 | Collections.sort(out); | ||
| 80 | return out; | ||
| 81 | } | ||
| 82 | } | ||