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.
68 lines
2.0 KiB
68 lines
2.0 KiB
package org.openrs2.deob.bytecode.transform
|
|
|
|
import com.github.michaelbull.logging.InlineLogger
|
|
import jakarta.inject.Singleton
|
|
import org.objectweb.asm.Opcodes
|
|
import org.objectweb.asm.tree.AbstractInsnNode
|
|
import org.objectweb.asm.tree.ClassNode
|
|
import org.objectweb.asm.tree.MethodNode
|
|
import org.objectweb.asm.tree.VarInsnNode
|
|
import org.openrs2.asm.classpath.ClassPath
|
|
import org.openrs2.asm.classpath.Library
|
|
import org.openrs2.asm.deleteExpression
|
|
import org.openrs2.asm.isPure
|
|
import org.openrs2.asm.transform.Transformer
|
|
import org.openrs2.deob.bytecode.analysis.LiveVariableAnalyzer
|
|
|
|
@Singleton
|
|
public class UnusedLocalTransformer : Transformer() {
|
|
private var localsRemoved = 0
|
|
|
|
override fun preTransform(classPath: ClassPath) {
|
|
localsRemoved = 0
|
|
}
|
|
|
|
override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean {
|
|
val analyzer = LiveVariableAnalyzer(clazz.name, method)
|
|
analyzer.analyze()
|
|
|
|
val deadStores = mutableListOf<AbstractInsnNode>()
|
|
|
|
for (insn in method.instructions) {
|
|
if (insn !is VarInsnNode || !STORE_OPCODES.contains(insn.opcode)) {
|
|
continue
|
|
}
|
|
|
|
val live = analyzer.getInSet(insn)?.contains(insn.`var`) ?: false
|
|
if (live) {
|
|
continue
|
|
}
|
|
|
|
deadStores += insn
|
|
}
|
|
|
|
for (insn in deadStores) {
|
|
if (method.instructions.deleteExpression(insn, AbstractInsnNode::isPure)) {
|
|
localsRemoved++
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
override fun postTransform(classPath: ClassPath) {
|
|
logger.info { "Removed $localsRemoved unused local variables" }
|
|
}
|
|
|
|
private companion object {
|
|
private val logger = InlineLogger()
|
|
|
|
private val STORE_OPCODES = setOf(
|
|
Opcodes.ISTORE,
|
|
Opcodes.LSTORE,
|
|
Opcodes.FSTORE,
|
|
Opcodes.DSTORE,
|
|
Opcodes.ASTORE
|
|
)
|
|
}
|
|
}
|
|
|