diff --git a/deob-ast/build.gradle.kts b/deob-ast/build.gradle.kts index 0eb713680c..f4bdfc6425 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 5298744f26..36bfd35a0d 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 0000000000..85e1901813 --- /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 c143836165..7054a78c3e 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 57c257ff83..89c28a8923 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 1b75244074..7cb9ad0f87 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 657e442603..1dfd54313a 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 78cc0b0165..3516cf4287 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 58ee8b3880..52b43ed8a7 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 9390f658ce..7efbd60402 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 5ac5fa216c..079a621b57 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 3635ef9acb..9487ce836f 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 234ecd6d0d..d55d1bd62e 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 485eeb3eda..652d7385c0 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 33b042150b..aa9ed344ce 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 10e9af88bb..c71b773f24 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 d743509efa..c768808cf9 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 3bc57dcd09..a7ee9d375b 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 7fa658ab25..3ad0fe6df3 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,