-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
MCPMappings.java
265 lines (245 loc) · 11.3 KB
/
MCPMappings.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package nallar.tickthreading.patcher.mappings;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import nallar.log.PatchLog;
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class MCPMappings extends Mappings {
private static final Pattern classObfuscatePattern = Pattern.compile("\\^class:([^\\^]+)\\^", Pattern.DOTALL | Pattern.MULTILINE);
private static final Pattern methodObfuscatePattern = Pattern.compile("\\^method:([^\\^/]+)/([^\\^/]+)\\^", Pattern.DOTALL | Pattern.MULTILINE);
private static final Pattern fieldObfuscatePattern = Pattern.compile("\\^field:([^\\^/]+)/([^\\^/]+)\\^", Pattern.DOTALL | Pattern.MULTILINE);
private final Map<String, String> methodSeargeMappings = new HashMap<String, String>();
private final Map<String, String> fieldSeargeMappings = new HashMap<String, String>();
private final BiMap<ClassDescription, ClassDescription> classMappings = HashBiMap.create();
private final BiMap<MethodDescription, MethodDescription> methodMappings = HashBiMap.create();
private final BiMap<MethodDescription, MethodDescription> methodSrgMappings = HashBiMap.create();
private final Map<FieldDescription, FieldDescription> fieldMappings = new HashMap<FieldDescription, FieldDescription>();
private final Map<FieldDescription, FieldDescription> fieldSrgMappings = new HashMap<FieldDescription, FieldDescription>();
private final Map<String, MethodDescription> parameterlessMethodMappings = new HashMap<String, MethodDescription>();
private final Map<String, MethodDescription> parameterlessSrgMethodMappings = new HashMap<String, MethodDescription>();
private final Map<String, String> shortClassNameToFullName = new HashMap<String, String>();
private final Map<String, List<String>> extendsMap;
private final boolean seargeMappings;
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
public MCPMappings(boolean seargeMappings) throws IOException {
this.seargeMappings = seargeMappings;
extendsMap = loadExtends(Mappings.class.getResourceAsStream("/extendsMap.obj"));
loadCsv(Mappings.class.getResourceAsStream("/methods.csv"), methodSeargeMappings);
loadCsv(Mappings.class.getResourceAsStream("/fields.csv"), fieldSeargeMappings);
loadSrg(Mappings.class.getResourceAsStream("/packaged.srg"));
methodSeargeMappings.clear();
fieldSeargeMappings.clear();
}
/*private void extendFieldMappings(Map<FieldDescription, FieldDescription> map) {
Map<FieldDescription, FieldDescription> copy = new HashMap<FieldDescription, FieldDescription>(map);
for (Map.Entry<FieldDescription, FieldDescription> e : copy.entrySet()) {
FieldDescription obfed = obfed.
while (true) {
String superClass = extendsMap.get(obfed.className);
if (superClass == null) {
break;
}
}
}
}*/
private Map<String, List<String>> loadExtends(InputStream resourceAsStream) throws IOException {
ObjectInputStream objectInputStream = new ObjectInputStream(resourceAsStream);
try {
Map<String, String> reversed = (Map<String, String>) objectInputStream.readObject();
Map<String, List<String>> extendsMap = new HashMap<String, List<String>>();
for (Map.Entry<String, String> e : reversed.entrySet()) {
List<String> l = extendsMap.get(e.getValue());
if (l == null) {
l = new ArrayList<String>();
extendsMap.put(e.getValue(), l);
}
l.add(e.getKey());
}
return extendsMap;
} catch (ClassNotFoundException e) {
PatchLog.severe("Failed to read extends mapping", e);
} finally {
objectInputStream.close();
}
return null;
}
@Override
public MethodDescription map(MethodDescription methodDescription) {
MethodDescription obfuscated = (seargeMappings ? methodSrgMappings : methodMappings).get(methodDescription);
if (obfuscated == null) {
obfuscated = (seargeMappings ? parameterlessSrgMethodMappings : parameterlessMethodMappings).get(methodDescription.getShortName());
if (methodDescription.isExact() || obfuscated == null) {
obfuscated = methodDescription;
obfuscated.obfuscateClasses();
}
}
return obfuscated;
}
@Override
public MethodDescription rmap(MethodDescription methodDescription) {
return (seargeMappings ? methodSrgMappings : methodMappings).inverse().get(methodDescription);
}
@Override
public String shortClassNameToFullClassName(String shortName) {
return shortClassNameToFullName.get(shortName);
}
@Override
public ClassDescription map(ClassDescription classDescription) {
return seargeMappings ? classDescription : classMappings.get(classDescription);
}
@Override
public FieldDescription map(FieldDescription fieldDescription) {
return (seargeMappings ? fieldSrgMappings : fieldMappings).get(fieldDescription);
}
private String classStringToClassName(String name) {
String mapped = shortClassNameToFullClassName(name);
if (mapped != null) {
name = mapped;
}
return map(new ClassDescription(name)).name;
}
@Override
public String obfuscate(String code) {
StringBuffer result = new StringBuffer();
{
Matcher methodMatcher = methodObfuscatePattern.matcher(code);
while (methodMatcher.find()) {
String className = shortClassNameToFullClassName(methodMatcher.group(1));
String methodDescriptionString = methodMatcher.group(2);
if (className == null) {
className = methodMatcher.group(1);
if (!className.contains(".")) {
PatchLog.severe("Could not find " + methodMatcher.group(1));
continue;
}
}
MethodDescription methodDescription = MethodDescription.fromString(className, methodDescriptionString);
MethodDescription mapped = map(methodDescription);
methodMatcher.appendReplacement(result, mapped.name);
}
methodMatcher.appendTail(result);
}
{
Matcher fieldMatcher = fieldObfuscatePattern.matcher(result);
result = new StringBuffer();
while (fieldMatcher.find()) {
String className = shortClassNameToFullClassName(fieldMatcher.group(1));
String fieldName = fieldMatcher.group(2);
if (className == null) {
className = fieldMatcher.group(1);
if (!className.contains(".")) {
PatchLog.severe("Could not find " + fieldMatcher.group(1));
continue;
}
}
FieldDescription fieldDescription = new FieldDescription(className, fieldName);
FieldDescription mapped = map(fieldDescription);
if (mapped == null) {
PatchLog.severe("Could not map " + fieldName);
fieldMatcher.appendReplacement(result, fieldName);
} else {
fieldMatcher.appendReplacement(result, mapped.name);
}
}
fieldMatcher.appendTail(result);
}
{
Matcher classMatcher = classObfuscatePattern.matcher(result);
result = new StringBuffer();
while (classMatcher.find()) {
String className = classStringToClassName(classMatcher.group(1));
if (className == null) {
PatchLog.severe("Could not find " + classMatcher.group(1));
continue;
}
classMatcher.appendReplacement(result, className);
}
classMatcher.appendTail(result);
}
return result.toString();
}
private void loadSrg(InputStream mappings) throws IOException {
Scanner srgScanner = new Scanner(mappings);
while (srgScanner.hasNextLine()) {
if (srgScanner.hasNext("CL:")) {
srgScanner.next();
String fromClass = srgScanner.next().replace('/', '.');
String toClass = srgScanner.next().replace('/', '.');
ClassDescription obfuscatedClass = new ClassDescription(fromClass);
ClassDescription deobfuscatedClass = new ClassDescription(toClass);
shortClassNameToFullName.put(deobfuscatedClass.name.substring(deobfuscatedClass.name.lastIndexOf('.') + 1), deobfuscatedClass.name);
classMappings.put(deobfuscatedClass, obfuscatedClass);
} else if (srgScanner.hasNext("FD:")) {
srgScanner.next();
String obfuscatedMCPName = srgScanner.next();
String seargeName = srgScanner.next();
seargeName = seargeName.substring(seargeName.lastIndexOf('/') + 1);
String deobfuscatedName = fieldSeargeMappings.get(seargeName);
if (deobfuscatedName == null) {
deobfuscatedName = seargeName;
}
FieldDescription obfuscatedField = new FieldDescription(obfuscatedMCPName);
FieldDescription deobfuscatedField = new FieldDescription(classMappings.inverse().get(new ClassDescription(obfuscatedField.className)).name, deobfuscatedName);
FieldDescription srgField = new FieldDescription(deobfuscatedField.className, seargeName);
fieldMappings.put(deobfuscatedField, obfuscatedField);
fieldSrgMappings.put(deobfuscatedField, srgField);
recursiveExtendFieldMappings(deobfuscatedField, srgField);
} else if (srgScanner.hasNext("MD:")) {
srgScanner.next();
String obfuscatedName = srgScanner.next();
String obfuscatedTypeInfo = srgScanner.next();
String seargeName = srgScanner.next();
String deobfuscatedTypeInfo = srgScanner.next();
String obfuscatedClassName = obfuscatedName.substring(0, obfuscatedName.lastIndexOf('/')).replace('/', '.');
obfuscatedName = obfuscatedName.substring(obfuscatedName.lastIndexOf('/') + 1);
String deobfuscatedClassName = seargeName.substring(0, seargeName.lastIndexOf('/')).replace('/', '.');
seargeName = seargeName.substring(seargeName.lastIndexOf('/') + 1);
String deobfuscatedName = methodSeargeMappings.get(seargeName);
if (deobfuscatedName == null) {
deobfuscatedName = seargeName;
}
MethodDescription deobfuscatedMethodDescription = new MethodDescription(deobfuscatedClassName, deobfuscatedName, deobfuscatedTypeInfo);
MethodDescription obfuscatedMethodDescription = new MethodDescription(obfuscatedClassName, obfuscatedName, obfuscatedTypeInfo);
MethodDescription srgMethodDescription = new MethodDescription(deobfuscatedClassName, seargeName, deobfuscatedTypeInfo);
methodMappings.put(deobfuscatedMethodDescription, obfuscatedMethodDescription);
methodSrgMappings.put(deobfuscatedMethodDescription, srgMethodDescription);
parameterlessMethodMappings.put(deobfuscatedMethodDescription.getShortName(), obfuscatedMethodDescription);
parameterlessSrgMethodMappings.put(deobfuscatedMethodDescription.getShortName(), srgMethodDescription);
} else {
srgScanner.nextLine();
}
}
mappings.close();
}
private void recursiveExtendFieldMappings(FieldDescription deobfuscatedField, FieldDescription srgField) {
List<String> extendedBy = extendsMap.get(deobfuscatedField.className);
if (extendedBy == null) {
return;
}
for (String className : extendedBy) {
FieldDescription newDeobf = new FieldDescription(className, deobfuscatedField.name);
FieldDescription newSrgField = new FieldDescription(className, srgField.name);
fieldSrgMappings.put(newDeobf, newSrgField);
recursiveExtendFieldMappings(newDeobf, newSrgField);
}
}
private static void loadCsv(InputStream mappingsCsv, Map<String, String> seargeMappings) throws IOException {
Scanner in = new Scanner(mappingsCsv);
try {
in.useDelimiter(",");
while (in.hasNextLine()) {
String seargeName = in.next();
String name = in.next();
String side = in.next();
in.nextLine();
if ("2".equals(side)) { // 2 = joined 'side'.
seargeMappings.put(seargeName, name);
}
}
} finally {
in.close();
}
mappingsCsv.close();
}
}