use of org.objectweb.asm.tree.JumpInsnNode in project Random-Things by lumien231.
the class ClassTransformer method patchRenderLivingBase.
private byte[] patchRenderLivingBase(byte[] basicClass) {
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(basicClass);
classReader.accept(classNode, 0);
logger.log(Level.DEBUG, "Found RenderLivingBase Class: " + classNode.name);
MethodNode canRenderName = null;
for (MethodNode mn : classNode.methods) {
if (mn.name.equals(MCPNames.method("func_177070_b"))) {
canRenderName = mn;
break;
}
}
if (canRenderName != null) {
logger.log(Level.DEBUG, "- Found canRenderName (1/1)");
LabelNode l1 = new LabelNode(new Label());
InsnList toInsert = new InsnList();
toInsert.add(new VarInsnNode(ALOAD, 1));
toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "canRenderName", "(Lnet/minecraft/entity/EntityLivingBase;)Z", false));
toInsert.add(new JumpInsnNode(IFGT, l1));
toInsert.add(new InsnNode(ICONST_0));
toInsert.add(new InsnNode(IRETURN));
toInsert.add(l1);
canRenderName.instructions.insert(toInsert);
}
CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
classNode.accept(writer);
return writer.toByteArray();
}
use of org.objectweb.asm.tree.JumpInsnNode in project Random-Things by lumien231.
the class ClassTransformer method patchLiquidBlock.
private byte[] patchLiquidBlock(byte[] basicClass) {
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(basicClass);
classReader.accept(classNode, 0);
logger.log(Level.DEBUG, "Found BlockLiquid Class: " + classNode.name);
MethodNode shouldSideBeRendered = null;
for (MethodNode mn : classNode.methods) {
if (mn.name.equals(MCPNames.method("func_176225_a"))) {
shouldSideBeRendered = mn;
break;
}
}
if (shouldSideBeRendered != null) {
logger.log(Level.DEBUG, " - Found shouldSideBeRendered (1/1)");
LabelNode l1 = new LabelNode(new Label());
InsnList toInsert = new InsnList();
toInsert.add(new VarInsnNode(ALOAD, 0));
toInsert.add(new VarInsnNode(ALOAD, 1));
toInsert.add(new VarInsnNode(ALOAD, 2));
toInsert.add(new VarInsnNode(ALOAD, 3));
toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "shouldLiquidSideBeRendered", "(Lnet/minecraft/block/BlockLiquid;Lnet/minecraft/world/IBlockAccess;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/EnumFacing;)I", false));
toInsert.add(new InsnNode(DUP));
toInsert.add(new JumpInsnNode(IFLT, l1));
toInsert.add(new InsnNode(IRETURN));
toInsert.add(l1);
toInsert.add(new InsnNode(POP));
shouldSideBeRendered.instructions.insert(toInsert);
}
CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
classNode.accept(writer);
return writer.toByteArray();
}
use of org.objectweb.asm.tree.JumpInsnNode in project Galacticraft by micdoodle8.
the class TweakTransformer method load.
public static void load() {
CodeChickenCoreModContainer.loadConfig();
tweaks = CodeChickenCoreModContainer.config.getTag("tweaks").setComment("Various tweaks that can be applied to game mechanics.").useBraces();
tweaks.removeTag("persistantLava");
if (tweaks.getTag("environmentallyFriendlyCreepers").setComment("If set to true, creepers will not destroy landscape. (A version of mobGriefing setting just for creepers)").getBooleanValue(false)) {
transformer.add(new MethodReplacer(new ObfMapping("net/minecraft/entity/monster/EntityCreeper", "func_146077_cc", "()V"), blocks.get("d_environmentallyFriendlyCreepers"), blocks.get("environmentallyFriendlyCreepers")));
}
if (tweaks.getTag("finiteWater").setComment("If set to true two adjacent water source blocks will not generate a third.").getBooleanValue(false)) {
transformer.add(new MethodTransformer(new ObfMapping("net/minecraft/block/BlockDynamicLiquid", "func_149674_a", "(Lnet/minecraft/world/World;IIILjava/util/Random;)V")) {
@Override
public void transform(MethodNode mv) {
InsnListSection key = findOnce(mv.instructions, blocks.get("finiteWater").list);
key.setLast(((JumpInsnNode) key.getLast()).label);
key.remove();
}
});
}
}
use of org.objectweb.asm.tree.JumpInsnNode in project BuildCraft by BuildCraft.
the class EventBusProviderASM method generateDirectHandler.
private byte[] generateDirectHandler(Method meth, Class<?> parClass, String clsName) {
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
ClassNode node = new ClassNode();
node.name = clsName.replace('.', '/');
node.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL;
node.interfaces = Lists.newArrayList(IEventHandler.class.getName().replace('.', '/'));
node.version = Opcodes.V1_6;
node.superName = "java/lang/Object";
String fd = Type.getDescriptor(meth.getDeclaringClass());
node.fields.add(new FieldNode(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "listener", fd, null, null));
// This method does:
// public ClassName(ListenerObject obj) {
// super();
// this.listener = obj;
// }
{
MethodNode consturctorMethod = new MethodNode();
consturctorMethod.access = Opcodes.ACC_PUBLIC;
consturctorMethod.desc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(meth.getDeclaringClass()));
consturctorMethod.name = "<init>";
consturctorMethod.exceptions = Lists.newArrayList();
consturctorMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
consturctorMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false));
consturctorMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
consturctorMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
consturctorMethod.instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, node.name, "listener", fd));
consturctorMethod.instructions.add(new InsnNode(Opcodes.RETURN));
node.methods.add(consturctorMethod);
}
// This method does:
// public final void handle(Object event) {
// if (!(event instanceof EventClass)) return;
// listener.<method_name>((EventClass) event);
// }
{
MethodNode generationMethod = new MethodNode();
// public final void handle(Object)
generationMethod.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL;
generationMethod.desc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class));
generationMethod.name = "handle";
generationMethod.exceptions = Lists.newArrayList();
// This block does:
// if (!(event instanceof EventClass)) return;
{
// ADD the first object (given as [this, event])
// -> event
generationMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
// event -> event instanceof PAR_CLASS -> boolean
generationMethod.instructions.add(new TypeInsnNode(Opcodes.INSTANCEOF, Type.getInternalName(parClass)));
LabelNode instanceLabel = new LabelNode();
// boolean -> if (boolean) GOTO instanceLabel ->
generationMethod.instructions.add(new JumpInsnNode(Opcodes.IFNE, instanceLabel));
// return;
generationMethod.instructions.add(new InsnNode(Opcodes.RETURN));
generationMethod.instructions.add(instanceLabel);
}
// This block does:
// listener.<method_name>(event);
{
generationMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
// -> ListenerObject
String desc = Type.getDescriptor(meth.getDeclaringClass());
generationMethod.instructions.add(new FieldInsnNode(Opcodes.GETFIELD, node.name, "listener", desc));
// -> Object
generationMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
// CheckCast
generationMethod.instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, Type.getInternalName(parClass)));
// ListenerObject, EventObject -> <method_name> ->
generationMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Type.getInternalName(meth.getDeclaringClass()), meth.getName(), Type.getMethodDescriptor(meth), false));
}
// return;
generationMethod.instructions.add(new InsnNode(Opcodes.RETURN));
node.methods.add(generationMethod);
}
node.accept(writer);
byte[] bytecode = writer.toByteArray();
return bytecode;
}
use of org.objectweb.asm.tree.JumpInsnNode in project soot by Sable.
the class AsmMethodSource method convert.
private void convert() {
ArrayDeque<Edge> worklist = new ArrayDeque<Edge>();
for (LabelNode ln : trapHandlers.keySet()) {
if (checkInlineExceptionHandler(ln))
handleInlineExceptionHandler(ln, worklist);
else
worklist.add(new Edge(ln, new ArrayList<Operand>()));
}
worklist.add(new Edge(instructions.getFirst(), new ArrayList<Operand>()));
conversionWorklist = worklist;
edges = HashBasedTable.create(1, 1);
do {
Edge edge = worklist.pollLast();
AbstractInsnNode insn = edge.insn;
stack = edge.stack;
edge.stack = null;
do {
int type = insn.getType();
if (type == FIELD_INSN) {
convertFieldInsn((FieldInsnNode) insn);
} else if (type == IINC_INSN) {
convertIincInsn((IincInsnNode) insn);
} else if (type == INSN) {
convertInsn((InsnNode) insn);
int op = insn.getOpcode();
if ((op >= IRETURN && op <= RETURN) || op == ATHROW) {
break;
}
} else if (type == INT_INSN) {
convertIntInsn((IntInsnNode) insn);
} else if (type == LDC_INSN) {
convertLdcInsn((LdcInsnNode) insn);
} else if (type == JUMP_INSN) {
JumpInsnNode jmp = (JumpInsnNode) insn;
convertJumpInsn(jmp);
int op = jmp.getOpcode();
if (op == JSR)
throw new UnsupportedOperationException("JSR!");
if (op != GOTO) {
/* ifX opcode, i.e. two successors */
AbstractInsnNode next = insn.getNext();
addEdges(insn, next, Collections.singletonList(jmp.label));
} else {
addEdges(insn, jmp.label, null);
}
break;
} else if (type == LOOKUPSWITCH_INSN) {
LookupSwitchInsnNode swtch = (LookupSwitchInsnNode) insn;
convertLookupSwitchInsn(swtch);
LabelNode dflt = swtch.dflt;
addEdges(insn, dflt, swtch.labels);
break;
} else if (type == METHOD_INSN) {
convertMethodInsn((MethodInsnNode) insn);
} else if (type == INVOKE_DYNAMIC_INSN) {
convertInvokeDynamicInsn((InvokeDynamicInsnNode) insn);
} else if (type == MULTIANEWARRAY_INSN) {
convertMultiANewArrayInsn((MultiANewArrayInsnNode) insn);
} else if (type == TABLESWITCH_INSN) {
TableSwitchInsnNode swtch = (TableSwitchInsnNode) insn;
convertTableSwitchInsn(swtch);
LabelNode dflt = swtch.dflt;
addEdges(insn, dflt, swtch.labels);
} else if (type == TYPE_INSN) {
convertTypeInsn((TypeInsnNode) insn);
} else if (type == VAR_INSN) {
if (insn.getOpcode() == RET)
throw new UnsupportedOperationException("RET!");
convertVarInsn((VarInsnNode) insn);
} else if (type == LABEL) {
convertLabel((LabelNode) insn);
} else if (type == LINE) {
convertLine((LineNumberNode) insn);
} else if (type == FRAME) {
// we can ignore it
} else
throw new RuntimeException("Unknown instruction type: " + type);
} while ((insn = insn.getNext()) != null);
} while (!worklist.isEmpty());
conversionWorklist = null;
edges = null;
}
Aggregations