summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/ConvertMain.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/cuchaz/enigma/ConvertMain.java')
-rw-r--r--src/cuchaz/enigma/ConvertMain.java322
1 files changed, 322 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/ConvertMain.java b/src/cuchaz/enigma/ConvertMain.java
new file mode 100644
index 0000000..17bd2f8
--- /dev/null
+++ b/src/cuchaz/enigma/ConvertMain.java
@@ -0,0 +1,322 @@
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 ******************************************************************************/
11package cuchaz.enigma;
12
13import java.io.File;
14import java.io.FileReader;
15import java.io.FileWriter;
16import java.io.IOException;
17import java.util.jar.JarFile;
18
19import cuchaz.enigma.convert.ClassMatches;
20import cuchaz.enigma.convert.MappingsConverter;
21import cuchaz.enigma.convert.MatchesReader;
22import cuchaz.enigma.convert.MatchesWriter;
23import cuchaz.enigma.convert.MemberMatches;
24import cuchaz.enigma.gui.ClassMatchingGui;
25import cuchaz.enigma.gui.MemberMatchingGui;
26import cuchaz.enigma.mapping.BehaviorEntry;
27import cuchaz.enigma.mapping.ClassEntry;
28import cuchaz.enigma.mapping.ClassMapping;
29import cuchaz.enigma.mapping.FieldEntry;
30import cuchaz.enigma.mapping.FieldMapping;
31import cuchaz.enigma.mapping.MappingParseException;
32import cuchaz.enigma.mapping.Mappings;
33import cuchaz.enigma.mapping.MappingsChecker;
34import cuchaz.enigma.mapping.MappingsReader;
35import cuchaz.enigma.mapping.MappingsWriter;
36import cuchaz.enigma.mapping.MethodMapping;
37
38
39public class ConvertMain {
40
41 public static void main(String[] args)
42 throws IOException, MappingParseException {
43
44 // init files
45 File home = new File(System.getProperty("user.home"));
46 JarFile sourceJar = new JarFile(new File(home, ".minecraft/versions/1.8/1.8.jar"));
47 JarFile destJar = new JarFile(new File(home, ".minecraft/versions/1.8.3/1.8.3.jar"));
48 File inMappingsFile = new File("../Enigma Mappings/1.8.mappings");
49 File outMappingsFile = new File("../Enigma Mappings/1.8.3.mappings");
50 Mappings mappings = new MappingsReader().read(new FileReader(inMappingsFile));
51 File classMatchesFile = new File(inMappingsFile.getName() + ".class.matches");
52 File fieldMatchesFile = new File(inMappingsFile.getName() + ".field.matches");
53 File methodMatchesFile = new File(inMappingsFile.getName() + ".method.matches");
54
55 // match classes
56 //computeClassMatches(classMatchesFile, sourceJar, destJar, mappings);
57 //editClasssMatches(classMatchesFile, sourceJar, destJar, mappings);
58 //convertMappings(outMappingsFile, sourceJar, destJar, mappings, classMatchesFile);
59
60 // match fields
61 //computeFieldMatches(fieldMatchesFile, destJar, outMappingsFile, classMatchesFile);
62 //editFieldMatches(sourceJar, destJar, outMappingsFile, mappings, classMatchesFile, fieldMatchesFile);
63 //convertMappings(outMappingsFile, sourceJar, destJar, mappings, classMatchesFile, fieldMatchesFile);
64
65 // match methods/constructors
66 //computeMethodMatches(methodMatchesFile, destJar, outMappingsFile, classMatchesFile);
67 //editMethodMatches(sourceJar, destJar, outMappingsFile, mappings, classMatchesFile, methodMatchesFile);
68 convertMappings(outMappingsFile, sourceJar, destJar, mappings, classMatchesFile, fieldMatchesFile, methodMatchesFile);
69 }
70
71 private static void computeClassMatches(File classMatchesFile, JarFile sourceJar, JarFile destJar, Mappings mappings)
72 throws IOException {
73 ClassMatches classMatches = MappingsConverter.computeClassMatches(sourceJar, destJar, mappings);
74 MatchesWriter.writeClasses(classMatches, classMatchesFile);
75 System.out.println("Wrote:\n\t" + classMatchesFile.getAbsolutePath());
76 }
77
78 private static void editClasssMatches(final File classMatchesFile, JarFile sourceJar, JarFile destJar, Mappings mappings)
79 throws IOException {
80 System.out.println("Reading class matches...");
81 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
82 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
83 deobfuscators.source.setMappings(mappings);
84 System.out.println("Starting GUI...");
85 new ClassMatchingGui(classMatches, deobfuscators.source, deobfuscators.dest).setSaveListener(new ClassMatchingGui.SaveListener() {
86 @Override
87 public void save(ClassMatches matches) {
88 try {
89 MatchesWriter.writeClasses(matches, classMatchesFile);
90 } catch (IOException ex) {
91 throw new Error(ex);
92 }
93 }
94 });
95 }
96
97 private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchesFile)
98 throws IOException {
99 System.out.println("Reading class matches...");
100 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
101 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
102 deobfuscators.source.setMappings(mappings);
103
104 Mappings newMappings = MappingsConverter.newMappings(classMatches, mappings, deobfuscators.source, deobfuscators.dest);
105
106 try (FileWriter out = new FileWriter(outMappingsFile)) {
107 new MappingsWriter().write(out, newMappings);
108 }
109 System.out.println("Write converted mappings to: " + outMappingsFile.getAbsolutePath());
110 }
111
112 private static void computeFieldMatches(File memberMatchesFile, JarFile destJar, File destMappingsFile, File classMatchesFile)
113 throws IOException, MappingParseException {
114
115 System.out.println("Reading class matches...");
116 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
117 System.out.println("Reading mappings...");
118 Mappings destMappings = new MappingsReader().read(new FileReader(destMappingsFile));
119 System.out.println("Indexing dest jar...");
120 Deobfuscator destDeobfuscator = new Deobfuscator(destJar);
121
122 System.out.println("Writing matches...");
123
124 // get the matched and unmatched mappings
125 MemberMatches<FieldEntry> fieldMatches = MappingsConverter.computeMemberMatches(
126 destDeobfuscator,
127 destMappings,
128 classMatches,
129 MappingsConverter.getFieldDoer()
130 );
131
132 MatchesWriter.writeMembers(fieldMatches, memberMatchesFile);
133 System.out.println("Wrote:\n\t" + memberMatchesFile.getAbsolutePath());
134 }
135
136 private static void editFieldMatches(JarFile sourceJar, JarFile destJar, File destMappingsFile, Mappings sourceMappings, File classMatchesFile, final File fieldMatchesFile)
137 throws IOException, MappingParseException {
138
139 System.out.println("Reading matches...");
140 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
141 MemberMatches<FieldEntry> fieldMatches = MatchesReader.readMembers(fieldMatchesFile);
142
143 // prep deobfuscators
144 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
145 deobfuscators.source.setMappings(sourceMappings);
146 Mappings destMappings = new MappingsReader().read(new FileReader(destMappingsFile));
147 MappingsChecker checker = new MappingsChecker(deobfuscators.dest.getJarIndex());
148 checker.dropBrokenMappings(destMappings);
149 deobfuscators.dest.setMappings(destMappings);
150
151 new MemberMatchingGui<FieldEntry>(classMatches, fieldMatches, deobfuscators.source, deobfuscators.dest).setSaveListener(new MemberMatchingGui.SaveListener<FieldEntry>() {
152 @Override
153 public void save(MemberMatches<FieldEntry> matches) {
154 try {
155 MatchesWriter.writeMembers(matches, fieldMatchesFile);
156 } catch (IOException ex) {
157 throw new Error(ex);
158 }
159 }
160 });
161 }
162
163 private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchesFile, File fieldMatchesFile)
164 throws IOException {
165
166 System.out.println("Reading matches...");
167 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
168 MemberMatches<FieldEntry> fieldMatches = MatchesReader.readMembers(fieldMatchesFile);
169
170 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
171 deobfuscators.source.setMappings(mappings);
172
173 // apply matches
174 Mappings newMappings = MappingsConverter.newMappings(classMatches, mappings, deobfuscators.source, deobfuscators.dest);
175 MappingsConverter.applyMemberMatches(newMappings, classMatches, fieldMatches, MappingsConverter.getFieldDoer());
176
177 // write out the converted mappings
178 try (FileWriter out = new FileWriter(outMappingsFile)) {
179 new MappingsWriter().write(out, newMappings);
180 }
181 System.out.println("Wrote converted mappings to:\n\t" + outMappingsFile.getAbsolutePath());
182 }
183
184
185 private static void computeMethodMatches(File methodMatchesFile, JarFile destJar, File destMappingsFile, File classMatchesFile)
186 throws IOException, MappingParseException {
187
188 System.out.println("Reading class matches...");
189 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
190 System.out.println("Reading mappings...");
191 Mappings destMappings = new MappingsReader().read(new FileReader(destMappingsFile));
192 System.out.println("Indexing dest jar...");
193 Deobfuscator destDeobfuscator = new Deobfuscator(destJar);
194
195 System.out.println("Writing method matches...");
196
197 // get the matched and unmatched mappings
198 MemberMatches<BehaviorEntry> methodMatches = MappingsConverter.computeMemberMatches(
199 destDeobfuscator,
200 destMappings,
201 classMatches,
202 MappingsConverter.getMethodDoer()
203 );
204
205 MatchesWriter.writeMembers(methodMatches, methodMatchesFile);
206 System.out.println("Wrote:\n\t" + methodMatchesFile.getAbsolutePath());
207 }
208
209 private static void editMethodMatches(JarFile sourceJar, JarFile destJar, File destMappingsFile, Mappings sourceMappings, File classMatchesFile, final File methodMatchesFile)
210 throws IOException, MappingParseException {
211
212 System.out.println("Reading matches...");
213 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
214 MemberMatches<BehaviorEntry> methodMatches = MatchesReader.readMembers(methodMatchesFile);
215
216 // prep deobfuscators
217 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
218 deobfuscators.source.setMappings(sourceMappings);
219 Mappings destMappings = new MappingsReader().read(new FileReader(destMappingsFile));
220 MappingsChecker checker = new MappingsChecker(deobfuscators.dest.getJarIndex());
221 checker.dropBrokenMappings(destMappings);
222 deobfuscators.dest.setMappings(destMappings);
223
224 new MemberMatchingGui<BehaviorEntry>(classMatches, methodMatches, deobfuscators.source, deobfuscators.dest).setSaveListener(new MemberMatchingGui.SaveListener<BehaviorEntry>() {
225 @Override
226 public void save(MemberMatches<BehaviorEntry> matches) {
227 try {
228 MatchesWriter.writeMembers(matches, methodMatchesFile);
229 } catch (IOException ex) {
230 throw new Error(ex);
231 }
232 }
233 });
234 }
235
236 private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchesFile, File fieldMatchesFile, File methodMatchesFile)
237 throws IOException {
238
239 System.out.println("Reading matches...");
240 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
241 MemberMatches<FieldEntry> fieldMatches = MatchesReader.readMembers(fieldMatchesFile);
242 MemberMatches<BehaviorEntry> methodMatches = MatchesReader.readMembers(methodMatchesFile);
243
244 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
245 deobfuscators.source.setMappings(mappings);
246
247 // apply matches
248 Mappings newMappings = MappingsConverter.newMappings(classMatches, mappings, deobfuscators.source, deobfuscators.dest);
249 MappingsConverter.applyMemberMatches(newMappings, classMatches, fieldMatches, MappingsConverter.getFieldDoer());
250 MappingsConverter.applyMemberMatches(newMappings, classMatches, methodMatches, MappingsConverter.getMethodDoer());
251
252 // check the final mappings
253 MappingsChecker checker = new MappingsChecker(deobfuscators.dest.getJarIndex());
254 checker.dropBrokenMappings(newMappings);
255
256 for (java.util.Map.Entry<ClassEntry,ClassMapping> mapping : checker.getDroppedClassMappings().entrySet()) {
257 System.out.println("WARNING: Broken class entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
258 }
259 for (java.util.Map.Entry<ClassEntry,ClassMapping> mapping : checker.getDroppedInnerClassMappings().entrySet()) {
260 System.out.println("WARNING: Broken inner class entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
261 }
262 for (java.util.Map.Entry<FieldEntry,FieldMapping> mapping : checker.getDroppedFieldMappings().entrySet()) {
263 System.out.println("WARNING: Broken field entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
264 }
265 for (java.util.Map.Entry<BehaviorEntry,MethodMapping> mapping : checker.getDroppedMethodMappings().entrySet()) {
266 System.out.println("WARNING: Broken behavior entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
267 }
268
269 // write out the converted mappings
270 try (FileWriter out = new FileWriter(outMappingsFile)) {
271 new MappingsWriter().write(out, newMappings);
272 }
273 System.out.println("Wrote converted mappings to:\n\t" + outMappingsFile.getAbsolutePath());
274 }
275
276 private static class Deobfuscators {
277
278 public Deobfuscator source;
279 public Deobfuscator dest;
280
281 public Deobfuscators(JarFile sourceJar, JarFile destJar) {
282 System.out.println("Indexing source jar...");
283 IndexerThread sourceIndexer = new IndexerThread(sourceJar);
284 sourceIndexer.start();
285 System.out.println("Indexing dest jar...");
286 IndexerThread destIndexer = new IndexerThread(destJar);
287 destIndexer.start();
288 sourceIndexer.joinOrBail();
289 destIndexer.joinOrBail();
290 source = sourceIndexer.deobfuscator;
291 dest = destIndexer.deobfuscator;
292 }
293 }
294
295 private static class IndexerThread extends Thread {
296
297 private JarFile m_jarFile;
298 public Deobfuscator deobfuscator;
299
300 public IndexerThread(JarFile jarFile) {
301 m_jarFile = jarFile;
302 deobfuscator = null;
303 }
304
305 public void joinOrBail() {
306 try {
307 join();
308 } catch (InterruptedException ex) {
309 throw new Error(ex);
310 }
311 }
312
313 @Override
314 public void run() {
315 try {
316 deobfuscator = new Deobfuscator(m_jarFile);
317 } catch (IOException ex) {
318 throw new Error(ex);
319 }
320 }
321 }
322}