From 6be3f915f0e9c4ee92a853703896bb9ecaa56b4b Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 7 Mar 2020 17:34:44 +0000 Subject: [PATCH] Add generate{Class,Field}Name methods Before extracting this functionality out, some of the methods were quite large. Signed-off-by: Graham --- .../dev/openrs2/deob/remap/TypedRemapper.kt | 90 +++++++++++-------- 1 file changed, 54 insertions(+), 36 deletions(-) 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 74df31e4..47505174 100644 --- a/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt +++ b/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt @@ -113,19 +113,7 @@ class TypedRemapper private constructor( return mapping.getOrDefault(name, name) } - var mappedName = name.substring(0, name.lastIndexOf('/') + 1) - - val superClass = clazz.superClass - if (superClass != null && superClass.name != "java/lang/Object") { - var superName = populateClassMapping(mapping, prefixes, superClass) - superName = superName.substring(superName.lastIndexOf('/') + 1) - mappedName += generateName(prefixes, superName + "_Sub") - } else if (clazz.`interface`) { - mappedName += generateName(prefixes, "Interface") - } else { - mappedName += generateName(prefixes, "Class") - } - + val mappedName = generateClassName(mapping, prefixes, clazz) mapping[name] = mappedName return mappedName } @@ -144,6 +132,28 @@ class TypedRemapper private constructor( return true } + private fun generateClassName( + mapping: MutableMap, + prefixes: MutableMap, + clazz: ClassMetadata + ): String { + val name = clazz.name + var mappedName = name.substring(0, name.lastIndexOf('/') + 1) + + val superClass = clazz.superClass + if (superClass != null && superClass.name != "java/lang/Object") { + var superName = populateClassMapping(mapping, prefixes, superClass) + superName = superName.substring(superName.lastIndexOf('/') + 1) + mappedName += generateName(prefixes, superName + "_Sub") + } else if (clazz.`interface`) { + mappedName += generateName(prefixes, "Interface") + } else { + mappedName += generateName(prefixes, "Class") + } + + return mappedName + } + private fun createFieldMapping( classPath: ClassPath, disjointSet: DisjointSet, @@ -157,29 +167,8 @@ class TypedRemapper private constructor( continue } - var prefix = "" - - var type = Type.getType(partition.iterator().next().desc) - if (type.sort == Type.ARRAY) { - prefix = "Array".repeat(type.dimensions) - type = type.elementType - } - - when (type.sort) { - Type.BOOLEAN, Type.BYTE, Type.CHAR, Type.SHORT, Type.INT, Type.LONG, Type.FLOAT, Type.DOUBLE -> { - prefix = type.className + prefix - } - Type.OBJECT -> { - var className = classMapping.getOrDefault(type.internalName, type.internalName) - className = className.substring(className.lastIndexOf('/') + 1) - prefix = className + prefix - } - else -> throw IllegalArgumentException("Unknown field type $type") - } - - prefix = prefix.indefiniteArticle() + prefix.capitalize() - - val mappedName = generateName(prefixes, prefix) + val type = Type.getType(partition.first().desc) + val mappedName = generateFieldName(prefixes, classMapping, type) for (field in partition) { mapping[field] = mappedName } @@ -200,6 +189,35 @@ class TypedRemapper private constructor( return true } + private fun generateFieldName( + prefixes: MutableMap, + classMapping: Map, + type: Type + ): String { + val dimensions: String + val elementType: Type + if (type.sort == Type.ARRAY) { + dimensions = "Array".repeat(type.dimensions) + elementType = type.elementType + } else { + dimensions = "" + elementType = type + } + + val prefix = when (elementType.sort) { + Type.BOOLEAN, Type.BYTE, Type.CHAR, Type.SHORT, Type.INT, Type.LONG, Type.FLOAT, Type.DOUBLE -> { + elementType.className + dimensions + } + Type.OBJECT -> { + val className = classMapping.getOrDefault(elementType.internalName, elementType.internalName) + className.substring(className.lastIndexOf('/') + 1) + dimensions + } + else -> throw IllegalArgumentException("Unknown field type $elementType") + } + + return generateName(prefixes, prefix.indefiniteArticle() + prefix.capitalize()) + } + fun isMethodRenamable(classPath: ClassPath, partition: DisjointSet.Partition): Boolean { for (method in partition) { val clazz = classPath[method.owner]!!