Skip to content

Commit 3438b55

Browse files
committed
working on the reflection stuff some more...
1 parent f6faba4 commit 3438b55

File tree

11 files changed

+439
-131
lines changed

11 files changed

+439
-131
lines changed

src/main/java/com/falsepattern/lib/internal/CoreLoadingPlugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.falsepattern.lib.internal;
22

3+
import lombok.Getter;
34
import lombok.val;
45

56
import cpw.mods.fml.relauncher.IFMLLoadingPlugin;
@@ -15,6 +16,8 @@
1516
@Name(Tags.MODID)
1617
@SortingIndex(500)
1718
public class CoreLoadingPlugin implements IFMLLoadingPlugin {
19+
@Getter
20+
private static boolean obfuscated;
1821
static {
1922
try {
2023
Class.forName("thermos.Thermos");
@@ -46,6 +49,7 @@ public String getSetupClass() {
4649

4750
@Override
4851
public void injectData(Map<String, Object> data) {
52+
obfuscated = (Boolean) data.get("runtimeDeobfuscationEnabled");
4953
}
5054

5155
@Override

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

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package com.falsepattern.lib.reflection;
22

3+
import com.falsepattern.lib.internal.CoreLoadingPlugin;
34
import com.falsepattern.lib.internal.FalsePatternLib;
45
import com.falsepattern.lib.reflection.storage.Lookup;
56
import com.falsepattern.lib.reflection.types.MappingType;
67
import com.falsepattern.lib.reflection.types.NameType;
8+
import com.falsepattern.lib.reflection.types.UniversalClass;
9+
import com.falsepattern.lib.reflection.types.UniversalField;
10+
import com.falsepattern.lib.reflection.types.UniversalMethod;
711
import com.falsepattern.lib.util.ResourceUtil;
8-
import lombok.NonNull;
912
import lombok.SneakyThrows;
1013
import lombok.val;
14+
import org.objectweb.asm.tree.FieldInsnNode;
15+
import org.objectweb.asm.tree.MethodInsnNode;
1116

12-
import java.io.IOException;
1317
import java.util.HashMap;
1418

1519
public class MappingManager {
@@ -37,31 +41,75 @@ private static synchronized void initialize() {
3741
val fieldMappings = ResourceUtil.getResourceStringFromJar("/fields.csv", FalsePatternLib.class).split("\n");
3842
for (int i = 1; i < fieldMappings.length; i++) {
3943
val line = fieldMappings[i].split(",");
40-
val field = new UniversalField(line, stringPool);
4144
val clazz = internalLookup.get(MappingType.Notch, line[0].substring(0, line[0].lastIndexOf('/')));
42-
clazz.addField(field);
45+
new UniversalField(clazz, line, stringPool);
4346
}
4447
}
4548
{
4649
val methodMappings = ResourceUtil.getResourceStringFromJar("/methods.csv", FalsePatternLib.class).split("\n");
4750
for (int i = 1; i < methodMappings.length; i++) {
4851
val line = methodMappings[i].split(",");
49-
val field = new UniversalMethod(line, stringPool);
5052
val clazz = internalLookup.get(MappingType.Notch, line[0].substring(0, line[0].lastIndexOf('/')));
51-
clazz.addMethod(field);
53+
new UniversalMethod(clazz, line, stringPool);
5254
}
5355
}
5456
}
5557

56-
public static UniversalClass classForName(@NonNull NameType nameType, @NonNull MappingType mappingType, String className) {
58+
public static UniversalClass classForName(NameType nameType, MappingType mappingType, String className) throws ClassNotFoundException {
5759
initialize();
58-
switch (nameType) {
59-
case Internal:
60-
return internalLookup.get(mappingType, className);
61-
case Regular:
62-
return regularLookup.get(mappingType, className);
63-
default:
64-
throw new IllegalArgumentException("Invalid enum value " + nameType);
60+
try {
61+
switch (nameType) {
62+
case Internal:
63+
return internalLookup.get(mappingType, className);
64+
case Regular:
65+
return regularLookup.get(mappingType, className);
66+
default:
67+
throw new IllegalArgumentException("Invalid enum value " + nameType);
68+
}
69+
} catch (Lookup.LookupException e) {
70+
throw new ClassNotFoundException("Could not find class \"" + className + "\" with " + nameType.name().toLowerCase() + " name in the " + mappingType.name() + " mapping.");
71+
}
72+
}
73+
74+
public static UniversalField getField(FieldInsnNode instruction) throws ClassNotFoundException,
75+
NoSuchFieldException {
76+
if (!CoreLoadingPlugin.isObfuscated()) {
77+
try {
78+
return classForName(NameType.Internal, MappingType.MCP, instruction.owner).getField(MappingType.MCP, instruction.name);
79+
} catch (ClassNotFoundException e) {
80+
throw new ClassNotFoundException("Could not find the class " + instruction.owner + " in the MCP mappings. Are you sure it's a Minecraft class? (we're in dev, cannot use SRG or Notch here).");
81+
}
82+
} else {
83+
try {
84+
return classForName(NameType.Internal, MappingType.SRG, instruction.owner).getField(MappingType.SRG, instruction.name);
85+
} catch (ClassNotFoundException e) {
86+
try {
87+
return classForName(NameType.Internal, MappingType.Notch, instruction.owner).getField(MappingType.Notch, instruction.name);
88+
} catch (ClassNotFoundException ex) {
89+
throw new ClassNotFoundException("Could not find the class " + instruction.owner + " neither in the SRG nor in the Notch mappings. Are you sure it's a Minecraft class? (we're in obf, cannot use MCP here)");
90+
}
91+
}
92+
}
93+
}
94+
95+
public static UniversalMethod getMethod(MethodInsnNode instruction)
96+
throws ClassNotFoundException, NoSuchMethodException {
97+
if (!CoreLoadingPlugin.isObfuscated()) {
98+
try {
99+
return classForName(NameType.Internal, MappingType.MCP, instruction.owner).getMethod(MappingType.MCP, instruction.name, instruction.desc);
100+
} catch (ClassNotFoundException e) {
101+
throw new ClassNotFoundException("Could not find the class " + instruction.owner + " in the MCP mappings. Are you sure it's a Minecraft class? (we're in dev, cannot use SRG or Notch here).");
102+
}
103+
} else {
104+
try {
105+
return classForName(NameType.Internal, MappingType.SRG, instruction.owner).getMethod(MappingType.SRG, instruction.name, instruction.desc);
106+
} catch (ClassNotFoundException e) {
107+
try {
108+
return classForName(NameType.Internal, MappingType.Notch, instruction.owner).getMethod(MappingType.Notch, instruction.name, instruction.desc);
109+
} catch (ClassNotFoundException ex) {
110+
throw new ClassNotFoundException("Could not find the class " + instruction.owner + " neither in the SRG nor in the Notch mappings. Are you sure it's a Minecraft class? (we're in obf, cannot use MCP here)");
111+
}
112+
}
65113
}
66114
}
67115
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.falsepattern.lib.reflection;
2+
3+
import lombok.SneakyThrows;
4+
5+
import java.lang.reflect.Field;
6+
import java.lang.reflect.Method;
7+
import java.lang.reflect.Modifier;
8+
9+
public class ReflectionUtil {
10+
private static final Field f_modifiers;
11+
static {
12+
try {
13+
f_modifiers = Field.class.getDeclaredField("modifiers");
14+
} catch (NoSuchFieldException e) {
15+
throw new RuntimeException(e);
16+
}
17+
f_modifiers.setAccessible(true);
18+
}
19+
20+
@SneakyThrows
21+
public static void jailBreak(Field field) {
22+
field.setAccessible(true);
23+
f_modifiers.set(field, field.getModifiers() & ~Modifier.FINAL);
24+
}
25+
26+
public static void jailBreak(Method method) {
27+
method.setAccessible(true);
28+
}
29+
}

src/main/java/com/falsepattern/lib/reflection/UniversalClass.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/main/java/com/falsepattern/lib/reflection/UniversalField.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/main/java/com/falsepattern/lib/reflection/UniversalMethod.java

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,77 @@
11
package com.falsepattern.lib.reflection.storage;
22

3-
import com.falsepattern.lib.reflection.MappedString;
43
import com.falsepattern.lib.reflection.types.MappingType;
54
import lombok.EqualsAndHashCode;
65
import lombok.NonNull;
76
import lombok.experimental.Accessors;
7+
import lombok.val;
88

99
import java.util.HashMap;
10+
import java.util.HashSet;
11+
import java.util.Iterator;
1012
import java.util.Map;
13+
import java.util.Set;
1114

1215
@Accessors(fluent = true)
1316
@EqualsAndHashCode
1417
public class Lookup<T> {
18+
private final Set<T> values = new HashSet<>();
1519
private final Map<String, T> notch = new HashMap<>();
1620
private final Map<String, T> srg = new HashMap<>();
1721
private final Map<String, T> mcp = new HashMap<>();
1822

19-
public void unwrap(MappedString mappedString, T value) {
23+
public boolean contains(T value) {
24+
return values.contains(value);
25+
}
26+
27+
public void unwrap(@NonNull MappedString mappedString, @NonNull T value) {
28+
if (contains(value)) {
29+
//Collision avoidance.
30+
values.remove(value);
31+
removeFirst(notch.entrySet().iterator(), value);
32+
removeFirst(srg.entrySet().iterator(), value);
33+
removeFirst(mcp.entrySet().iterator(), value);
34+
}
35+
values.add(value);
2036
notch.put(mappedString.notch, value);
2137
srg.put(mappedString.srg, value);
2238
mcp.put(mappedString.mcp, value);
2339
}
2440

25-
public T get(@NonNull MappingType mappingType, @NonNull String key) {
41+
public T get(MappingType mappingType, String key) throws LookupException {
42+
T result;
2643
switch (mappingType) {
2744
case Notch:
28-
return notch.get(key);
45+
result = notch.get(key);
46+
break;
2947
case SRG:
30-
return srg.get(key);
48+
result = srg.get(key);
49+
break;
3150
case MCP:
32-
return mcp.get(key);
51+
result = mcp.get(key);
52+
break;
3353
default:
3454
throw new IllegalArgumentException("Invalid enum value " + mappingType);
3555
}
56+
if (result == null) {
57+
throw new LookupException("No such key " + key + " in " + mappingType);
58+
}
59+
return result;
60+
}
61+
62+
private static <T> void removeFirst(Iterator<Map.Entry<String, T>> iterator, T value) {
63+
while (iterator.hasNext()) {
64+
val entry = iterator.next();
65+
if (entry.getValue().equals(value)) {
66+
iterator.remove();
67+
return;
68+
}
69+
}
70+
}
71+
72+
public static class LookupException extends Exception {
73+
public LookupException(String message) {
74+
super(message);
75+
}
3676
}
3777
}

src/main/java/com/falsepattern/lib/reflection/MappedString.java renamed to src/main/java/com/falsepattern/lib/reflection/storage/MappedString.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package com.falsepattern.lib.reflection;
1+
package com.falsepattern.lib.reflection.storage;
22

33
import com.falsepattern.lib.reflection.types.MappingType;
4+
import lombok.AccessLevel;
45
import lombok.EqualsAndHashCode;
56
import lombok.Getter;
6-
import lombok.NonNull;
7+
import lombok.RequiredArgsConstructor;
78
import lombok.ToString;
89
import lombok.experimental.Accessors;
910

@@ -14,6 +15,7 @@
1415
@ToString
1516
@EqualsAndHashCode
1617
@Getter
18+
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
1719
public class MappedString {
1820
public final String notch;
1921
public final String srg;
@@ -24,7 +26,13 @@ public MappedString(String[] source, int offset, int stride, Function<String, St
2426
mcp = stringPool.computeIfAbsent(remapper.apply(source[offset + stride * 2]), (str) -> str);
2527
}
2628

27-
public String get(@NonNull MappingType type) {
29+
public static MappedString fuse(MappedString a, MappedString b, String delimiter, Map<String, String> stringPool) {
30+
return new MappedString(stringPool.computeIfAbsent(a.notch + delimiter + b.notch, (str) -> str),
31+
stringPool.computeIfAbsent(a.srg + delimiter + b.srg , (str) -> str),
32+
stringPool.computeIfAbsent(a.mcp + delimiter + b.mcp , (str) -> str));
33+
}
34+
35+
public String get(MappingType type) {
2836
switch (type) {
2937
case Notch:
3038
return notch;

0 commit comments

Comments
 (0)