|
| 1 | +package org.angelauramc.methodsInjectorAgent.mod_compatibility_injector; |
| 2 | + |
| 3 | +import java.lang.instrument.ClassFileTransformer; |
| 4 | +import java.lang.instrument.Instrumentation; |
| 5 | +import java.security.ProtectionDomain; |
| 6 | + |
| 7 | +import org.angelauramc.methodsInjectorAgent.lwjgl2_methods_injector.ALC10Injector; |
| 8 | +import org.objectweb.asm.ClassReader; |
| 9 | +import org.objectweb.asm.ClassVisitor; |
| 10 | +import org.objectweb.asm.ClassWriter; |
| 11 | +import org.objectweb.asm.MethodVisitor; |
| 12 | +import org.objectweb.asm.Opcodes; |
| 13 | + |
| 14 | +// Older veil versions overrides imgui.library.name with libimgui-javaarm64.dylib if on arm/arm64 |
| 15 | +// which isn't the right lib. So veil fails to load. |
| 16 | +public class VeilImguiOverrideDisable extends ClassVisitor implements ClassFileTransformer { |
| 17 | + protected VeilImguiOverrideDisable(int api) { |
| 18 | + super(api); |
| 19 | + } |
| 20 | + |
| 21 | + public static void premain(String args, Instrumentation inst) { |
| 22 | + inst.addTransformer(new ClassFileTransformer() { |
| 23 | + public byte[] transform(ClassLoader l, String name, Class c, |
| 24 | + ProtectionDomain d, byte[] b) { |
| 25 | + if (!"foundry/veil/impl/client/imgui/VeilImGuiImpl".equals(name)) { |
| 26 | + return null; |
| 27 | + } |
| 28 | + ClassReader cr = new ClassReader(b); |
| 29 | + ClassWriter cw = new ClassWriter(cr, 0); |
| 30 | + ClassVisitor cv = new DisableMethodAdapter(cw); |
| 31 | + cr.accept(cv, 0); |
| 32 | + return cw.toByteArray(); |
| 33 | + } |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + public static class DisableMethodAdapter extends ClassVisitor { |
| 38 | + |
| 39 | + public DisableMethodAdapter(ClassVisitor cv) { |
| 40 | + super(Opcodes.ASM4, cv); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { |
| 45 | + MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); |
| 46 | + if (name.equals("setImGuiPath") |
| 47 | + && desc.equals("()V")) { |
| 48 | + try { // Minecraft makes it ugly if we use println |
| 49 | + System.out.write(("Amethyst-Android: Patching VeilImGuiImpl for faulty setImGuiPath()...\n" + |
| 50 | + "This issue is fixed in Veil 3.1.1 and above. See https://github.com/FoundryMC/Veil/commit/8e0e09365049a106bfa3634e2ed78a0310c5b4df\n" + |
| 51 | + "The intended target of this patch is Veil 3.1.0 and below. If you see this log output without Veil 3.1.0 or lower running, please send a bug report to Amethyst-Android.\n" + |
| 52 | + "https://github.com/AngelAuraMC/Amethyst-Android/issues/new?template=bug_report.yml").getBytes()); |
| 53 | + |
| 54 | + System.out.flush(); |
| 55 | + } catch (Exception ignored) {} |
| 56 | + return getMethodVisitor(mv); |
| 57 | + } |
| 58 | + return mv; |
| 59 | + } |
| 60 | + private MethodVisitor getMethodVisitor(MethodVisitor delegate) { |
| 61 | + return new MethodVisitor(this.api, null) { |
| 62 | + @Override |
| 63 | + public void visitCode() { |
| 64 | + delegate.visitCode(); |
| 65 | + delegate.visitInsn(Opcodes.RETURN); // Add our return |
| 66 | + delegate.visitMaxs(0, 0); // ClassWriter.COMPUTE_FRAMES will fix this |
| 67 | + delegate.visitEnd(); |
| 68 | + } |
| 69 | + }; |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | + |
0 commit comments