Convert pre-increments to post-increments where possible

master
Graham 5 years ago
parent c2809a16f8
commit 7d1dc59d6a
  1. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt
  2. 40
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.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<Path>) {
BitMaskTransformer(),
ValueOfTransformer(),
NewInstanceTransformer(),
IncrementTransformer(),
EncloseTransformer()
)
}

@ -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
}
}
}
Loading…
Cancel
Save