From 6e9a7f36ef12ae2f0ad438da3d40bd3ce716f1e7 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 23 Dec 2019 20:43:10 +0000 Subject: [PATCH] Convert RightClickTransformer to Kotlin --- .../transform/BufferSizeTransformer.java | 93 ------------------- .../transform/BufferSizeTransformer.kt | 75 +++++++++++++++ 2 files changed, 75 insertions(+), 93 deletions(-) delete mode 100644 bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.java create mode 100644 bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.kt diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.java b/bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.java deleted file mode 100644 index f1361a94..00000000 --- a/bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.java +++ /dev/null @@ -1,93 +0,0 @@ -package dev.openrs2.bundler.transform; - -import java.util.Optional; - -import dev.openrs2.asm.InsnMatcher; -import dev.openrs2.asm.InsnNodeUtilsKt; -import dev.openrs2.asm.MemberRef; -import dev.openrs2.asm.MethodNodeUtilsKt; -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.FieldInsnNode; -import org.objectweb.asm.tree.LdcInsnNode; -import org.objectweb.asm.tree.MethodNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public final class BufferSizeTransformer extends Transformer { - private static final Logger logger = LoggerFactory.getLogger(BufferSizeTransformer.class); - - private static final InsnMatcher GPP1_POS_MATCHER = InsnMatcher.compile("LDC (INVOKESPECIAL | INVOKEVIRTUAL) GETSTATIC"); - private static final InsnMatcher NEW_BUFFER_MATCHER = InsnMatcher.compile("NEW DUP (SIPUSH | LDC) INVOKESPECIAL PUTSTATIC"); - - private static Optional findBuffer(MethodNode method) { - return GPP1_POS_MATCHER.match(method) - .filter(match -> { - var ldc = (LdcInsnNode) match.get(0); - return ldc.cst.equals("gpp1 pos:"); - }) - .map(match -> { - var getstatic = (FieldInsnNode) match.get(2); - return new MemberRef(getstatic); - }) - .findAny(); - } - - private MemberRef buffer; - private int buffersResized; - - @Override - protected void preTransform(ClassPath classPath) { - buffer = null; - buffersResized = 0; - - for (var library : classPath.getLibraries()) { - for (var clazz : library) { - for (var method : clazz.methods) { - if (!MethodNodeUtilsKt.hasCode(method)) { - continue; - } - - var buffer = findBuffer(method); - if (buffer.isEmpty()) { - continue; - } - - this.buffer = buffer.orElseThrow(); - logger.info("Identified buffer: {}", this.buffer); - break; - } - } - } - } - - @Override - protected boolean transformCode(ClassPath classPath, Library library, ClassNode clazz, MethodNode method) { - if (buffer == null) { - return false; - } - - NEW_BUFFER_MATCHER.match(method).forEach(match -> { - var putstatic = (FieldInsnNode) match.get(4); - if (!putstatic.owner.equals(buffer.getOwner())) { - return; - } else if (!putstatic.name.equals(buffer.getName())) { - return; - } else if (!putstatic.desc.equals(buffer.getDesc())) { - return; - } - - method.instructions.set(match.get(2), InsnNodeUtilsKt.createIntConstant(65535)); - buffersResized++; - }); - - return false; - } - - @Override - protected void postTransform(ClassPath classPath) { - logger.info("Resized {} buffers to 65535 bytes", buffersResized); - } -} diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.kt b/bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.kt new file mode 100644 index 00000000..33fb14a5 --- /dev/null +++ b/bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.kt @@ -0,0 +1,75 @@ +package dev.openrs2.bundler.transform + +import com.github.michaelbull.logging.InlineLogger +import dev.openrs2.asm.InsnMatcher +import dev.openrs2.asm.MemberRef +import dev.openrs2.asm.classpath.ClassPath +import dev.openrs2.asm.classpath.Library +import dev.openrs2.asm.createIntConstant +import dev.openrs2.asm.hasCode +import dev.openrs2.asm.transform.Transformer +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.FieldInsnNode +import org.objectweb.asm.tree.LdcInsnNode +import org.objectweb.asm.tree.MethodNode +import kotlin.streams.asSequence + +class BufferSizeTransformer : Transformer() { + private var buffer: MemberRef? = null + private var buffersResized = 0 + + override fun preTransform(classPath: ClassPath) { + buffer = null + buffersResized = 0 + + for (library in classPath.libraries) { + for (clazz in library) { + for (method in clazz.methods) { + if (!method.hasCode()) { + continue + } + + this.buffer = findBuffer(method) ?: continue + logger.info { "Identified buffer: ${this.buffer}" } + break + } + } + } + } + + override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean { + if (buffer == null) { + return false + } + + NEW_BUFFER_MATCHER.match(method).forEach { + val putstatic = it[4] as FieldInsnNode + if (MemberRef(putstatic) == buffer!!) { + method.instructions[it[2]] = createIntConstant(65535) + buffersResized++ + } + } + + return false + } + + override fun postTransform(classPath: ClassPath) { + logger.info { "Resized $buffersResized buffers to 65535 bytes" } + } + + companion object { + private val logger = InlineLogger() + private val GPP1_POS_MATCHER = InsnMatcher.compile("LDC (INVOKESPECIAL | INVOKEVIRTUAL) GETSTATIC") + private val NEW_BUFFER_MATCHER = InsnMatcher.compile("NEW DUP (SIPUSH | LDC) INVOKESPECIAL PUTSTATIC") + + private fun findBuffer(method: MethodNode): MemberRef? { + return GPP1_POS_MATCHER.match(method).asSequence().filter { + val ldc = it[0] as LdcInsnNode + ldc.cst == "gpp1 pos:" + }.map { + val getstatic = it[2] as FieldInsnNode + MemberRef(getstatic) + }.firstOrNull() + } + } +}