From 312dffe044d3b11d0a89c286d9651b0f5b1281c7 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Wed, 27 Nov 2024 17:13:23 +0100 Subject: test: Make all minecraft methods public during testing This is meant to fix minecraft failing to access package private methods of itself due to changes in naming --- .../moe/nea/firmament/testagent/AgentMain.java | 21 +++++++++++++++ .../testagent/ProtectedToPublicClassRewriter.java | 31 ++++++++++++++++++++++ .../ProtectedToPublicClassTransformer.java | 30 +++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 testagent/src/main/java/moe/nea/firmament/testagent/AgentMain.java create mode 100644 testagent/src/main/java/moe/nea/firmament/testagent/ProtectedToPublicClassRewriter.java create mode 100644 testagent/src/main/java/moe/nea/firmament/testagent/ProtectedToPublicClassTransformer.java (limited to 'testagent/src') diff --git a/testagent/src/main/java/moe/nea/firmament/testagent/AgentMain.java b/testagent/src/main/java/moe/nea/firmament/testagent/AgentMain.java new file mode 100644 index 0000000..79023d8 --- /dev/null +++ b/testagent/src/main/java/moe/nea/firmament/testagent/AgentMain.java @@ -0,0 +1,21 @@ +package moe.nea.firmament.testagent; + +import java.lang.instrument.Instrumentation; + +public class AgentMain { + + public static void premain( + String agentArgs, Instrumentation inst) { + System.out.println("Pre-Main Firmament Test Agent"); + AgentMain.inject(inst); + } + + public static void agentmain( + String agentArgs, Instrumentation inst) { + System.out.println("Injected Firmament Test Agent"); + AgentMain.inject(inst); + } + + private static void inject(Instrumentation inst) { + inst.addTransformer(new ProtectedToPublicClassTransformer(inst)); } +} diff --git a/testagent/src/main/java/moe/nea/firmament/testagent/ProtectedToPublicClassRewriter.java b/testagent/src/main/java/moe/nea/firmament/testagent/ProtectedToPublicClassRewriter.java new file mode 100644 index 0000000..7d9aa56 --- /dev/null +++ b/testagent/src/main/java/moe/nea/firmament/testagent/ProtectedToPublicClassRewriter.java @@ -0,0 +1,31 @@ +package moe.nea.firmament.testagent; + +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.FieldVisitor; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; + +public class ProtectedToPublicClassRewriter extends ClassVisitor { + public ProtectedToPublicClassRewriter(ClassWriter writer) { + super(Opcodes.ASM9, writer); + } + + int makePublic(int flags) { + if ((flags & Opcodes.ACC_PROTECTED) != 0) + return (flags & ~Opcodes.ACC_PROTECTED) | Opcodes.ACC_PUBLIC; + if ((flags & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE)) == 0) + return flags | Opcodes.ACC_PUBLIC; + return flags; + } + + @Override + public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) { + return super.visitField(makePublic(access), name, descriptor, signature, value); + } + + @Override + public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { + return super.visitMethod(makePublic(access), name, descriptor, signature, exceptions); + } +} diff --git a/testagent/src/main/java/moe/nea/firmament/testagent/ProtectedToPublicClassTransformer.java b/testagent/src/main/java/moe/nea/firmament/testagent/ProtectedToPublicClassTransformer.java new file mode 100644 index 0000000..5d59035 --- /dev/null +++ b/testagent/src/main/java/moe/nea/firmament/testagent/ProtectedToPublicClassTransformer.java @@ -0,0 +1,30 @@ +package moe.nea.firmament.testagent; + +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; + +import java.lang.instrument.ClassFileTransformer; +import java.lang.instrument.IllegalClassFormatException; +import java.lang.instrument.Instrumentation; +import java.security.ProtectionDomain; + +public class ProtectedToPublicClassTransformer implements ClassFileTransformer { + public ProtectedToPublicClassTransformer(Instrumentation inst) { + } + + @Override + public byte[] transform(ClassLoader loader, + String className, + Class classBeingRedefined, + ProtectionDomain protectionDomain, + byte[] classfileBuffer) + throws IllegalClassFormatException { + if (!className.startsWith("net/minecraft/")) return classfileBuffer; + if (classfileBuffer == null) return null; + var reader = new ClassReader(classfileBuffer); + var writer = new ClassWriter(0); + var transformer = new ProtectedToPublicClassRewriter(writer); + reader.accept(transformer, 0); + return writer.toByteArray(); + } +} -- cgit