From 113820a9a272de0965ce0e5866091596955d7870 Mon Sep 17 00:00:00 2001 From: Graham Date: Fri, 22 May 2020 18:03:49 +0100 Subject: [PATCH] Add ExtendedRemapper type with support for moving fields/methods Signed-off-by: Graham --- .../java/dev/openrs2/asm/ClassNodeRemapper.kt | 10 +++--- .../dev/openrs2/asm/classpath/ClassPath.kt | 3 +- .../openrs2/asm/classpath/ExtendedRemapper.kt | 13 ++++++++ .../java/dev/openrs2/asm/classpath/Library.kt | 3 +- .../dev/openrs2/deob/remap/PrefixRemapper.kt | 31 ++++++++++++------- .../dev/openrs2/deob/remap/TypedRemapper.kt | 4 +-- 6 files changed, 41 insertions(+), 23 deletions(-) create mode 100644 asm/src/main/java/dev/openrs2/asm/classpath/ExtendedRemapper.kt diff --git a/asm/src/main/java/dev/openrs2/asm/ClassNodeRemapper.kt b/asm/src/main/java/dev/openrs2/asm/ClassNodeRemapper.kt index 1f13a00c0c..f8f8ccea32 100644 --- a/asm/src/main/java/dev/openrs2/asm/ClassNodeRemapper.kt +++ b/asm/src/main/java/dev/openrs2/asm/ClassNodeRemapper.kt @@ -1,6 +1,6 @@ package dev.openrs2.asm -import org.objectweb.asm.commons.Remapper +import dev.openrs2.asm.classpath.ExtendedRemapper import org.objectweb.asm.tree.AbstractInsnNode import org.objectweb.asm.tree.ClassNode import org.objectweb.asm.tree.FieldInsnNode @@ -11,7 +11,7 @@ import org.objectweb.asm.tree.MethodInsnNode import org.objectweb.asm.tree.MultiANewArrayInsnNode import org.objectweb.asm.tree.TypeInsnNode -fun ClassNode.remap(remapper: Remapper) { +fun ClassNode.remap(remapper: ExtendedRemapper) { val originalName = name name = remapper.mapType(originalName) signature = remapper.mapSignature(signature, false) @@ -45,18 +45,18 @@ fun ClassNode.remap(remapper: Remapper) { } } -private fun AbstractInsnNode.remap(remapper: Remapper) { +private fun AbstractInsnNode.remap(remapper: ExtendedRemapper) { when (this) { is FrameNode -> throw UnsupportedOperationException("SKIP_FRAMES and COMPUTE_FRAMES must be used") is FieldInsnNode -> { val originalOwner = owner - owner = remapper.mapType(originalOwner) + owner = remapper.mapFieldOwner(originalOwner, name, desc) name = remapper.mapFieldName(originalOwner, name, desc) desc = remapper.mapDesc(desc) } is MethodInsnNode -> { val originalOwner = owner - owner = remapper.mapType(originalOwner) + owner = remapper.mapMethodOwner(originalOwner, name, desc) name = remapper.mapMethodName(originalOwner, name, desc) desc = remapper.mapDesc(desc) } diff --git a/asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.kt b/asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.kt index c3b16a085b..3df2c52679 100644 --- a/asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.kt +++ b/asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.kt @@ -6,7 +6,6 @@ import dev.openrs2.asm.toBinaryClassName import dev.openrs2.util.collect.DisjointSet import dev.openrs2.util.collect.ForestDisjointSet import org.objectweb.asm.Opcodes -import org.objectweb.asm.commons.Remapper import org.objectweb.asm.tree.AbstractInsnNode import org.objectweb.asm.tree.ClassNode @@ -76,7 +75,7 @@ class ClassPath( return null } - fun remap(remapper: Remapper) { + fun remap(remapper: ExtendedRemapper) { for (library in libraries) { library.remap(remapper) } diff --git a/asm/src/main/java/dev/openrs2/asm/classpath/ExtendedRemapper.kt b/asm/src/main/java/dev/openrs2/asm/classpath/ExtendedRemapper.kt new file mode 100644 index 0000000000..a8130ed67d --- /dev/null +++ b/asm/src/main/java/dev/openrs2/asm/classpath/ExtendedRemapper.kt @@ -0,0 +1,13 @@ +package dev.openrs2.asm.classpath + +import org.objectweb.asm.commons.Remapper + +abstract class ExtendedRemapper : Remapper() { + open fun mapFieldOwner(owner: String, name: String, desc: String): String { + return mapType(owner) + } + + open fun mapMethodOwner(owner: String, name: String, desc: String): String { + return mapType(owner) + } +} diff --git a/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt b/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt index 5d89544d7f..8d3232c921 100644 --- a/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt +++ b/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt @@ -4,7 +4,6 @@ import com.github.michaelbull.logging.InlineLogger import dev.openrs2.asm.io.LibraryReader import dev.openrs2.asm.io.LibraryWriter import dev.openrs2.asm.remap -import org.objectweb.asm.commons.Remapper import org.objectweb.asm.tree.ClassNode import java.nio.file.Files import java.nio.file.Path @@ -41,7 +40,7 @@ class Library() : Iterable { return classes.values.iterator() } - fun remap(remapper: Remapper) { + fun remap(remapper: ExtendedRemapper) { for (clazz in classes.values) { clazz.remap(remapper) } diff --git a/deob/src/main/java/dev/openrs2/deob/remap/PrefixRemapper.kt b/deob/src/main/java/dev/openrs2/deob/remap/PrefixRemapper.kt index 92ca59140b..b270225a54 100644 --- a/deob/src/main/java/dev/openrs2/deob/remap/PrefixRemapper.kt +++ b/deob/src/main/java/dev/openrs2/deob/remap/PrefixRemapper.kt @@ -1,22 +1,29 @@ package dev.openrs2.deob.remap +import dev.openrs2.asm.classpath.ExtendedRemapper import dev.openrs2.asm.classpath.Library import dev.openrs2.asm.filter.ClassFilter -import org.objectweb.asm.commons.Remapper -import org.objectweb.asm.commons.SimpleRemapper -object PrefixRemapper { - fun create(library: Library, prefix: String, excluded: ClassFilter): Remapper { - val mapping = mutableMapOf() +class PrefixRemapper(private val prefix: String, private val classes: Set) : ExtendedRemapper() { + override fun map(internalName: String): String { + return if (classes.contains(internalName)) { + prefix + internalName + } else { + internalName + } + } + + companion object { + fun create(library: Library, prefix: String, excluded: ClassFilter): ExtendedRemapper { + val classes = mutableSetOf() - for (clazz in library) { - if (excluded.matches(clazz.name)) { - mapping[clazz.name] = clazz.name - } else { - mapping[clazz.name] = prefix + clazz.name + for (clazz in library) { + if (!excluded.matches(clazz.name)) { + classes += clazz.name + } } - } - return SimpleRemapper(mapping) + return PrefixRemapper(prefix, classes) + } } } diff --git a/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt b/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt index bcdc3816ae..059f25fea9 100644 --- a/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt +++ b/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt @@ -5,6 +5,7 @@ import dev.openrs2.asm.MemberDesc import dev.openrs2.asm.MemberRef import dev.openrs2.asm.classpath.ClassMetadata import dev.openrs2.asm.classpath.ClassPath +import dev.openrs2.asm.classpath.ExtendedRemapper import dev.openrs2.asm.filter.ClassFilter import dev.openrs2.asm.filter.MemberFilter import dev.openrs2.deob.Profile @@ -12,13 +13,12 @@ import dev.openrs2.util.collect.DisjointSet import dev.openrs2.util.indefiniteArticle import org.objectweb.asm.Opcodes import org.objectweb.asm.Type -import org.objectweb.asm.commons.Remapper class TypedRemapper private constructor( private val classes: Map, private val fields: Map, private val methods: Map -) : Remapper() { +) : ExtendedRemapper() { override fun map(internalName: String): String { return classes.getOrDefault(internalName, internalName) }