|
| 1 | +package org.embeddedt.modernfix.testing; |
| 2 | + |
| 3 | +import org.objectweb.asm.ClassReader; |
| 4 | +import org.objectweb.asm.ClassWriter; |
| 5 | +import org.objectweb.asm.Opcodes; |
| 6 | +import org.objectweb.asm.tree.AbstractInsnNode; |
| 7 | +import org.objectweb.asm.tree.ClassNode; |
| 8 | +import org.objectweb.asm.tree.MethodInsnNode; |
| 9 | +import org.objectweb.asm.tree.MethodNode; |
| 10 | + |
| 11 | +import java.lang.instrument.ClassFileTransformer; |
| 12 | +import java.lang.instrument.IllegalClassFormatException; |
| 13 | +import java.lang.instrument.Instrumentation; |
| 14 | +import java.security.ProtectionDomain; |
| 15 | +import java.util.ListIterator; |
| 16 | + |
| 17 | +public class Agent { |
| 18 | + /** |
| 19 | + * Simple agent that transforms Fabric Loader to never mark game JARs as system libraries. |
| 20 | + * |
| 21 | + * Ugly, but usable workaround for <a href="https://github.com/FabricMC/fabric-loader/issues/817">issue #817</a> |
| 22 | + * on the Loader bug tracker. |
| 23 | + */ |
| 24 | + public static void premain(String args, Instrumentation instrumentation) { |
| 25 | + instrumentation.addTransformer(new ClassFileTransformer() { |
| 26 | + @Override |
| 27 | + public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { |
| 28 | + if(className.equals("net/fabricmc/loader/impl/game/LibClassifier")) { |
| 29 | + ClassNode node = new ClassNode(); |
| 30 | + ClassReader reader = new ClassReader(classfileBuffer); |
| 31 | + reader.accept(node, 0); |
| 32 | + for(MethodNode m : node.methods) { |
| 33 | + if(m.name.equals("<init>")) { |
| 34 | + ListIterator<AbstractInsnNode> iter = m.instructions.iterator(); |
| 35 | + int addMatches = 0; |
| 36 | + while(iter.hasNext()) { |
| 37 | + AbstractInsnNode n = iter.next(); |
| 38 | + if(n instanceof MethodInsnNode) { |
| 39 | + MethodInsnNode invokeNode = (MethodInsnNode)n; |
| 40 | + if(invokeNode.name.equals("add") && invokeNode.owner.equals("java/util/Set") && invokeNode.desc.equals("(Ljava/lang/Object;)Z")) { |
| 41 | + addMatches++; |
| 42 | + if(addMatches == 2) { |
| 43 | + iter.set(new MethodInsnNode(Opcodes.INVOKESTATIC, "org/embeddedt/modernfix/testing/AgentHooks", "addLibraryWithCheck", "(Ljava/util/Set;Ljava/lang/Object;)Z", false)); |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); |
| 52 | + node.accept(writer); |
| 53 | + byte[] finalArray = writer.toByteArray(); |
| 54 | + //dumpDebugClass(className, finalArray); |
| 55 | + return finalArray; |
| 56 | + } |
| 57 | + return classfileBuffer; |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | +} |
0 commit comments