PacketEvent.java
2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.mumfrey.liteloader.common.transformers;
import java.util.HashSet;
import java.util.Set;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.IntInsnNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.VarInsnNode;
import com.mumfrey.liteloader.core.runtime.Obf;
import com.mumfrey.liteloader.core.runtime.Packets;
import com.mumfrey.liteloader.transformers.event.Event;
import com.mumfrey.liteloader.transformers.event.EventInfo;
/**
* Special event used to hook all packets
*
* @author Adam Mummery-Smith
*/
public class PacketEvent extends Event
{
private static Set<String> names = new HashSet<String>();
/**
* Soft index for this packet, used as a lookup for speed when determining
* handlers.
*/
private int packetIndex;
PacketEvent(Packets packet)
{
super(PacketEvent.getPacketEventName(packet), true, 1000);
this.packetIndex = packet.getIndex();
this.verbose = false;
}
/* (non-Javadoc)
* @see com.mumfrey.liteloader.transformers.event.Event
* #getEventInfoClassName()
*/
@Override
public String getEventInfoClassName()
{
return "com/mumfrey/liteloader/common/transformers/PacketEventInfo";
}
/* (non-Javadoc)
* @see com.mumfrey.liteloader.transformers.event.Event
* #invokeEventInfoConstructor(org.objectweb.asm.tree.InsnList,
* boolean)
*/
@Override
protected int invokeEventInfoConstructor(InsnList insns, boolean cancellable, boolean pushReturnValue, int marshallVar)
{
int ctorMAXS = 0;
insns.add(new LdcInsnNode(this.name)); ctorMAXS++;
insns.add(this.methodIsStatic ? new InsnNode(Opcodes.ACONST_NULL) : new VarInsnNode(Opcodes.ALOAD, 0)); ctorMAXS++;
insns.add(new InsnNode(cancellable ? Opcodes.ICONST_1 : Opcodes.ICONST_0)); ctorMAXS++;
insns.add(new IntInsnNode(Opcodes.BIPUSH, this.packetIndex));
insns.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, this.eventInfoClass, Obf.constructor.name,
EventInfo.getConstructorDescriptor().replace(")", "I)"), false));
return ctorMAXS;
}
private static String getPacketEventName(Packets packet)
{
String baseName = "on" + packet.getShortName();
if (!PacketEvent.names.contains(baseName))
{
PacketEvent.names.add(baseName);
return baseName;
}
for (int ordinal = 1; ordinal < 33; ordinal++)
{
String offsetName = String.format("%s#%d", baseName, ordinal);
if (!PacketEvent.names.contains(offsetName))
{
PacketEvent.names.add(offsetName);
return offsetName;
}
}
throw new IllegalArgumentException("Too many packet events with the same name: " + baseName);
}
}