From 16725e38b3ae99da90e93bd669c007b2f6472ca1 Mon Sep 17 00:00:00 2001 From: Alexdoru <57050655+Alexdoru@users.noreply.github.com> Date: Wed, 4 Sep 2024 17:28:33 +0200 Subject: [PATCH] Remove registration of recipes of gravisuite and advanced solar panel (#964) * change how it empties methods in BibliocraftTransformer * nuke GraviSuite recipes * nuke AdvancedSolarPanel recipes --------- Co-authored-by: Martin Robertz --- dependencies.gradle | 2 +- settings.gradle | 2 +- .../coremod/DreamClassTransformer.java | 9 +- .../coremod/IDreamTransformer.java | 18 ++-- .../ItemFocusWardingTransformer.java | 3 +- .../AdvancedSolarPanelTransformer.java | 86 +++++++++++++++++++ .../recipenukers/BibliocraftTransformer.java | 12 ++- .../recipenukers/GraviSuiteTransformer.java | 46 ++++++++++ .../TravellersGearTransformer.java | 5 +- 9 files changed, 159 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/dreammaster/coremod/transformers/recipenukers/AdvancedSolarPanelTransformer.java create mode 100644 src/main/java/com/dreammaster/coremod/transformers/recipenukers/GraviSuiteTransformer.java diff --git a/dependencies.gradle b/dependencies.gradle index 7de113a62..ea0018abd 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -22,7 +22,7 @@ dependencies { compileOnly("com.github.GTNewHorizons:amunra:0.6.0:dev") { transitive = false } compileOnly("com.github.GTNewHorizons:Galacticraft:3.2.4-GTNH:dev") { transitive = false } compileOnly("com.github.GTNewHorizons:ForestryMC:4.9.10:dev") { transitive = false } - compileOnlyApi("com.github.GTNewHorizons:Mobs-Info:0.4.5-GTNH:dev") + compileOnlyApi("com.github.GTNewHorizons:Mobs-Info:0.4.6-GTNH:dev") runtimeOnlyNonPublishable rfg.deobf("curse.maven:biomes-o-plenty-220318:2499612") runtimeOnlyNonPublishable("com.github.GTNewHorizons:WailaHarvestability:1.2.1-GTNH:dev") diff --git a/settings.gradle b/settings.gradle index 242692e5d..3d3ba12c7 100644 --- a/settings.gradle +++ b/settings.gradle @@ -17,7 +17,7 @@ pluginManagement { } plugins { - id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.26' + id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.27' } diff --git a/src/main/java/com/dreammaster/coremod/DreamClassTransformer.java b/src/main/java/com/dreammaster/coremod/DreamClassTransformer.java index 20560644e..7187792a1 100644 --- a/src/main/java/com/dreammaster/coremod/DreamClassTransformer.java +++ b/src/main/java/com/dreammaster/coremod/DreamClassTransformer.java @@ -10,7 +10,9 @@ import org.objectweb.asm.tree.ClassNode; import com.dreammaster.coremod.transformers.ItemFocusWardingTransformer; +import com.dreammaster.coremod.transformers.recipenukers.AdvancedSolarPanelTransformer; import com.dreammaster.coremod.transformers.recipenukers.BibliocraftTransformer; +import com.dreammaster.coremod.transformers.recipenukers.GraviSuiteTransformer; import com.dreammaster.coremod.transformers.recipenukers.TravellersGearTransformer; public class DreamClassTransformer implements IClassTransformer { @@ -21,9 +23,11 @@ public class DreamClassTransformer implements IClassTransformer { public DreamClassTransformer() { // register your transformers here + registerTransformer(new AdvancedSolarPanelTransformer()); registerTransformer(new BibliocraftTransformer()); - registerTransformer(new TravellersGearTransformer()); + registerTransformer(new GraviSuiteTransformer()); registerTransformer(new ItemFocusWardingTransformer()); + registerTransformer(new TravellersGearTransformer()); } private void registerTransformer(IDreamTransformer transformer) { @@ -46,7 +50,8 @@ public byte[] transform(String name, String transformedName, byte[] basicClass) final ClassNode classNode = new ClassNode(); classReader.accept(classNode, 0); final ClassWriter classWriter = new ClassWriter(0); - transformer.transform(classNode).accept(classWriter); + transformer.transform(classNode); + classNode.accept(classWriter); return classWriter.toByteArray(); } diff --git a/src/main/java/com/dreammaster/coremod/IDreamTransformer.java b/src/main/java/com/dreammaster/coremod/IDreamTransformer.java index 39bf0552d..d1d0aefbb 100644 --- a/src/main/java/com/dreammaster/coremod/IDreamTransformer.java +++ b/src/main/java/com/dreammaster/coremod/IDreamTransformer.java @@ -3,7 +3,6 @@ import org.objectweb.asm.Opcodes; import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.InsnList; import org.objectweb.asm.tree.InsnNode; import org.objectweb.asm.tree.LdcInsnNode; import org.objectweb.asm.tree.MethodInsnNode; @@ -17,9 +16,9 @@ public interface IDreamTransformer { String[] targetedClasses(); /** - * Returns the transformed ClassNode + * Performs transformations to the ClassNode */ - ClassNode transform(ClassNode classNode); + void transform(ClassNode classNode); // ================================================= @@ -50,13 +49,16 @@ default boolean checkLdcInsnNode(AbstractInsnNode insnNode, Object obj) { /** * Method to empty the methodNode and replace its body with just a RETURN Opcode */ - default void emptyTheMethodNode(MethodNode methodNode) { - final InsnList insnList = new InsnList(); - insnList.add(new InsnNode(Opcodes.RETURN)); - methodNode.instructions = insnList; - methodNode.localVariables.clear(); + default void emptyMethodNode(MethodNode methodNode) { + if (!methodNode.desc.equals("()V")) { + throw new IllegalArgumentException("emptyMethodNode can only be used for ()V methods"); + } + methodNode.instructions.clear(); + methodNode.instructions.add(new InsnNode(Opcodes.RETURN)); methodNode.maxStack = 0; methodNode.maxLocals = 0; + methodNode.localVariables = null; + methodNode.tryCatchBlocks = null; } } diff --git a/src/main/java/com/dreammaster/coremod/transformers/ItemFocusWardingTransformer.java b/src/main/java/com/dreammaster/coremod/transformers/ItemFocusWardingTransformer.java index da98660aa..671beae67 100644 --- a/src/main/java/com/dreammaster/coremod/transformers/ItemFocusWardingTransformer.java +++ b/src/main/java/com/dreammaster/coremod/transformers/ItemFocusWardingTransformer.java @@ -25,7 +25,7 @@ public String[] targetedClasses() { } @Override - public ClassNode transform(ClassNode classNode) { + public void transform(ClassNode classNode) { if (DreamCoreMod.patchItemFocusWarding) { DreamCoreMod.logger.info("Transforming ItemFocusWarding"); for (final MethodNode methodNode : classNode.methods) { @@ -73,7 +73,6 @@ public ClassNode transform(ClassNode classNode) { mv.visitMaxs(1, 2); mv.visitEnd(); } - return classNode; } } diff --git a/src/main/java/com/dreammaster/coremod/transformers/recipenukers/AdvancedSolarPanelTransformer.java b/src/main/java/com/dreammaster/coremod/transformers/recipenukers/AdvancedSolarPanelTransformer.java new file mode 100644 index 000000000..39f1fa415 --- /dev/null +++ b/src/main/java/com/dreammaster/coremod/transformers/recipenukers/AdvancedSolarPanelTransformer.java @@ -0,0 +1,86 @@ +package com.dreammaster.coremod.transformers.recipenukers; + +import java.util.ListIterator; + +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.AbstractInsnNode; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.FieldInsnNode; +import org.objectweb.asm.tree.InsnNode; +import org.objectweb.asm.tree.MethodInsnNode; +import org.objectweb.asm.tree.MethodNode; + +import com.dreammaster.coremod.DreamCoreMod; +import com.dreammaster.coremod.IDreamTransformer; + +public class AdvancedSolarPanelTransformer implements IDreamTransformer { + + @Override + public String[] targetedClasses() { + return new String[] { "advsolar.common.AdvancedSolarPanel" }; + } + + @Override + public void transform(ClassNode classNode) { + for (MethodNode mn : classNode.methods) { + if (mn.name.equals("afterModsLoaded") + && mn.desc.equals("(Lcpw/mods/fml/common/event/FMLPostInitializationEvent;)V")) { + // changes afterModsLoaded method to : + // public void afterModsLoaded(FMLPostInitializationEvent event) { + // MTAPI.manager = MTRecipeManager.instance; + // } + mn.instructions.clear(); + mn.instructions.add( + new FieldInsnNode( + Opcodes.GETSTATIC, + "advsolar/utils/MTRecipeManager", + "instance", + "Ladvsolar/utils/MTRecipeManager;")); + mn.instructions.add( + new FieldInsnNode( + Opcodes.PUTSTATIC, + "advsolar/api/MTAPI", + "manager", + "Ladvsolar/api/IMTRecipeManager;")); + mn.instructions.add(new InsnNode(Opcodes.RETURN)); + DreamCoreMod.logger.info("Taking a sledgehammer to {}.{}{}", classNode.name, mn.name, mn.desc); + } else if (mn.name.equals("preInit") + && mn.desc.equals("(Lcpw/mods/fml/common/event/FMLPreInitializationEvent;)V")) { + // deletes the 4 lines of Recipes.compressor.addRecipe..... + final ListIterator it = mn.instructions.iterator(); + boolean isDeleting = false; + int ordinal = 0; + while (it.hasNext()) { + final AbstractInsnNode insnNode = it.next(); + if (isCompressorFieldNode(insnNode)) { + isDeleting = true; + } + if (isDeleting) { + it.remove(); + } + if (isAddRecipeMethodNode(insnNode)) { + ordinal++; + if (ordinal == 4) break; + } + } + DreamCoreMod.logger.info("Taking a sledgehammer to {}.{}{}", classNode.name, mn.name, mn.desc); + } + } + } + + private static boolean isCompressorFieldNode(AbstractInsnNode node) { + return node instanceof FieldInsnNode && node.getOpcode() == Opcodes.GETSTATIC + && ((FieldInsnNode) node).owner.equals("ic2/api/recipe/Recipes") + && ((FieldInsnNode) node).name.equals("compressor") + && ((FieldInsnNode) node).desc.equals("Lic2/api/recipe/IMachineRecipeManager;"); + } + + private static boolean isAddRecipeMethodNode(AbstractInsnNode node) { + return node instanceof MethodInsnNode && node.getOpcode() == Opcodes.INVOKEINTERFACE + && ((MethodInsnNode) node).owner.equals("ic2/api/recipe/IMachineRecipeManager") + && ((MethodInsnNode) node).name.equals("addRecipe") + && ((MethodInsnNode) node).desc.equals( + "(Lic2/api/recipe/IRecipeInput;Lnet/minecraft/nbt/NBTTagCompound;[Lnet/minecraft/item/ItemStack;)V"); + } + +} diff --git a/src/main/java/com/dreammaster/coremod/transformers/recipenukers/BibliocraftTransformer.java b/src/main/java/com/dreammaster/coremod/transformers/recipenukers/BibliocraftTransformer.java index 62e4b2535..4ee500c1c 100644 --- a/src/main/java/com/dreammaster/coremod/transformers/recipenukers/BibliocraftTransformer.java +++ b/src/main/java/com/dreammaster/coremod/transformers/recipenukers/BibliocraftTransformer.java @@ -16,15 +16,13 @@ public String[] targetedClasses() { } @Override - public ClassNode transform(ClassNode classNode) { - for (final MethodNode methodNode : classNode.methods) { - if ("addRecipies".equals(methodNode.name) || "initRecipes".equals(methodNode.name)) { - DreamCoreMod.logger - .info("Taking a sledgehammer to {}.{}{}", classNode.name, methodNode.name, methodNode.desc); - emptyTheMethodNode(methodNode); + public void transform(ClassNode classNode) { + for (final MethodNode mn : classNode.methods) { + if (("addRecipies".equals(mn.name) || "initRecipes".equals(mn.name)) && "()V".equals(mn.desc)) { + DreamCoreMod.logger.info("Taking a sledgehammer to {}.{}{}", classNode.name, mn.name, mn.desc); + emptyMethodNode(mn); } } - return classNode; } } diff --git a/src/main/java/com/dreammaster/coremod/transformers/recipenukers/GraviSuiteTransformer.java b/src/main/java/com/dreammaster/coremod/transformers/recipenukers/GraviSuiteTransformer.java new file mode 100644 index 000000000..7d0df5176 --- /dev/null +++ b/src/main/java/com/dreammaster/coremod/transformers/recipenukers/GraviSuiteTransformer.java @@ -0,0 +1,46 @@ +package com.dreammaster.coremod.transformers.recipenukers; + +import java.util.ListIterator; + +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.AbstractInsnNode; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.InsnNode; +import org.objectweb.asm.tree.MethodNode; +import org.objectweb.asm.tree.TypeInsnNode; + +import com.dreammaster.coremod.DreamCoreMod; +import com.dreammaster.coremod.IDreamTransformer; + +public class GraviSuiteTransformer implements IDreamTransformer { + + @Override + public String[] targetedClasses() { + return new String[] { "gravisuite.GraviSuite" }; + } + + @Override + public void transform(ClassNode classNode) { + for (MethodNode mn : classNode.methods) { + if (mn.name.equals("afterModsLoaded") + && mn.desc.equals("(Lcpw/mods/fml/common/event/FMLPostInitializationEvent;)V")) { + final ListIterator it = mn.instructions.iterator(); + boolean delete = false; + while (it.hasNext()) { + final AbstractInsnNode insnNode = it.next(); + if (!delete && insnNode instanceof TypeInsnNode + && insnNode.getOpcode() == Opcodes.NEW + && ((TypeInsnNode) insnNode).desc.equals("net/minecraft/item/ItemStack")) { + delete = true; + } + if (delete) { + it.remove(); + } + } + mn.instructions.add(new InsnNode(Opcodes.RETURN)); + DreamCoreMod.logger.info("Taking a sledgehammer to {}.{}{}", classNode.name, mn.name, mn.desc); + } + } + } + +} diff --git a/src/main/java/com/dreammaster/coremod/transformers/recipenukers/TravellersGearTransformer.java b/src/main/java/com/dreammaster/coremod/transformers/recipenukers/TravellersGearTransformer.java index 7fdc2f3c0..0925f8850 100644 --- a/src/main/java/com/dreammaster/coremod/transformers/recipenukers/TravellersGearTransformer.java +++ b/src/main/java/com/dreammaster/coremod/transformers/recipenukers/TravellersGearTransformer.java @@ -18,7 +18,7 @@ public String[] targetedClasses() { } @Override - public ClassNode transform(ClassNode classNode) { + public void transform(ClassNode classNode) { for (MethodNode methodNode : classNode.methods) { if (checkMethodNode(methodNode, "init", "(Lcpw/mods/fml/common/event/FMLInitializationEvent;)V")) { /* @@ -45,14 +45,13 @@ public ClassNode transform(ClassNode classNode) { classNode.name, methodNode.name, methodNode.desc); - return classNode; + return; } else if (addItemCallsCount == 5) { iterator.remove(); } } } } - return classNode; } }