Add TernaryTransformer

bzip2
Graham 5 years ago
parent eadea9231c
commit 12d3af48dd
  1. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.java
  2. 9
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.java
  3. 25
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.java
  4. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.java

@ -17,6 +17,7 @@ import dev.openrs2.deob.ast.transform.BinaryExprOrderTransformer;
import dev.openrs2.deob.ast.transform.ComplementTransformer; import dev.openrs2.deob.ast.transform.ComplementTransformer;
import dev.openrs2.deob.ast.transform.IfElseTransformer; import dev.openrs2.deob.ast.transform.IfElseTransformer;
import dev.openrs2.deob.ast.transform.NegativeLiteralTransformer; import dev.openrs2.deob.ast.transform.NegativeLiteralTransformer;
import dev.openrs2.deob.ast.transform.TernaryTransformer;
import dev.openrs2.deob.ast.transform.Transformer; import dev.openrs2.deob.ast.transform.Transformer;
public final class AstDeobfuscator { public final class AstDeobfuscator {
@ -24,6 +25,7 @@ public final class AstDeobfuscator {
new NegativeLiteralTransformer(), new NegativeLiteralTransformer(),
new ComplementTransformer(), new ComplementTransformer(),
new IfElseTransformer(), new IfElseTransformer(),
new TernaryTransformer(),
new BinaryExprOrderTransformer(), new BinaryExprOrderTransformer(),
new AddSubTransformer() new AddSubTransformer()
); );

@ -2,10 +2,9 @@ package dev.openrs2.deob.ast.transform;
import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.UnaryExpr;
import com.github.javaparser.ast.stmt.IfStmt; import com.github.javaparser.ast.stmt.IfStmt;
import com.github.javaparser.ast.stmt.Statement; import com.github.javaparser.ast.stmt.Statement;
import dev.openrs2.deob.ast.util.ExprUtils;
import dev.openrs2.deob.ast.visitor.NegateExprVisitor; import dev.openrs2.deob.ast.visitor.NegateExprVisitor;
public final class IfElseTransformer extends Transformer { public final class IfElseTransformer extends Transformer {
@ -36,10 +35,6 @@ public final class IfElseTransformer extends Transformer {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
private static boolean isNot(Expression expr) {
return expr.isUnaryExpr() && expr.asUnaryExpr().getOperator() == UnaryExpr.Operator.LOGICAL_COMPLEMENT;
}
@Override @Override
public void transform(CompilationUnit unit) { public void transform(CompilationUnit unit) {
unit.findAll(IfStmt.class).forEach(stmt -> { unit.findAll(IfStmt.class).forEach(stmt -> {
@ -63,7 +58,7 @@ public final class IfElseTransformer extends Transformer {
* Prefer if (a) over if (!a). We don't swap != as it makes * Prefer if (a) over if (!a). We don't swap != as it makes
* checking bitwise flags look worse. * checking bitwise flags look worse.
*/ */
if (isNot(condition)) { if (ExprUtils.isNot(condition)) {
stmt.setCondition(condition.accept(new NegateExprVisitor(), null)); stmt.setCondition(condition.accept(new NegateExprVisitor(), null));
stmt.setThenStmt(elseStmt); stmt.setThenStmt(elseStmt);
stmt.setElseStmt(thenStmt); stmt.setElseStmt(thenStmt);

@ -0,0 +1,25 @@
package dev.openrs2.deob.ast.transform;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.ConditionalExpr;
import dev.openrs2.deob.ast.util.ExprUtils;
import dev.openrs2.deob.ast.visitor.NegateExprVisitor;
public final class TernaryTransformer extends Transformer {
@Override
public void transform(CompilationUnit unit) {
unit.findAll(ConditionalExpr.class).forEach(expr -> {
var condition = expr.getCondition();
if (!ExprUtils.isNot(condition)) {
return;
}
var thenExpr = expr.getThenExpr();
var elseExpr = expr.getElseExpr();
expr.setCondition(condition.accept(new NegateExprVisitor(), null));
expr.setThenExpr(elseExpr.clone());
expr.setElseExpr(thenExpr.clone());
});
}
}

@ -14,6 +14,10 @@ public final class ExprUtils {
return new LongLiteralExpr(Long.toString(value).concat("L")); return new LongLiteralExpr(Long.toString(value).concat("L"));
} }
public static boolean isNot(Expression expr) {
return expr.isUnaryExpr() && expr.asUnaryExpr().getOperator() == UnaryExpr.Operator.LOGICAL_COMPLEMENT;
}
public static Expression negate(Expression expr) { public static Expression negate(Expression expr) {
if (expr.isUnaryExpr() && expr.asUnaryExpr().getOperator() == UnaryExpr.Operator.MINUS) { if (expr.isUnaryExpr() && expr.asUnaryExpr().getOperator() == UnaryExpr.Operator.MINUS) {
return expr.asUnaryExpr().getExpression().clone(); return expr.asUnaryExpr().getExpression().clone();

Loading…
Cancel
Save