|
1 | 1 | package com.falsepattern.lib.reflection; |
2 | 2 |
|
| 3 | +import com.falsepattern.lib.internal.CoreLoadingPlugin; |
3 | 4 | import com.falsepattern.lib.internal.FalsePatternLib; |
4 | 5 | import com.falsepattern.lib.reflection.storage.Lookup; |
5 | 6 | import com.falsepattern.lib.reflection.types.MappingType; |
6 | 7 | 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; |
7 | 11 | import com.falsepattern.lib.util.ResourceUtil; |
8 | | -import lombok.NonNull; |
9 | 12 | import lombok.SneakyThrows; |
10 | 13 | import lombok.val; |
| 14 | +import org.objectweb.asm.tree.FieldInsnNode; |
| 15 | +import org.objectweb.asm.tree.MethodInsnNode; |
11 | 16 |
|
12 | | -import java.io.IOException; |
13 | 17 | import java.util.HashMap; |
14 | 18 |
|
15 | 19 | public class MappingManager { |
@@ -37,31 +41,75 @@ private static synchronized void initialize() { |
37 | 41 | val fieldMappings = ResourceUtil.getResourceStringFromJar("/fields.csv", FalsePatternLib.class).split("\n"); |
38 | 42 | for (int i = 1; i < fieldMappings.length; i++) { |
39 | 43 | val line = fieldMappings[i].split(","); |
40 | | - val field = new UniversalField(line, stringPool); |
41 | 44 | val clazz = internalLookup.get(MappingType.Notch, line[0].substring(0, line[0].lastIndexOf('/'))); |
42 | | - clazz.addField(field); |
| 45 | + new UniversalField(clazz, line, stringPool); |
43 | 46 | } |
44 | 47 | } |
45 | 48 | { |
46 | 49 | val methodMappings = ResourceUtil.getResourceStringFromJar("/methods.csv", FalsePatternLib.class).split("\n"); |
47 | 50 | for (int i = 1; i < methodMappings.length; i++) { |
48 | 51 | val line = methodMappings[i].split(","); |
49 | | - val field = new UniversalMethod(line, stringPool); |
50 | 52 | val clazz = internalLookup.get(MappingType.Notch, line[0].substring(0, line[0].lastIndexOf('/'))); |
51 | | - clazz.addMethod(field); |
| 53 | + new UniversalMethod(clazz, line, stringPool); |
52 | 54 | } |
53 | 55 | } |
54 | 56 | } |
55 | 57 |
|
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 { |
57 | 59 | 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 | + } |
65 | 113 | } |
66 | 114 | } |
67 | 115 | } |
0 commit comments