Add initial BinaryExprOrderTransformer

master
Graham 5 years ago
parent 5e616b2d88
commit e9b2ad6d62
  1. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.java
  2. 48
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.java

@ -13,6 +13,7 @@ import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeS
import com.github.javaparser.utils.SourceRoot;
import com.google.common.collect.ImmutableList;
import dev.openrs2.deob.ast.transform.AddSubTransformer;
import dev.openrs2.deob.ast.transform.BinaryExprOrderTransformer;
import dev.openrs2.deob.ast.transform.ComplementTransformer;
import dev.openrs2.deob.ast.transform.IfElseTransformer;
import dev.openrs2.deob.ast.transform.Transformer;
@ -21,7 +22,8 @@ public final class AstDeobfuscator {
private static final ImmutableList<Transformer> TRANSFORMERS = ImmutableList.of(
new AddSubTransformer(),
new ComplementTransformer(),
new IfElseTransformer()
new IfElseTransformer(),
new BinaryExprOrderTransformer()
);
public static void main(String[] args) {

@ -0,0 +1,48 @@
package dev.openrs2.deob.ast.transform;
import java.util.Optional;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.BinaryExpr;
public final class BinaryExprOrderTransformer extends Transformer {
private static Optional<BinaryExpr.Operator> flip(BinaryExpr.Operator op) {
// TODO(gpe): handle the PLUS operator (we can't flip it if the type of the expression is String)
switch (op) {
case MULTIPLY:
case EQUALS:
case NOT_EQUALS:
case BINARY_AND:
case BINARY_OR:
case XOR:
case OR:
case AND:
return Optional.of(op);
case GREATER:
return Optional.of(BinaryExpr.Operator.LESS);
case GREATER_EQUALS:
return Optional.of(BinaryExpr.Operator.LESS_EQUALS);
case LESS:
return Optional.of(BinaryExpr.Operator.GREATER);
case LESS_EQUALS:
return Optional.of(BinaryExpr.Operator.GREATER_EQUALS);
default:
return Optional.empty();
}
}
@Override
public void transform(CompilationUnit unit) {
unit.findAll(BinaryExpr.class).forEach(expr -> {
flip(expr.getOperator()).ifPresent(op -> {
var left = expr.getLeft();
var right = expr.getRight();
if (left.isLiteralExpr() && !right.isLiteralExpr()) {
expr.setOperator(op);
expr.setLeft(right);
expr.setRight(left);
}
});
});
}
}
Loading…
Cancel
Save