From fc718a76727ec4fc4b3e7ab9a6db35aae868473f Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 24 May 2020 13:02:27 +0100 Subject: [PATCH] Use partitions in TypedRemapper maps This avoids the need to add multiple entries for the same field/method. Signed-off-by: Graham --- .../deob/remap/FieldMappingGenerator.kt | 11 +++-- .../deob/remap/MethodMappingGenerator.kt | 12 +++--- .../dev/openrs2/deob/remap/TypedRemapper.kt | 40 ++++++++++++++----- 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/deob/src/main/java/dev/openrs2/deob/remap/FieldMappingGenerator.kt b/deob/src/main/java/dev/openrs2/deob/remap/FieldMappingGenerator.kt index 094b60c7..74572f9b 100644 --- a/deob/src/main/java/dev/openrs2/deob/remap/FieldMappingGenerator.kt +++ b/deob/src/main/java/dev/openrs2/deob/remap/FieldMappingGenerator.kt @@ -10,13 +10,14 @@ import org.objectweb.asm.Type class FieldMappingGenerator( private val classPath: ClassPath, private val excludedFields: MemberFilter, + private val inheritedFieldSets: DisjointSet, private val classMapping: Map ) { - private val inheritedFieldSets = classPath.createInheritedFieldSets() private val nameGenerator = NameGenerator() - private val mapping = mutableMapOf() - fun generate(): Map { + fun generate(): Map, String> { + val mapping = mutableMapOf, String>() + for (partition in inheritedFieldSets) { if (!isRenamable(partition)) { continue @@ -24,9 +25,7 @@ class FieldMappingGenerator( val type = Type.getType(partition.first().desc) val mappedName = generateName(type) - for (field in partition) { - mapping[field] = mappedName - } + mapping[partition] = mappedName } return mapping diff --git a/deob/src/main/java/dev/openrs2/deob/remap/MethodMappingGenerator.kt b/deob/src/main/java/dev/openrs2/deob/remap/MethodMappingGenerator.kt index 88637f7e..9266adde 100644 --- a/deob/src/main/java/dev/openrs2/deob/remap/MethodMappingGenerator.kt +++ b/deob/src/main/java/dev/openrs2/deob/remap/MethodMappingGenerator.kt @@ -9,13 +9,13 @@ import org.objectweb.asm.Opcodes class MethodMappingGenerator( private val classPath: ClassPath, - private val excludedMethods: MemberFilter + private val excludedMethods: MemberFilter, + private val inheritedMethodSets: DisjointSet ) { - private val inheritedMethodSets = classPath.createInheritedMethodSets() private var index = 0 - fun generate(): Map { - val mapping = mutableMapOf() + fun generate(): Map, String> { + val mapping = mutableMapOf, String>() for (partition in inheritedMethodSets) { @Suppress("DEPRECATION") @@ -24,9 +24,7 @@ class MethodMappingGenerator( } val mappedName = "method" + ++index - for (method in partition) { - mapping[method] = mappedName - } + mapping[partition] = mappedName } return mapping 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 90702327..6a4fe62d 100644 --- a/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt +++ b/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt @@ -5,22 +5,29 @@ import dev.openrs2.asm.MemberRef import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.classpath.ExtendedRemapper import dev.openrs2.deob.Profile +import dev.openrs2.util.collect.DisjointSet class TypedRemapper private constructor( + private val inheritedFieldSets: DisjointSet, + private val inheritedMethodSets: DisjointSet, private val classes: Map, - private val fields: Map, - private val methods: Map + private val fields: Map, String>, + private val methods: Map, String> ) : ExtendedRemapper() { override fun map(internalName: String): String { return classes.getOrDefault(internalName, internalName) } 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 { - 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 { @@ -29,15 +36,27 @@ class TypedRemapper private constructor( private val LIBRARY_PREFIX_REGEX = Regex("^(?:loader|unpackclass)_") fun create(classPath: ClassPath, profile: Profile): TypedRemapper { + val inheritedFieldSets = classPath.createInheritedFieldSets() + val inheritedMethodSets = classPath.createInheritedMethodSets() + val classes = ClassMappingGenerator(classPath, profile.excludedClasses).generate() - val fields = FieldMappingGenerator(classPath, profile.excludedFields, classes).generate() - val methods = MethodMappingGenerator(classPath, profile.excludedMethods).generate() + val fields = FieldMappingGenerator( + classPath, + profile.excludedFields, + inheritedFieldSets, + classes + ).generate() + val methods = MethodMappingGenerator( + classPath, + profile.excludedMethods, + inheritedMethodSets + ).generate() verifyMapping(classes, profile.maxObfuscatedNameLen) verifyMemberMapping(fields, profile.maxObfuscatedNameLen) verifyMemberMapping(methods, profile.maxObfuscatedNameLen) - return TypedRemapper(classes, fields, methods) + return TypedRemapper(inheritedFieldSets, inheritedMethodSets, classes, fields, methods) } private fun verifyMapping(mapping: Map, maxObfuscatedNameLen: Int) { @@ -46,9 +65,12 @@ class TypedRemapper private constructor( } } - private fun verifyMemberMapping(mapping: Map, maxObfuscatedNameLen: Int) { + private fun verifyMemberMapping( + mapping: Map, String>, + maxObfuscatedNameLen: Int + ) { for ((key, value) in mapping) { - verifyMapping(key.name, value, maxObfuscatedNameLen) + verifyMapping(key.first().name, value, maxObfuscatedNameLen) } }