summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java')
-rw-r--r--src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java
new file mode 100644
index 0000000..a3b0616
--- /dev/null
+++ b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java
@@ -0,0 +1,137 @@
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.mapping;
12
13import com.google.common.base.Charsets;
14
15import java.io.*;
16import java.util.ArrayList;
17import java.util.Collections;
18import java.util.List;
19
20public class MappingsEnigmaWriter {
21
22 public void write(File out, Mappings mappings, boolean isDirectoryFormat) throws IOException {
23 if (!isDirectoryFormat)
24 {
25 PrintWriter outputWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(out), Charsets.UTF_8));
26 write(outputWriter, mappings);
27 outputWriter.close();
28 }
29 else
30 writeAsDirectory(out, mappings);
31 }
32
33 public void writeAsDirectory(File target, Mappings mappings) throws IOException {
34 if (!target.exists() && !target.mkdirs())
35 throw new IOException("Cannot create mapping directory!");
36
37 for (ClassMapping classMapping : sorted(mappings.classes())) {
38 File obFile = new File(target, classMapping.getObfFullName() + ".mapping");
39 File result;
40 if (classMapping.getDeobfName() == null)
41 result = obFile;
42 else
43 {
44 // Make sure that old version of the file doesn't exist
45 if (obFile.exists())
46 obFile.delete();
47 result = new File(target, classMapping.getDeobfName() + ".mapping");
48 }
49
50 if (!result.getParentFile().exists())
51 result.getParentFile().mkdirs();
52 result.createNewFile();
53 PrintWriter outputWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(result), Charsets.UTF_8));
54 write(outputWriter, classMapping, 0);
55 outputWriter.close();
56 }
57
58 // Remove empty directories
59 File[] fileList = target.listFiles();
60 if (fileList != null)
61 {
62 for (File file : fileList)
63 {
64 if (file.isDirectory())
65 {
66 File[] childFiles = file.listFiles();
67 if (childFiles != null && childFiles.length == 0)
68 file.delete();
69 }
70 }
71 }
72
73 }
74
75 public void write(PrintWriter out, Mappings mappings) throws IOException {
76 for (ClassMapping classMapping : sorted(mappings.classes())) {
77 write(out, classMapping, 0);
78 }
79 }
80
81 private void write(PrintWriter out, ClassMapping classMapping, int depth) throws IOException {
82 if (classMapping.getDeobfName() == null) {
83 out.format("%sCLASS %s\n", getIndent(depth), classMapping.getObfFullName());
84 } else {
85 out.format("%sCLASS %s %s\n", getIndent(depth), classMapping.getObfFullName(), classMapping.getDeobfName());
86 }
87
88 for (ClassMapping innerClassMapping : sorted(classMapping.innerClasses())) {
89 write(out, innerClassMapping, depth + 1);
90 }
91
92 for (FieldMapping fieldMapping : sorted(classMapping.fields())) {
93 write(out, fieldMapping, depth + 1);
94 }
95
96 for (MethodMapping methodMapping : sorted(classMapping.methods())) {
97 write(out, methodMapping, depth + 1);
98 }
99 }
100
101 private void write(PrintWriter out, FieldMapping fieldMapping, int depth) throws IOException {
102 out.format("%sFIELD %s %s %s\n", getIndent(depth), fieldMapping.getObfName(), fieldMapping.getDeobfName(), fieldMapping.getObfType().toString());
103 }
104
105 private void write(PrintWriter out, MethodMapping methodMapping, int depth) throws IOException {
106 if (methodMapping.getDeobfName() == null) {
107 out.format("%sMETHOD %s %s\n", getIndent(depth), methodMapping.getObfName(), methodMapping.getObfSignature());
108 } else {
109 out.format("%sMETHOD %s %s %s\n", getIndent(depth), methodMapping.getObfName(), methodMapping.getDeobfName(), methodMapping.getObfSignature());
110 }
111
112 for (ArgumentMapping argumentMapping : sorted(methodMapping.arguments())) {
113 write(out, argumentMapping, depth + 1);
114 }
115 }
116
117 private void write(PrintWriter out, ArgumentMapping argumentMapping, int depth) throws IOException {
118 out.format("%sARG %d %s\n", getIndent(depth), argumentMapping.getIndex(), argumentMapping.getName());
119 }
120
121 private <T extends Comparable<T>> List<T> sorted(Iterable<T> classes) {
122 List<T> out = new ArrayList<T>();
123 for (T t : classes) {
124 out.add(t);
125 }
126 Collections.sort(out);
127 return out;
128 }
129
130 private String getIndent(int depth) {
131 StringBuilder buf = new StringBuilder();
132 for (int i = 0; i < depth; i++) {
133 buf.append("\t");
134 }
135 return buf.toString();
136 }
137}