|
26 | 26 | import com.falsepattern.lib.mixin.MixinInfo;
|
27 | 27 | import lombok.val;
|
28 | 28 | import org.objectweb.asm.tree.ClassNode;
|
| 29 | +import org.objectweb.asm.tree.MethodNode; |
| 30 | + |
| 31 | +import java.util.List; |
29 | 32 |
|
30 | 33 | 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"; |
31 | 39 | @Override
|
32 | 40 | public String getName() {
|
33 | 41 | return "IMixinPluginTransformer";
|
34 | 42 | }
|
35 | 43 |
|
36 | 44 | @Override
|
37 | 45 | 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)); |
39 | 48 | }
|
40 | 49 |
|
41 | 50 | @Override
|
42 | 51 | 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) { |
44 | 60 | if (!MixinInfo.isMixinBooterLegacy()) {
|
45 | 61 | 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); |
54 | 85 | }
|
55 | 86 | }
|
56 | 87 | }
|
|
0 commit comments