From 01f30c5f037634465e23926668211067d9bdcaa4 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 23 Dec 2019 20:47:25 +0000 Subject: [PATCH] Convert RightClickTransformer to Kotlin --- .../transform/RightClickTransformer.java | 65 ------------------- .../transform/RightClickTransformer.kt | 52 +++++++++++++++ 2 files changed, 52 insertions(+), 65 deletions(-) delete mode 100644 bundler/src/main/java/dev/openrs2/bundler/transform/RightClickTransformer.java create mode 100644 bundler/src/main/java/dev/openrs2/bundler/transform/RightClickTransformer.kt diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/RightClickTransformer.java b/bundler/src/main/java/dev/openrs2/bundler/transform/RightClickTransformer.java deleted file mode 100644 index 84e1f237..00000000 --- a/bundler/src/main/java/dev/openrs2/bundler/transform/RightClickTransformer.java +++ /dev/null @@ -1,65 +0,0 @@ -package dev.openrs2.bundler.transform; - -import dev.openrs2.asm.classpath.ClassPath; -import dev.openrs2.asm.classpath.Library; -import dev.openrs2.asm.transform.Transformer; -import org.objectweb.asm.Opcodes; -import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.FieldInsnNode; -import org.objectweb.asm.tree.InsnList; -import org.objectweb.asm.tree.InsnNode; -import org.objectweb.asm.tree.MethodInsnNode; -import org.objectweb.asm.tree.MethodNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public final class RightClickTransformer extends Transformer { - private static final Logger logger = LoggerFactory.getLogger(RightClickTransformer.class); - - private int metaDownCalls; - - @Override - protected void preTransform(ClassPath classPath) { - metaDownCalls = 0; - } - - @Override - protected boolean transformCode(ClassPath classPath, Library library, ClassNode clazz, MethodNode method) { - for (var it = method.instructions.iterator(); it.hasNext(); ) { - var insn = it.next(); - if (insn.getOpcode() != Opcodes.INVOKEVIRTUAL) { - continue; - } - - var invokevirtual = (MethodInsnNode) insn; - if (!invokevirtual.owner.equals("java/awt/event/MouseEvent") && !invokevirtual.owner.equals("java/awt/event/InputEvent")) { - continue; - } - - if (!invokevirtual.name.equals("isMetaDown")) { - continue; - } - - if (!invokevirtual.desc.equals("()Z")) { - continue; - } - - invokevirtual.name = "getModifiersEx"; - invokevirtual.desc = "()I"; - - var list = new InsnList(); - list.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/awt/event/MouseEvent", "BUTTON3_DOWN_MASK", "I")); - list.add(new InsnNode(Opcodes.IAND)); - method.instructions.insert(invokevirtual, list); - - metaDownCalls++; - } - - return false; - } - - @Override - protected void postTransform(ClassPath classPath) { - logger.info("Replaced {} isMetaDown calls with getModifiersEx", metaDownCalls); - } -} diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/RightClickTransformer.kt b/bundler/src/main/java/dev/openrs2/bundler/transform/RightClickTransformer.kt new file mode 100644 index 00000000..96580c82 --- /dev/null +++ b/bundler/src/main/java/dev/openrs2/bundler/transform/RightClickTransformer.kt @@ -0,0 +1,52 @@ +package dev.openrs2.bundler.transform + +import com.github.michaelbull.logging.InlineLogger +import dev.openrs2.asm.classpath.ClassPath +import dev.openrs2.asm.classpath.Library +import dev.openrs2.asm.transform.Transformer +import org.objectweb.asm.Opcodes +import org.objectweb.asm.tree.* + +class RightClickTransformer : Transformer() { + private var metaDownCalls = 0 + + override fun preTransform(classPath: ClassPath) { + metaDownCalls = 0 + } + + override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean { + for (insn in method.instructions) { + if (insn !is MethodInsnNode || insn.opcode != Opcodes.INVOKEVIRTUAL) { + continue + } + + if (insn.owner != "java/awt/event/MouseEvent" && insn.owner != "java/awt/event/InputEvent") { + continue + } + + if (insn.name != "isMetaDown" || insn.desc != "()Z") { + continue + } + + insn.name = "getModifiersEx" + insn.desc = "()I" + + val list = InsnList() + list.add(FieldInsnNode(Opcodes.GETSTATIC, "java/awt/event/MouseEvent", "BUTTON3_DOWN_MASK", "I")) + list.add(InsnNode(Opcodes.IAND)) + method.instructions.insert(insn, list) + + metaDownCalls++ + } + + return false + } + + override fun postTransform(classPath: ClassPath) { + logger.info { "Replaced $metaDownCalls isMetaDown calls with getModifiersEx" } + } + + companion object { + private val logger = InlineLogger() + } +}