From aff58e5e73d33ef5111529ad5752a1a999730ed6 Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 27 Aug 2022 09:45:00 +0100 Subject: [PATCH] Split FinalTransformer into Final{Class,Method}Transformer Signed-off-by: Graham --- .../bytecode/BytecodeDeobfuscatorModule.kt | 6 +- .../transform/FinalClassTransformer.kt | 63 +++++++++++++++++++ ...ansformer.kt => FinalMethodTransformer.kt} | 43 +------------ 3 files changed, 69 insertions(+), 43 deletions(-) create mode 100644 deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/transform/FinalClassTransformer.kt rename deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/transform/{FinalTransformer.kt => FinalMethodTransformer.kt} (64%) diff --git a/deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/BytecodeDeobfuscatorModule.kt b/deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/BytecodeDeobfuscatorModule.kt index 701a8d64..132c0f9a 100644 --- a/deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/BytecodeDeobfuscatorModule.kt +++ b/deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/BytecodeDeobfuscatorModule.kt @@ -15,8 +15,9 @@ import org.openrs2.deob.bytecode.transform.EmptyClassTransformer import org.openrs2.deob.bytecode.transform.ExceptionTracingTransformer import org.openrs2.deob.bytecode.transform.FernflowerExceptionTransformer import org.openrs2.deob.bytecode.transform.FieldOrderTransformer +import org.openrs2.deob.bytecode.transform.FinalClassTransformer import org.openrs2.deob.bytecode.transform.FinalFieldTransformer -import org.openrs2.deob.bytecode.transform.FinalTransformer +import org.openrs2.deob.bytecode.transform.FinalMethodTransformer import org.openrs2.deob.bytecode.transform.InvokeSpecialTransformer import org.openrs2.deob.bytecode.transform.MethodOrderTransformer import org.openrs2.deob.bytecode.transform.MonitorTransformer @@ -71,7 +72,8 @@ public object BytecodeDeobfuscatorModule : AbstractModule() { binder.addBinding().to(EmptyClassTransformer::class.java) binder.addBinding().to(MethodOrderTransformer::class.java) binder.addBinding().to(VisibilityTransformer::class.java) - binder.addBinding().to(FinalTransformer::class.java) + binder.addBinding().to(FinalClassTransformer::class.java) + binder.addBinding().to(FinalMethodTransformer::class.java) binder.addBinding().to(FinalFieldTransformer::class.java) binder.addBinding().to(OverrideTransformer::class.java) binder.addBinding().to(RedundantGotoTransformer::class.java) diff --git a/deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/transform/FinalClassTransformer.kt b/deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/transform/FinalClassTransformer.kt new file mode 100644 index 00000000..c1ac09bf --- /dev/null +++ b/deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/transform/FinalClassTransformer.kt @@ -0,0 +1,63 @@ +package org.openrs2.deob.bytecode.transform + +import com.github.michaelbull.logging.InlineLogger +import org.objectweb.asm.Opcodes +import org.objectweb.asm.tree.ClassNode +import org.openrs2.asm.classpath.ClassPath +import org.openrs2.asm.classpath.Library +import org.openrs2.asm.transform.Transformer +import javax.inject.Singleton + +@Singleton +public class FinalClassTransformer : Transformer() { + private val superClasses = mutableListOf() + + override fun preTransform(classPath: ClassPath) { + superClasses.clear() + } + + override fun transformClass(classPath: ClassPath, library: Library, clazz: ClassNode): Boolean { + val superClass = clazz.superName + if (superClass != null) { + superClasses += superClass + } + + superClasses.addAll(clazz.interfaces) + + return false + } + + private fun isClassFinal(clazz: ClassNode): Boolean { + if ((clazz.access and (Opcodes.ACC_ABSTRACT or Opcodes.ACC_INTERFACE)) != 0) { + return false + } + + return !superClasses.contains(clazz.name) + } + + override fun postTransform(classPath: ClassPath) { + var classesChanged = 0 + + for (library in classPath.libraries) { + for (clazz in library) { + val access = clazz.access + + if (isClassFinal(clazz)) { + clazz.access = access or Opcodes.ACC_FINAL + } else { + clazz.access = access and Opcodes.ACC_FINAL.inv() + } + + if (clazz.access != access) { + classesChanged++ + } + } + } + + logger.info { "Updated final modifier on $classesChanged classes" } + } + + private companion object { + private val logger = InlineLogger() + } +} diff --git a/deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/transform/FinalTransformer.kt b/deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/transform/FinalMethodTransformer.kt similarity index 64% rename from deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/transform/FinalTransformer.kt rename to deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/transform/FinalMethodTransformer.kt index 321f0c8b..1d5fce24 100644 --- a/deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/transform/FinalTransformer.kt +++ b/deob-bytecode/src/main/kotlin/org/openrs2/deob/bytecode/transform/FinalMethodTransformer.kt @@ -12,28 +12,15 @@ import org.openrs2.util.collect.DisjointSet import javax.inject.Singleton @Singleton -public class FinalTransformer : Transformer() { - private val superClasses = mutableListOf() +public class FinalMethodTransformer : Transformer() { private lateinit var inheritedMethodSets: DisjointSet private var methodsChanged = 0 override fun preTransform(classPath: ClassPath) { - superClasses.clear() inheritedMethodSets = classPath.createInheritedMethodSets() methodsChanged = 0 } - override fun transformClass(classPath: ClassPath, library: Library, clazz: ClassNode): Boolean { - val superClass = clazz.superName - if (superClass != null) { - superClasses += superClass - } - - superClasses.addAll(clazz.interfaces) - - return false - } - private fun isMethodFinal(classPath: ClassPath, clazz: ClassNode, method: MethodNode): Boolean { if (method.name == "") { return false @@ -83,34 +70,8 @@ public class FinalTransformer : Transformer() { return false } - private fun isClassFinal(clazz: ClassNode): Boolean { - if ((clazz.access and (Opcodes.ACC_ABSTRACT or Opcodes.ACC_INTERFACE)) != 0) { - return false - } - - return !superClasses.contains(clazz.name) - } - override fun postTransform(classPath: ClassPath) { - var classesChanged = 0 - - for (library in classPath.libraries) { - for (clazz in library) { - val access = clazz.access - - if (isClassFinal(clazz)) { - clazz.access = access or Opcodes.ACC_FINAL - } else { - clazz.access = access and Opcodes.ACC_FINAL.inv() - } - - if (clazz.access != access) { - classesChanged++ - } - } - } - - logger.info { "Updated final modifier on $classesChanged classes and $methodsChanged methods" } + logger.info { "Updated final modifier on $methodsChanged methods" } } private companion object {