Skip to content

Commit

Permalink
Move DWheel patch to ASM
Browse files Browse the repository at this point in the history
  • Loading branch information
kappa-maintainer committed Jun 17, 2024
1 parent ca49a00 commit e4716fb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
)
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/cleanroommc/fugue/helper/Mouse.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit e4716fb

Please sign in to comment.