Use partitions in TypedRemapper maps

This avoids the need to add multiple entries for the same field/method.

Signed-off-by: Graham <gpe@openrs2.dev>
Graham 5 years ago
parent 9b1f0c6886
commit fc718a7672
  1. 11
      deob/src/main/java/dev/openrs2/deob/remap/FieldMappingGenerator.kt
  2. 12
      deob/src/main/java/dev/openrs2/deob/remap/MethodMappingGenerator.kt
  3. 40
      deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt

@ -10,13 +10,14 @@ import org.objectweb.asm.Type
class FieldMappingGenerator( class FieldMappingGenerator(
private val classPath: ClassPath, private val classPath: ClassPath,
private val excludedFields: MemberFilter, private val excludedFields: MemberFilter,
private val inheritedFieldSets: DisjointSet<MemberRef>,
private val classMapping: Map<String, String> private val classMapping: Map<String, String>
) { ) {
private val inheritedFieldSets = classPath.createInheritedFieldSets()
private val nameGenerator = NameGenerator() private val nameGenerator = NameGenerator()
private val mapping = mutableMapOf<MemberRef, String>()
fun generate(): Map<MemberRef, String> { fun generate(): Map<DisjointSet.Partition<MemberRef>, String> {
val mapping = mutableMapOf<DisjointSet.Partition<MemberRef>, String>()
for (partition in inheritedFieldSets) { for (partition in inheritedFieldSets) {
if (!isRenamable(partition)) { if (!isRenamable(partition)) {
continue continue
@ -24,9 +25,7 @@ class FieldMappingGenerator(
val type = Type.getType(partition.first().desc) val type = Type.getType(partition.first().desc)
val mappedName = generateName(type) val mappedName = generateName(type)
for (field in partition) { mapping[partition] = mappedName
mapping[field] = mappedName
}
} }
return mapping return mapping

@ -9,13 +9,13 @@ import org.objectweb.asm.Opcodes
class MethodMappingGenerator( class MethodMappingGenerator(
private val classPath: ClassPath, private val classPath: ClassPath,
private val excludedMethods: MemberFilter private val excludedMethods: MemberFilter,
private val inheritedMethodSets: DisjointSet<MemberRef>
) { ) {
private val inheritedMethodSets = classPath.createInheritedMethodSets()
private var index = 0 private var index = 0
fun generate(): Map<MemberRef, String> { fun generate(): Map<DisjointSet.Partition<MemberRef>, String> {
val mapping = mutableMapOf<MemberRef, String>() val mapping = mutableMapOf<DisjointSet.Partition<MemberRef>, String>()
for (partition in inheritedMethodSets) { for (partition in inheritedMethodSets) {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
@ -24,9 +24,7 @@ class MethodMappingGenerator(
} }
val mappedName = "method" + ++index val mappedName = "method" + ++index
for (method in partition) { mapping[partition] = mappedName
mapping[method] = mappedName
}
} }
return mapping return mapping

@ -5,22 +5,29 @@ import dev.openrs2.asm.MemberRef
import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.classpath.ClassPath
import dev.openrs2.asm.classpath.ExtendedRemapper import dev.openrs2.asm.classpath.ExtendedRemapper
import dev.openrs2.deob.Profile import dev.openrs2.deob.Profile
import dev.openrs2.util.collect.DisjointSet
class TypedRemapper private constructor( class TypedRemapper private constructor(
private val inheritedFieldSets: DisjointSet<MemberRef>,
private val inheritedMethodSets: DisjointSet<MemberRef>,
private val classes: Map<String, String>, private val classes: Map<String, String>,
private val fields: Map<MemberRef, String>, private val fields: Map<DisjointSet.Partition<MemberRef>, String>,
private val methods: Map<MemberRef, String> private val methods: Map<DisjointSet.Partition<MemberRef>, String>
) : ExtendedRemapper() { ) : ExtendedRemapper() {
override fun map(internalName: String): String { override fun map(internalName: String): String {
return classes.getOrDefault(internalName, internalName) return classes.getOrDefault(internalName, internalName)
} }
override fun mapFieldName(owner: String, name: String, descriptor: String): String { override fun mapFieldName(owner: String, name: String, descriptor: String): String {
return fields.getOrDefault(MemberRef(owner, name, descriptor), name) val member = MemberRef(owner, name, descriptor)
val partition = inheritedFieldSets[member] ?: return name
return fields.getOrDefault(partition, name)
} }
override fun mapMethodName(owner: String, name: String, descriptor: String): String { override fun mapMethodName(owner: String, name: String, descriptor: String): String {
return methods.getOrDefault(MemberRef(owner, name, descriptor), name) val member = MemberRef(owner, name, descriptor)
val partition = inheritedMethodSets[member] ?: return name
return methods.getOrDefault(partition, name)
} }
companion object { companion object {
@ -29,15 +36,27 @@ class TypedRemapper private constructor(
private val LIBRARY_PREFIX_REGEX = Regex("^(?:loader|unpackclass)_") private val LIBRARY_PREFIX_REGEX = Regex("^(?:loader|unpackclass)_")
fun create(classPath: ClassPath, profile: Profile): TypedRemapper { fun create(classPath: ClassPath, profile: Profile): TypedRemapper {
val inheritedFieldSets = classPath.createInheritedFieldSets()
val inheritedMethodSets = classPath.createInheritedMethodSets()
val classes = ClassMappingGenerator(classPath, profile.excludedClasses).generate() val classes = ClassMappingGenerator(classPath, profile.excludedClasses).generate()
val fields = FieldMappingGenerator(classPath, profile.excludedFields, classes).generate() val fields = FieldMappingGenerator(
val methods = MethodMappingGenerator(classPath, profile.excludedMethods).generate() classPath,
profile.excludedFields,
inheritedFieldSets,
classes
).generate()
val methods = MethodMappingGenerator(
classPath,
profile.excludedMethods,
inheritedMethodSets
).generate()
verifyMapping(classes, profile.maxObfuscatedNameLen) verifyMapping(classes, profile.maxObfuscatedNameLen)
verifyMemberMapping(fields, profile.maxObfuscatedNameLen) verifyMemberMapping(fields, profile.maxObfuscatedNameLen)
verifyMemberMapping(methods, profile.maxObfuscatedNameLen) verifyMemberMapping(methods, profile.maxObfuscatedNameLen)
return TypedRemapper(classes, fields, methods) return TypedRemapper(inheritedFieldSets, inheritedMethodSets, classes, fields, methods)
} }
private fun verifyMapping(mapping: Map<String, String>, maxObfuscatedNameLen: Int) { private fun verifyMapping(mapping: Map<String, String>, maxObfuscatedNameLen: Int) {
@ -46,9 +65,12 @@ class TypedRemapper private constructor(
} }
} }
private fun verifyMemberMapping(mapping: Map<MemberRef, String>, maxObfuscatedNameLen: Int) { private fun verifyMemberMapping(
mapping: Map<DisjointSet.Partition<MemberRef>, String>,
maxObfuscatedNameLen: Int
) {
for ((key, value) in mapping) { for ((key, value) in mapping) {
verifyMapping(key.name, value, maxObfuscatedNameLen) verifyMapping(key.first().name, value, maxObfuscatedNameLen)
} }
} }

Loading…
Cancel
Save