Skip to content

Commit

Permalink
Try to make the remapper even less prone to failing
Browse files Browse the repository at this point in the history
  • Loading branch information
BluSpring committed Jul 13, 2023
1 parent eab8ad6 commit 038669a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.bluspring.kilt.loader.remap

import net.fabricmc.loader.api.FabricLoader
import org.objectweb.asm.commons.Remapper
import org.objectweb.asm.signature.SignatureReader
import org.objectweb.asm.signature.SignatureWriter
Expand Down Expand Up @@ -73,14 +74,33 @@ class KiltAsmRemapper(
val mappedPair = methodMappings[name] ?: return name
val mapped = mappedPair.first

if (KiltRemapper.remapDescriptor(descriptor).replaceAfter(")", "") != mappedPair.second.replaceAfter(")", ""))
val remappedDescriptor = KiltRemapper.remapDescriptor(descriptor)
val remappedDescriptorNoEnd = remappedDescriptor.replaceAfter(")", "")

if (remappedDescriptorNoEnd != mappedPair.second.replaceAfter(")", ""))
return name

if ((!name.startsWith("m_") && !name.startsWith("f_")) && !name.endsWith("_")) {
// Ignore these owner types specifically, because they are confirmed to be safe.
if (ownerWhitelist.any { owner.startsWith(it) })
return name

// Try to see if these mappings can be used to help
val srgClassTree = KiltRemapper.srgIntermediaryTree.classes.firstOrNull { it.getName("searge") == owner }

if (srgClassTree != null) {
val matchingMethod = srgClassTree.methods.firstOrNull { it.getName("searge") == name && it.getDescriptor("intermediary").replaceAfter(")", "") == remappedDescriptorNoEnd }

if (matchingMethod != null) {
val intermediaryName = matchingMethod.getName("intermediary")

return if (KiltRemapper.useNamed)
FabricLoader.getInstance().mappingResolver.mapMethodName("intermediary", srgClassTree.getName("intermediary"), intermediaryName, KiltRemapper.remapDescriptor(descriptor, toIntermediary = true))
else
intermediaryName
}
}

// Because of the way the remapper works, it remaps every name sharing the same f_num_ and m_num_ names
// without checking the owners, because it functions under the assumption that every intermediate name
// is of the same mapped name. However, that assumption fails when it reaches a non-intermediate name,
Expand Down
12 changes: 8 additions & 4 deletions src/main/kotlin/xyz/bluspring/kilt/loader/remap/KiltRemapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object KiltRemapper {
// Keeps track of the remapper changes, so every time I update the remapper,
// it remaps all the mods following the remapper changes.
// this can update by like 12 versions in 1 update, so don't worry too much about it.
const val REMAPPER_VERSION = 34
const val REMAPPER_VERSION = 35

private val logger = LoggerFactory.getLogger("Kilt Remapper")
// This is created automatically using https://github.com/BluSpring/srg2intermediary
Expand Down Expand Up @@ -405,17 +405,21 @@ object KiltRemapper {
return exceptions
}

fun remapClass(name: String): String {
fun remapClass(name: String, toIntermediary: Boolean = false): String {
val workaround = kiltWorkaroundTree.classes.firstOrNull { it.getRawName("forge") == name }?.getRawName("kilt")

if (toIntermediary) {
return workaround ?: srgIntermediaryTree.classes.firstOrNull { it.getName("searge") == name }?.getName("intermediary") ?: name
}

return workaround ?: classMappings[name] ?: name
}

fun unmapClass(name: String): String {
return classMappings.entries.firstOrNull { it.value == name }?.key ?: name
}

fun remapDescriptor(descriptor: String, reverse: Boolean = false): String {
fun remapDescriptor(descriptor: String, reverse: Boolean = false, toIntermediary: Boolean = false): String {
var formedString = ""

var incompleteString = ""
Expand All @@ -434,7 +438,7 @@ object KiltRemapper {

val name = incompleteString.removePrefix("L").removeSuffix(";")
formedString += if (!reverse)
remapClass(name)
remapClass(name, toIntermediary)
else
unmapClass(name)

Expand Down

0 comments on commit 038669a

Please sign in to comment.