From e4716fbd0c976c3ac30bb818befc5ce7399290ed Mon Sep 17 00:00:00 2001 From: arcade_kappa Date: Mon, 17 Jun 2024 21:35:27 +0800 Subject: [PATCH] Move DWheel patch to ASM --- .../cleanroommc/fugue/config/FugueConfig.java | 2 +- .../com/cleanroommc/fugue/helper/Mouse.java | 11 +++++ .../universal/DWheelTransformer.java | 41 ++++++++----------- 3 files changed, 29 insertions(+), 25 deletions(-) create mode 100644 src/main/java/com/cleanroommc/fugue/helper/Mouse.java diff --git a/src/main/java/com/cleanroommc/fugue/config/FugueConfig.java b/src/main/java/com/cleanroommc/fugue/config/FugueConfig.java index f9f03e3..cf092a9 100644 --- a/src/main/java/com/cleanroommc/fugue/config/FugueConfig.java +++ b/src/main/java/com/cleanroommc/fugue/config/FugueConfig.java @@ -86,7 +86,7 @@ public class FugueConfig { @Config.Comment( """ - Java 8's UUID creation if flawed. It allow invalid UUIDs to be created. + Java 8's UUID creation is flawed. It allow invalid UUIDs to be created. This was fixed in later Java, but old mods still need a solution. Target classes here will be patched to use a helper method we provide.""" ) diff --git a/src/main/java/com/cleanroommc/fugue/helper/Mouse.java b/src/main/java/com/cleanroommc/fugue/helper/Mouse.java new file mode 100644 index 0000000..5ee4a8e --- /dev/null +++ b/src/main/java/com/cleanroommc/fugue/helper/Mouse.java @@ -0,0 +1,11 @@ +package com.cleanroommc.fugue.helper; + +public class Mouse { + public static int getEventDWheel() { + return org.lwjgl.input.Mouse.getEventDWheel() * 120; + } + + public static int getDWheel() { + return org.lwjgl.input.Mouse.getDWheel() * 120; + } +} diff --git a/src/main/java/com/cleanroommc/fugue/transformer/universal/DWheelTransformer.java b/src/main/java/com/cleanroommc/fugue/transformer/universal/DWheelTransformer.java index 263eaa8..cef464d 100644 --- a/src/main/java/com/cleanroommc/fugue/transformer/universal/DWheelTransformer.java +++ b/src/main/java/com/cleanroommc/fugue/transformer/universal/DWheelTransformer.java @@ -1,36 +1,29 @@ package com.cleanroommc.fugue.transformer.universal; -import com.cleanroommc.fugue.common.Fugue; -import javassist.CannotCompileException; -import javassist.ClassPool; -import javassist.CtClass; -import javassist.expr.ExprEditor; -import javassist.expr.MethodCall; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.MethodInsnNode; import top.outlands.foundation.IExplicitTransformer; -import java.io.ByteArrayInputStream; - public class DWheelTransformer implements IExplicitTransformer { @Override public byte[] transform(byte[] bytes) { - try { - CtClass cc = ClassPool.getDefault().makeClass(new ByteArrayInputStream(bytes)); - cc.instrument(new ExprEditor() { - @Override - public void edit(MethodCall m) throws CannotCompileException { - if (m.getMethodName().equals("getEventDWheel") && m.getClassName().equals("org.lwjgl.input.Mouse")) { - m.replace("$_ = $proceed($$) * 120;"); - } - if (m.getMethodName().equals("getDWheel") && m.getClassName().equals("org.lwjgl.input.Mouse")) { - m.replace("$_ = $proceed($$) * 120;"); + ClassReader reader = new ClassReader(bytes); + ClassNode classNode = new ClassNode(); + reader.accept(classNode, 0); + classNode.methods.forEach(methodNode -> methodNode.instructions.forEach(abstractInsnNode -> { + if (abstractInsnNode instanceof MethodInsnNode methodInsnNode) { + if (methodInsnNode.owner.equals("org/lwjgl/input/Mouse")) { + if (methodInsnNode.name.equals("getEventDWheel") || methodInsnNode.name.equals("getDWheel")) { + methodInsnNode.owner = "com/cleanroommc/fugue/helper/Mouse"; } } - }); - bytes = cc.toBytecode(); - } catch (Throwable t) { - Fugue.LOGGER.error(t); - } - return bytes; + } + })); + ClassWriter writer = new ClassWriter(0); + classNode.accept(writer); + return writer.toByteArray(); } }