Skip to content

Commit a8d7d71

Browse files
committed
Add method to map descriptors and fix method mapping on partial descriptors
1 parent b9c4b02 commit a8d7d71

2 files changed

Lines changed: 97 additions & 18 deletions

File tree

src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/MappingUtils.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,64 @@
55
import org.jetbrains.annotations.Nullable;
66

77
public interface MappingUtils {
8+
/**
9+
*
10+
* @param className original class name
11+
* @return remapped class name
12+
*/
813
static String mapClass(String className) {
914
return MappingsUtilsImpl.mapClass(className);
1015
}
1116

17+
/**
18+
*
19+
* @param className remapped class name
20+
* @return original class name
21+
*/
1222
static String unmapClass(String className) {
1323
return MappingsUtilsImpl.unmapClass(className);
1424
}
1525

26+
/**
27+
*
28+
* @param className original class name
29+
* @param fieldName
30+
* @param fieldDesc
31+
* @return
32+
*/
1633
static MappingUtils.ClassMember mapField(String className, String fieldName, @Nullable String fieldDesc) {
1734
return MappingsUtilsImpl.mapField(className, fieldName, fieldDesc);
1835
}
1936

37+
/**
38+
*
39+
* @param className remapped class name
40+
* @param fieldName
41+
* @param fieldDesc
42+
* @return
43+
*/
2044
static MappingUtils.ClassMember mapFieldFromRemappedClass(String className, String fieldName, @Nullable String fieldDesc) {
2145
return MappingsUtilsImpl.mapFieldFromRemappedClass(className, fieldName, fieldDesc);
2246
}
2347

48+
/**
49+
*
50+
* @param className original class name
51+
* @param methodName
52+
* @param methodDesc
53+
* @return
54+
*/
2455
static MappingUtils.ClassMember mapMethod(String className, String methodName, String methodDesc) {
2556
return MappingsUtilsImpl.mapMethod(className, methodName, methodDesc);
2657
}
2758

59+
/**
60+
*
61+
* @param className remapped class name
62+
* @param methodName
63+
* @param methodDesc
64+
* @return
65+
*/
2866
static MappingUtils.ClassMember mapMethodFromRemappedClass(String className, String methodName, String methodDesc) {
2967
return MappingsUtilsImpl.mapMethodFromRemappedClass(className, methodName, methodDesc);
3068
}
@@ -37,6 +75,15 @@ static MappingUtils.ClassMember mapMethod(Class<?> owner, String methodName, Cla
3775
return MappingsUtilsImpl.mapMethod(owner, methodName, parameterTypes);
3876
}
3977

78+
/**
79+
*
80+
* @param desc original descriptor
81+
* @return remapped descriptor
82+
*/
83+
static String mapDescriptor(String desc) {
84+
return MappingsUtilsImpl.mapDescriptor(desc);
85+
}
86+
4087
class ClassMember {
4188
public final @NotNull String name;
4289
public final @Nullable String desc;

src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/MappingsUtilsImpl.java

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -233,25 +233,9 @@ public static void completeMappingsFromTr(TrEnvironment trEnvironment) {
233233
}
234234
}
235235

236-
int propagated = 0;
237-
238-
for (Map.Entry<ExtendedClassMember, List<String>> entry : classMembers.entrySet()) {
239-
List<String> toAdd = new ArrayList<>(entry.getValue());
240-
241-
while (!toAdd.isEmpty()) {
242-
TrClass trClass = trEnvironment.getClass(toAdd.remove(0));
243-
if (trClass == null) continue;
244-
245-
List<String> children = trClass.getChildren().stream().map(TrClass::getName).collect(Collectors.toList());
236+
gatherChildClassCandidates(trEnvironment, classMembers);
246237

247-
for (String child : children) {
248-
if (!entry.getValue().contains(child)) {
249-
toAdd.add(child);
250-
entry.getValue().add(child);
251-
}
252-
}
253-
}
254-
}
238+
int propagated = 0;
255239

256240
for (Map.Entry<ExtendedClassMember, List<String>> entry : classMembers.entrySet()) {
257241
ExtendedClassMember member = entry.getKey();
@@ -294,6 +278,26 @@ public static void completeMappingsFromTr(TrEnvironment trEnvironment) {
294278
System.out.println("Propagated: " + propagated + " methods");
295279
}
296280

281+
private static void gatherChildClassCandidates(TrEnvironment trEnvironment, Map<ExtendedClassMember, List<String>> classMembers) {
282+
for (Map.Entry<ExtendedClassMember, List<String>> entry : classMembers.entrySet()) {
283+
List<String> toAdd = new ArrayList<>(entry.getValue());
284+
285+
while (!toAdd.isEmpty()) {
286+
TrClass trClass = trEnvironment.getClass(toAdd.remove(0));
287+
if (trClass == null) continue;
288+
289+
List<String> children = trClass.getChildren().stream().map(TrClass::getName).collect(Collectors.toList());
290+
291+
for (String child : children) {
292+
if (!entry.getValue().contains(child)) {
293+
toAdd.add(child);
294+
entry.getValue().add(child);
295+
}
296+
}
297+
}
298+
}
299+
}
300+
297301
static class ExtendedClassMember extends MappingUtils.ClassMember {
298302
public final String owner;
299303
public ExtendedClassMember(String name, @Nullable String desc, String owner) {
@@ -342,6 +346,11 @@ public static MappingUtils.ClassMember mapMethod(String className, String method
342346

343347
MappingTree.MethodMapping methodMapping = FULL_MAPPINGS.getMethod(className, methodName, methodDesc, srcNamespace);
344348

349+
if (methodMapping == null) {
350+
MappingTree.ClassMapping classMapping = FULL_MAPPINGS.getClass(className, srcNamespace);
351+
if (classMapping != null) methodMapping = mapMethodWithPartialDesc(classMapping, methodName, methodDesc, srcNamespace);
352+
}
353+
345354
return mapMember(methodName, methodDesc, targetNamespace, methodMapping);
346355
}
347356

@@ -353,9 +362,25 @@ public static MappingUtils.ClassMember mapMethodFromRemappedClass(String classNa
353362
if (classMapping == null) return new MappingUtils.ClassMember(methodName, methodDesc);
354363

355364
MappingTree.MethodMapping methodMapping = classMapping.getMethod(methodName, methodDesc, srcNamespace);
365+
366+
if (methodMapping == null) methodMapping = mapMethodWithPartialDesc(classMapping, methodName, methodDesc, srcNamespace);
367+
356368
return mapMember(methodName, methodDesc, targetNamespace, methodMapping);
357369
}
358370

371+
private static MappingTree.MethodMapping mapMethodWithPartialDesc(MappingTree.ClassMapping classMapping, String methodName, String methodDesc, int namespace) {
372+
for (MappingTree.MethodMapping methodMapping : classMapping.getMethods()) {
373+
String name = methodMapping.getName(namespace);
374+
String desc = methodMapping.getDesc(namespace);
375+
376+
if (name != null && name.equals(methodName) && desc != null && desc.startsWith(methodDesc)) {
377+
return methodMapping;
378+
}
379+
}
380+
381+
return null;
382+
}
383+
359384
@NotNull
360385
private static MappingUtils.ClassMember mapMember(String memberName, @Nullable String memberDesc, int targetNamespace, MappingTree.MemberMapping memberMapping) {
361386
if (memberMapping == null) return new MappingUtils.ClassMember(memberName, memberDesc);
@@ -429,4 +454,11 @@ private static String classTypeToDescriptor(Class<?>[] classTypes) {
429454

430455
return desc + ")";
431456
}
457+
458+
public static String mapDescriptor(String desc) {
459+
int srcNamespace = FULL_MAPPINGS.getNamespaceId("official");
460+
int targetNamespace = FULL_MAPPINGS.getNamespaceId(getTargetNamespace());
461+
462+
return FULL_MAPPINGS.mapDesc(desc, srcNamespace, targetNamespace);
463+
}
432464
}

0 commit comments

Comments
 (0)