Skip to content

Commit

Permalink
add conflicting PL method fixer and fix mixin refmap remapping withou…
Browse files Browse the repository at this point in the history
…t owner classes
  • Loading branch information
BluSpring committed Oct 31, 2023
1 parent fbc2c27 commit 72609c6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
@Mixin(StructureTemplate.class)
public abstract class StructureTemplateInject implements StructureTemplateInjection {
@CreateStatic
private static Vec3 transformedVec3d(StructurePlaceSettings placementIn, Vec3 pos) {
private static Vec3 forge$transformedVec3d(StructurePlaceSettings placementIn, Vec3 pos) {
return StructureTemplateInjection.transformedVec3d(placementIn, pos);
}

@CreateStatic
private static List<StructureTemplate.StructureEntityInfo> processEntityInfos(@Nullable StructureTemplate template, LevelAccessor level, BlockPos blockPos, StructurePlaceSettings structurePlaceSettings, List<StructureTemplate.StructureEntityInfo> structureEntityInfoList) {
private static List<StructureTemplate.StructureEntityInfo> forge$processEntityInfos(@Nullable StructureTemplate template, LevelAccessor level, BlockPos blockPos, StructurePlaceSettings structurePlaceSettings, List<StructureTemplate.StructureEntityInfo> structureEntityInfoList) {
return StructureTemplateInjection.processEntityInfos(template, level, blockPos, structurePlaceSettings, structureEntityInfoList);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.slf4j.LoggerFactory
import xyz.bluspring.kilt.Kilt
import xyz.bluspring.kilt.loader.KiltLoader
import xyz.bluspring.kilt.loader.mod.ForgeMod
import xyz.bluspring.kilt.loader.remap.fixers.ConflictingStaticMethodFixer
import xyz.bluspring.kilt.loader.remap.fixers.EventClassVisibilityFixer
import xyz.bluspring.kilt.loader.remap.fixers.EventEmptyInitializerFixer
import xyz.bluspring.kilt.util.KiltHelper
Expand All @@ -49,7 +50,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 = 106
const val REMAPPER_VERSION = 107

val logConsumer = Consumer<String> {
logger.debug(it)
Expand Down Expand Up @@ -285,7 +286,7 @@ object KiltRemapper {
)
} else {
// If the refmap is missing an owner class, try to figure it out
if (srgField.startsWith("f_") && srgField.endsWith("_"))
if (!srgField.startsWith("f_") || !srgField.endsWith("_"))
srgField // short-circuit if it doesn't look like a field
else {
if (nameMappingCache.contains(srgField))
Expand Down Expand Up @@ -333,7 +334,7 @@ object KiltRemapper {
// If the refmap is missing an owner class, try to figure it out
// Since record classes can provide methods with f_num_, these have to be
// taken into account.
if ((srgMethod.startsWith("f_") || srgMethod.startsWith("m_")) && srgMethod.endsWith("_"))
if (!(srgMethod.startsWith("f_") || srgMethod.startsWith("m_")) || !srgMethod.endsWith("_"))
srgMethod // short-circuit if it doesn't look like a method
else {
if (nameMappingCache.contains(srgMethod))
Expand Down Expand Up @@ -527,6 +528,7 @@ object KiltRemapper {

val visitor = EnhancedClassRemapper(classWriter, srgRemapper, RenamingTransformer(srgRemapper, false))
classNode.accept(visitor)
ConflictingStaticMethodFixer.fixClass(classNode)

// We need to remap to the SRG name, otherwise the remapper completely fails in production environments.
val srgName = intermediarySrgMapping.remapClass(entry.name.removePrefix("/").removeSuffix(".class"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package xyz.bluspring.kilt.loader.remap.fixers

import net.fabricmc.loader.api.FabricLoader
import org.objectweb.asm.Opcodes
import org.objectweb.asm.tree.AbstractInsnNode
import org.objectweb.asm.tree.ClassNode
import org.objectweb.asm.tree.MethodInsnNode
import xyz.bluspring.kilt.loader.remap.KiltRemapper

// Porting Lib makes some methods virtual instead of static.
// Let's remap the methods that conflict with Porting Lib.
object ConflictingStaticMethodFixer {
private val mappingResolver = FabricLoader.getInstance().mappingResolver

private val conflictingMethods = mapOf(
mappingResolver.mapClassName("intermediary", "net.minecraft.class_3499").replace(".", "/") to listOf(
"transformedVec3d" to KiltRemapper.remapDescriptor("(Lnet/minecraft/class_3492;Lnet/minecraft/class_243;)Lnet/minecraft/class_243;"),
"processEntityInfos" to KiltRemapper.remapDescriptor("(Lnet/minecraft/class_3499;Lnet/minecraft/class_1936;Lnet/minecraft/class_2338;Lnet/minecraft/class_3492;Ljava/util/List;)Ljava/util/List;")
)
)

fun fixClass(classNode: ClassNode) {
for (method in classNode.methods) {
val newNodeMap = mutableMapOf<AbstractInsnNode, AbstractInsnNode>()

for (insnNode in method.instructions) {
// Target static invokes specifically
if (insnNode is MethodInsnNode && insnNode.opcode == Opcodes.INVOKESTATIC) {
val specificClass = conflictingMethods.keys.firstOrNull { it == insnNode.owner } ?: continue
val methodList = conflictingMethods[specificClass]!!

if (methodList.none { it.first == insnNode.name && it.second == insnNode.desc })
continue

// prefix with Forge
val node = MethodInsnNode(insnNode.opcode, insnNode.owner, "forge\$${insnNode.name}", insnNode.desc)
newNodeMap[insnNode] = node
}
}

if (newNodeMap.isNotEmpty()) {
for ((oldNode, newNode) in newNodeMap) {
method.instructions.set(oldNode, newNode)
}
}
}
}
}

0 comments on commit 72609c6

Please sign in to comment.