From 10cfa6b87dbfa90b9188634b4c1c70b46c71d24e Mon Sep 17 00:00:00 2001 From: Cat Core <34719527+arthurbambou@users.noreply.github.com> Date: Mon, 21 Aug 2023 15:12:38 +0200 Subject: [PATCH] Improve NEI Compat again --- .../compat/BetterClassWriter.java | 39 ++++++++++++++ .../fabricatedforge/compat/CompatUtils.java | 22 ++++++++ .../catcore/fabricatedforge/compat/Dummy.java | 1 + .../compat/mixin/nei/NEITransformerMixin.java | 52 +++++++++++++++++++ .../fabricated-forge.mods.mixins.json | 1 + .../fml/relauncher/IClassTransformer.java | 2 +- 6 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/BetterClassWriter.java create mode 100644 mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/CompatUtils.java create mode 100644 mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/mixin/nei/NEITransformerMixin.java diff --git a/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/BetterClassWriter.java b/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/BetterClassWriter.java new file mode 100644 index 00000000..2af51b92 --- /dev/null +++ b/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/BetterClassWriter.java @@ -0,0 +1,39 @@ +package fr.catcore.fabricatedforge.compat; + +import net.fabricmc.loader.api.FabricLoader; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; + +public class BetterClassWriter extends ClassWriter { + public BetterClassWriter(int flags) { + super(flags); + } + + public BetterClassWriter(ClassReader classReader, int flags) { + super(classReader, flags); + } + + @Override + protected String getCommonSuperClass(String type1, String type2) { + try { + return super.getCommonSuperClass(type1, type2); + } catch (TypeNotPresentException e) { + if ("net/minecraft/class_1071".equals(type1) && "net/minecraft/class_987".equals(type2)) { + return "java/lang/Object"; + } + + if ("net/minecraft/class_987".equals(type1) && "java/lang/Object".equals(type2)) { + return type2; + } + + System.out.println("Common of: " + type1 + " " + type2); + e.printStackTrace(); + return "java/lang/Object"; + } + } + + @Override + protected ClassLoader getClassLoader() { + return FabricLoader.getInstance().getClass().getClassLoader(); + } +} diff --git a/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/CompatUtils.java b/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/CompatUtils.java new file mode 100644 index 00000000..6a1027e8 --- /dev/null +++ b/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/CompatUtils.java @@ -0,0 +1,22 @@ +package fr.catcore.fabricatedforge.compat; + +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.ClassNode; + +public class CompatUtils { + public static ClassNode createNode(byte[] bytes, int parsingOptions) { + ClassNode node = new ClassNode(Opcodes.ASM4); + ClassReader reader = new ClassReader(bytes); + reader.accept(node, parsingOptions); + + return node; + } + + public static byte[] writeClass(ClassNode node, int flags) { + BetterClassWriter classWriter = new BetterClassWriter(flags); + node.accept(classWriter); + + return classWriter.toByteArray(); + } +} diff --git a/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/Dummy.java b/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/Dummy.java index 9f148b0b..6ecac0f3 100644 --- a/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/Dummy.java +++ b/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/Dummy.java @@ -5,5 +5,6 @@ public class Dummy implements PreLaunchEntrypoint { @Override public void onPreLaunch() { + System.out.println("Gotcha Mixin!"); } } diff --git a/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/mixin/nei/NEITransformerMixin.java b/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/mixin/nei/NEITransformerMixin.java new file mode 100644 index 00000000..8e3fbb8f --- /dev/null +++ b/mod-compat/src/main/java/fr/catcore/fabricatedforge/compat/mixin/nei/NEITransformerMixin.java @@ -0,0 +1,52 @@ +package fr.catcore.fabricatedforge.compat.mixin.nei; + +import codechicken.core.asm.ClassHeirachyManager; +import codechicken.core.asm.InstructionComparator; +import codechicken.core.asm.ObfuscationManager; +import codechicken.nei.asm.NEITransformer; +import fr.catcore.fabricatedforge.compat.CompatUtils; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.InsnList; +import org.objectweb.asm.tree.MethodNode; +import org.objectweb.asm.tree.VarInsnNode; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; + +@Mixin(NEITransformer.class) +public class NEITransformerMixin { + /** + * @author CatCore + * @reason fix classwriter + */ + @Overwrite(remap = false) + public byte[] transformer001(String name, byte[] bytes) { + ObfuscationManager.ClassMapping classmap = new ObfuscationManager.ClassMapping("aqh"); + + if (ClassHeirachyManager.classExtends(name, classmap.classname, bytes)) { + ClassNode node = CompatUtils.createNode(bytes, 0); + + ObfuscationManager.MethodMapping methodmap = new ObfuscationManager.MethodMapping("apn", "c", "()V"); + ObfuscationManager.MethodMapping supermap = new ObfuscationManager.MethodMapping(node.superName, methodmap); + InsnList supercall = new InsnList(); + supercall.add(new VarInsnNode(25, 0)); + supercall.add(supermap.toInsn(183)); + + for(MethodNode methodnode : node.methods) { + if (methodmap.matches(methodnode)) { + InsnList importantNodeList = InstructionComparator.getImportantList(methodnode.instructions); + + if (!InstructionComparator.insnListMatches(importantNodeList, supercall, 0)) { + methodnode.instructions.insertBefore(methodnode.instructions.getFirst(), supercall); + System.out.println("Inserted super call into " + name + "." + supermap.name); + } + } + } + + bytes = CompatUtils.writeClass(node, 3); + } + + return bytes; + } +} diff --git a/mod-compat/src/main/resources/fabricated-forge.mods.mixins.json b/mod-compat/src/main/resources/fabricated-forge.mods.mixins.json index 17855345..aee5cb21 100644 --- a/mod-compat/src/main/resources/fabricated-forge.mods.mixins.json +++ b/mod-compat/src/main/resources/fabricated-forge.mods.mixins.json @@ -12,6 +12,7 @@ "codechickencore.FieldMappingMixin", "codechickencore.MethodMappingMixin", "nei.FurnaceRecipeHandlerMixin", + "nei.NEITransformerMixin", "nei.TMIUninstallerMixin" ], "client": [ diff --git a/src/main/java/cpw/mods/fml/relauncher/IClassTransformer.java b/src/main/java/cpw/mods/fml/relauncher/IClassTransformer.java index ab7f46ba..8833e6da 100644 --- a/src/main/java/cpw/mods/fml/relauncher/IClassTransformer.java +++ b/src/main/java/cpw/mods/fml/relauncher/IClassTransformer.java @@ -53,7 +53,7 @@ default boolean handlesClass(String s, String s1) { } transformed.get(this).add(s); - if (s.equals("net.minecraft.class_415") && this.getClass().getName().equals("codechicken.nei.asm.NEITransformer")) return false; + return true; } } \ No newline at end of file