Add NotTransformer

It's fairly similar to the ComplementTransformer, and translates
expressions like:

    !a == !b => a == b
    !a == b  => a != b

Signed-off-by: Graham <gpe@openrs2.dev>
Graham 4 years ago
parent b20115f1a7
commit 4bc11159d8
  1. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscatorModule.kt
  2. 62
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NotTransformer.kt

@ -19,6 +19,7 @@ import dev.openrs2.deob.ast.transform.IfElseTransformer
import dev.openrs2.deob.ast.transform.IncrementTransformer import dev.openrs2.deob.ast.transform.IncrementTransformer
import dev.openrs2.deob.ast.transform.NegativeLiteralTransformer import dev.openrs2.deob.ast.transform.NegativeLiteralTransformer
import dev.openrs2.deob.ast.transform.NewInstanceTransformer import dev.openrs2.deob.ast.transform.NewInstanceTransformer
import dev.openrs2.deob.ast.transform.NotTransformer
import dev.openrs2.deob.ast.transform.RedundantCastTransformer import dev.openrs2.deob.ast.transform.RedundantCastTransformer
import dev.openrs2.deob.ast.transform.TernaryTransformer import dev.openrs2.deob.ast.transform.TernaryTransformer
import dev.openrs2.deob.ast.transform.Transformer import dev.openrs2.deob.ast.transform.Transformer
@ -35,6 +36,7 @@ object AstDeobfuscatorModule : AbstractModule() {
binder.addBinding().to(UnencloseTransformer::class.java) binder.addBinding().to(UnencloseTransformer::class.java)
binder.addBinding().to(NegativeLiteralTransformer::class.java) binder.addBinding().to(NegativeLiteralTransformer::class.java)
binder.addBinding().to(ComplementTransformer::class.java) binder.addBinding().to(ComplementTransformer::class.java)
binder.addBinding().to(NotTransformer::class.java)
binder.addBinding().to(CharLiteralTransformer::class.java) binder.addBinding().to(CharLiteralTransformer::class.java)
binder.addBinding().to(IfElseTransformer::class.java) binder.addBinding().to(IfElseTransformer::class.java)
binder.addBinding().to(TernaryTransformer::class.java) binder.addBinding().to(TernaryTransformer::class.java)

@ -0,0 +1,62 @@
package dev.openrs2.deob.ast.transform
import com.github.javaparser.ast.CompilationUnit
import com.github.javaparser.ast.expr.BinaryExpr
import com.github.javaparser.ast.expr.BinaryExpr.Operator.EQUALS
import com.github.javaparser.ast.expr.BinaryExpr.Operator.NOT_EQUALS
import com.github.javaparser.ast.expr.BooleanLiteralExpr
import com.github.javaparser.ast.expr.Expression
import com.github.javaparser.ast.expr.UnaryExpr
import com.github.javaparser.ast.expr.UnaryExpr.Operator.LOGICAL_COMPLEMENT
import dev.openrs2.deob.ast.Library
import dev.openrs2.deob.ast.LibraryGroup
import dev.openrs2.deob.ast.util.not
import dev.openrs2.deob.ast.util.walk
import javax.inject.Singleton
@Singleton
class NotTransformer : Transformer() {
override fun transformUnit(group: LibraryGroup, library: Library, unit: CompilationUnit) {
unit.walk { expr: BinaryExpr ->
val op = expr.operator.flip() ?: return@walk
val left = expr.left
val right = expr.right
val bothLiteral = left is BooleanLiteralExpr && right is BooleanLiteralExpr
if (bothLiteral) {
return@walk
}
val leftNotOrLiteral = left.isNotOrLiteral()
val rightNotOrLiteral = right.isNotOrLiteral()
if (leftNotOrLiteral && rightNotOrLiteral) {
expr.left = left.not()
expr.right = right.not()
return@walk
} else if (leftNotOrLiteral) {
expr.operator = op
expr.left = left.not()
} else if (rightNotOrLiteral) {
expr.operator = op
expr.right = right.not()
}
}
}
private fun Expression.isNot(): Boolean {
return this is UnaryExpr && operator == LOGICAL_COMPLEMENT
}
private fun Expression.isNotOrLiteral(): Boolean {
return isNot() || this is BooleanLiteralExpr
}
private fun BinaryExpr.Operator.flip(): BinaryExpr.Operator? {
return when (this) {
EQUALS -> NOT_EQUALS
NOT_EQUALS -> EQUALS
else -> null
}
}
}
Loading…
Cancel
Save