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 fcfb3bc153..be62dd1bb2 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 @@ -15,6 +15,7 @@ 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.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 @@ -87,6 +88,7 @@ class AstDeobfuscator(private val modules: List) { BitMaskTransformer(), ValueOfTransformer(), NewInstanceTransformer(), + IncrementTransformer(), EncloseTransformer() ) } 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 new file mode 100644 index 0000000000..788b0a4322 --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt @@ -0,0 +1,40 @@ +package dev.openrs2.deob.ast.transform + +import com.github.javaparser.ast.CompilationUnit +import com.github.javaparser.ast.Node +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 + +class IncrementTransformer : Transformer() { + override fun transform(unit: CompilationUnit) { + unit.walk(Node.TreeTraversal.POSTORDER) { stmt: ExpressionStmt -> + if (!stmt.expression.isUnaryExpr) { + return@walk + } + + val unaryExpr = stmt.expression.asUnaryExpr() + unaryExpr.operator = prefixToPostfix(unaryExpr.operator) + } + + unit.walk(Node.TreeTraversal.POSTORDER) { stmt: ForStmt -> + stmt.update.forEach { + if (!it.isUnaryExpr) { + return@forEach + } + + val unaryExpr = it.asUnaryExpr() + unaryExpr.operator = prefixToPostfix(unaryExpr.operator) + } + } + } + + companion object { + fun prefixToPostfix(operator: UnaryExpr.Operator) = when (operator) { + UnaryExpr.Operator.PREFIX_INCREMENT -> UnaryExpr.Operator.POSTFIX_INCREMENT + UnaryExpr.Operator.PREFIX_DECREMENT -> UnaryExpr.Operator.POSTFIX_DECREMENT + else -> operator + } + } +}