From fb18a7227d3effafd37c0d67a8f1650b68b66779 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 23 Dec 2019 15:06:52 +0000 Subject: [PATCH] Convert FieldOrderTransformer to Kotlin --- .../deob/transform/FieldOrderTransformer.java | 56 ------------------- .../deob/transform/FieldOrderTransformer.kt | 46 +++++++++++++++ 2 files changed, 46 insertions(+), 56 deletions(-) delete mode 100644 deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.java create mode 100644 deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.kt diff --git a/deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.java deleted file mode 100644 index 9e1d08fc5c..0000000000 --- a/deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.java +++ /dev/null @@ -1,56 +0,0 @@ -package dev.openrs2.deob.transform; - -import java.util.HashMap; - -import dev.openrs2.asm.MemberDesc; -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; - -public final class FieldOrderTransformer extends Transformer { - private static final String CONSTRUCTOR = ""; - private static final String STATIC_CONSTRUCTOR = ""; - - private static void sortFields(ClassNode clazz, String ctorName, int opcode) { - clazz.methods.stream() - .filter(m -> m.name.equals(ctorName)) - .findFirst() - .ifPresent(ctor -> { - var fields = new HashMap(); - var index = 0; - - for (var it = ctor.instructions.iterator(); it.hasNext(); ) { - var insn = it.next(); - if (insn.getOpcode() != opcode) { - continue; - } - - var putfield = (FieldInsnNode) insn; - if (!putfield.owner.equals(clazz.name)) { - continue; - } - - var desc = new MemberDesc(putfield); - if (!fields.containsKey(desc)) { - fields.put(desc, index++); - } - } - - clazz.fields.sort((a, b) -> { - var indexA = fields.getOrDefault(new MemberDesc(a), -1); - var indexB = fields.getOrDefault(new MemberDesc(b), -1); - return Integer.compare(indexA, indexB); - }); - }); - } - - @Override - public boolean transformClass(ClassPath classPath, Library library, ClassNode clazz) { - sortFields(clazz, CONSTRUCTOR, Opcodes.PUTFIELD); - sortFields(clazz, STATIC_CONSTRUCTOR, Opcodes.PUTSTATIC); - return false; - } -} diff --git a/deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.kt b/deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.kt new file mode 100644 index 0000000000..edbb14d4bb --- /dev/null +++ b/deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.kt @@ -0,0 +1,46 @@ +package dev.openrs2.deob.transform + +import dev.openrs2.asm.MemberDesc +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 + +class FieldOrderTransformer : Transformer() { + public override fun transformClass(classPath: ClassPath, library: Library, clazz: ClassNode): Boolean { + sortFields(clazz, CONSTRUCTOR, Opcodes.PUTFIELD) + sortFields(clazz, STATIC_CONSTRUCTOR, Opcodes.PUTSTATIC) + return false + } + + companion object { + private const val CONSTRUCTOR = "" + private const val STATIC_CONSTRUCTOR = "" + + private fun sortFields(clazz: ClassNode, ctorName: String, opcode: Int) { + val ctor = clazz.methods.find { it.name == ctorName } ?: return + + val fields = mutableMapOf() + var index = 0 + for (insn in ctor.instructions) { + if (insn.opcode != opcode) { + continue + } + + val putfield = insn as FieldInsnNode + if (putfield.owner != clazz.name) { + continue + } + + val desc = MemberDesc(putfield) + if (!fields.containsKey(desc)) { + fields[desc] = index++ + } + } + + clazz.fields.sortBy { fields.getOrDefault(MemberDesc(it), -1) } + } + } +}