Skip to content

Commit f12b740

Browse files
committed
Start implementing the ASM helpers
1 parent 5f740d9 commit f12b740

File tree

7 files changed

+204
-0
lines changed

7 files changed

+204
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package com.falsepattern.lib.asm;
2+
3+
import com.falsepattern.lib.internal.CoreLoadingPlugin;
4+
import com.falsepattern.lib.mapping.MappingManager;
5+
import com.falsepattern.lib.mapping.types.MappingType;
6+
import com.falsepattern.lib.mapping.types.NameType;
7+
import com.falsepattern.lib.mapping.types.UniversalClass;
8+
import com.falsepattern.lib.mapping.types.UniversalField;
9+
import com.falsepattern.lib.mapping.types.UniversalMethod;
10+
import lombok.SneakyThrows;
11+
import lombok.val;
12+
import org.objectweb.asm.tree.ClassNode;
13+
import org.objectweb.asm.tree.FieldNode;
14+
import org.objectweb.asm.tree.MethodNode;
15+
16+
import java.util.Arrays;
17+
import java.util.Objects;
18+
19+
public class ASMUtil {
20+
public static FieldNode findFieldStandard(ClassNode cn, String name, boolean optional) {
21+
for (final FieldNode ret : cn.fields) {
22+
if (name.equals(ret.name)) {
23+
return ret;
24+
}
25+
}
26+
if (optional) {
27+
return null;
28+
}
29+
throw new AsmFieldNotFoundException(name);
30+
}
31+
32+
@SneakyThrows
33+
public static FieldNode findFieldFromMCP(ClassNode cn, String fieldName, boolean optional) {
34+
val classMapping = discoverClassMappingType(cn);
35+
if (classMapping == null) {
36+
throw new AsmClassNotFoundException("The class " + cn + " is not from Minecraft, or the mapping manager" +
37+
"doesn't have it, cannot use findFieldFromMCP! Use findFieldStandard instead!");
38+
}
39+
return findFieldStandard(cn,
40+
MappingManager.classForName(NameType.Internal, classMapping, cn.name)
41+
.getField(MappingType.MCP, fieldName)
42+
.getName(classMapping),
43+
optional);
44+
}
45+
46+
public static FieldNode findFieldFromUniversal(ClassNode cn, UniversalField field, boolean optional) {
47+
String[] possibilities = CoreLoadingPlugin.isObfuscated() ?
48+
new String[]{field.getName(MappingType.SRG), field.getName(MappingType.Notch)} :
49+
new String[] {field.getName(MappingType.MCP)};
50+
for (final FieldNode ret : cn.fields) {
51+
if (anyEquals(ret.name, possibilities)) {
52+
return ret;
53+
}
54+
}
55+
if (optional) {
56+
return null;
57+
}
58+
throw new AsmFieldNotFoundException(possibilities.length == 1 ? possibilities[0] : Arrays.toString(possibilities));
59+
}
60+
61+
public static MethodNode findMethodStandard(ClassNode cn, String name, String descriptor, boolean optional) {
62+
for (final MethodNode ret : cn.methods) {
63+
if (name.equals(ret.name) && descriptor.equals(ret.desc)) {
64+
return ret;
65+
}
66+
}
67+
if (optional) {
68+
return null;
69+
}
70+
throw new AsmMethodNotFoundException(name);
71+
}
72+
73+
@SneakyThrows
74+
public static MethodNode findMethodFromMCP(ClassNode cn, String mcpName, String mcpDescriptor, boolean optional) {
75+
val classMapping = discoverClassMappingType(cn);
76+
if (classMapping == null) {
77+
throw new AsmClassNotFoundException("The class " + cn + " is not from Minecraft, or the mapping manager" +
78+
"doesn't have it, cannot use findMethodFromMCP! Use findFieldStandard instead!");
79+
}
80+
val method = MappingManager.classForName(NameType.Internal, classMapping, cn.name)
81+
.getMethod(MappingType.MCP, mcpName, mcpDescriptor);
82+
return findMethodStandard(cn, method.getName(classMapping), method.getDescriptor(classMapping), optional);
83+
}
84+
85+
public static MethodNode findMethodFromUniversal(ClassNode cn, UniversalMethod method, boolean optional) {
86+
String[] possibleNames = CoreLoadingPlugin.isObfuscated() ?
87+
new String[]{method.getName(MappingType.SRG), method.getName(MappingType.Notch)} :
88+
new String[] {method.getName(MappingType.MCP)};
89+
String[] possibleDescriptors = CoreLoadingPlugin.isObfuscated() ?
90+
new String[]{method.getDescriptor(MappingType.SRG), method.getDescriptor(MappingType.Notch)} :
91+
new String[] {method.getDescriptor(MappingType.MCP)};
92+
for (final MethodNode ret : cn.methods) {
93+
if (anyEquals(ret.name, possibleNames) && anyEquals(ret.desc, possibleDescriptors)) {
94+
return ret;
95+
}
96+
}
97+
if (optional) {
98+
return null;
99+
}
100+
throw new AsmFieldNotFoundException(possibleDescriptors.length == 1 ? possibleDescriptors[0] : Arrays.toString(possibleDescriptors));
101+
}
102+
103+
public static MappingType discoverClassMappingType(ClassNode cn) {
104+
if (CoreLoadingPlugin.isObfuscated()) {
105+
if (MappingManager.containsClass(NameType.Internal, MappingType.MCP, cn.name)) {
106+
return MappingType.MCP;
107+
}
108+
} else if (MappingManager.containsClass(NameType.Internal, MappingType.SRG, cn.name)) {
109+
return MappingType.SRG;
110+
} else if (MappingManager.containsClass(NameType.Internal, MappingType.Notch, cn.name)) {
111+
return MappingType.Notch;
112+
}
113+
return null;
114+
}
115+
116+
public static UniversalClass toUniversalClass(ClassNode cn) {
117+
if (CoreLoadingPlugin.isObfuscated()) {
118+
try {
119+
return MappingManager.classForName(NameType.Internal, MappingType.MCP, cn.name);
120+
} catch (ClassNotFoundException e) {
121+
throw new AsmClassNotFoundException(cn.name);
122+
}
123+
} else {
124+
try {
125+
return MappingManager.classForName(NameType.Internal, MappingType.SRG, cn.name);
126+
} catch (ClassNotFoundException e) {
127+
try {
128+
return MappingManager.classForName(NameType.Internal, MappingType.Notch, cn.name);
129+
} catch (ClassNotFoundException ex) {
130+
throw new AsmClassNotFoundException(cn.name);
131+
}
132+
}
133+
}
134+
}
135+
136+
public static boolean anyEquals(String str, String... options) {
137+
for (val option: options) {
138+
if (Objects.equals(str, option)) {
139+
return true;
140+
}
141+
}
142+
return false;
143+
}
144+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.falsepattern.lib.asm;
2+
3+
public class AsmClassNotFoundException extends AsmTransformException {
4+
public AsmClassNotFoundException(final String clazz) {
5+
super("can't find class " + clazz);
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.falsepattern.lib.asm;
2+
3+
public class AsmFieldNotFoundException extends AsmTransformException {
4+
public AsmFieldNotFoundException(final String field) {
5+
super("can't find field " + field);
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.falsepattern.lib.asm;
2+
3+
public class AsmMethodNotFoundException extends AsmTransformException {
4+
public AsmMethodNotFoundException(final String method) {
5+
super("can't find method " + method);
6+
}
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.falsepattern.lib.asm;
2+
3+
public class AsmTransformException extends RuntimeException {
4+
public AsmTransformException(final String message) {
5+
super(message);
6+
}
7+
8+
public AsmTransformException(final Throwable cause) {
9+
super(cause);
10+
}
11+
12+
public AsmTransformException(final String message, final Throwable cause) {
13+
super(message, cause);
14+
}
15+
}

src/main/java/com/falsepattern/lib/mapping/MappingManager.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ public static UniversalClass classForName(NameType nameType, MappingType mapping
7474
}
7575
}
7676

77+
public static boolean containsClass(NameType nameType, MappingType mappingType, String className) {
78+
switch (nameType) {
79+
case Internal:
80+
return internalLookup.containsKey(mappingType, className);
81+
case Regular:
82+
return regularLookup.containsKey(mappingType, className);
83+
default:
84+
throw new IllegalArgumentException("Invalid enum value " + nameType);
85+
}
86+
}
87+
7788
public static UniversalField getField(FieldInsnNode instruction) throws ClassNotFoundException,
7889
NoSuchFieldException {
7990
if (!CoreLoadingPlugin.isObfuscated()) {

src/main/java/com/falsepattern/lib/mapping/storage/Lookup.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public boolean contains(T value) {
2424
return values.contains(value);
2525
}
2626

27+
public boolean containsKey(MappingType mappingType, String key) {
28+
switch (mappingType) {
29+
case Notch:
30+
return notch.containsKey(key);
31+
case SRG:
32+
return srg.containsKey(key);
33+
case MCP:
34+
return mcp.containsKey(key);
35+
default:
36+
throw new IllegalArgumentException("Invalid enum value " + mappingType);
37+
}
38+
}
39+
2740
public void unwrap(@NonNull MappedString mappedString, @NonNull T value) {
2841
if (contains(value)) {
2942
//Collision avoidance.

0 commit comments

Comments
 (0)