summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/mapping/MappingsSRGWriter.java
blob: 32f0ee9f299c49776a6af735b55018d1ba1e6bbc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cuchaz.enigma.mapping;

import com.google.common.base.Charsets;
import cuchaz.enigma.analysis.TranslationIndex;
import cuchaz.enigma.mapping.entry.ReferencedEntryPool;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Mark on 11/08/2016.
 */
public class MappingsSRGWriter {

	public void write(File file, Mappings mappings) throws IOException {
		if (file.exists()) {
			file.delete();
		}
		file.createNewFile();

		TranslationIndex index = new TranslationIndex(new ReferencedEntryPool());

		PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8));
		List<String> fieldMappings = new ArrayList<>();
		List<String> methodMappings = new ArrayList<>();
		for (ClassMapping classMapping : sorted(mappings.classes())) {
			if (classMapping.getDeobfName() == null || classMapping.getObfSimpleName() == null || classMapping.getDeobfName() == null) {
				continue;
			}
			writer.write("CL: " + classMapping.getObfSimpleName() + " " + classMapping.getDeobfName());
			writer.write(System.lineSeparator());
			for (ClassMapping innerClassMapping : sorted(classMapping.innerClasses())) {
				if (innerClassMapping.getDeobfName() == null || innerClassMapping.getObfSimpleName() == null || innerClassMapping.getDeobfName() == null) {
					continue;
				}
				String innerClassName = classMapping.getObfSimpleName() + "$" + innerClassMapping.getObfSimpleName();
				String innerDeobfClassName = classMapping.getDeobfName() + "$" + innerClassMapping.getDeobfName();
				writer.write("CL: " + innerClassName + " " + classMapping.getDeobfName() + "$" + innerClassMapping.getDeobfName());
				writer.write(System.lineSeparator());
				for (FieldMapping fieldMapping : sorted(innerClassMapping.fields())) {
					fieldMappings.add("FD: " + innerClassName + "/" + fieldMapping.getObfName() + " " + innerDeobfClassName + "/" + fieldMapping.getDeobfName());
				}

				for (MethodMapping methodMapping : sorted(innerClassMapping.methods())) {
					methodMappings.add("MD: " + innerClassName + "/" + methodMapping.getObfName() + " " + methodMapping.getObfDesc() + " " + innerDeobfClassName + "/" + methodMapping.getDeobfName() + " " + mappings.getTranslator(TranslationDirection.DEOBFUSCATING, index).getTranslatedMethodDesc(methodMapping.getObfDesc()));
				}
			}

			for (FieldMapping fieldMapping : sorted(classMapping.fields())) {
				fieldMappings.add("FD: " + classMapping.getObfFullName() + "/" + fieldMapping.getObfName() + " " + classMapping.getDeobfName() + "/" + fieldMapping.getDeobfName());
			}

			for (MethodMapping methodMapping : sorted(classMapping.methods())) {
				methodMappings.add("MD: " + classMapping.getObfFullName() + "/" + methodMapping.getObfName() + " " + methodMapping.getObfDesc() + " " + classMapping.getDeobfName() + "/" + methodMapping.getDeobfName() + " " + mappings.getTranslator(TranslationDirection.DEOBFUSCATING, index).getTranslatedMethodDesc(methodMapping.getObfDesc()));
			}
		}
		for (String fd : fieldMappings) {
			writer.write(fd);
			writer.write(System.lineSeparator());
		}

		for (String md : methodMappings) {
			writer.write(md);
			writer.write(System.lineSeparator());
		}

		writer.close();
	}

	private <T extends Comparable<T>> List<T> sorted(Iterable<T> classes) {
		List<T> out = new ArrayList<>();
		for (T t : classes) {
			out.add(t);
		}
		Collections.sort(out);
		return out;
	}
}