forked from openrs2/openrs2
parent
6464f8dc77
commit
be14ffdc2d
@ -0,0 +1,6 @@ |
||||
package dev.openrs2.asm |
||||
|
||||
import org.objectweb.asm.ClassVisitor |
||||
import org.objectweb.asm.Opcodes |
||||
|
||||
object NopClassVisitor : ClassVisitor(Opcodes.ASM7) |
@ -0,0 +1,37 @@ |
||||
package dev.openrs2.deob |
||||
|
||||
import org.objectweb.asm.Attribute |
||||
import org.objectweb.asm.ByteVector |
||||
import org.objectweb.asm.ClassWriter |
||||
import org.objectweb.asm.Label |
||||
import org.objectweb.asm.tree.LabelNode |
||||
|
||||
class OriginalPcTable(private val pcs: Map<LabelNode, Int>) : Attribute("OriginalPcTable") { |
||||
override fun isCodeAttribute(): Boolean { |
||||
return true |
||||
} |
||||
|
||||
override fun isUnknown(): Boolean { |
||||
return false |
||||
} |
||||
|
||||
override fun getLabels(): Array<Label> { |
||||
return pcs.keys.map(LabelNode::getLabel).toTypedArray() |
||||
} |
||||
|
||||
override fun write( |
||||
classWriter: ClassWriter, |
||||
code: ByteArray?, |
||||
codeLength: Int, |
||||
maxStack: Int, |
||||
maxLocals: Int |
||||
): ByteVector { |
||||
val buf = ByteVector() |
||||
buf.putShort(pcs.size) |
||||
for ((label, pc) in pcs) { |
||||
buf.putShort(label.label.offset) |
||||
buf.putShort(pc) |
||||
} |
||||
return buf |
||||
} |
||||
} |
@ -0,0 +1,55 @@ |
||||
package dev.openrs2.deob.transform |
||||
|
||||
import com.github.michaelbull.logging.InlineLogger |
||||
import dev.openrs2.asm.classpath.ClassPath |
||||
import dev.openrs2.asm.classpath.Library |
||||
import dev.openrs2.asm.removeDeadCode |
||||
import dev.openrs2.asm.transform.Transformer |
||||
import dev.openrs2.deob.OriginalPcTable |
||||
import org.objectweb.asm.tree.ClassNode |
||||
import org.objectweb.asm.tree.LabelNode |
||||
import org.objectweb.asm.tree.MethodNode |
||||
|
||||
class OriginalPcRestoreTransformer : Transformer() { |
||||
private var originalPcsRestored = 0 |
||||
|
||||
override fun preTransform(classPath: ClassPath) { |
||||
originalPcsRestored = 0 |
||||
} |
||||
|
||||
override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean { |
||||
method.removeDeadCode(clazz.name) |
||||
|
||||
val pcs = mutableMapOf<LabelNode, Int>() |
||||
|
||||
for (insn in method.instructions) { |
||||
if (insn.opcode == -1) { |
||||
continue |
||||
} |
||||
|
||||
val originalPc = classPath.originalPcs[insn] ?: continue |
||||
|
||||
val label = LabelNode() |
||||
method.instructions.insertBefore(insn, label) |
||||
|
||||
pcs[label] = originalPc |
||||
|
||||
originalPcsRestored++ |
||||
} |
||||
|
||||
if (method.attrs == null) { |
||||
method.attrs = mutableListOf() |
||||
} |
||||
method.attrs.add(OriginalPcTable(pcs)) |
||||
|
||||
return false |
||||
} |
||||
|
||||
override fun postTransform(classPath: ClassPath) { |
||||
logger.info { "Restored $originalPcsRestored original instruction indexes" } |
||||
} |
||||
|
||||
companion object { |
||||
private val logger = InlineLogger() |
||||
} |
||||
} |
@ -0,0 +1,28 @@ |
||||
package dev.openrs2.deob.transform |
||||
|
||||
import com.github.michaelbull.logging.InlineLogger |
||||
import dev.openrs2.asm.classpath.ClassPath |
||||
import dev.openrs2.asm.classpath.Library |
||||
import dev.openrs2.asm.transform.Transformer |
||||
import org.objectweb.asm.tree.ClassNode |
||||
import org.objectweb.asm.tree.MethodNode |
||||
|
||||
class OriginalPcSaveTransformer : Transformer() { |
||||
override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean { |
||||
for ((pc, insn) in method.instructions.withIndex()) { |
||||
if (insn.opcode != -1) { |
||||
classPath.originalPcs[insn] = pc |
||||
} |
||||
} |
||||
|
||||
return false |
||||
} |
||||
|
||||
override fun postTransform(classPath: ClassPath) { |
||||
logger.info { "Saved ${classPath.originalPcs.size} original instruction indexes" } |
||||
} |
||||
|
||||
companion object { |
||||
private val logger = InlineLogger() |
||||
} |
||||
} |
Loading…
Reference in new issue