diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt index 99592dd0f2..ca15f65e1d 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt @@ -3,6 +3,8 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.expr.Expression +import com.github.javaparser.ast.expr.IntegerLiteralExpr +import com.github.javaparser.ast.expr.LongLiteralExpr import com.github.javaparser.ast.expr.UnaryExpr import dev.openrs2.deob.ast.util.hasSideEffects import dev.openrs2.deob.ast.util.isString @@ -45,8 +47,16 @@ class AddSubTransformer : Transformer() { private fun Expression.isNegative(): Boolean { return when { isUnaryExpr -> asUnaryExpr().operator == UnaryExpr.Operator.MINUS - isIntegerLiteralExpr -> asIntegerLiteralExpr().asInt() < 0 - isLongLiteralExpr -> asLongLiteralExpr().asLong() < 0 + isIntegerLiteralExpr -> when (val n = asIntegerLiteralExpr().asNumber()) { + IntegerLiteralExpr.MAX_31_BIT_UNSIGNED_VALUE_AS_LONG -> false + is Int -> n < 0 + else -> error("Invalid IntegerLiteralExpr type") + } + isLongLiteralExpr -> when (val n = asLongLiteralExpr().asNumber()) { + LongLiteralExpr.MAX_63_BIT_UNSIGNED_VALUE_AS_BIG_INTEGER -> false + is Long -> n < 0 + else -> error("Invalid LongLiteralExpr type") + } else -> false } } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt index e8e1701ba3..1b01f4da35 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt @@ -3,6 +3,8 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.expr.IntegerLiteralExpr +import dev.openrs2.deob.ast.util.checkedAsInt +import dev.openrs2.deob.ast.util.checkedAsLong import dev.openrs2.deob.ast.util.createLong import dev.openrs2.deob.ast.util.isIntegerOrLongLiteral import dev.openrs2.deob.ast.util.walk @@ -27,9 +29,9 @@ class BitMaskTransformer : Transformer() { return@walk } - val shamt = shamtExpr.asIntegerLiteralExpr().asInt() + val shamt = shamtExpr.asIntegerLiteralExpr().checkedAsInt() if (maskExpr.isIntegerLiteralExpr) { - var mask = maskExpr.asIntegerLiteralExpr().asInt() + var mask = maskExpr.asIntegerLiteralExpr().checkedAsInt() mask = when (shiftOp) { BinaryExpr.Operator.SIGNED_RIGHT_SHIFT -> mask shr shamt @@ -37,9 +39,9 @@ class BitMaskTransformer : Transformer() { else -> error("Invalid shiftOp") } - maskExpr = IntegerLiteralExpr(mask) + maskExpr = IntegerLiteralExpr(mask.toString()) } else { - var mask = maskExpr.asLongLiteralExpr().asLong() + var mask = maskExpr.asLongLiteralExpr().checkedAsLong() mask = when (shiftOp) { BinaryExpr.Operator.SIGNED_RIGHT_SHIFT -> mask shr shamt diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt index 94778153e6..f80a9eac4f 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt @@ -5,6 +5,8 @@ import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.expr.Expression import com.github.javaparser.ast.expr.IntegerLiteralExpr import com.github.javaparser.ast.expr.UnaryExpr +import dev.openrs2.deob.ast.util.checkedAsInt +import dev.openrs2.deob.ast.util.checkedAsLong import dev.openrs2.deob.ast.util.createLong import dev.openrs2.deob.ast.util.isIntegerOrLongLiteral import dev.openrs2.deob.ast.util.walk @@ -49,8 +51,8 @@ class ComplementTransformer : Transformer() { private fun Expression.complement(): Expression { return when { isUnaryExpr -> asUnaryExpr().expression - isIntegerLiteralExpr -> IntegerLiteralExpr(asIntegerLiteralExpr().asInt().inv()) - isLongLiteralExpr -> createLong(asLongLiteralExpr().asLong().inv()) + isIntegerLiteralExpr -> IntegerLiteralExpr(asIntegerLiteralExpr().checkedAsInt().inv().toString()) + isLongLiteralExpr -> createLong(asLongLiteralExpr().checkedAsLong().inv()) else -> throw IllegalArgumentException() } } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.kt index 3b44aa872e..2017170938 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.kt @@ -7,6 +7,22 @@ import com.github.javaparser.ast.expr.IntegerLiteralExpr import com.github.javaparser.ast.expr.LongLiteralExpr import com.github.javaparser.ast.expr.UnaryExpr +fun IntegerLiteralExpr.checkedAsInt(): Int { + val n = asNumber() + if (n !is Int) { + error("Invalid IntegerLiteralExpr type") + } + return n +} + +fun LongLiteralExpr.checkedAsLong(): Long { + val n = asNumber() + if (n !is Long) { + error("Invalid LongLiteralExpr type") + } + return n +} + fun Expression.isIntegerOrLongLiteral(): Boolean { return isIntegerLiteralExpr || isLongLiteralExpr } @@ -19,9 +35,17 @@ fun Expression.negate(): Expression { return if (isUnaryExpr && asUnaryExpr().operator == UnaryExpr.Operator.MINUS) { asUnaryExpr().expression.clone() } else if (isIntegerLiteralExpr) { - IntegerLiteralExpr(-asIntegerLiteralExpr().asInt()) + when (val n = asIntegerLiteralExpr().asNumber()) { + IntegerLiteralExpr.MAX_31_BIT_UNSIGNED_VALUE_AS_LONG -> IntegerLiteralExpr(Integer.MIN_VALUE.toString()) + is Int -> IntegerLiteralExpr((-n.toInt()).toString()) + else -> error("Invalid IntegerLiteralExpr type") + } } else if (isLongLiteralExpr) { - createLong(-asLongLiteralExpr().asLong()) + when (val n = asLongLiteralExpr().asNumber()) { + LongLiteralExpr.MAX_63_BIT_UNSIGNED_VALUE_AS_BIG_INTEGER -> createLong(Long.MIN_VALUE) + is Long -> createLong(-n) + else -> error("Invalid LongLiteralExpr type") + } } else { throw IllegalArgumentException() }