forked from TyphoonMC/TyphoonCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket_registry.go
72 lines (61 loc) · 2.09 KB
/
packet_registry.go
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
package typhoon
import (
"log"
"reflect"
)
var (
packets map[int64]reflect.Type = make(map[int64]reflect.Type)
)
type Packet interface {
Write(*Player) error
Read(*Player, int) error
Handle(*Player)
Id() (int, Protocol)
}
func PacketTypeHash(state State, id int) int64 {
return int64(id) ^ (int64(state) << 32)
}
func initPackets() {
packets[PacketTypeHash(HANDSHAKING, 0x00)] = reflect.TypeOf((*PacketHandshake)(nil)).Elem()
packets[PacketTypeHash(STATUS, 0x00)] = reflect.TypeOf((*PacketStatusRequest)(nil)).Elem()
packets[PacketTypeHash(STATUS, 0x01)] = reflect.TypeOf((*PacketStatusPing)(nil)).Elem()
packets[PacketTypeHash(LOGIN, 0x00)] = reflect.TypeOf((*PacketLoginStart)(nil)).Elem()
packets[PacketTypeHash(PLAY, 0x01)] = reflect.TypeOf((*PacketPlayTabCompleteServerbound)(nil)).Elem()
packets[PacketTypeHash(PLAY, 0x02)] = reflect.TypeOf((*PacketPlayChat)(nil)).Elem()
packets[PacketTypeHash(PLAY, 0x03)] = reflect.TypeOf((*PacketPlayClientStatus)(nil)).Elem()
packets[PacketTypeHash(PLAY, 0x09)] = reflect.TypeOf((*PacketPlayPluginMessage)(nil)).Elem()
packets[PacketTypeHash(PLAY, 0x0B)] = reflect.TypeOf((*PacketPlayKeepAlive)(nil)).Elem()
packets[PacketTypeHash(PLAY, 0x0D)] = reflect.TypeOf((*PacketPlayPlayerPosition)(nil)).Elem()
packets[PacketTypeHash(PLAY, 0x0E)] = reflect.TypeOf((*PacketPlayPlayerLook)(nil)).Elem()
packets[PacketTypeHash(PLAY, 0x0F)] = reflect.TypeOf((*PacketPlayPlayer)(nil)).Elem()
}
func (player *Player) HandlePacket(id int, length int) (packet Packet, err error) {
typ := packets[PacketTypeHash(player.state, id)]
if typ == nil {
if config.Logs {
log.Printf("%d -> Unknown packet #%d\n", player.id, id)
}
var buff []byte
nbr := 0
if length > 500 {
buff = make([]byte, 500)
} else {
buff = make([]byte, length)
}
for nbr < length {
if length-nbr > 500 {
player.io.rdr.Read(buff)
nbr += 500
} else {
player.io.rdr.Read(buff[:length-nbr])
nbr = length
}
}
return nil, nil
}
packet, _ = reflect.New(typ).Interface().(Packet)
if err = packet.Read(player, length); err != nil {
return nil, err
}
return
}