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.
54 lines
1.4 KiB
54 lines
1.4 KiB
package dev.openrs2.asm
|
|
|
|
import org.objectweb.asm.tree.AbstractInsnNode
|
|
import org.objectweb.asm.tree.InsnList
|
|
|
|
private val ANY_INSN = { _: AbstractInsnNode -> true }
|
|
private val JUMP_OR_LABEL = listOf(AbstractInsnNode.LABEL, AbstractInsnNode.JUMP_INSN)
|
|
|
|
fun getExpression(
|
|
last: AbstractInsnNode,
|
|
filter: (AbstractInsnNode) -> Boolean = ANY_INSN
|
|
): List<AbstractInsnNode>? {
|
|
val expr = mutableListOf<AbstractInsnNode>()
|
|
|
|
var height = 0
|
|
var insn: AbstractInsnNode? = last
|
|
do {
|
|
val (pops, pushes) = insn!!.stackMetadata()
|
|
if (insn !== last) {
|
|
expr.add(insn)
|
|
height -= pushes
|
|
}
|
|
height += pops
|
|
|
|
if (height == 0) {
|
|
return expr.asReversed()
|
|
}
|
|
|
|
insn = insn.previous
|
|
} while (insn != null && insn.type !in JUMP_OR_LABEL && filter(insn))
|
|
|
|
return null
|
|
}
|
|
|
|
fun InsnList.replaceExpression(
|
|
last: AbstractInsnNode,
|
|
replacement: AbstractInsnNode,
|
|
filter: (AbstractInsnNode) -> Boolean = ANY_INSN
|
|
): Boolean {
|
|
val expr = getExpression(last, filter) ?: return false
|
|
expr.forEach(this::remove)
|
|
this[last] = replacement
|
|
return true
|
|
}
|
|
|
|
fun InsnList.deleteExpression(
|
|
last: AbstractInsnNode,
|
|
filter: (AbstractInsnNode) -> Boolean = ANY_INSN
|
|
): Boolean {
|
|
val expr = getExpression(last, filter) ?: return false
|
|
expr.forEach(this::remove)
|
|
remove(last)
|
|
return true
|
|
}
|
|
|