From 3c9edb0291794eb496ccae5fd438f7237c9e3b8e Mon Sep 17 00:00:00 2001 From: Graham Date: Wed, 20 May 2020 20:11:00 +0100 Subject: [PATCH] Use dependency injection in the AST deobfuscator Signed-off-by: Graham --- deob-ast/build.gradle.kts | 1 + .../dev/openrs2/deob/ast/AstDeobfuscator.kt | 44 ++++--------------- .../openrs2/deob/ast/AstDeobfuscatorModule.kt | 41 +++++++++++++++++ .../openrs2/deob/ast/DeobfuscateAstCommand.kt | 6 ++- .../deob/ast/transform/AddSubTransformer.kt | 2 + .../transform/BinaryExprOrderTransformer.kt | 2 + .../deob/ast/transform/BitMaskTransformer.kt | 2 + .../ast/transform/ComplementTransformer.kt | 2 + .../deob/ast/transform/EncloseTransformer.kt | 2 + .../transform/ForLoopConditionTransformer.kt | 2 + .../deob/ast/transform/GlTransformer.kt | 2 + .../deob/ast/transform/IdentityTransformer.kt | 2 + .../deob/ast/transform/IfElseTransformer.kt | 2 + .../ast/transform/IncrementTransformer.kt | 2 + .../transform/NegativeLiteralTransformer.kt | 2 + .../ast/transform/NewInstanceTransformer.kt | 2 + .../deob/ast/transform/TernaryTransformer.kt | 2 + .../ast/transform/UnencloseTransformer.kt | 2 + .../deob/ast/transform/ValueOfTransformer.kt | 2 + 19 files changed, 85 insertions(+), 37 deletions(-) create mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscatorModule.kt diff --git a/deob-ast/build.gradle.kts b/deob-ast/build.gradle.kts index 0eb71368..f4bdfc64 100644 --- a/deob-ast/build.gradle.kts +++ b/deob-ast/build.gradle.kts @@ -14,6 +14,7 @@ dependencies { implementation(project(":util")) implementation("com.github.javaparser:javaparser-symbol-solver-core:${Versions.javaParser}") implementation("com.google.guava:guava:${Versions.guava}") + implementation("com.google.inject:guice:${Versions.guice}") implementation("org.jdom:jdom2:${Versions.jdom}") } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt index 5298744f..36bfd35a 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt @@ -10,26 +10,17 @@ import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeS import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver import com.github.javaparser.utils.SourceRoot import com.github.michaelbull.logging.InlineLogger -import dev.openrs2.deob.ast.transform.AddSubTransformer -import dev.openrs2.deob.ast.transform.BinaryExprOrderTransformer -import dev.openrs2.deob.ast.transform.BitMaskTransformer -import dev.openrs2.deob.ast.transform.ComplementTransformer -import dev.openrs2.deob.ast.transform.EncloseTransformer -import dev.openrs2.deob.ast.transform.ForLoopConditionTransformer -import dev.openrs2.deob.ast.transform.GlTransformer -import dev.openrs2.deob.ast.transform.IdentityTransformer -import dev.openrs2.deob.ast.transform.IfElseTransformer -import dev.openrs2.deob.ast.transform.IncrementTransformer -import dev.openrs2.deob.ast.transform.NegativeLiteralTransformer -import dev.openrs2.deob.ast.transform.NewInstanceTransformer -import dev.openrs2.deob.ast.transform.TernaryTransformer -import dev.openrs2.deob.ast.transform.UnencloseTransformer -import dev.openrs2.deob.ast.transform.ValueOfTransformer +import dev.openrs2.deob.ast.transform.Transformer import java.nio.file.Path import java.util.function.Function +import javax.inject.Inject +import javax.inject.Singleton -class AstDeobfuscator(private val modules: List) { - fun run() { +@Singleton +class AstDeobfuscator @Inject constructor( + private val transformers: Set<@JvmSuppressWildcards Transformer> +) { + fun run(modules: List) { val solver = CombinedTypeSolver(ReflectionTypeSolver(true)) for (module in modules) { solver.add(JavaParserTypeSolver(module)) @@ -63,7 +54,7 @@ class AstDeobfuscator(private val modules: List) { } } - for (transformer in TRANSFORMERS) { + for (transformer in transformers) { logger.info { "Running transformer ${transformer.javaClass.simpleName}" } transformer.transform(units) } @@ -78,23 +69,6 @@ class AstDeobfuscator(private val modules: List) { private companion object { private val logger = InlineLogger() - private val TRANSFORMERS = listOf( - UnencloseTransformer(), - NegativeLiteralTransformer(), - ComplementTransformer(), - IfElseTransformer(), - TernaryTransformer(), - BinaryExprOrderTransformer(), - AddSubTransformer(), - IdentityTransformer(), - BitMaskTransformer(), - ValueOfTransformer(), - NewInstanceTransformer(), - IncrementTransformer(), - ForLoopConditionTransformer(), - GlTransformer(), - EncloseTransformer() - ) private val PC_ANNOTATION_REGEX = Regex("@Pc\\(([0-9]+)\\)\\s+") private fun stripNewlineAfterPcAnnotation(s: String): String { diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscatorModule.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscatorModule.kt new file mode 100644 index 00000000..85e19018 --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscatorModule.kt @@ -0,0 +1,41 @@ +package dev.openrs2.deob.ast + +import com.google.inject.AbstractModule +import com.google.inject.multibindings.Multibinder +import dev.openrs2.deob.ast.transform.AddSubTransformer +import dev.openrs2.deob.ast.transform.BinaryExprOrderTransformer +import dev.openrs2.deob.ast.transform.BitMaskTransformer +import dev.openrs2.deob.ast.transform.ComplementTransformer +import dev.openrs2.deob.ast.transform.EncloseTransformer +import dev.openrs2.deob.ast.transform.ForLoopConditionTransformer +import dev.openrs2.deob.ast.transform.GlTransformer +import dev.openrs2.deob.ast.transform.IdentityTransformer +import dev.openrs2.deob.ast.transform.IfElseTransformer +import dev.openrs2.deob.ast.transform.IncrementTransformer +import dev.openrs2.deob.ast.transform.NegativeLiteralTransformer +import dev.openrs2.deob.ast.transform.NewInstanceTransformer +import dev.openrs2.deob.ast.transform.TernaryTransformer +import dev.openrs2.deob.ast.transform.Transformer +import dev.openrs2.deob.ast.transform.UnencloseTransformer +import dev.openrs2.deob.ast.transform.ValueOfTransformer + +object AstDeobfuscatorModule : AbstractModule() { + override fun configure() { + val binder = Multibinder.newSetBinder(binder(), Transformer::class.java) + binder.addBinding().to(UnencloseTransformer::class.java) + binder.addBinding().to(NegativeLiteralTransformer::class.java) + binder.addBinding().to(ComplementTransformer::class.java) + binder.addBinding().to(IfElseTransformer::class.java) + binder.addBinding().to(TernaryTransformer::class.java) + binder.addBinding().to(BinaryExprOrderTransformer::class.java) + binder.addBinding().to(AddSubTransformer::class.java) + binder.addBinding().to(IdentityTransformer::class.java) + binder.addBinding().to(BitMaskTransformer::class.java) + binder.addBinding().to(ValueOfTransformer::class.java) + binder.addBinding().to(NewInstanceTransformer::class.java) + binder.addBinding().to(IncrementTransformer::class.java) + binder.addBinding().to(ForLoopConditionTransformer::class.java) + binder.addBinding().to(GlTransformer::class.java) + binder.addBinding().to(EncloseTransformer::class.java) + } +} diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/DeobfuscateAstCommand.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/DeobfuscateAstCommand.kt index c1438361..7054a78c 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/DeobfuscateAstCommand.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/DeobfuscateAstCommand.kt @@ -1,13 +1,16 @@ package dev.openrs2.deob.ast import com.github.ajalt.clikt.core.CliktCommand +import com.google.inject.Guice import java.nio.file.Paths fun main(args: Array) = DeobfuscateAstCommand().main(args) class DeobfuscateAstCommand : CliktCommand(name = "deob-ast") { override fun run() { - val deobfuscator = AstDeobfuscator( + val injector = Guice.createInjector(AstDeobfuscatorModule) + val deobfuscator = injector.getInstance(AstDeobfuscator::class.java) + deobfuscator.run( listOf( Paths.get("nonfree/client/src/main/java"), Paths.get("nonfree/gl/src/main/java"), @@ -17,6 +20,5 @@ class DeobfuscateAstCommand : CliktCommand(name = "deob-ast") { Paths.get("nonfree/unpackclass/src/main/java") ) ) - deobfuscator.run() } } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt index 57c257ff..89c28a89 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt @@ -10,7 +10,9 @@ import dev.openrs2.deob.ast.util.hasSideEffects import dev.openrs2.deob.ast.util.isString import dev.openrs2.deob.ast.util.negate import dev.openrs2.deob.ast.util.walk +import javax.inject.Singleton +@Singleton class AddSubTransformer : Transformer() { override fun transformUnit( units: Map, diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt index 1b752440..7cb9ad0f 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt @@ -4,7 +4,9 @@ import com.github.javaparser.ast.CompilationUnit import com.github.javaparser.ast.expr.BinaryExpr import dev.openrs2.deob.ast.util.isString import dev.openrs2.deob.ast.util.walk +import javax.inject.Singleton +@Singleton class BinaryExprOrderTransformer : Transformer() { override fun transformUnit( units: Map, diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt index 657e4426..1dfd5431 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt @@ -8,7 +8,9 @@ import dev.openrs2.deob.ast.util.checkedAsLong import dev.openrs2.deob.ast.util.isIntegerOrLongLiteral import dev.openrs2.deob.ast.util.toLongLiteralExpr import dev.openrs2.deob.ast.util.walk +import javax.inject.Singleton +@Singleton class BitMaskTransformer : Transformer() { override fun transformUnit( units: Map, diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt index 78cc0b01..3516cf42 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt @@ -10,7 +10,9 @@ import dev.openrs2.deob.ast.util.checkedAsLong import dev.openrs2.deob.ast.util.isIntegerOrLongLiteral import dev.openrs2.deob.ast.util.toLongLiteralExpr import dev.openrs2.deob.ast.util.walk +import javax.inject.Singleton +@Singleton class ComplementTransformer : Transformer() { override fun transformUnit( units: Map, diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.kt index 58ee8b38..52b43ed8 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.kt @@ -5,7 +5,9 @@ import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.expr.EnclosedExpr import com.github.javaparser.ast.expr.Expression import dev.openrs2.deob.ast.util.walk +import javax.inject.Singleton +@Singleton class EncloseTransformer : Transformer() { private enum class Associativity { LEFT, diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt index 9390f658..7efbd604 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt @@ -5,7 +5,9 @@ import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.stmt.ForStmt import dev.openrs2.deob.ast.util.hasSideEffects import dev.openrs2.deob.ast.util.walk +import javax.inject.Singleton +@Singleton class ForLoopConditionTransformer : Transformer() { override fun transformUnit( units: Map, diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/GlTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/GlTransformer.kt index 5ac5fa21..079a621b 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/GlTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/GlTransformer.kt @@ -25,7 +25,9 @@ import dev.openrs2.deob.ast.gl.GlParameter import dev.openrs2.deob.ast.gl.GlRegistry import dev.openrs2.deob.ast.util.checkedAsInt import dev.openrs2.deob.ast.util.walk +import javax.inject.Singleton +@Singleton class GlTransformer(private val registry: GlRegistry = GlRegistry.parse()) : Transformer() { private val enums = mutableSetOf() diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt index 3635ef9a..9487ce83 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt @@ -5,7 +5,9 @@ import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.expr.Expression import com.github.javaparser.ast.expr.UnaryExpr import dev.openrs2.deob.ast.util.walk +import javax.inject.Singleton +@Singleton class IdentityTransformer : Transformer() { override fun transformUnit( units: Map, diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt index 234ecd6d..d55d1bd6 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt @@ -7,7 +7,9 @@ import com.github.javaparser.ast.stmt.Statement import dev.openrs2.deob.ast.util.countNots import dev.openrs2.deob.ast.util.not import dev.openrs2.deob.ast.util.walk +import javax.inject.Singleton +@Singleton class IfElseTransformer : Transformer() { override fun transformUnit( units: Map, diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt index 485eeb3e..652d7385 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt @@ -5,7 +5,9 @@ import com.github.javaparser.ast.expr.UnaryExpr import com.github.javaparser.ast.stmt.ExpressionStmt import com.github.javaparser.ast.stmt.ForStmt import dev.openrs2.deob.ast.util.walk +import javax.inject.Singleton +@Singleton class IncrementTransformer : Transformer() { override fun transformUnit( units: Map, diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt index 33b04215..aa9ed344 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt @@ -5,7 +5,9 @@ import com.github.javaparser.ast.expr.UnaryExpr import dev.openrs2.deob.ast.util.isIntegerOrLongLiteral import dev.openrs2.deob.ast.util.negate import dev.openrs2.deob.ast.util.walk +import javax.inject.Singleton +@Singleton class NegativeLiteralTransformer : Transformer() { override fun transformUnit( units: Map, diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt index 10e9af88..c71b773f 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt @@ -3,7 +3,9 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit import com.github.javaparser.ast.expr.MethodCallExpr import dev.openrs2.deob.ast.util.walk +import javax.inject.Singleton +@Singleton class NewInstanceTransformer : Transformer() { override fun transformUnit( units: Map, diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt index d743509e..c768808c 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt @@ -5,7 +5,9 @@ import com.github.javaparser.ast.expr.ConditionalExpr import dev.openrs2.deob.ast.util.countNots import dev.openrs2.deob.ast.util.not import dev.openrs2.deob.ast.util.walk +import javax.inject.Singleton +@Singleton class TernaryTransformer : Transformer() { override fun transformUnit( units: Map, diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.kt index 3bc57dcd..a7ee9d37 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.kt @@ -3,7 +3,9 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit import com.github.javaparser.ast.expr.EnclosedExpr import dev.openrs2.deob.ast.util.walk +import javax.inject.Singleton +@Singleton class UnencloseTransformer : Transformer() { override fun transformUnit( units: Map, diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ValueOfTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ValueOfTransformer.kt index 7fa658ab..3ad0fe6d 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ValueOfTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ValueOfTransformer.kt @@ -5,7 +5,9 @@ import com.github.javaparser.ast.expr.MethodCallExpr import com.github.javaparser.ast.expr.ObjectCreationExpr import com.github.javaparser.ast.expr.TypeExpr import dev.openrs2.deob.ast.util.walk +import javax.inject.Singleton +@Singleton class ValueOfTransformer : Transformer() { override fun transformUnit( units: Map,