From 2d0466aee4e9c77667d6893036a0940659c31061 Mon Sep 17 00:00:00 2001 From: Cat Core Date: Sat, 27 Apr 2024 15:54:04 +0200 Subject: [PATCH] Update to MRAPI 1.21.x switch to api v1. --- build.gradle | 2 +- gradle.properties | 2 +- .../compat/asm/ASMRemapperTransformer.java | 118 +--------- .../fabricatedforge/ForgeModRemapper.java | 217 +++++++++++++----- src/main/resources/fabric.mod.json | 2 +- 5 files changed, 161 insertions(+), 180 deletions(-) diff --git a/build.gradle b/build.gradle index a53b8488..85676559 100644 --- a/build.gradle +++ b/build.gradle @@ -112,7 +112,7 @@ dependencies { include(group: "local", name: "guava", version: "12.0.1") include(group: "local", name: "bcprov-jdk15on", version: "147") -// modRuntimeOnly(files("./forge-${project.minecraft_version}-${project.forge_version}-universal-intermediary.jar")) + modRuntimeOnly(files("./forge-${project.minecraft_version}-${project.forge_version}-universal-intermediary.jar")) } subprojects { diff --git a/gradle.properties b/gradle.properties index 3bc98da2..d4ebf205 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ org.gradle.jvmargs=-Xmx2G loader_version = 0.15.10 # Legacy Fabric API - mod_remapping_api = 1.20.0 + mod_remapping_api = 1.21.1 forge_version = 6.6.2.534 mixin_extras_version=0.2.1 spasm_version=0.2 diff --git a/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/asm/ASMRemapperTransformer.java b/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/asm/ASMRemapperTransformer.java index 66aa9408..67074c38 100644 --- a/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/asm/ASMRemapperTransformer.java +++ b/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/asm/ASMRemapperTransformer.java @@ -1,8 +1,6 @@ package fr.catcore.fabricatedforge.compat.asm; import fr.catcore.modremapperapi.api.IClassTransformer; -import io.github.fabriccompatibiltylayers.modremappingapi.api.MappingUtils; -import org.jetbrains.annotations.Nullable; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes; @@ -25,71 +23,8 @@ public class ASMRemapperTransformer implements IClassTransformer, Opcodes { "org.lwjgl." }; - private final static Map INSTANCE_REPLACER = arrayToMap(new String[][]{ - {"org/objectweb/asm/tree/FieldInsnNode", "fr/catcore/fabricatedforge/compat/asm/BetterFieldInsnNode"}, - {"org/objectweb/asm/tree/MethodInsnNode", "fr/catcore/fabricatedforge/compat/asm/BetterMethodInsnNode"}, - {"org/objectweb/asm/ClassWriter", "fr/catcore/fabricatedforge/compat/asm/BetterClassWriter"} - }); - - private final static Map>> CALL_REPLACER = new HashMap<>(); - private final static List TRANSFORMED = new ArrayList<>(); - private static void registerCallReplacer(String className, Object... args) { - Map> classMap = CALL_REPLACER.compute(className, (s, stringMapMap) -> new HashMap<>()); - - for (int i = 0; i < args.length; i++) { - String name = (String) args[i]; - String desc = (String) args[i + 1]; - ClassAwareMember member = (ClassAwareMember) args[i + 2]; - - Map methodMap = classMap.compute(name, (s, stringClassAwareMemberMap) -> new HashMap<>()); - methodMap.put(desc, member); - - i += 2; - } - } - - static { - registerCallReplacer("java/lang/Class", - "getDeclaredMethod", "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", new ClassAwareMember( - "fr/catcore/fabricatedforge/compat/asm/RemapAwareClass", - "getDeclaredMethod", - "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;" - ), - - "getDeclaredField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;", new ClassAwareMember( - "fr/catcore/fabricatedforge/compat/asm/RemapAwareClass", - "getDeclaredField", - "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Field;" - ), - - "getMethod", "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", new ClassAwareMember( - "fr/catcore/fabricatedforge/compat/asm/RemapAwareClass", - "getMethod", - "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;" - ), - - "getField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;", new ClassAwareMember( - "fr/catcore/fabricatedforge/compat/asm/RemapAwareClass", - "getField", - "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Field;" - ), - - "forName", "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;", new ClassAwareMember( - "fr/catcore/fabricatedforge/compat/asm/RemapAwareClass", - "forName", - "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;" - ), - - "forName", "(Ljava/lang/String;)Ljava/lang/Class;", new ClassAwareMember( - "fr/catcore/fabricatedforge/compat/asm/RemapAwareClass", - "forName", - "(Ljava/lang/String;)Ljava/lang/Class;" - ) - ); - } - @Override public boolean handlesClass(String s, String s1) { for (String exclude : EXCLUDED) { @@ -110,50 +45,7 @@ public byte[] transformClass(String s, String s1, byte[] bytes) { boolean touched = false; - for (MethodNode methodNode : classNode.methods) { - AbstractInsnNode insnNode = methodNode.instructions.getFirst(); - - if (insnNode != null) { - while (insnNode != null) { - if (insnNode.getOpcode() == NEW && insnNode instanceof TypeInsnNode) { - TypeInsnNode typeInsnNode = (TypeInsnNode) insnNode; - - if (INSTANCE_REPLACER.containsKey(typeInsnNode.desc)) { - typeInsnNode.desc = INSTANCE_REPLACER.get(typeInsnNode.desc); - touched = true; - } - } else if (insnNode instanceof MethodInsnNode) { - MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode; - - if (methodInsnNode.getOpcode() == INVOKESPECIAL) { - if (methodInsnNode.name.equals("") && INSTANCE_REPLACER.containsKey(methodInsnNode.owner)) { - methodInsnNode.owner = INSTANCE_REPLACER.get(methodInsnNode.owner); - touched = true; - } - } else if (methodInsnNode.getOpcode() == INVOKEVIRTUAL || methodInsnNode.getOpcode() == INVOKESTATIC) { - if (CALL_REPLACER.containsKey(methodInsnNode.owner)) { - Map> classMethods = CALL_REPLACER.get(methodInsnNode.owner); - - if (classMethods.containsKey(methodInsnNode.name)) { - Map classMethod = classMethods.get(methodInsnNode.name); - - if (classMethod.containsKey(methodInsnNode.desc)) { - ClassAwareMember classAwareMember = classMethod.get(methodInsnNode.desc); - methodInsnNode.setOpcode(INVOKESTATIC); - methodInsnNode.owner = classAwareMember.owner; - methodInsnNode.name = classAwareMember.name; - methodInsnNode.desc = classAwareMember.desc; - touched = true; - } - } - } - } - } - - insnNode = insnNode.getNext(); - } - } - } + // Transform class here if (touched) { ClassWriter classWriter = new BetterClassWriter(3); @@ -175,12 +67,4 @@ private static Map arrayToMap(String[][] arrays) { return map; } - - public static class ClassAwareMember extends MappingUtils.ClassMember { - public final String owner; - public ClassAwareMember(String owner, String name, @Nullable String desc) { - super(name, desc); - this.owner = owner; - } - } } diff --git a/src/main/java/fr/catcore/fabricatedforge/ForgeModRemapper.java b/src/main/java/fr/catcore/fabricatedforge/ForgeModRemapper.java index 7c6dae6e..7b66da15 100644 --- a/src/main/java/fr/catcore/fabricatedforge/ForgeModRemapper.java +++ b/src/main/java/fr/catcore/fabricatedforge/ForgeModRemapper.java @@ -1,20 +1,16 @@ package fr.catcore.fabricatedforge; -import fr.catcore.modremapperapi.api.ModRemapper; -import fr.catcore.modremapperapi.api.RemapLibrary; -import fr.catcore.modremapperapi.remapping.RemapUtil; -import fr.catcore.modremapperapi.remapping.VisitorInfos; -import net.fabricmc.loader.api.FabricLoader; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.MappingBuilder; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.VisitorInfos; +import net.fabricmc.api.EnvType; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Optional; public class ForgeModRemapper implements ModRemapper { private static final BArrayList FORGE_EXCLUDED = new BArrayList(); - private static final Map> EXCLUDED = new HashMap<>(); @Override public String[] getJarFolders() { @@ -22,63 +18,176 @@ public String[] getJarFolders() { } @Override - public RemapLibrary[] getRemapLibraries() { - return new RemapLibrary[] { - new RemapLibrary( - Constants.FORGE_URL, - FORGE_EXCLUDED, - "forge.zip" - ) - }; + public void addRemapLibraries(List list, EnvType envType) { + list.add(new io.github.fabriccompatibiltylayers.modremappingapi.api.v1.RemapLibrary( + Constants.FORGE_URL, + FORGE_EXCLUDED, + "forge.zip" + )); } @Override - public Map> getExclusions() { - return new HashMap<>(EXCLUDED); + public void registerMappings(MappingBuilder mappingBuilder) { + // ModLoader mappings + mappingBuilder.addMapping("BaseMod", "net/minecraft/BaseMod"); + mappingBuilder.addMapping("EntityRendererProxy", "net/minecraft/EntityRendererProxy"); + mappingBuilder.addMapping("FMLRendererAccessLibrary", "net/minecraft/FMLRendererAccessLibrary"); + mappingBuilder.addMapping("MLProp", "net/minecraft/MLProp"); + mappingBuilder.addMapping("ModLoader", "net/minecraft/ModLoader"); + mappingBuilder.addMapping("ModTextureAnimation", "net/minecraft/ModTextureAnimation"); + mappingBuilder.addMapping("ModTextureStatic", "net/minecraft/ModTextureStatic"); + mappingBuilder.addMapping("TradeEntry", "net/minecraft/TradeEntry"); } @Override - public void getMappingList(RemapUtil.MappingList mappings) { - // ModLoader mappings - mappings.add("BaseMod", "net/minecraft/BaseMod"); - mappings.add("EntityRendererProxy", "net/minecraft/EntityRendererProxy"); - mappings.add("FMLRendererAccessLibrary", "net/minecraft/FMLRendererAccessLibrary"); - mappings.add("MLProp", "net/minecraft/MLProp"); - mappings.add("ModLoader", "net/minecraft/ModLoader"); - mappings.add("ModTextureAnimation", "net/minecraft/ModTextureAnimation"); - mappings.add("ModTextureStatic", "net/minecraft/ModTextureStatic"); - mappings.add("TradeEntry", "net/minecraft/TradeEntry"); + public void registerPreVisitors(io.github.fabriccompatibiltylayers.modremappingapi.api.v1.VisitorInfos visitorInfos) { + } @Override - public void registerVisitors(VisitorInfos infos) { - infos.registerMethodMethodIns( - new VisitorInfos.MethodNamed("net/minecraft/class_197", "setBurnProperties"), - new VisitorInfos.MethodNamed("fr/catcore/fabricatedforge/forged/ReflectionUtils", "Block_setBurnProperties") + public void registerPostVisitors(io.github.fabriccompatibiltylayers.modremappingapi.api.v1.VisitorInfos visitorInfos) { + // Reflection Remappers + visitorInfos.registerInstantiation( + "org/objectweb/asm/tree/FieldInsnNode", + "fr/catcore/fabricatedforge/compat/asm/BetterFieldInsnNode" ); - - infos.registerMethodFieldIns( - new VisitorInfos.MethodNamed("net/minecraft/class_9", "allowedBiomes"), - new VisitorInfos.MethodNamed("fr/catcore/fabricatedforge/forged/reflection/ReflectedStrongholdStructure", "allowedBiomes") + visitorInfos.registerInstantiation( + "org/objectweb/asm/tree/MethodInsnNode", + "fr/catcore/fabricatedforge/compat/asm/BetterMethodInsnNode" ); - infos.registerMethodFieldIns( - new VisitorInfos.MethodNamed("net/minecraft/class_1175", "allowedBiomes"), - new VisitorInfos.MethodNamed("fr/catcore/fabricatedforge/forged/reflection/ReflectedLayeredBiomeSource", "allowedBiomes") + visitorInfos.registerInstantiation( + "org/objectweb/asm/ClassWriter", + "fr/catcore/fabricatedforge/compat/asm/BetterClassWriter" ); - infos.registerMethodFieldIns( - new VisitorInfos.MethodNamed("net/minecraft/class_570", "NAME_TAG_RANGE"), - new VisitorInfos.MethodNamed("fr/catcore/fabricatedforge/forged/ReflectionUtils", "NAME_TAG_RANGE") + visitorInfos.registerMethodInvocation( + "java/lang/Class", + "getDeclaredMethod", + "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", + new VisitorInfos.FullClassMember( + "fr/catcore/fabricatedforge/compat/asm/RemapAwareClass", + "getDeclaredMethod", + "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", + true + ) + ); + visitorInfos.registerMethodInvocation( + "java/lang/Class", + "getDeclaredField", + "(Ljava/lang/String;)Ljava/lang/reflect/Field;", + new VisitorInfos.FullClassMember( + "fr/catcore/fabricatedforge/compat/asm/RemapAwareClass", + "getDeclaredField", + "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Field;", + true + ) ); - infos.registerMethodFieldIns( - new VisitorInfos.MethodNamed("net/minecraft/class_570", "NAME_TAG_RANGE_SNEAK"), - new VisitorInfos.MethodNamed("fr/catcore/fabricatedforge/forged/ReflectionUtils", "NAME_TAG_RANGE_SNEAK") + visitorInfos.registerMethodInvocation( + "java/lang/Class", + "getMethod", + "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", + new VisitorInfos.FullClassMember( + "fr/catcore/fabricatedforge/compat/asm/RemapAwareClass", + "getMethod", + "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", + true + ) + ); + visitorInfos.registerMethodInvocation( + "java/lang/Class", + "getField", + "(Ljava/lang/String;)Ljava/lang/reflect/Field;", + new VisitorInfos.FullClassMember( + "fr/catcore/fabricatedforge/compat/asm/RemapAwareClass", + "getField", + "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Field;", + true + ) + ); + visitorInfos.registerMethodInvocation( + "java/lang/Class", + "forName", + "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;", + new VisitorInfos.FullClassMember( + "fr/catcore/fabricatedforge/compat/asm/RemapAwareClass", + "forName", + "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;", + true + ) + ); + visitorInfos.registerMethodInvocation( + "java/lang/Class", + "forName", + "(Ljava/lang/String;)Ljava/lang/Class;", + new VisitorInfos.FullClassMember( + "fr/catcore/fabricatedforge/compat/asm/RemapAwareClass", + "forName", + "(Ljava/lang/String;)Ljava/lang/Class;", + true + ) ); + // Forge added fields and methods + visitorInfos.registerMethodInvocation( + "net/minecraft/class_197", + "setBurnProperties", + "", + new VisitorInfos.FullClassMember( + "fr/catcore/fabricatedforge/forged/ReflectionUtils", + "Block_setBurnProperties", + null + ) + ); - infos.registerMethodFieldIns( - new VisitorInfos.MethodNamed("net/minecraft/class_988", "PERSISTED_NBT_TAG"), - new VisitorInfos.MethodNamed("fr/catcore/fabricatedforge/forged/ReflectionUtils", "PERSISTED_NBT_TAG") + visitorInfos.registerFieldRef( + "net/minecraft/class_9", + "allowedBiomes", + "", + new VisitorInfos.FullClassMember( + "fr/catcore/fabricatedforge/forged/reflection/ReflectedStrongholdStructure", + "allowedBiomes", + null + ) + ); + visitorInfos.registerFieldRef( + "net/minecraft/class_1175", + "allowedBiomes", + "", + new VisitorInfos.FullClassMember( + "fr/catcore/fabricatedforge/forged/reflection/ReflectedLayeredBiomeSource", + "allowedBiomes", + null + ) + ); + visitorInfos.registerFieldRef( + "net/minecraft/class_570", + "NAME_TAG_RANGE", + "", + new VisitorInfos.FullClassMember( + "fr/catcore/fabricatedforge/forged/ReflectionUtils", + "NAME_TAG_RANGE", + null + ) + ); + visitorInfos.registerFieldRef( + "net/minecraft/class_570", + "NAME_TAG_RANGE_SNEAK", + "", + new VisitorInfos.FullClassMember( + "fr/catcore/fabricatedforge/forged/ReflectionUtils", + "NAME_TAG_RANGE_SNEAK", + null + ) + ); + visitorInfos.registerFieldRef( + "net/minecraft/class_988", + "PERSISTED_NBT_TAG", + "", + new VisitorInfos.FullClassMember( + "fr/catcore/fabricatedforge/forged/ReflectionUtils", + "PERSISTED_NBT_TAG", + null + ) ); } @@ -307,18 +416,6 @@ public void afterRemap() { .put("zz"); } - public static class Entry { - public final String name; - public final String descriptor; - public final String owner; - - public Entry(String name, String descriptor, String owner) { - this.name = name; - this.descriptor = descriptor; - this.owner = owner; - } - } - public static class BArrayList extends fr.catcore.modremapperapi.utils.BArrayList { @Override diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 8d771f2c..bdd749b0 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -24,7 +24,7 @@ "environment": "*", "accessWidener": "fabricated-forge.accesswidener", "entrypoints": { - "mod-remapper-api:modremapper": [ + "mod-remapper-api:modremapper_v1": [ "fr.catcore.fabricatedforge.ForgeModRemapper" ] },