|
| 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 | +} |
0 commit comments