Skip to content

Commit

Permalink
Add ASM patch to FoamFix, I hate mods writing ASM not like everyone else
Browse files Browse the repository at this point in the history
  • Loading branch information
kappa-maintainer committed Jan 11, 2024
1 parent 27599fb commit c4e7ce2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Also check [The Fugue Plane](https://forgottenrealms.fandom.com/wiki/Fugue_Plane
* Aqua Acrobatics
* Advanced Rocketry
* Entity Distance
* FoamFix

## Note
Add ! to start of the file name to fix Lag Goggles.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mappings_version=39-1.12
mod_id=fugue
mod_name=Fugue
mod_main_class=Fugue
mod_version=0.3.2
mod_version=0.3.3
mod_base_package=com.cleanroommc.fugue
mod_authors=kappa_maintainer
mod_description=A mod that patch dead mods for Cleanroom
7 changes: 6 additions & 1 deletion src/main/java/com/cleanroommc/FugueLoadingPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class FugueLoadingPlugin implements IFMLLoadingPlugin, IEarlyMixinLoader
Launch.classLoader.addTransformerExclusionFilter("com.github.terminatornl.laggoggles.");
Launch.classLoader.addTransformerExclusionFilter("quaternary.botaniatweaks.");
Launch.classLoader.addTransformerExclusionFilter("zmaster587.advancedRocketry.asm.");
Launch.classLoader.addTransformerExclusion("pl.asie.foamfix.shared.");
Launch.classLoader.addTransformerExclusion("pl.asie.patchy.handlers.");
Launch.classLoader.addTransformerExclusion("pl.asie.foamfix.coremod.patches.BlockPosPatch$BlockPosMethodVisitor");
Launch.classLoader.addTransformerExclusionFilter("pl.asie.foamfix");
Launch.classLoader.registerTransformer("com.cleanroommc.transformer.InitializerTransformer");
}

Expand All @@ -24,7 +28,8 @@ public String[] getASMTransformerClass() {
return new String[]{
"com.cleanroommc.transformer.EnderCoreTransformerTransformer",
"com.cleanroommc.transformer.InitializerTransformer",
"com.cleanroommc.transformer.ClassTransformerTransformer"
"com.cleanroommc.transformer.ClassTransformerTransformer",
"com.cleanroommc.transformer.FoamFixTransformer"
};
}

Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/cleanroommc/transformer/FoamFixTransformer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.cleanroommc.transformer;

import com.cleanroommc.Fugue;
import net.minecraft.launchwrapper.IClassTransformer;
import net.minecraft.launchwrapper.Launch;
import net.minecraft.launchwrapper.LaunchClassLoader;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.*;

import java.util.LinkedHashMap;

public class FoamFixTransformer implements IClassTransformer {
private int count = 4;
private static final LinkedHashMap<String, String> classMap = new LinkedHashMap<>();
static {
classMap.put("pl.asie.foamfix.coremod.patches.BlockPosPatch", "patchOtherClass");
classMap.put("pl.asie.foamfix.coremod.patches.GhostBusterBlockStateAccessPatch", "apply");
classMap.put("pl.asie.foamfix.coremod.patches.ClassGetSimpleNamePatch", "apply");
classMap.put("pl.asie.patchy.helpers.ConstructorReplacingTransformer", "apply");
}
@Override
public byte[] transform(String s, String s1, byte[] bytes) {
if (bytes == null)
{
return null;
}

if (count == 0)
{
return bytes;
}

for (String clazz : classMap.keySet()) {
if (s1.equals(clazz)) {
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(bytes);
classReader.accept(classNode, 0);
boolean modified = false;
if (classNode.methods != null) {
for (MethodNode methodNode : classNode.methods) {
if (methodNode.name.equals(classMap.get(clazz))) {

InsnList instructions = methodNode.instructions;
if (instructions != null) {
for (AbstractInsnNode insnNode : instructions) {
if (insnNode instanceof LdcInsnNode ldcInsnNode) {
instructions.insert(ldcInsnNode, new LdcInsnNode(Opcodes.ASM9));
instructions.remove(ldcInsnNode);
modified = true;
}
}
}
}
}
}
if (modified) {
Launch.classLoader.addTransformerExclusion(clazz);
count--;
ClassWriter classWriter = new ClassWriter(0);

classNode.accept(classWriter);
return classWriter.toByteArray();
}
return bytes;
}
}
return bytes;
}
}

0 comments on commit c4e7ce2

Please sign in to comment.