Remove redundant casts

Signed-off-by: Graham <gpe@openrs2.dev>
Graham 4 years ago
parent 9a4401c678
commit d1b8fbd094
  1. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscatorModule.kt
  2. 83
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/RedundantCastTransformer.kt

@ -17,6 +17,7 @@ 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.RedundantCastTransformer
import dev.openrs2.deob.ast.transform.TernaryTransformer
import dev.openrs2.deob.ast.transform.Transformer
import dev.openrs2.deob.ast.transform.UnencloseTransformer
@ -42,6 +43,7 @@ object AstDeobfuscatorModule : AbstractModule() {
binder.addBinding().to(NewInstanceTransformer::class.java)
binder.addBinding().to(IncrementTransformer::class.java)
binder.addBinding().to(ForLoopConditionTransformer::class.java)
binder.addBinding().to(RedundantCastTransformer::class.java)
binder.addBinding().to(GlTransformer::class.java)
binder.addBinding().to(EncloseTransformer::class.java)
}

@ -0,0 +1,83 @@
package dev.openrs2.deob.ast.transform
import com.github.javaparser.ast.CompilationUnit
import com.github.javaparser.ast.expr.AssignExpr
import com.github.javaparser.ast.expr.CastExpr
import com.github.javaparser.ast.expr.Expression
import com.github.javaparser.ast.expr.MethodCallExpr
import com.github.javaparser.ast.expr.NullLiteralExpr
import com.github.javaparser.ast.expr.ObjectCreationExpr
import com.github.javaparser.ast.expr.VariableDeclarationExpr
import com.github.javaparser.resolution.MethodAmbiguityException
import dev.openrs2.deob.ast.Library
import dev.openrs2.deob.ast.LibraryGroup
import dev.openrs2.deob.ast.util.walk
class RedundantCastTransformer : Transformer() {
override fun transformUnit(group: LibraryGroup, library: Library, unit: CompilationUnit) {
// remove double casts
unit.walk { expr: CastExpr ->
val innerExpr = expr.expression
if (innerExpr is CastExpr && expr.type == innerExpr.type) {
expr.expression = innerExpr.expression.clone()
}
}
// remove null argument casts if the call remains unambiguous
unit.walk { expr: MethodCallExpr ->
for (i in expr.arguments.indices) {
val arg = expr.arguments[i]
if (!isCastedNull(arg)) {
continue
}
expr.arguments[i] = NullLiteralExpr()
try {
expr.resolve()
} catch (ex: MethodAmbiguityException) {
expr.arguments[i] = arg
}
}
}
unit.walk { expr: ObjectCreationExpr ->
for (i in expr.arguments.indices) {
val arg = expr.arguments[i]
if (!isCastedNull(arg)) {
continue
}
expr.arguments[i] = NullLiteralExpr()
try {
expr.resolve()
} catch (ex: MethodAmbiguityException) {
expr.arguments[i] = arg
}
}
}
// remove null assignment casts
unit.walk { expr: VariableDeclarationExpr ->
for (variable in expr.variables) {
variable.initializer.ifPresent { initializer ->
if (isCastedNull(initializer)) {
initializer.replace(NullLiteralExpr())
}
}
}
}
unit.walk { expr: AssignExpr ->
if (isCastedNull(expr.value)) {
expr.value = NullLiteralExpr()
}
}
}
private fun isCastedNull(expr: Expression): Boolean {
if (expr !is CastExpr) {
return false
}
return expr.expression is NullLiteralExpr
}
}
Loading…
Cancel
Save