|
| 1 | +package org.embeddedt.modernfix.core.launchplugin.transformer; |
| 2 | + |
| 3 | +import cpw.mods.modlauncher.serviceapi.ILaunchPluginService; |
| 4 | +import org.embeddedt.modernfix.core.launchplugin.CoreLaunchPluginService; |
| 5 | +import org.objectweb.asm.Opcodes; |
| 6 | +import org.objectweb.asm.tree.*; |
| 7 | + |
| 8 | +/** |
| 9 | + * Injects an early-return into {@code CapabilityProvider#areCapsCompatible} that skips lazy |
| 10 | + * initialization when both providers are lazy, uninitialized, of the same class, and carry equal |
| 11 | + * {@code lazyData}. In that case the capabilities are trivially compatible and the full init can |
| 12 | + * be avoided. |
| 13 | + * @author embeddedt |
| 14 | + */ |
| 15 | +public class CapabilityProviderTransformer implements CoreLaunchPluginService.Transformer { |
| 16 | + private static final String OWNER = "net/minecraftforge/common/capabilities/CapabilityProvider"; |
| 17 | + private static final String COMPOUND_TAG = "net/minecraft/nbt/CompoundTag"; |
| 18 | + private static final String HELPER_NAME = "mfix$skipLazyInit"; |
| 19 | + private static final String HELPER_DESC = "(L" + OWNER + ";L" + OWNER + ";)Z"; |
| 20 | + |
| 21 | + @Override |
| 22 | + public int transform(ClassNode node) { |
| 23 | + String targetDesc = "(L" + OWNER + ";)Z"; |
| 24 | + for (MethodNode method : node.methods) { |
| 25 | + if (method.name.equals("areCapsCompatible") && method.desc.equals(targetDesc)) { |
| 26 | + injectCall(method); |
| 27 | + node.methods.add(buildHelper()); |
| 28 | + break; |
| 29 | + } |
| 30 | + } |
| 31 | + return ILaunchPluginService.ComputeFlags.COMPUTE_FRAMES; |
| 32 | + } |
| 33 | + |
| 34 | + private MethodNode buildHelper() { |
| 35 | + MethodNode mn = new MethodNode( |
| 36 | + Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, |
| 37 | + HELPER_NAME, HELPER_DESC, null, null); |
| 38 | + InsnList il = mn.instructions; |
| 39 | + LabelNode returnFalse = new LabelNode(); |
| 40 | + |
| 41 | + // if (!self.isLazy) goto returnFalse |
| 42 | + il.add(new VarInsnNode(Opcodes.ALOAD, 0)); |
| 43 | + il.add(new FieldInsnNode(Opcodes.GETFIELD, OWNER, "isLazy", "Z")); |
| 44 | + il.add(new JumpInsnNode(Opcodes.IFEQ, returnFalse)); |
| 45 | + |
| 46 | + // if (self.initialized) goto returnFalse |
| 47 | + il.add(new VarInsnNode(Opcodes.ALOAD, 0)); |
| 48 | + il.add(new FieldInsnNode(Opcodes.GETFIELD, OWNER, "initialized", "Z")); |
| 49 | + il.add(new JumpInsnNode(Opcodes.IFNE, returnFalse)); |
| 50 | + |
| 51 | + // if (!other.isLazy) goto returnFalse |
| 52 | + il.add(new VarInsnNode(Opcodes.ALOAD, 1)); |
| 53 | + il.add(new FieldInsnNode(Opcodes.GETFIELD, OWNER, "isLazy", "Z")); |
| 54 | + il.add(new JumpInsnNode(Opcodes.IFEQ, returnFalse)); |
| 55 | + |
| 56 | + // if (other.initialized) goto returnFalse |
| 57 | + il.add(new VarInsnNode(Opcodes.ALOAD, 1)); |
| 58 | + il.add(new FieldInsnNode(Opcodes.GETFIELD, OWNER, "initialized", "Z")); |
| 59 | + il.add(new JumpInsnNode(Opcodes.IFNE, returnFalse)); |
| 60 | + |
| 61 | + // if (other.getClass() != self.getClass()) goto returnFalse |
| 62 | + il.add(new VarInsnNode(Opcodes.ALOAD, 1)); |
| 63 | + il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false)); |
| 64 | + il.add(new VarInsnNode(Opcodes.ALOAD, 0)); |
| 65 | + il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false)); |
| 66 | + il.add(new JumpInsnNode(Opcodes.IF_ACMPNE, returnFalse)); |
| 67 | + |
| 68 | + // if (!Objects.equals(self.lazyData, other.lazyData)) goto returnFalse |
| 69 | + il.add(new VarInsnNode(Opcodes.ALOAD, 0)); |
| 70 | + il.add(new FieldInsnNode(Opcodes.GETFIELD, OWNER, "lazyData", "L" + COMPOUND_TAG + ";")); |
| 71 | + il.add(new VarInsnNode(Opcodes.ALOAD, 1)); |
| 72 | + il.add(new FieldInsnNode(Opcodes.GETFIELD, OWNER, "lazyData", "L" + COMPOUND_TAG + ";")); |
| 73 | + il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/Objects", "equals", "(Ljava/lang/Object;Ljava/lang/Object;)Z", false)); |
| 74 | + il.add(new JumpInsnNode(Opcodes.IFEQ, returnFalse)); |
| 75 | + |
| 76 | + il.add(new InsnNode(Opcodes.ICONST_1)); |
| 77 | + il.add(new InsnNode(Opcodes.IRETURN)); |
| 78 | + |
| 79 | + il.add(returnFalse); |
| 80 | + il.add(new InsnNode(Opcodes.ICONST_0)); |
| 81 | + il.add(new InsnNode(Opcodes.IRETURN)); |
| 82 | + |
| 83 | + mn.maxLocals = 2; |
| 84 | + mn.maxStack = 2; |
| 85 | + return mn; |
| 86 | + } |
| 87 | + |
| 88 | + private void injectCall(MethodNode method) { |
| 89 | + InsnList il = new InsnList(); |
| 90 | + LabelNode skip = new LabelNode(); |
| 91 | + |
| 92 | + il.add(new VarInsnNode(Opcodes.ALOAD, 0)); |
| 93 | + il.add(new VarInsnNode(Opcodes.ALOAD, 1)); |
| 94 | + il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, OWNER, HELPER_NAME, HELPER_DESC, false)); |
| 95 | + il.add(new JumpInsnNode(Opcodes.IFEQ, skip)); |
| 96 | + il.add(new InsnNode(Opcodes.ICONST_1)); |
| 97 | + il.add(new InsnNode(Opcodes.IRETURN)); |
| 98 | + il.add(skip); |
| 99 | + |
| 100 | + method.instructions.insert(il); |
| 101 | + } |
| 102 | +} |
0 commit comments