From c2809a16f8fc5ebd62f10479c74a1acf0a43319c Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 9 Jan 2020 19:06:41 +0000 Subject: [PATCH] Add OverrideTransformer --- .../openrs2/asm/classpath/ClassMetadata.kt | 25 +++++++++ .../java/dev/openrs2/deob/Deobfuscator.kt | 4 +- .../deob/transform/OverrideTransformer.kt | 56 +++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 deob/src/main/java/dev/openrs2/deob/transform/OverrideTransformer.kt diff --git a/asm/src/main/java/dev/openrs2/asm/classpath/ClassMetadata.kt b/asm/src/main/java/dev/openrs2/asm/classpath/ClassMetadata.kt index 2574d50d..2116deba 100644 --- a/asm/src/main/java/dev/openrs2/asm/classpath/ClassMetadata.kt +++ b/asm/src/main/java/dev/openrs2/asm/classpath/ClassMetadata.kt @@ -13,6 +13,31 @@ abstract class ClassMetadata { abstract fun isNative(method: MemberDesc): Boolean + fun isOverride(method: MemberDesc): Boolean { + val superClass = this.superClass + if (superClass != null) { + if (superClass.methods.contains(method)) { + return true + } + + if (superClass.isOverride(method)) { + return true + } + } + + for (superInterface in superInterfaces) { + if (superInterface.methods.contains(method)) { + return true + } + + if (superInterface.isOverride(method)) { + return true + } + } + + return false + } + override fun equals(other: Any?): Boolean { if (this === other) return true if (other !is ClassMetadata) return false diff --git a/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt b/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt index 11349384..2ff5ba53 100644 --- a/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt +++ b/deob/src/main/java/dev/openrs2/deob/Deobfuscator.kt @@ -19,6 +19,7 @@ import dev.openrs2.deob.transform.ExceptionTracingTransformer import dev.openrs2.deob.transform.FieldOrderTransformer import dev.openrs2.deob.transform.OpaquePredicateTransformer import dev.openrs2.deob.transform.OriginalNameTransformer +import dev.openrs2.deob.transform.OverrideTransformer import dev.openrs2.deob.transform.RemapTransformer import dev.openrs2.deob.transform.ResetTransformer import dev.openrs2.deob.transform.UnusedArgTransformer @@ -157,7 +158,8 @@ class Deobfuscator(private val input: Path, private val output: Path) { DummyLocalTransformer(), UnusedArgTransformer(), ResetTransformer(), - AccessTransformer() + AccessTransformer(), + OverrideTransformer() ) } } diff --git a/deob/src/main/java/dev/openrs2/deob/transform/OverrideTransformer.kt b/deob/src/main/java/dev/openrs2/deob/transform/OverrideTransformer.kt new file mode 100644 index 00000000..7bae704b --- /dev/null +++ b/deob/src/main/java/dev/openrs2/deob/transform/OverrideTransformer.kt @@ -0,0 +1,56 @@ +package dev.openrs2.deob.transform + +import com.github.michaelbull.logging.InlineLogger +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.Type +import org.objectweb.asm.tree.AnnotationNode +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.MethodNode + +class OverrideTransformer : Transformer() { + private var overrides = 0 + + override fun preTransform(classPath: ClassPath) { + overrides = 0 + } + + override fun transformCode( + classPath: ClassPath, + library: Library, + clazz: ClassNode, + method: MethodNode + ): Boolean { + if (method.name == "" || method.name == "" || method.access and Opcodes.ACC_STATIC != 0) { + return false + } + + if (!classPath[clazz.name].isOverride(MemberDesc(method))) { + return false + } + + if (method.visibleAnnotations != null && method.visibleAnnotations.any { it.desc == OVERRIDE_DESC }) { + return false + } + + if (method.visibleAnnotations == null) { + method.visibleAnnotations = mutableListOf() + } + method.visibleAnnotations.add(AnnotationNode(OVERRIDE_DESC)) + overrides++ + + return false + } + + override fun postTransform(classPath: ClassPath) { + logger.info { "Added $overrides override annotations" } + } + + companion object { + val logger = InlineLogger() + val OVERRIDE_DESC: String = Type.getDescriptor(Override::class.java) + } +}