Open-source multiplayer game server compatible with the RuneScape client
https://www.openrs2.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.6 KiB
65 lines
2.6 KiB
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.Library
|
|
import dev.openrs2.deob.ast.LibraryGroup
|
|
import dev.openrs2.deob.ast.util.checkedAsInt
|
|
import dev.openrs2.deob.ast.util.checkedAsLong
|
|
import dev.openrs2.deob.ast.util.isIntegerOrLongLiteral
|
|
import dev.openrs2.deob.ast.util.toLongLiteralExpr
|
|
import dev.openrs2.deob.ast.util.walk
|
|
import javax.inject.Singleton
|
|
|
|
@Singleton
|
|
class ComplementTransformer : Transformer() {
|
|
override fun transformUnit(group: LibraryGroup, library: Library, unit: CompilationUnit) {
|
|
unit.walk { expr: BinaryExpr ->
|
|
val op = complement(expr.operator) ?: return@walk
|
|
|
|
val left = expr.left
|
|
val right = expr.right
|
|
val bothLiteral = left.isIntegerOrLongLiteral() && right.isIntegerOrLongLiteral()
|
|
|
|
if (left.isComplementOrLiteral() && right.isComplementOrLiteral() && !bothLiteral) {
|
|
expr.operator = op
|
|
expr.left = left.complement()
|
|
expr.right = right.complement()
|
|
}
|
|
}
|
|
}
|
|
|
|
private companion object {
|
|
private fun Expression.isComplement(): Boolean {
|
|
return this is UnaryExpr && operator == UnaryExpr.Operator.BITWISE_COMPLEMENT
|
|
}
|
|
|
|
private fun Expression.isComplementOrLiteral(): Boolean {
|
|
return isComplement() || isIntegerOrLongLiteral()
|
|
}
|
|
|
|
private fun complement(op: BinaryExpr.Operator): BinaryExpr.Operator? {
|
|
return when (op) {
|
|
BinaryExpr.Operator.EQUALS, BinaryExpr.Operator.NOT_EQUALS -> op
|
|
BinaryExpr.Operator.GREATER -> BinaryExpr.Operator.LESS
|
|
BinaryExpr.Operator.GREATER_EQUALS -> BinaryExpr.Operator.LESS_EQUALS
|
|
BinaryExpr.Operator.LESS -> BinaryExpr.Operator.GREATER
|
|
BinaryExpr.Operator.LESS_EQUALS -> BinaryExpr.Operator.GREATER_EQUALS
|
|
else -> null
|
|
}
|
|
}
|
|
|
|
private fun Expression.complement(): Expression {
|
|
return when (this) {
|
|
is UnaryExpr -> expression
|
|
is IntegerLiteralExpr -> IntegerLiteralExpr(checkedAsInt().inv().toString())
|
|
is LongLiteralExpr -> checkedAsLong().inv().toLongLiteralExpr()
|
|
else -> throw IllegalArgumentException()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|