From f4a608acafb7342655c1ce87acb20dab4e313a23 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 23 Dec 2019 20:10:06 +0000 Subject: [PATCH] Convert LoadLibraryTransformer to Kotlin --- .../transform/LoadLibraryTransformer.java | 62 ------------------- .../transform/LoadLibraryTransformer.kt | 47 ++++++++++++++ 2 files changed, 47 insertions(+), 62 deletions(-) delete mode 100644 bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.java create mode 100644 bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.kt diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.java b/bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.java deleted file mode 100644 index 4644c727..00000000 --- a/bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.java +++ /dev/null @@ -1,62 +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.LdcInsnNode; -import org.objectweb.asm.tree.MethodNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public final class LoadLibraryTransformer extends Transformer { - private static final Logger logger = LoggerFactory.getLogger(LoadLibraryTransformer.class); - - private static final InsnMatcher AMD64_CHECK_MATCHER = InsnMatcher.compile("GETSTATIC LDC INVOKEVIRTUAL IFNE GETSTATIC LDC INVOKEVIRTUAL IFNE"); - - private int jnilibs, amd64Checks; - - @Override - protected void preTransform(ClassPath classPath) { - jnilibs = 0; - amd64Checks = 0; - } - - @Override - protected boolean transformCode(ClassPath classPath, Library library, ClassNode clazz, MethodNode method) { - var foundJnilib = false; - - for (var insn : method.instructions) { - if (insn.getOpcode() == Opcodes.LDC) { - var ldc = (LdcInsnNode) insn; - if (ldc.cst.equals("libjaggl.jnilib")) { - ldc.cst = "libjaggl.dylib"; - foundJnilib = true; - jnilibs++; - break; - } - } - } - - if (!foundJnilib) { - return false; - } - - AMD64_CHECK_MATCHER.match(method).forEach(match -> { - var ldc = (LdcInsnNode) match.get(1); - if (ldc.cst.equals("amd64")) { - match.forEach(method.instructions::remove); - amd64Checks++; - } - }); - - return false; - } - - @Override - protected void postTransform(ClassPath classPath) { - logger.info("Replaced {} jnilibs with dylibs and removed {} amd64 jagmisc checks", jnilibs, amd64Checks); - } -} diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.kt b/bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.kt new file mode 100644 index 00000000..1667dfe0 --- /dev/null +++ b/bundler/src/main/java/dev/openrs2/bundler/transform/LoadLibraryTransformer.kt @@ -0,0 +1,47 @@ +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.tree.ClassNode +import org.objectweb.asm.tree.LdcInsnNode +import org.objectweb.asm.tree.MethodNode + +class LoadLibraryTransformer : Transformer() { + private var jnilibs = 0 + private var amd64Checks = 0 + + override fun preTransform(classPath: ClassPath) { + jnilibs = 0 + amd64Checks = 0 + } + + override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean { + val jnilib = method.instructions.find { it is LdcInsnNode && it.cst == "libjaggl.jnilib" } ?: return false + jnilib as LdcInsnNode + jnilib.cst = "libjaggl.dylib" + jnilibs++ + + AMD64_CHECK_MATCHER.match(method).forEach { + val ldc = it[1] as LdcInsnNode + if (ldc.cst == "amd64") { + it.forEach(method.instructions::remove) + amd64Checks++ + } + } + + return false + } + + override fun postTransform(classPath: ClassPath) { + logger.info { "Replaced $jnilibs jnilibs with dylibs and removed $amd64Checks amd64 jagmisc checks" } + } + + companion object { + private val logger = InlineLogger() + private val AMD64_CHECK_MATCHER = + InsnMatcher.compile("GETSTATIC LDC INVOKEVIRTUAL IFNE GETSTATIC LDC INVOKEVIRTUAL IFNE") + } +}