From 611ac6de846080ed9051a316d7cf9ff452dfd121 Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 7 Jun 2020 14:50:47 +0100 Subject: [PATCH] Add initial NameMap support to TypedRemapper This commit adds support for mapping: * class names * field names * field owners * method names * method owners It does not yet support argument names, local variable names or re-ordering method arguments. Local variable names are blocked on Fernflower patches and method argument re-ordering is blocked on the IR. Signed-off-by: Graham --- .../main/java/dev/openrs2/deob/map/NameMap.kt | 49 +++++++++++++++++++ .../deob/remap/ClassMappingGenerator.kt | 7 ++- .../deob/remap/FieldMappingGenerator.kt | 5 +- .../deob/remap/MethodMappingGenerator.kt | 6 ++- .../deob/remap/StaticFieldUnscrambler.kt | 5 +- .../deob/remap/StaticMethodUnscrambler.kt | 4 +- .../dev/openrs2/deob/remap/TypedRemapper.kt | 13 ++++- .../deob/transform/RemapTransformer.kt | 8 ++- 8 files changed, 86 insertions(+), 11 deletions(-) diff --git a/deob-map/src/main/java/dev/openrs2/deob/map/NameMap.kt b/deob-map/src/main/java/dev/openrs2/deob/map/NameMap.kt index cc6ed6a00b..4ae0fe51b3 100644 --- a/deob-map/src/main/java/dev/openrs2/deob/map/NameMap.kt +++ b/deob-map/src/main/java/dev/openrs2/deob/map/NameMap.kt @@ -1,6 +1,7 @@ package dev.openrs2.deob.map import dev.openrs2.asm.MemberRef +import dev.openrs2.util.collect.DisjointSet import java.util.SortedMap import java.util.TreeMap @@ -16,4 +17,52 @@ data class NameMap( fields.putAll(other.fields) methods.putAll(other.methods) } + + fun mapClassName(name: String, default: String): String { + return classes.getOrDefault(name, default) + } + + fun mapFieldName(partition: DisjointSet.Partition, default: String): String { + for (member in partition) { + val field = fields[member] + if (field != null) { + return field.name + } + } + + return default + } + + fun mapFieldOwner(partition: DisjointSet.Partition, default: String): String { + for (member in partition) { + val field = fields[member] + if (field != null) { + return field.owner + } + } + + return default + } + + fun mapMethodName(partition: DisjointSet.Partition, default: String): String { + for (member in partition) { + val method = methods[member] + if (method != null) { + return method.name + } + } + + return default + } + + fun mapMethodOwner(partition: DisjointSet.Partition, default: String): String { + for (member in partition) { + val method = methods[member] + if (method != null) { + return method.owner + } + } + + return default + } } diff --git a/deob/src/main/java/dev/openrs2/deob/remap/ClassMappingGenerator.kt b/deob/src/main/java/dev/openrs2/deob/remap/ClassMappingGenerator.kt index 03a2d8c95a..b3af008d8b 100644 --- a/deob/src/main/java/dev/openrs2/deob/remap/ClassMappingGenerator.kt +++ b/deob/src/main/java/dev/openrs2/deob/remap/ClassMappingGenerator.kt @@ -3,11 +3,13 @@ package dev.openrs2.deob.remap import dev.openrs2.asm.classpath.ClassMetadata import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.filter.ClassFilter +import dev.openrs2.deob.map.NameMap import org.objectweb.asm.Opcodes class ClassMappingGenerator( private val classPath: ClassPath, - private val excludedClasses: ClassFilter + private val excludedClasses: ClassFilter, + private val nameMap: NameMap ) { private val nameGenerator = NameGenerator() private val mapping = mutableMapOf() @@ -16,6 +18,9 @@ class ClassMappingGenerator( for (clazz in classPath.libraryClasses) { populateMapping(clazz) } + + mapping.replaceAll(nameMap::mapClassName) + return mapping } 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 74572f9b0e..2f7a5c98d6 100644 --- a/deob/src/main/java/dev/openrs2/deob/remap/FieldMappingGenerator.kt +++ b/deob/src/main/java/dev/openrs2/deob/remap/FieldMappingGenerator.kt @@ -3,6 +3,7 @@ package dev.openrs2.deob.remap import dev.openrs2.asm.MemberRef import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.filter.MemberFilter +import dev.openrs2.deob.map.NameMap import dev.openrs2.util.collect.DisjointSet import dev.openrs2.util.indefiniteArticle import org.objectweb.asm.Type @@ -10,6 +11,7 @@ import org.objectweb.asm.Type class FieldMappingGenerator( private val classPath: ClassPath, private val excludedFields: MemberFilter, + private val nameMap: NameMap, private val inheritedFieldSets: DisjointSet, private val classMapping: Map ) { @@ -24,8 +26,7 @@ class FieldMappingGenerator( } val type = Type.getType(partition.first().desc) - val mappedName = generateName(type) - mapping[partition] = mappedName + mapping[partition] = nameMap.mapFieldName(partition, generateName(type)) } 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 9266addece..de268661fb 100644 --- a/deob/src/main/java/dev/openrs2/deob/remap/MethodMappingGenerator.kt +++ b/deob/src/main/java/dev/openrs2/deob/remap/MethodMappingGenerator.kt @@ -4,12 +4,14 @@ import dev.openrs2.asm.MemberDesc import dev.openrs2.asm.MemberRef import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.filter.MemberFilter +import dev.openrs2.deob.map.NameMap import dev.openrs2.util.collect.DisjointSet import org.objectweb.asm.Opcodes class MethodMappingGenerator( private val classPath: ClassPath, private val excludedMethods: MemberFilter, + private val nameMap: NameMap, private val inheritedMethodSets: DisjointSet ) { private var index = 0 @@ -23,8 +25,8 @@ class MethodMappingGenerator( continue } - val mappedName = "method" + ++index - mapping[partition] = mappedName + val generatedName = "method" + ++index + mapping[partition] = nameMap.mapMethodName(partition, generatedName) } return mapping diff --git a/deob/src/main/java/dev/openrs2/deob/remap/StaticFieldUnscrambler.kt b/deob/src/main/java/dev/openrs2/deob/remap/StaticFieldUnscrambler.kt index 2038a1ae26..2299629be9 100644 --- a/deob/src/main/java/dev/openrs2/deob/remap/StaticFieldUnscrambler.kt +++ b/deob/src/main/java/dev/openrs2/deob/remap/StaticFieldUnscrambler.kt @@ -6,6 +6,7 @@ import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.filter.MemberFilter import dev.openrs2.asm.getExpression import dev.openrs2.asm.isSequential +import dev.openrs2.deob.map.NameMap import dev.openrs2.util.collect.DisjointSet import org.objectweb.asm.Opcodes import org.objectweb.asm.tree.AbstractInsnNode @@ -15,6 +16,7 @@ import org.objectweb.asm.tree.MethodNode class StaticFieldUnscrambler( private val classPath: ClassPath, private val excludedFields: MemberFilter, + private val nameMap: NameMap, private val inheritedFieldSets: DisjointSet, staticClassNameGenerator: NameGenerator ) { @@ -50,7 +52,8 @@ class StaticFieldUnscrambler( val member = MemberRef(clazz, field) val partition = inheritedFieldSets[member]!! - fields[partition] = StaticField(generator.generate(), simpleInitializers[desc]) + val owner = nameMap.mapFieldOwner(partition, generator.generate()) + fields[partition] = StaticField(owner, simpleInitializers[desc]) } } } diff --git a/deob/src/main/java/dev/openrs2/deob/remap/StaticMethodUnscrambler.kt b/deob/src/main/java/dev/openrs2/deob/remap/StaticMethodUnscrambler.kt index eff836ebc7..60a2225bdf 100644 --- a/deob/src/main/java/dev/openrs2/deob/remap/StaticMethodUnscrambler.kt +++ b/deob/src/main/java/dev/openrs2/deob/remap/StaticMethodUnscrambler.kt @@ -3,12 +3,14 @@ package dev.openrs2.deob.remap import dev.openrs2.asm.MemberRef import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.filter.MemberFilter +import dev.openrs2.deob.map.NameMap import dev.openrs2.util.collect.DisjointSet import org.objectweb.asm.Opcodes class StaticMethodUnscrambler( private val classPath: ClassPath, private val excludedMethods: MemberFilter, + private val nameMap: NameMap, private val inheritedMethodSets: DisjointSet, staticClassNameGenerator: NameGenerator ) { @@ -37,7 +39,7 @@ class StaticMethodUnscrambler( val member = MemberRef(clazz, method) val partition = inheritedMethodSets[member]!! - owners[partition] = generator.generate() + owners[partition] = nameMap.mapMethodOwner(partition, generator.generate()) } } } 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 e8b5b282ae..a5d883b0fc 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.MemberRef import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.classpath.ExtendedRemapper import dev.openrs2.deob.Profile +import dev.openrs2.deob.map.NameMap import dev.openrs2.util.collect.DisjointSet import org.objectweb.asm.tree.AbstractInsnNode @@ -56,20 +57,26 @@ class TypedRemapper private constructor( private val LIBRARY_PREFIX_REGEX = Regex("^(?:loader|unpackclass)_") - fun create(classPath: ClassPath, profile: Profile): TypedRemapper { + fun create(classPath: ClassPath, profile: Profile, nameMap: NameMap): TypedRemapper { val inheritedFieldSets = classPath.createInheritedFieldSets() val inheritedMethodSets = classPath.createInheritedMethodSets() - val classes = ClassMappingGenerator(classPath, profile.excludedClasses).generate() + val classes = ClassMappingGenerator( + classPath, + profile.excludedClasses, + nameMap + ).generate() val fields = FieldMappingGenerator( classPath, profile.excludedFields, + nameMap, inheritedFieldSets, classes ).generate() val methods = MethodMappingGenerator( classPath, profile.excludedMethods, + nameMap, inheritedMethodSets ).generate() @@ -81,12 +88,14 @@ class TypedRemapper private constructor( val staticFields = StaticFieldUnscrambler( classPath, profile.excludedFields, + nameMap, inheritedFieldSets, staticClassNameGenerator ).unscramble() val staticMethods = StaticMethodUnscrambler( classPath, profile.excludedMethods, + nameMap, inheritedMethodSets, staticClassNameGenerator ).unscramble() diff --git a/deob/src/main/java/dev/openrs2/deob/transform/RemapTransformer.kt b/deob/src/main/java/dev/openrs2/deob/transform/RemapTransformer.kt index e8a768e119..5445fc092d 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/RemapTransformer.kt +++ b/deob/src/main/java/dev/openrs2/deob/transform/RemapTransformer.kt @@ -3,13 +3,17 @@ package dev.openrs2.deob.transform import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.transform.Transformer import dev.openrs2.deob.Profile +import dev.openrs2.deob.map.NameMap import dev.openrs2.deob.remap.TypedRemapper import javax.inject.Inject import javax.inject.Singleton @Singleton -class RemapTransformer @Inject constructor(private val profile: Profile) : Transformer() { +class RemapTransformer @Inject constructor( + private val profile: Profile, + private val nameMap: NameMap +) : Transformer() { override fun preTransform(classPath: ClassPath) { - classPath.remap(TypedRemapper.create(classPath, profile)) + classPath.remap(TypedRemapper.create(classPath, profile, nameMap)) } }