From 4a8704f566bc48ef19337f04c9afbd89271f2916 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 23 Dec 2019 20:28:52 +0000 Subject: [PATCH] Convert HostCheckTransformer to Kotlin --- .../transform/HostCheckTransformer.java | 90 ------------------- .../bundler/transform/HostCheckTransformer.kt | 63 +++++++++++++ 2 files changed, 63 insertions(+), 90 deletions(-) delete mode 100644 bundler/src/main/java/dev/openrs2/bundler/transform/HostCheckTransformer.java create mode 100644 bundler/src/main/java/dev/openrs2/bundler/transform/HostCheckTransformer.kt diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/HostCheckTransformer.java b/bundler/src/main/java/dev/openrs2/bundler/transform/HostCheckTransformer.java deleted file mode 100644 index d5bc9d92..00000000 --- a/bundler/src/main/java/dev/openrs2/bundler/transform/HostCheckTransformer.java +++ /dev/null @@ -1,90 +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.Type; -import org.objectweb.asm.tree.ClassNode; -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 HostCheckTransformer extends Transformer { - private static final Logger logger = LoggerFactory.getLogger(HostCheckTransformer.class); - - private static final InsnMatcher GET_HOST_MATCHER = InsnMatcher.compile("INVOKEVIRTUAL INVOKEVIRTUAL INVOKEVIRTUAL"); - - private int hostChecks; - - @Override - protected void preTransform(ClassPath classPath) { - hostChecks = 0; - } - - @Override - protected boolean transformCode(ClassPath classPath, Library library, ClassNode clazz, MethodNode method) { - if (Type.getReturnType(method.desc).getSort() != Type.BOOLEAN) { - return false; - } - - GET_HOST_MATCHER.match(method).forEach(match -> { - var insn1 = (MethodInsnNode) match.get(0); - if (!insn1.owner.equals(clazz.name)) { - return; - } - - if (!insn1.name.equals("getDocumentBase")) { - return; - } - - if (!insn1.desc.equals("()Ljava/net/URL;")) { - return; - } - - var insn2 = (MethodInsnNode) match.get(1); - if (!insn2.owner.equals("java/net/URL")) { - return; - } - - if (!insn2.name.equals("getHost")) { - return; - } - - if (!insn2.desc.equals("()Ljava/lang/String;")) { - return; - } - - var insn3 = (MethodInsnNode) match.get(2); - if (!insn3.owner.equals("java/lang/String")) { - return; - } - - if (!insn3.name.equals("toLowerCase")) { - return; - } - - if (!insn3.desc.equals("()Ljava/lang/String;")) { - return; - } - - method.instructions.clear(); - method.tryCatchBlocks.clear(); - - method.instructions.add(new InsnNode(Opcodes.ICONST_1)); - method.instructions.add(new InsnNode(Opcodes.IRETURN)); - - hostChecks++; - }); - - return false; - } - - @Override - protected void postTransform(ClassPath classPath) { - logger.info("Removed {} host checks", hostChecks); - } -} diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/HostCheckTransformer.kt b/bundler/src/main/java/dev/openrs2/bundler/transform/HostCheckTransformer.kt new file mode 100644 index 00000000..2604fb23 --- /dev/null +++ b/bundler/src/main/java/dev/openrs2/bundler/transform/HostCheckTransformer.kt @@ -0,0 +1,63 @@ +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.Type +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.InsnNode +import org.objectweb.asm.tree.MethodInsnNode +import org.objectweb.asm.tree.MethodNode + +class HostCheckTransformer : Transformer() { + private var hostChecks = 0 + + override fun preTransform(classPath: ClassPath) { + hostChecks = 0 + } + + override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean { + if (Type.getReturnType(method.desc).sort != Type.BOOLEAN) { + return false + } + + GET_HOST_MATCHER.match(method).forEach { + val insn1 = it[0] as MethodInsnNode + if (insn1.owner != clazz.name || insn1.name != "getDocumentBase" || insn1.desc != "()Ljava/net/URL;") { + return@forEach + } + + val insn2 = it[1] as MethodInsnNode + if (insn2.owner != "java/net/URL" || insn2.name != "getHost" || insn2.desc != "()Ljava/lang/String;") { + return@forEach + } + + val insn3 = it[2] as MethodInsnNode + if (insn3.owner != "java/lang/String" || insn3.name != "toLowerCase" || insn3.desc != "()Ljava/lang/String;") { + return@forEach + } + + method.instructions.clear() + method.tryCatchBlocks.clear() + + method.instructions.add(InsnNode(Opcodes.ICONST_1)) + method.instructions.add(InsnNode(Opcodes.IRETURN)) + + hostChecks++ + } + + return false + } + + override fun postTransform(classPath: ClassPath) { + logger.info { "Removed $hostChecks host checks" } + } + + companion object { + private val logger = InlineLogger() + private val GET_HOST_MATCHER = InsnMatcher.compile("INVOKEVIRTUAL INVOKEVIRTUAL INVOKEVIRTUAL") + } +}