From ad53f9a78d5e8366f7cb6d0e3745ce043c179315 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 2 Mar 2020 19:53:26 +0000 Subject: [PATCH] Sort methods Fernflower was previously placing constructors in the middle of instance methods. They look much nicer at the top. This commit also ensures static methods appear before instance methods. --- .../java/dev/openrs2/deob/Deobfuscator.kt | 2 + .../deob/transform/MethodOrderTransformer.kt | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 deob/src/main/java/dev/openrs2/deob/transform/MethodOrderTransformer.kt diff --git a/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt b/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt index c7b2b7eb..4c31c1e9 100644 --- a/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt +++ b/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt @@ -21,6 +21,7 @@ import dev.openrs2.deob.transform.ExceptionTracingTransformer import dev.openrs2.deob.transform.FieldOrderTransformer import dev.openrs2.deob.transform.FinalTransformer import dev.openrs2.deob.transform.InvokeSpecialTransformer +import dev.openrs2.deob.transform.MethodOrderTransformer import dev.openrs2.deob.transform.OpaquePredicateTransformer import dev.openrs2.deob.transform.OriginalNameTransformer import dev.openrs2.deob.transform.OverrideTransformer @@ -170,6 +171,7 @@ class Deobfuscator(private val input: Path, private val output: Path) { InvokeSpecialTransformer(), StaticScramblingTransformer(), EmptyClassTransformer(), + MethodOrderTransformer(), VisibilityTransformer(), OverrideTransformer() ) diff --git a/deob/src/main/java/dev/openrs2/deob/transform/MethodOrderTransformer.kt b/deob/src/main/java/dev/openrs2/deob/transform/MethodOrderTransformer.kt new file mode 100644 index 00000000..d47464b1 --- /dev/null +++ b/deob/src/main/java/dev/openrs2/deob/transform/MethodOrderTransformer.kt @@ -0,0 +1,37 @@ +package dev.openrs2.deob.transform + +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.MethodNode + +class MethodOrderTransformer : Transformer() { + override fun transformClass(classPath: ClassPath, library: Library, clazz: ClassNode): Boolean { + clazz.methods.sortWith(STATIC_COMPARATOR.then(INIT_COMPARATOR)) + return false + } + + companion object { + 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 val INIT_COMPARATOR = Comparator { a, b -> + val aInit = a.name.startsWith('<') + val bInit = b.name.startsWith('<') + when { + aInit && !bInit -> -1 + !aInit && bInit -> 1 + else -> 0 + } + } + } +}