Flip > and >= in for loop conditions to < and <= respectively

Signed-off-by: Graham <gpe@openrs2.dev>
bzip2
Desetude 5 years ago committed by Graham
parent cac3191a4a
commit d8c3f4e31c
  1. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt
  2. 28
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt

@ -14,6 +14,7 @@ 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.IfElseTransformer
import dev.openrs2.deob.ast.transform.IncrementTransformer
import dev.openrs2.deob.ast.transform.NegativeLiteralTransformer
@ -89,6 +90,7 @@ class AstDeobfuscator(private val modules: List<Path>) {
ValueOfTransformer(),
NewInstanceTransformer(),
IncrementTransformer(),
ForLoopConditionTransformer(),
EncloseTransformer()
)
}

@ -0,0 +1,28 @@
package dev.openrs2.deob.ast.transform
import com.github.javaparser.ast.CompilationUnit
import com.github.javaparser.ast.Node
import com.github.javaparser.ast.expr.BinaryExpr
import com.github.javaparser.ast.stmt.ForStmt
import dev.openrs2.deob.ast.util.walk
class ForLoopConditionTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
unit.walk(Node.TreeTraversal.POSTORDER) { stmt: ForStmt ->
stmt.compare.ifPresent { compare ->
if (!compare.isBinaryExpr) {
return@ifPresent
}
val expr = compare.asBinaryExpr()
val flipped = when (expr.operator) {
BinaryExpr.Operator.GREATER -> BinaryExpr.Operator.LESS
BinaryExpr.Operator.GREATER_EQUALS -> BinaryExpr.Operator.LESS_EQUALS
else -> return@ifPresent
}
stmt.setCompare(BinaryExpr(expr.right, expr.left, flipped))
}
}
}
}
Loading…
Cancel
Save