Skip to content

Commit

Permalink
Improve NEI Compat again
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed Aug 21, 2023
1 parent ac95874 commit 10cfa6b
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
public class Dummy implements PreLaunchEntrypoint {
@Override
public void onPreLaunch() {
System.out.println("Gotcha Mixin!");
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"codechickencore.FieldMappingMixin",
"codechickencore.MethodMappingMixin",
"nei.FurnaceRecipeHandlerMixin",
"nei.NEITransformerMixin",
"nei.TMIUninstallerMixin"
],
"client": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 10cfa6b

Please sign in to comment.