-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
MCPMappings.java
114 lines (104 loc) · 4.8 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
package me.nallar.tickthreading.mappings;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
public class MCPMappings extends Mappings {
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 Map<MethodDescription, MethodDescription> methodMappings = new HashMap<MethodDescription, MethodDescription>();
private final Map<FieldDescription, FieldDescription> fieldMappings = new HashMap<FieldDescription, FieldDescription>();
private final Map<String, MethodDescription> parameterlessMethodMappings = new HashMap<String, MethodDescription>();
@Override
public MethodDescription map(MethodDescription methodDescription) {
MethodDescription obfuscated = methodMappings.get(methodDescription);
if (obfuscated == null) {
obfuscated = parameterlessMethodMappings.get(methodDescription.getShortName());
}
return obfuscated;
}
@Override
public ClassDescription map(ClassDescription classDescription) {
return classMappings.get(classDescription);
}
@Override
public FieldDescription map(FieldDescription fieldDescription) {
return fieldMappings.get(fieldDescription);
}
public MCPMappings(File mcpDir) throws IOException {
parse(mcpDir);
}
void parse(File mcpDir) throws IOException {
loadCsv(new File(mcpDir, "methods.csv"), methodSeargeMappings);
loadCsv(new File(mcpDir, "fields.csv"), fieldSeargeMappings);
loadSrg(new File(mcpDir, "packaged.srg"));
methodSeargeMappings.clear();
fieldSeargeMappings.clear();
}
private void loadSrg(File mappingsSrg) throws IOException {
Scanner srgScanner = new Scanner(mappingsSrg);
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);
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);
fieldMappings.put(deobfuscatedField, obfuscatedField);
} 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);
methodMappings.put(deobfuscatedMethodDescription, obfuscatedMethodDescription);
parameterlessMethodMappings.put(deobfuscatedMethodDescription.getShortName(), obfuscatedMethodDescription);
} else {
srgScanner.nextLine();
}
}
}
private void loadCsv(File 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 (side.equals("2")) { // 2 = joined 'side'.
seargeMappings.put(seargeName, name);
}
}
} finally {
in.close();
}
}
}