Skip to content

Commit

Permalink
Remove deprecated transformer code
Browse files Browse the repository at this point in the history
- Best to remove this dead code to avoid unnecessary classloading
  • Loading branch information
jchung01 committed Jun 7, 2024
1 parent be666ef commit 883887b
Showing 1 changed file with 0 additions and 142 deletions.
142 changes: 0 additions & 142 deletions src/main/java/org/dimdev/jeid/core/JEIDTransformer.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package org.dimdev.jeid.core;

import net.minecraft.launchwrapper.IClassTransformer;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.RegistryNamespaced;
import net.minecraftforge.fml.relauncher.FMLLaunchHandler;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.*;

import java.util.Iterator;
Expand All @@ -25,9 +20,6 @@
* <a href="https://github.com/zabi94/MaxPotionIDExtender">MaxPotionIDExtender by Zabi94</a>
*/
public class JEIDTransformer implements IClassTransformer {
@Deprecated
public static RegistryNamespaced<ResourceLocation, Potion> REGISTRY;

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
if (transformedName.equals("net.minecraft.item.ItemStack")) {
Expand Down Expand Up @@ -107,106 +99,6 @@ private byte[] transformItemStack(byte[] basicClass) {
return cw.toByteArray();
}

/**
* This is no longer used as it has been converted into a mixin.
* @see org.dimdev.jeid.mixin.core.potion.MixinSPacketRemoveEntityEffect
*/
@Deprecated
private byte[] transformSPacketRemoveEntityEffect(byte[] basicClass) {
ClassReader cr = new ClassReader(basicClass);
ClassNode cn = new ClassNode();
cr.accept(cn, 0);

// Redirect readUnsignedByte (aka short) to readInt
String packetBuffer = "net/minecraft/network/PacketBuffer";
String descPacketData = "(L" + packetBuffer + ";)V";
MethodNode rpd = locateMethod(cn, descPacketData, "readPacketData", "func_148837_a");
AbstractInsnNode target = locateTargetInsn(rpd, n -> n.getOpcode() == Opcodes.INVOKEVIRTUAL && ((MethodInsnNode) n).name.equals("readUnsignedByte"));
rpd.instructions.insert(target, new MethodInsnNode(Opcodes.INVOKEVIRTUAL, packetBuffer, "readInt", "()I", false));
rpd.instructions.remove(target);

// Redirect writeByte to writeInt
MethodNode wpd = locateMethod(cn, descPacketData, "writePacketData", "func_148840_b");
target = locateTargetInsn(wpd, n -> n.getOpcode() == Opcodes.INVOKEVIRTUAL && ((MethodInsnNode) n).name.equals("writeByte"));
wpd.instructions.insert(target, new MethodInsnNode(Opcodes.INVOKEVIRTUAL, packetBuffer, "writeInt", "(I)Lio/netty/buffer/ByteBuf;", false));
wpd.instructions.remove(target);

ClassWriter cw = new ClassWriter(0);
cn.accept(cw);
return cw.toByteArray();
}

/**
* This is no longer used as it has been converted into a mixin.
* @see org.dimdev.jeid.mixin.core.potion.MixinSPacketEntityEffect
*/
@Deprecated
private byte[] transformSPacketEntityEffect(byte[] basicClass) {
ClassReader cr = new ClassReader(basicClass);
ClassNode cn = new ClassNode();
cr.accept(cn, 0);

String potionEffect = "net/minecraft/potion/PotionEffect";
String sPacketEntityEffect = "net/minecraft/network/play/server/SPacketEntityEffect";
String packetBuffer = "net/minecraft/network/PacketBuffer";
String descPacketData = "(L" + packetBuffer + ";)V";
// Add new field, int effectInt
cn.fields.add(new FieldNode(Opcodes.ACC_PUBLIC, "effectInt", "I", null, 0));

MethodNode mnInit = locateMethod(cn, "(IL" + potionEffect + ";)V", "<init>");
Iterator<AbstractInsnNode> i = mnInit.instructions.iterator();
AbstractInsnNode targetNode = null;
int line = 0;
while (i.hasNext() && targetNode == null) {
AbstractInsnNode node = i.next();
if (node instanceof LineNumberNode) {
if (line == 1) {
targetNode = node;
}
line++;
}
}

if (targetNode == null) {
throw new RuntimeException("Can't find target node for SPacketEntityEffect constructor");
}

// Initialize effectInt in constructors
// These are reversed, they get pushed down the stack
mnInit.instructions.insert(targetNode, new FieldInsnNode(Opcodes.PUTFIELD, sPacketEntityEffect, "effectInt", "I"));
mnInit.instructions.insert(targetNode, new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(this.getClass()), "getIdFromPotEffect", "(L" + potionEffect + ";)I", false));
mnInit.instructions.insert(targetNode, new VarInsnNode(Opcodes.ALOAD, 2));
mnInit.instructions.insert(targetNode, new VarInsnNode(Opcodes.ALOAD, 0));

MethodNode mnEmptyInit = locateMethod(cn, "()V", "<init>");
AbstractInsnNode tgt = locateTargetInsn(mnEmptyInit, n -> n.getOpcode() == Opcodes.RETURN);
mnEmptyInit.instructions.insertBefore(tgt, new VarInsnNode(Opcodes.ALOAD, 0));
mnEmptyInit.instructions.insertBefore(tgt, new LdcInsnNode(0));
mnEmptyInit.instructions.insertBefore(tgt, new FieldInsnNode(Opcodes.PUTFIELD, sPacketEntityEffect, "effectInt", "I"));

// Patch readPacketData: this.effectInt = buf.readVarInt();
MethodNode mnReadPacket = locateMethod(cn, descPacketData, "readPacketData", "func_148837_a");
AbstractInsnNode target = locateTargetInsn(mnReadPacket, n -> n.getOpcode() == Opcodes.RETURN).getPrevious().getPrevious();
mnReadPacket.instructions.insertBefore(target, new VarInsnNode(Opcodes.ALOAD, 0));
mnReadPacket.instructions.insertBefore(target, new VarInsnNode(Opcodes.ALOAD, 1));
mnReadPacket.instructions.insertBefore(target, new MethodInsnNode(Opcodes.INVOKEVIRTUAL, packetBuffer, (JEIDLoadingPlugin.isDeobf ? "readVarInt" : "func_150792_a"), "()I", false));
mnReadPacket.instructions.insertBefore(target, new FieldInsnNode(Opcodes.PUTFIELD, sPacketEntityEffect, "effectInt", "I"));

// Patch writePacketData: buf.writeVarInt(effectInt);
MethodNode mnWritePacket = locateMethod(cn, descPacketData, "writePacketData", "func_148840_b");
AbstractInsnNode wpTarget = locateTargetInsn(mnWritePacket, n -> n.getOpcode() == Opcodes.RETURN).getPrevious().getPrevious();
mnWritePacket.instructions.insertBefore(wpTarget, new VarInsnNode(Opcodes.ALOAD, 1));
mnWritePacket.instructions.insertBefore(wpTarget, new VarInsnNode(Opcodes.ALOAD, 0));
mnWritePacket.instructions.insertBefore(wpTarget, new FieldInsnNode(Opcodes.GETFIELD, sPacketEntityEffect, "effectInt", "I"));
mnWritePacket.instructions.insertBefore(wpTarget, new MethodInsnNode(Opcodes.INVOKEVIRTUAL, packetBuffer, (JEIDLoadingPlugin.isDeobf ? "writeVarInt" : "func_150787_b"), "(I)L" + packetBuffer + ";", false));
mnWritePacket.instructions.insertBefore(wpTarget, new InsnNode(Opcodes.POP));

// Max stack size has been modified
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
cn.accept(cw);
return cw.toByteArray();
}

private byte[] transformPotionEffect(byte[] basicClass) {
ClassReader cr = new ClassReader(basicClass);
ClassNode cn = new ClassNode();
Expand Down Expand Up @@ -236,39 +128,5 @@ private byte[] transformPotionEffect(byte[] basicClass) {
cn.accept(cw);
return cw.toByteArray();
}

/**
* This is no longer used as it has been converted into a mixin.
* @see org.dimdev.jeid.mixin.core.potion.client.MixinNetHandlerPlayClient
*/
@Deprecated
private byte[] transformNetHandlerPlayClient(byte[] basicClass) {
ClassReader cr = new ClassReader(basicClass);
ClassNode cn = new ClassNode();
cr.accept(cn, 0);

String sPacketEntityEffect = "net/minecraft/network/play/server/SPacketEntityEffect";
MethodNode mn = locateMethod(cn, "(L" + sPacketEntityEffect + ";)V", "handleEntityEffect", "func_147260_a");
AbstractInsnNode target = locateTargetInsn(mn, n -> n.getOpcode() == Opcodes.SIPUSH);
mn.instructions.remove(target.getPrevious());
mn.instructions.remove(target.getNext());
// Redirect getEffectID to effectInt
mn.instructions.insertBefore(target, new FieldInsnNode(Opcodes.GETFIELD, sPacketEntityEffect, "effectInt", "I"));
// Remove bitwise operation
mn.instructions.remove(target);

ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
cn.accept(cw);
return cw.toByteArray();
}

/**
* No longer used.
* @see JEIDTransformer#transformSPacketRemoveEntityEffect(byte[])
*/
@Deprecated
public static int getIdFromPotEffect(PotionEffect pe) {
return REGISTRY.getIDForObject(pe.getPotion());
}
}

0 comments on commit 883887b

Please sign in to comment.