-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacketRegistry.js
More file actions
34 lines (34 loc) · 1.06 KB
/
PacketRegistry.js
File metadata and controls
34 lines (34 loc) · 1.06 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
class PacketRegistry {
constructor() {
this.packetTypes = [];
this.registerPacket(PrimitivePacket);
}
registerPacket(packetType) {
if (this.packetTypes.includes(packetType))
return;
this.packetTypes.push(packetType);
}
registerPackets(registry) {
for (var packetType of registry.packetTypes)
this.registerPacket(packetType);
}
getPacketId(packet) {
var clazz = null;
var id = 0;
for (var i = 0; i < this.packetTypes.length; i++) {
var packetType = this.packetTypes[i];
if (packet instanceof packetType && (clazz === null || packetType.prototype instanceof clazz)) {
clazz = packetType;
id = i;
}
}
if (clazz === null)
throw new Error("The packet type " + packet.constructor.name + " is not registered!");
return id;
}
getPacketType(id) {
if (id < 0 || id >= this.packetTypes.length)
return null;
return this.packetTypes[id];
}
}