From 1d889357a9014409fe9fe66c28873dd23049c7eb Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 23 Dec 2019 20:22:01 +0000 Subject: [PATCH] Convert MacResizeTransformer to Kotlin --- .../transform/MacResizeTransformer.java | 68 ------------------- .../bundler/transform/MacResizeTransformer.kt | 60 ++++++++++++++++ 2 files changed, 60 insertions(+), 68 deletions(-) delete mode 100644 bundler/src/main/java/dev/openrs2/bundler/transform/MacResizeTransformer.java create mode 100644 bundler/src/main/java/dev/openrs2/bundler/transform/MacResizeTransformer.kt diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/MacResizeTransformer.java b/bundler/src/main/java/dev/openrs2/bundler/transform/MacResizeTransformer.java deleted file mode 100644 index fd00f2eb..00000000 --- a/bundler/src/main/java/dev/openrs2/bundler/transform/MacResizeTransformer.java +++ /dev/null @@ -1,68 +0,0 @@ -package dev.openrs2.bundler.transform; - -import dev.openrs2.asm.InsnMatcher; -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.JumpInsnNode; -import org.objectweb.asm.tree.LdcInsnNode; -import org.objectweb.asm.tree.MethodInsnNode; -import org.objectweb.asm.tree.MethodNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public final class MacResizeTransformer extends Transformer { - private static final Logger logger = LoggerFactory.getLogger(MacResizeTransformer.class); - - private static final InsnMatcher DETECT_MAC_MATCHER = InsnMatcher.compile("GETSTATIC LDC INVOKEVIRTUAL (IFEQ | IFNE)"); - - private int branchesRemoved; - - @Override - protected void preTransform(ClassPath classPath) { - branchesRemoved = 0; - } - - @Override - protected boolean transformCode(ClassPath classPath, Library library, ClassNode clazz, MethodNode method) { - DETECT_MAC_MATCHER.match(method).forEach(match -> { - var getstatic = (FieldInsnNode) match.get(0); - if (getstatic.owner.equals("loader") || getstatic.owner.equals(clazz.name) || !getstatic.desc.equals("Ljava/lang/String;")) { - return; - } - - var ldc = (LdcInsnNode) match.get(1); - if (!ldc.cst.equals("mac")) { - return; - } - - var invokevirtual = (MethodInsnNode) match.get(2); - if (!invokevirtual.owner.equals("java/lang/String") || !invokevirtual.name.equals("startsWith") || !invokevirtual.desc.equals("(Ljava/lang/String;)Z")) { - return; - } - - method.instructions.remove(getstatic); - method.instructions.remove(ldc); - method.instructions.remove(invokevirtual); - - var branch = (JumpInsnNode) match.get(3); - if (branch.getOpcode() == Opcodes.IFEQ) { - branch.setOpcode(Opcodes.GOTO); - } else { - method.instructions.remove(branch); - } - - branchesRemoved++; - }); - - return false; - } - - @Override - protected void postTransform(ClassPath classPath) { - logger.info("Removed {} branches to macOS-specific resize logic", branchesRemoved); - } -} diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/MacResizeTransformer.kt b/bundler/src/main/java/dev/openrs2/bundler/transform/MacResizeTransformer.kt new file mode 100644 index 00000000..cd39957f --- /dev/null +++ b/bundler/src/main/java/dev/openrs2/bundler/transform/MacResizeTransformer.kt @@ -0,0 +1,60 @@ +package dev.openrs2.bundler.transform + +import com.github.michaelbull.logging.InlineLogger +import dev.openrs2.asm.InsnMatcher +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 MacResizeTransformer : Transformer() { + private var branchesRemoved = 0 + + override fun preTransform(classPath: ClassPath) { + branchesRemoved = 0 + } + + override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean { + DETECT_MAC_MATCHER.match(method).forEach { + val getstatic = it[0] as FieldInsnNode + if (getstatic.owner == "loader" || getstatic.owner == clazz.name || getstatic.desc != "Ljava/lang/String;") { + return@forEach + } + + val ldc = it[1] as LdcInsnNode + if (ldc.cst != "mac") { + return@forEach + } + + val invokevirtual = it[2] as MethodInsnNode + if (invokevirtual.owner != "java/lang/String" || invokevirtual.name != "startsWith" || invokevirtual.desc != "(Ljava/lang/String;)Z") { + return@forEach + } + + method.instructions.remove(getstatic) + method.instructions.remove(ldc) + method.instructions.remove(invokevirtual) + + val branch = it[3] as JumpInsnNode + if (branch.opcode == Opcodes.IFEQ) { + branch.opcode = Opcodes.GOTO + } else { + method.instructions.remove(branch) + } + + branchesRemoved++ + } + + return false + } + + override fun postTransform(classPath: ClassPath) { + logger.info { "Removed $branchesRemoved branches to macOS-specific resize logic" } + } + + companion object { + private val logger = InlineLogger() + private val DETECT_MAC_MATCHER = InsnMatcher.compile("GETSTATIC LDC INVOKEVIRTUAL (IFEQ | IFNE)") + } +}