Skip to content

Commit ca9292c

Browse files
committed
fix mixin plugin transformer again
1 parent 673269f commit ca9292c

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

src/main/java/com/falsepattern/lib/internal/asm/IMixinPluginTransformer.java

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,62 @@
2626
import com.falsepattern.lib.mixin.MixinInfo;
2727
import lombok.val;
2828
import org.objectweb.asm.tree.ClassNode;
29+
import org.objectweb.asm.tree.MethodNode;
30+
31+
import java.util.List;
2932

3033
public class IMixinPluginTransformer implements IClassNodeTransformer {
34+
private static final String CLASSNODE_MIXINBOOTER = "org/spongepowered/libraries/org/objectweb/asm/tree/ClassNode";
35+
private static final String CLASSNODE_SPONGEMIXINS = "org/spongepowered/asm/lib/tree/ClassNode";
36+
private static final String IMIXINPLUGIN = Tags.GROUPNAME + ".mixin.IMixinPlugin";
37+
private static final String IMIXINPLUGIN_INTERNAL = IMIXINPLUGIN.replace('.', '/');
38+
private static final String IMIXINCONFIGPLUGIN_INTERNAL = "org/spongepowered/asm/mixin/extensibility/IMixinConfigPlugin";
3139
@Override
3240
public String getName() {
3341
return "IMixinPluginTransformer";
3442
}
3543

3644
@Override
3745
public boolean shouldTransform(ClassNode cn, String transformedName, boolean obfuscated) {
38-
return transformedName.equals(Tags.GROUPNAME + ".mixin.IMixinPlugin");
46+
return transformedName.equals(IMIXINPLUGIN) ||
47+
cn.interfaces.stream().anyMatch((i) -> i.equals(IMIXINPLUGIN_INTERNAL) || i.equals(IMIXINCONFIGPLUGIN_INTERNAL));
3948
}
4049

4150
@Override
4251
public void transform(ClassNode cn, String transformedName, boolean obfuscated) {
43-
val methods = cn.methods;
52+
if (transformedName.equals(IMIXINPLUGIN)) {
53+
transformIMixinPlugin(cn);
54+
} else {
55+
transformPlugin(cn, transformedName);
56+
}
57+
}
58+
59+
private static void transformIMixinPlugin(ClassNode cn) {
4460
if (!MixinInfo.isMixinBooterLegacy()) {
4561
FPTransformer.LOG.info("Could not detect MixinBooterLegacy. Converting IMixinPlugin to legacy compat mode.");
46-
for (val method : methods) {
47-
if (method.name.equals("preApply") || method.name.equals("postApply")) {
48-
method.desc = method.desc.replace("org/spongepowered/libraries/org/objectweb/asm/tree/ClassNode",
49-
"org/spongepowered/asm/lib/tree/ClassNode");
50-
for (val local: method.localVariables) {
51-
local.desc = local.desc.replace("org/spongepowered/libraries/org/objectweb/asm/tree/ClassNode",
52-
"org/spongepowered/asm/lib/tree/ClassNode");
53-
}
62+
doRename(cn.methods, CLASSNODE_MIXINBOOTER, CLASSNODE_SPONGEMIXINS);
63+
}
64+
}
65+
66+
private static void transformPlugin(ClassNode cn, String transformedName) {
67+
FPTransformer.LOG.info("Transforming " + transformedName + " to fit current mixin environment.");
68+
if (!MixinInfo.isMixinBooterLegacy()) {
69+
doRename(cn.methods, CLASSNODE_MIXINBOOTER, CLASSNODE_SPONGEMIXINS);
70+
} else {
71+
doRename(cn.methods, CLASSNODE_SPONGEMIXINS, CLASSNODE_MIXINBOOTER);
72+
}
73+
}
74+
75+
private static void doRename(List<MethodNode> methods, String from, String to) {
76+
for (val method : methods) {
77+
if (method.name.equals("preApply") || method.name.equals("postApply")) {
78+
val newDesc = method.desc.replace(from, to);
79+
if (!method.desc.equals(newDesc)) {
80+
FPTransformer.LOG.debug(method.name + method.desc + " -> " + method.name + newDesc);
81+
}
82+
method.desc = newDesc;
83+
for (val local: method.localVariables) {
84+
local.desc = local.desc.replace(from, to);
5485
}
5586
}
5687
}

0 commit comments

Comments
 (0)