From 84ade956affbb1509849a44dfdaedd7312e9a9b7 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 2 Mar 2020 19:54:00 +0000 Subject: [PATCH] Sort static fields above instance fields --- .../openrs2/deob/transform/FieldOrderTransformer.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.kt b/deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.kt index 77745318..d7e0d113 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.kt +++ b/deob/src/main/java/dev/openrs2/deob/transform/FieldOrderTransformer.kt @@ -7,6 +7,7 @@ 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.FieldNode class FieldOrderTransformer : Transformer() { override fun transformClass(classPath: ClassPath, library: Library, clazz: ClassNode): Boolean { @@ -18,6 +19,15 @@ class FieldOrderTransformer : Transformer() { companion object { private const val CONSTRUCTOR = "" private const val STATIC_CONSTRUCTOR = "" + private val STATIC_COMPARATOR = Comparator { a, b -> + val aStatic = a.access and Opcodes.ACC_STATIC != 0 + val bStatic = b.access and Opcodes.ACC_STATIC != 0 + when { + aStatic && !bStatic -> -1 + !aStatic && bStatic -> 1 + else -> 0 + } + } private fun sortFields(clazz: ClassNode, ctorName: String, opcode: Int) { val ctor = clazz.methods.find { it.name == ctorName } ?: return @@ -40,7 +50,7 @@ class FieldOrderTransformer : Transformer() { } } - clazz.fields.sortBy { fields.getOrDefault(MemberDesc(it), -1) } + clazz.fields.sortWith(STATIC_COMPARATOR.thenBy { fields.getOrDefault(MemberDesc(it), -1) }) } } }