Add generate{Class,Field}Name methods

Before extracting this functionality out, some of the methods were quite
large.

Signed-off-by: Graham <gpe@openrs2.dev>
bzip2
Graham 5 years ago
parent c4ea6cf150
commit 6be3f915f0
  1. 90
      deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt

@ -113,19 +113,7 @@ class TypedRemapper private constructor(
return mapping.getOrDefault(name, name) return mapping.getOrDefault(name, name)
} }
var mappedName = name.substring(0, name.lastIndexOf('/') + 1) val mappedName = generateClassName(mapping, prefixes, clazz)
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")
}
mapping[name] = mappedName mapping[name] = mappedName
return mappedName return mappedName
} }
@ -144,6 +132,28 @@ class TypedRemapper private constructor(
return true return true
} }
private fun generateClassName(
mapping: MutableMap<String, String>,
prefixes: MutableMap<String, Int>,
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( private fun createFieldMapping(
classPath: ClassPath, classPath: ClassPath,
disjointSet: DisjointSet<MemberRef>, disjointSet: DisjointSet<MemberRef>,
@ -157,29 +167,8 @@ class TypedRemapper private constructor(
continue continue
} }
var prefix = "" val type = Type.getType(partition.first().desc)
val mappedName = generateFieldName(prefixes, classMapping, type)
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)
for (field in partition) { for (field in partition) {
mapping[field] = mappedName mapping[field] = mappedName
} }
@ -200,6 +189,35 @@ class TypedRemapper private constructor(
return true return true
} }
private fun generateFieldName(
prefixes: MutableMap<String, Int>,
classMapping: Map<String, String>,
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<MemberRef>): Boolean { fun isMethodRenamable(classPath: ClassPath, partition: DisjointSet.Partition<MemberRef>): Boolean {
for (method in partition) { for (method in partition) {
val clazz = classPath[method.owner]!! val clazz = classPath[method.owner]!!

Loading…
Cancel
Save