summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/ConvertMain.java
diff options
context:
space:
mode:
authorGravatar jeff2015-03-09 20:08:15 -0400
committerGravatar jeff2015-03-09 20:08:15 -0400
commit1ad33bfe0a96b1b4a1f3c02cf2c054e8a101dfd8 (patch)
tree86ce734a5bb32a7b205c096118faf41996521a43 /src/cuchaz/enigma/ConvertMain.java
parentstarting on field matching gui (diff)
downloadenigma-fork-1ad33bfe0a96b1b4a1f3c02cf2c054e8a101dfd8.tar.gz
enigma-fork-1ad33bfe0a96b1b4a1f3c02cf2c054e8a101dfd8.tar.xz
enigma-fork-1ad33bfe0a96b1b4a1f3c02cf2c054e8a101dfd8.zip
field matcher is starting to be useful
Diffstat (limited to 'src/cuchaz/enigma/ConvertMain.java')
-rw-r--r--src/cuchaz/enigma/ConvertMain.java135
1 files changed, 108 insertions, 27 deletions
diff --git a/src/cuchaz/enigma/ConvertMain.java b/src/cuchaz/enigma/ConvertMain.java
index 624eb40..a5a00e8 100644
--- a/src/cuchaz/enigma/ConvertMain.java
+++ b/src/cuchaz/enigma/ConvertMain.java
@@ -4,8 +4,12 @@ import java.io.File;
4import java.io.FileReader; 4import java.io.FileReader;
5import java.io.FileWriter; 5import java.io.FileWriter;
6import java.io.IOException; 6import java.io.IOException;
7import java.util.Set;
7import java.util.jar.JarFile; 8import java.util.jar.JarFile;
8 9
10import com.google.common.collect.BiMap;
11import com.google.common.collect.Sets;
12
9import cuchaz.enigma.convert.ClassMatches; 13import cuchaz.enigma.convert.ClassMatches;
10import cuchaz.enigma.convert.FieldMatches; 14import cuchaz.enigma.convert.FieldMatches;
11import cuchaz.enigma.convert.MappingsConverter; 15import cuchaz.enigma.convert.MappingsConverter;
@@ -13,11 +17,18 @@ import cuchaz.enigma.convert.MatchesReader;
13import cuchaz.enigma.convert.MatchesWriter; 17import cuchaz.enigma.convert.MatchesWriter;
14import cuchaz.enigma.gui.ClassMatchingGui; 18import cuchaz.enigma.gui.ClassMatchingGui;
15import cuchaz.enigma.gui.FieldMatchingGui; 19import cuchaz.enigma.gui.FieldMatchingGui;
20import cuchaz.enigma.mapping.ClassEntry;
21import cuchaz.enigma.mapping.ClassMapping;
22import cuchaz.enigma.mapping.ClassNameReplacer;
23import cuchaz.enigma.mapping.EntryFactory;
24import cuchaz.enigma.mapping.FieldEntry;
25import cuchaz.enigma.mapping.FieldMapping;
16import cuchaz.enigma.mapping.MappingParseException; 26import cuchaz.enigma.mapping.MappingParseException;
17import cuchaz.enigma.mapping.Mappings; 27import cuchaz.enigma.mapping.Mappings;
18import cuchaz.enigma.mapping.MappingsChecker; 28import cuchaz.enigma.mapping.MappingsChecker;
19import cuchaz.enigma.mapping.MappingsReader; 29import cuchaz.enigma.mapping.MappingsReader;
20import cuchaz.enigma.mapping.MappingsWriter; 30import cuchaz.enigma.mapping.MappingsWriter;
31import cuchaz.enigma.mapping.Type;
21 32
22 33
23public class ConvertMain { 34public class ConvertMain {
@@ -32,13 +43,14 @@ public class ConvertMain {
32 File inMappingsFile = new File("../Enigma Mappings/1.8.mappings"); 43 File inMappingsFile = new File("../Enigma Mappings/1.8.mappings");
33 File outMappingsFile = new File("../Enigma Mappings/1.8.3.mappings"); 44 File outMappingsFile = new File("../Enigma Mappings/1.8.3.mappings");
34 Mappings mappings = new MappingsReader().read(new FileReader(inMappingsFile)); 45 Mappings mappings = new MappingsReader().read(new FileReader(inMappingsFile));
35 File classMatchingFile = new File(inMappingsFile.getName() + ".class.matching"); 46 File classMatchesFile = new File(inMappingsFile.getName() + ".class.matches");
36 File fieldMatchingFile = new File(inMappingsFile.getName() + ".field.matching"); 47 File fieldMatchesFile = new File(inMappingsFile.getName() + ".field.matches");
37 48
38 //computeMatches(classMatchingFile, sourceJar, destJar, mappings); 49 //computeClassMatches(classMatchingFile, sourceJar, destJar, mappings);
39 //editClasssMatches(classMatchingFile, sourceJar, destJar, mappings); 50 //editClasssMatches(classMatchingFile, sourceJar, destJar, mappings);
40 //convertMappings(outMappingsFile, sourceJar, destJar, mappings, classMatchingFile); 51 //convertMappings(outMappingsFile, sourceJar, destJar, mappings, classMatchingFile);
41 editFieldMatches(sourceJar, destJar, outMappingsFile, mappings, classMatchingFile, fieldMatchingFile); 52 //computeFieldMatches(fieldMatchesFile, destJar, outMappingsFile, classMatchesFile);
53 editFieldMatches(sourceJar, destJar, outMappingsFile, mappings, classMatchesFile, fieldMatchesFile);
42 54
43 /* TODO 55 /* TODO
44 // write out the converted mappings 56 // write out the converted mappings
@@ -49,17 +61,17 @@ public class ConvertMain {
49 */ 61 */
50 } 62 }
51 63
52 private static void computeMatches(File classMatchingFile, JarFile sourceJar, JarFile destJar, Mappings mappings) 64 private static void computeClassMatches(File classMatchesFile, JarFile sourceJar, JarFile destJar, Mappings mappings)
53 throws IOException { 65 throws IOException {
54 ClassMatches classMatches = MappingsConverter.computeMatches(sourceJar, destJar, mappings); 66 ClassMatches classMatches = MappingsConverter.computeMatches(sourceJar, destJar, mappings);
55 MatchesWriter.writeClasses(classMatches, classMatchingFile); 67 MatchesWriter.writeClasses(classMatches, classMatchesFile);
56 System.out.println("Wrote:\n\t" + classMatchingFile.getAbsolutePath()); 68 System.out.println("Wrote:\n\t" + classMatchesFile.getAbsolutePath());
57 } 69 }
58 70
59 private static void editClasssMatches(final File classMatchingFile, JarFile sourceJar, JarFile destJar, Mappings mappings) 71 private static void editClasssMatches(final File classMatchesFile, JarFile sourceJar, JarFile destJar, Mappings mappings)
60 throws IOException { 72 throws IOException {
61 System.out.println("Reading matches..."); 73 System.out.println("Reading class matches...");
62 ClassMatches classMatches = MatchesReader.readClasses(classMatchingFile); 74 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
63 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar); 75 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
64 deobfuscators.source.setMappings(mappings); 76 deobfuscators.source.setMappings(mappings);
65 System.out.println("Starting GUI..."); 77 System.out.println("Starting GUI...");
@@ -67,7 +79,7 @@ public class ConvertMain {
67 @Override 79 @Override
68 public void save(ClassMatches matches) { 80 public void save(ClassMatches matches) {
69 try { 81 try {
70 MatchesWriter.writeClasses(matches, classMatchingFile); 82 MatchesWriter.writeClasses(matches, classMatchesFile);
71 } catch (IOException ex) { 83 } catch (IOException ex) {
72 throw new Error(ex); 84 throw new Error(ex);
73 } 85 }
@@ -75,10 +87,10 @@ public class ConvertMain {
75 }); 87 });
76 } 88 }
77 89
78 private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchingFile) 90 private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchesFile)
79 throws IOException { 91 throws IOException {
80 System.out.println("Reading matches..."); 92 System.out.println("Reading class matches...");
81 ClassMatches classMatches = MatchesReader.readClasses(classMatchingFile); 93 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
82 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar); 94 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
83 deobfuscators.source.setMappings(mappings); 95 deobfuscators.source.setMappings(mappings);
84 96
@@ -90,19 +102,90 @@ public class ConvertMain {
90 System.out.println("Write converted mappings to: " + outMappingsFile.getAbsolutePath()); 102 System.out.println("Write converted mappings to: " + outMappingsFile.getAbsolutePath());
91 } 103 }
92 104
93 private static void editFieldMatches(JarFile sourceJar, JarFile destJar, File destMappingsFile, Mappings sourceMappings, File classMatchingFile, final File fieldMatchingFile) 105 private static void computeFieldMatches(File fieldMatchesFile, JarFile destJar, File destMappingsFile, File classMatchesFile)
94 throws IOException, MappingParseException { 106 throws IOException, MappingParseException {
95 107
96 System.out.println("Reading matches..."); 108 System.out.println("Reading class matches...");
97 ClassMatches classMatches = MatchesReader.readClasses(classMatchingFile); 109 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
98 FieldMatches fieldMatches; 110 System.out.println("Reading mappings...");
99 if (fieldMatchingFile.exists() /* TEMP */ && false) { 111 Mappings destMappings = new MappingsReader().read(new FileReader(destMappingsFile));
100 // TODO 112 System.out.println("Indexing dest jar...");
101 //fieldMatches = MatchesReader.readFields(fieldMatchingFile); 113 Deobfuscator destDeobfuscator = new Deobfuscator(destJar);
102 } else { 114
103 fieldMatches = new FieldMatches(); 115 System.out.println("Writing field matches...");
116
117 // get the matched and unmatched field mappings
118 FieldMatches fieldMatches = new FieldMatches();
119
120 // unmatched source fields are easy
121 MappingsChecker checker = new MappingsChecker(destDeobfuscator.getJarIndex());
122 checker.dropBrokenMappings(destMappings);
123 for (FieldEntry destObfField : checker.getDroppedFieldMappings().keySet()) {
124 FieldEntry srcObfField = translate(destObfField, classMatches.getUniqueMatches().inverse());
125 fieldMatches.addUnmatchedSourceField(srcObfField);
126 }
127
128 // get matched fields (anything that's left after the checks/drops is matched(
129 for (ClassMapping classMapping : destMappings.classes()) {
130 collectMatchedFields(fieldMatches, classMapping, classMatches);
131 }
132
133 // get unmatched dest fields
134 Set<FieldEntry> unmatchedDestFields = Sets.newHashSet();
135 for (FieldEntry destFieldEntry : destDeobfuscator.getJarIndex().getObfFieldEntries()) {
136 if (!fieldMatches.isDestMatched(destFieldEntry)) {
137 unmatchedDestFields.add(destFieldEntry);
138 }
139 }
140 fieldMatches.addUnmatchedDestFields(unmatchedDestFields);
141
142 MatchesWriter.writeFields(fieldMatches, fieldMatchesFile);
143 System.out.println("Wrote:\n\t" + fieldMatchesFile.getAbsolutePath());
144 }
145
146 private static void collectMatchedFields(FieldMatches fieldMatches, ClassMapping destClassMapping, ClassMatches classMatches) {
147
148 // get the fields for this class
149 for (FieldMapping destFieldMapping : destClassMapping.fields()) {
150 FieldEntry destObfField = EntryFactory.getObfFieldEntry(destClassMapping, destFieldMapping);
151 FieldEntry srcObfField = translate(destObfField, classMatches.getUniqueMatches().inverse());
152 fieldMatches.addMatch(srcObfField, destObfField);
104 } 153 }
105 154
155 // recurse
156 for (ClassMapping destInnerClassMapping : destClassMapping.innerClasses()) {
157 collectMatchedFields(fieldMatches, destInnerClassMapping, classMatches);
158 }
159 }
160
161 private static FieldEntry translate(FieldEntry in, BiMap<ClassEntry,ClassEntry> map) {
162 return new FieldEntry(
163 map.get(in.getClassEntry()),
164 in.getName(),
165 translate(in.getType(), map)
166 );
167 }
168
169 private static Type translate(Type type, final BiMap<ClassEntry,ClassEntry> map) {
170 return new Type(type, new ClassNameReplacer() {
171 @Override
172 public String replace(String inClassName) {
173 ClassEntry outClassEntry = map.get(new ClassEntry(inClassName));
174 if (outClassEntry == null) {
175 return null;
176 }
177 return outClassEntry.getName();
178 }
179 });
180 }
181
182 private static void editFieldMatches(JarFile sourceJar, JarFile destJar, File destMappingsFile, Mappings sourceMappings, File classMatchesFile, final File fieldMatchesFile)
183 throws IOException, MappingParseException {
184
185 System.out.println("Reading matches...");
186 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
187 FieldMatches fieldMatches = MatchesReader.readFields(fieldMatchesFile);
188
106 // prep deobfuscators 189 // prep deobfuscators
107 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar); 190 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
108 deobfuscators.source.setMappings(sourceMappings); 191 deobfuscators.source.setMappings(sourceMappings);
@@ -111,16 +194,14 @@ public class ConvertMain {
111 checker.dropBrokenMappings(destMappings); 194 checker.dropBrokenMappings(destMappings);
112 deobfuscators.dest.setMappings(destMappings); 195 deobfuscators.dest.setMappings(destMappings);
113 196
114 new FieldMatchingGui(classMatches, fieldMatches, checker.getDroppedFieldMappings(), deobfuscators.source, deobfuscators.dest).setSaveListener(new FieldMatchingGui.SaveListener() { 197 new FieldMatchingGui(classMatches, fieldMatches, deobfuscators.source, deobfuscators.dest).setSaveListener(new FieldMatchingGui.SaveListener() {
115 @Override 198 @Override
116 public void save(FieldMatches matches) { 199 public void save(FieldMatches matches) {
117 /* TODO
118 try { 200 try {
119 MatchesWriter.writeFields(matches, fieldMatchingFile); 201 MatchesWriter.writeFields(matches, fieldMatchesFile);
120 } catch (IOException ex) { 202 } catch (IOException ex) {
121 throw new Error(ex); 203 throw new Error(ex);
122 } 204 }
123 */
124 } 205 }
125 }); 206 });
126 } 207 }