summaryrefslogtreecommitdiff
path: root/classic/packets.go
blob: 776e867168e676e0440b3d5091194f7daa407f22 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package classic

import (
	"io"
	"fmt"
	"bytes"
	"bufio"
	"encoding/binary"
)

type FByte int8
type FShort int16
type String [64]byte
type LevelData [1024]byte

type Packet interface {
	PacketId() byte
}

const (
	NonOpUser = 0x00
	OpUser = 0x64
)
const (
	NoCpe = 0x00
	UseCpe = 0x42
)

type PlayerId struct {
	Version byte
	Username String
	VerificationKey String
	Ext byte
}
func (p *PlayerId) PacketId() byte {
	return 0x00
}

type ServerId struct {
	Version byte
	ServerName String
	Motd String
	UserType byte
}
func (p *ServerId) PacketId() byte {
	return 0x00
}

type Ping struct {}
func (p *Ping) PacketId() byte {
	return 0x01
}

type LevelInit struct {}
func (p *LevelInit) PacketId() byte {
	return 0x02
}

type LevelDataChunk struct {
	Length int16
	Data LevelData
	PercentComplete byte
}
func (p *LevelDataChunk) PacketId() byte {
	return 0x03
}

type LevelFinalize struct {
	Width int16
	Height int16
	Length int16
}
func (p *LevelFinalize) PacketId() byte {
	return 0x04
}

const (
	BlockDestroyed = iota
	BlockCreated 
)
type ClientSetBlock struct {
	X, Y, Z int16
	Mode byte
	Block byte
}
func (p *ClientSetBlock) PacketId() byte {
	return 0x05
}

type SetBlock struct {
	X, Y, Z int16
	Block byte
}
func (p *SetBlock) PacketId() byte {
	return 0x06
}

type SpawnPlayer struct {
	PlayerId int8
	Username String
	X, Y, Z FShort
	Yaw, Pitch byte
}
func (p *SpawnPlayer) PacketId() byte {
	return 0x07
}

type SetPosFacing struct {
	PlayerId int8
	X, Y, Z FShort
	Yaw, Pitch byte
}
func (p *SetPosFacing) PacketId() byte {
	return 0x08
}

type UpdatePosFacing struct {
	UpdatePos
	UpdateFacing
}
func (p *UpdatePosFacing) PacketId() byte {
	return 0x09
}

type UpdatePos struct {
	DeltaX, DeltaY, DeltaZ FByte
}
func (p *UpdatePos) PacketId() byte {
	return 0x0a
}

type UpdateFacing struct {
	Yaw, Pitch byte
}
func (p *UpdateFacing) PacketId() byte {
	return 0x0b
}

type DespawnPlayer struct {
	PlayerId int8
}
func (p *DespawnPlayer) PacketId() byte {
	return 0x0c
}

type Message struct {
	PlayerId int8
	Message String
}
func (p *Message) PacketId() byte {
	return 0x0d
}

type DisconnectPlayer struct {
	Reason String
}
func (p *DisconnectPlayer) PacketId() byte {
	return 0x0e
}

type UpdateUserType struct {
	Type byte
}
func (p *UpdateUserType) PacketId() byte {
	return 0x0f
}

func createPacketType(packetId byte, client bool, ext map[string]bool) Packet {
	switch packetId {
	case 0x00:
		if client {
			return &ServerId {}
		} else {
			return &PlayerId {}
		}
	case 0x01: return &Ping {}
	case 0x02: return &LevelInit {}
	case 0x03: return &LevelDataChunk {}
	case 0x04: return &LevelFinalize {}
	case 0x05: return &ClientSetBlock {}
	case 0x06: return &SetBlock {}
	case 0x07:
		if !ext["ExtEntityPositions"] {
			return &SpawnPlayer {}
		} else {
			return &SpawnPlayerExt {}
		}
	case 0x08:
		if !ext["ExtEntityPositions"] {
			return &SetPosFacing {}
		} else {
			return &SetPosFacingExt {}
		}
	case 0x09: return &UpdatePosFacing {}
	case 0x0a: return &UpdatePos {}
	case 0x0b: return &UpdateFacing {}
	case 0x0c: return &DespawnPlayer {}
	case 0x0d: return &Message {}
	case 0x0e: return &DisconnectPlayer {}
	case 0x0f: return &UpdateUserType {}
	case 0x10: return &ExtInfo {}
	case 0x11: return &ExtEntry {}
	case 0x13: return &CustomBlocksSupportLevel {}
	case 0x16: return &AddPlayerName {}
	case 0x18: return &RemovePlayerName {}
	case 0x1c: return &SetBlockPermission {}
	case 0x20: return &HackControl {}
	case 0x21: return &AddEntity2 {}
	case 0x23: return &DefineBlock {}
	case 0x24: return &RemoveBlockDefinition {}
	case 0x25: return &DefineBlockExt {}
	case 0x29: return &SetMapEnvProperty {}
	default: return nil
	}
}

func readPacket(
		rd io.Reader, client bool, ext map[string]bool) (Packet, error) {
	var b [1]byte
	_, err := io.ReadFull(rd, b[:])
	if err != nil {
		return nil, fmt.Errorf("Read packet: %w", err)
	}
	packetId := b[0]

	packet := createPacketType(packetId, client, ext)
	if packet == nil {
		return nil, fmt.Errorf("Unknown packet type: 0x%02x", packetId)
	}
	err = binary.Read(rd, binary.BigEndian, packet)
	if err != nil {
		return nil, fmt.Errorf("Read packet: %w", err)
	}
	return packet, nil
}

func SReadPacket(rd io.Reader, ext map[string]bool) (Packet, error) {
	return readPacket(rd, false, ext)
}

func CReadPacket(rd io.Reader, ext map[string]bool) (Packet, error) {
	return readPacket(rd, true, ext)
}

func WritePacket(wr io.Writer, packet Packet) error {
	bw := bufio.NewWriter(wr)
	bw.WriteByte(packet.PacketId())
	err := binary.Write(bw, binary.BigEndian, packet)
	if err != nil {
		return fmt.Errorf("Write packet: %w", err)
	}
	return bw.Flush()
}

func PadString(str string) String {
	var pstr String
	copy(pstr[:], []byte(str))
	if len(str) < 64 {
		copy(pstr[len(str):], bytes.Repeat([]byte(" "), 64 - len(str)))
	}
	return pstr
}

func UnpadString(pstr String) string {
	return string(bytes.TrimRight(pstr[:], " "))
}

func (s String) String() string {
	return UnpadString(s)
}