Convert RightClickTransformer to Kotlin

master
Graham 5 years ago
parent 4a8704f566
commit 6e9a7f36ef
  1. 93
      bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.java
  2. 75
      bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.kt

@ -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<MemberRef> 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);
}
}

@ -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()
}
}
}
Loading…
Cancel
Save