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.
 
 
 
 
openrs2/asm/src/main/java/org/openrs2/asm/InsnListUtils.kt

53 lines
1.3 KiB

package org.openrs2.asm
import org.objectweb.asm.tree.AbstractInsnNode
import org.objectweb.asm.tree.InsnList
private val ANY_INSN = { _: AbstractInsnNode -> true }
public 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.isSequential && filter(insn))
return null
}
public 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
}
public 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
}