Convert MacResizeTransformer to Kotlin

master
Graham 5 years ago
parent f4a608acaf
commit 1d889357a9
  1. 68
      bundler/src/main/java/dev/openrs2/bundler/transform/MacResizeTransformer.java
  2. 60
      bundler/src/main/java/dev/openrs2/bundler/transform/MacResizeTransformer.kt

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

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