-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMappingUtil.java
More file actions
384 lines (325 loc) · 18.1 KB
/
MappingUtil.java
File metadata and controls
384 lines (325 loc) · 18.1 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package org.parchmentmc.compass.util;
import net.minecraftforge.srgutils.IMappingBuilder;
import net.minecraftforge.srgutils.IMappingFile;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.parchmentmc.compass.util.download.ObfuscationMapsDownloader;
import org.parchmentmc.feather.mapping.MappingDataBuilder;
import org.parchmentmc.feather.mapping.MappingDataContainer;
import org.parchmentmc.feather.metadata.ClassMetadata;
import org.parchmentmc.feather.metadata.FieldMetadata;
import org.parchmentmc.feather.metadata.MethodMetadata;
import org.parchmentmc.feather.metadata.SourceMetadata;
import org.parchmentmc.feather.named.Named;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import static org.parchmentmc.feather.mapping.MappingDataBuilder.MutableClassData;
import static org.parchmentmc.feather.mapping.MappingDataBuilder.MutableFieldData;
import static org.parchmentmc.feather.mapping.MappingDataBuilder.MutableMethodData;
import static org.parchmentmc.feather.mapping.MappingDataBuilder.MutablePackageData;
import static org.parchmentmc.feather.mapping.MappingDataBuilder.MutableParameterData;
import static org.parchmentmc.feather.mapping.MappingDataBuilder.copyOf;
public class MappingUtil {
public static IMappingFile loadAndEnsureSuperset(Path client, Path server) {
IMappingFile clientMap, serverMap;
try (InputStream clientInput = Files.newInputStream(client);
InputStream serverInput = Files.newInputStream(server)) {
clientMap = IMappingFile.load(clientInput);
serverMap = IMappingFile.load(serverInput);
} catch (IOException e) {
throw new RuntimeException("Exception while loading client and server obfuscation maps", e);
}
boolean superset = isSuperset(serverMap, clientMap);
if (!superset) {
throw new RuntimeException("'wot'; Client obfuscation map is not a superset of server obfuscation map");
}
return clientMap;
}
/**
* Returns {@code true} if the {@code superset} mapping file is a superset of the {@code set} mapping file.
*
* @param set The target mapping file
* @param superset The superset mapping file
* @return {@code true} if {@code superset} is a superset of {@code set}
*/
static boolean isSuperset(IMappingFile set, IMappingFile superset) {
List<String> superSetPackages = superset.getPackages().stream().map(Object::toString).collect(Collectors.toList());
// Check if all packages are present in superset
if (!set.getPackages().stream().map(Object::toString).allMatch(superSetPackages::contains)) return false;
List<String> superSetClasses = superset.getClasses().stream().map(IMappingFile.INode::getOriginal).collect(Collectors.toList());
for (IMappingFile.IClass cls : set.getClasses()) {
// Check that class is present in superset
if (!superSetClasses.contains(cls.getOriginal())) return false;
IMappingFile.IClass superCls = superset.getClass(cls.getOriginal());
List<String> superClsFields = superCls.getFields().stream().map(Object::toString).collect(Collectors.toList());
// Check if all fields are present in superclass
if (!cls.getFields().stream().map(Object::toString).allMatch(superClsFields::contains)) return false;
for (IMappingFile.IMethod method : cls.getMethods()) {
// Check that method is present in superclass
IMappingFile.IMethod superMethod = superCls.getMethod(method.getOriginal(), method.getDescriptor());
if (superMethod == null) return false;
}
}
return true;
}
public static MappingDataBuilder createBuilderFrom(IMappingFile mappingFile, boolean reversed) {
MappingDataBuilder builder = new MappingDataBuilder();
// Copy packages
mappingFile.getPackages().forEach(pkg -> builder.createPackage(reversed ? pkg.getMapped() : pkg.getOriginal()));
// Copy classes
mappingFile.getClasses().forEach(cls -> {
MutableClassData classBuilder = builder.createClass(reversed ? cls.getMapped() : cls.getOriginal());
// Copy fields of classes
cls.getFields().forEach(field ->
classBuilder.createField(reversed ? field.getMapped() : field.getOriginal(),
reversed ? field.getMappedDescriptor() : field.getDescriptor()));
// Copy methods of classes
cls.getMethods().forEach(method -> {
MutableMethodData methodBuilder = classBuilder.createMethod(
reversed ? method.getMapped() : method.getOriginal(),
reversed ? method.getMappedDescriptor() : method.getDescriptor());
// Copy parameters of methods
method.getParameters().forEach(param ->
methodBuilder.createParameter((byte) param.getIndex())
.setName(reversed ? param.getMapped() : param.getOriginal()));
});
});
return builder;
}
public static MappingDataBuilder constructPackageData(MappingDataContainer container) {
if (container instanceof MappingDataBuilder) {
return constructPackageData((MappingDataBuilder) container);
}
return constructPackageData(copyOf(container));
}
// Creates packages based on the classes
public static MappingDataBuilder constructPackageData(MappingDataBuilder builder) {
for (MutableClassData cls : builder.getClasses()) {
int lastIndex = cls.getName().lastIndexOf('/');
if (lastIndex != -1) { // has a package
String pkgName = cls.getName().substring(0, lastIndex);
builder.getOrCreatePackage(pkgName);
}
}
return builder;
}
/**
* Constructs packages for the given mapping file based on the class names, and returns a new mapping file with the
* constructed packages.
*
* <p>Packages constructed by this method (whether pre-existing or not) will have the {@code constructed} metadata key with
* a value of {@code "true"}.</p>
*
* @param mapping The mapping file to construct pages
* @return a copy of the mapping file with the constructed packages
*/
public static IMappingFile constructPackageData(IMappingFile mapping) {
IMappingBuilder builder = IMappingBuilder.create("left", "right");
for (IMappingFile.IPackage pkg : mapping.getPackages()) {
IMappingBuilder.IPackage newPkg = builder.addPackage(pkg.getOriginal(), pkg.getMapped());
pkg.getMetadata().forEach(newPkg::meta);
}
for (IMappingFile.IClass cls : mapping.getClasses()) {
int lastIndex = cls.getOriginal().lastIndexOf('/');
if (lastIndex != -1) {
String pkgName = cls.getOriginal().substring(0, lastIndex);
builder.addPackage(pkgName, pkgName)
.meta("constructed", "true");
}
IMappingBuilder.IClass newCls = builder.addClass(cls.getOriginal(), cls.getMapped());
cls.getMetadata().forEach(newCls::meta);
for (IMappingFile.IField field : cls.getFields()) {
IMappingBuilder.IField newField = newCls.field(field.getOriginal(), field.getMapped())
.descriptor(field.getDescriptor());
field.getMetadata().forEach(newField::meta);
}
for (IMappingFile.IMethod method : cls.getMethods()) {
IMappingBuilder.IMethod newMethod = newCls.method(method.getDescriptor(), method.getOriginal(), method.getMapped());
method.getMetadata().forEach(newMethod::meta);
for (IMappingFile.IParameter param : method.getParameters()) {
IMappingBuilder.IParameter newParam = newMethod.parameter(param.getIndex(), param.getOriginal(), param.getMapped());
param.getMetadata().forEach(newParam::meta);
}
}
}
return builder.build().getMap("left", "right");
}
// Mapping should be names from data -> target names
public static MappingDataContainer remapData(MappingDataContainer data, IMappingFile mapping) {
MappingDataBuilder builder = new MappingDataBuilder();
data.getPackages().forEach(pkg -> builder.createPackage(mapping.remapPackage(pkg.getName())).addJavadoc(pkg.getJavadoc()));
data.getClasses().forEach(cls -> {
IMappingFile.IClass mappedClass = mapping.getClass(cls.getName());
String clsName = cls.getName();
if (mappedClass != null) {
clsName = mappedClass.getMapped();
}
MutableClassData classData = builder.createClass(clsName).addJavadoc(cls.getJavadoc());
cls.getFields().forEach(field -> {
String fieldName = field.getName();
String fieldDescriptor = mapping.remapDescriptor(field.getDescriptor());
if (mappedClass != null) {
IMappingFile.IField mappedField = mappedClass.getField(field.getName());
if (mappedField != null) {
fieldName = mappedField.getMapped();
if (mappedField.getMappedDescriptor() != null) {
fieldDescriptor = mappedField.getMappedDescriptor();
}
}
}
classData.createField(fieldName, fieldDescriptor).addJavadoc(field.getJavadoc());
});
cls.getMethods().forEach(method -> {
String methodName = method.getName();
String methodDescriptor = method.getDescriptor();
if (mappedClass != null) {
IMappingFile.IMethod mappedMethod = mappedClass.getMethod(method.getName(), method.getDescriptor());
if (mappedMethod != null) {
methodName = mappedMethod.getMapped();
methodDescriptor = mappedMethod.getMappedDescriptor();
}
}
MutableMethodData methodData = classData.createMethod(methodName, methodDescriptor)
.addJavadoc(method.getJavadoc());
// TODO: determine better logic for handling parameters
method.getParameters().forEach(param -> methodData.createParameter(param.getIndex()).setName(param.getName()).setJavadoc(param.getJavadoc()));
});
});
return builder;
}
public static void copyData(MappingDataContainer base, MappingDataBuilder builder) {
// Copy packages
base.getPackages().forEach(pkg -> builder.createPackage(pkg.getName()).addJavadoc(pkg.getJavadoc()));
// Copy classes
base.getClasses().forEach(cls -> {
MutableClassData classData = builder.createClass(cls.getName()).addJavadoc(cls.getJavadoc());
// Copy fields
cls.getFields().forEach(field -> classData.createField(field.getName(), field.getDescriptor()).addJavadoc(field.getJavadoc()));
// Copy methods
cls.getMethods().forEach(method -> {
MutableMethodData methodData = classData.createMethod(method.getName(), method.getDescriptor()).addJavadoc(method.getJavadoc());
// Copy parameters
method.getParameters().forEach(param -> methodData.createParameter(param.getIndex()).setName(param.getName()).setJavadoc(param.getJavadoc()));
});
});
}
public static MappingDataBuilder loadOfficialData(ObfuscationMapsDownloader obfuscationMapsDownloader) {
IMappingFile obfMap = obfuscationMapsDownloader.getObfuscationMap().get();
return loadOfficialData(obfMap);
}
public static MappingDataBuilder loadOfficialData(IMappingFile obfToMoj) {
return constructPackageData(createBuilderFrom(obfToMoj, false));
}
public static void removeUndocumented(MappingDataBuilder builder) {
builder.getPackages().stream()
.filter(s -> s.getJavadoc().isEmpty())
.map(MutablePackageData::getName)
.collect(Collectors.toSet())
.forEach(builder::removePackage);
//noinspection Convert2MethodRef damn you javac
builder.getClasses().stream()
.peek(cls -> cls.getFields().stream()
.filter(field -> field.getJavadoc().isEmpty())
.map(MutableFieldData::getName)
.collect(Collectors.toSet())
.forEach(field -> cls.removeField(field)))
.peek(cls -> cls.getMethods().stream()
.peek(method -> method.getParameters().stream()
.filter(param -> param.getName() == null && param.getJavadoc() == null)
.map(MutableParameterData::getIndex)
.collect(Collectors.toSet())
.forEach(method::removeParameter))
.filter(method -> method.getJavadoc().isEmpty() && method.getParameters().isEmpty())
.collect(Collectors.toSet())
.forEach(method -> cls.removeMethod(method.getName(), method.getDescriptor())))
.filter(cls -> cls.getJavadoc().isEmpty() && cls.getFields().isEmpty() && cls.getMethods().isEmpty())
.map(MutableClassData::getName)
.collect(Collectors.toSet())
.forEach(builder::removeClass);
}
public static Map<String, ClassMetadata> buildClassMetadataMap(@Nullable SourceMetadata metadata) {
if (metadata == null) return Collections.emptyMap();
final HashMap<String, ClassMetadata> classMetadataMap = new HashMap<>();
final ArrayDeque<ClassMetadata> toTraverse = new ArrayDeque<>(metadata.getClasses());
ClassMetadata current;
while ((current = toTraverse.poll()) != null) {
classMetadataMap.put(current.getName().getMojangName().orElse(""), current);
toTraverse.addAll(current.getInnerClasses());
}
return classMetadataMap;
}
/**
* Returns the matching field metadata from the class metadata according to the name from the naming function.
*
* @param nameFunction the naming function
* @param classMetadata the class metadata, may be {@code null}
* @param fieldName the field name
* @return the matching field metadata, or {@code null} if either the class metadata is {@code null} or there is no
* field metadata in the class which has a matching name
*/
@Nullable
private static FieldMetadata getFieldMetadata(Function<Named, Optional<String>> nameFunction,
@Nullable ClassMetadata classMetadata, String fieldName) {
if (classMetadata == null) return null;
return classMetadata.getFields().stream()
.filter(s -> nameFunction.apply(s.getName()).orElse("").contentEquals(fieldName))
.findFirst().orElse(null);
}
/**
* Returns the matching field metadata from the class metadata according to {@linkplain Named#getMojangName()
* Mojang names}.
*
* @param classMetadata the class metadata, may be {@code null}
* @param fieldName the field name
* @return the matching field metadata, or {@code null} if either the class metadata is {@code null} or there is no
* field metadata in the class which has a matching Mojang name
* @see #getFieldMetadata(Function, ClassMetadata, String)
*/
@Nullable
public static FieldMetadata getFieldMetadata(@Nullable ClassMetadata classMetadata, String fieldName) {
return getFieldMetadata(Named::getMojangName, classMetadata, fieldName);
}
/**
* Returns the matching method metadata from the class metadata according to the names from the naming function.
*
* @param nameFunction the naming function
* @param classMetadata the class metadata, may be {@code null}
* @param methodName the method name
* @param methodDesc the method description
* @return the matching method metadata, or {@code null} if either the class metadata is {@code null} or there is no
* method metadata in the class which has a matching name and descriptor
*/
@Nullable
private static MethodMetadata getMethodMetadata(Function<Named, Optional<String>> nameFunction,
@Nullable ClassMetadata classMetadata, String methodName, String methodDesc) {
if (classMetadata == null) return null;
return classMetadata.getMethods().stream()
.filter(s -> nameFunction.apply(s.getName()).orElse("").contentEquals(methodName)
&& nameFunction.apply(s.getDescriptor()).orElse("").contentEquals(methodDesc))
.findFirst().orElse(null);
}
/**
* Returns the matching method metadata from the class metadata according to {@linkplain Named#getMojangName()
* Mojang names}.
*
* @param classMetadata the class metadata, may be {@code null}
* @param methodName the method name
* @param methodDesc the method description
* @return the matching method metadata, or {@code null} if either the class metadata is {@code null} or there is no
* method metadata in the class which has a matching name and descriptor with Mojang names
* @see #getMethodMetadata(Function, ClassMetadata, String, String)
*/
@Nullable
public static MethodMetadata getMethodMetadata(@Nullable ClassMetadata classMetadata, String methodName, String methodDesc) {
return getMethodMetadata(Named::getMojangName, classMetadata, methodName, methodDesc);
}
}