Use dependency injection in the AST deobfuscator

Signed-off-by: Graham <gpe@openrs2.dev>
Graham 5 years ago
parent 5c8b031719
commit 3c9edb0291
  1. 1
      deob-ast/build.gradle.kts
  2. 44
      deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt
  3. 41
      deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscatorModule.kt
  4. 6
      deob-ast/src/main/java/dev/openrs2/deob/ast/DeobfuscateAstCommand.kt
  5. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt
  6. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt
  7. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt
  8. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt
  9. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.kt
  10. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt
  11. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/GlTransformer.kt
  12. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt
  13. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt
  14. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt
  15. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt
  16. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt
  17. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt
  18. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.kt
  19. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ValueOfTransformer.kt

@ -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}")
}

@ -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<Path>) {
fun run() {
@Singleton
class AstDeobfuscator @Inject constructor(
private val transformers: Set<@JvmSuppressWildcards Transformer>
) {
fun run(modules: List<Path>) {
val solver = CombinedTypeSolver(ReflectionTypeSolver(true))
for (module in modules) {
solver.add(JavaParserTypeSolver(module))
@ -63,7 +54,7 @@ class AstDeobfuscator(private val modules: List<Path>) {
}
}
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<Path>) {
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 {

@ -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)
}
}

@ -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<String>) = 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()
}
}

@ -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<String, CompilationUnit>,

@ -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<String, CompilationUnit>,

@ -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<String, CompilationUnit>,

@ -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<String, CompilationUnit>,

@ -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,

@ -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<String, CompilationUnit>,

@ -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<GlEnum>()

@ -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<String, CompilationUnit>,

@ -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<String, CompilationUnit>,

@ -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<String, CompilationUnit>,

@ -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<String, CompilationUnit>,

@ -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<String, CompilationUnit>,

@ -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<String, CompilationUnit>,

@ -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<String, CompilationUnit>,

@ -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<String, CompilationUnit>,

Loading…
Cancel
Save