/
types.go
243 lines (217 loc) · 4.78 KB
/
types.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
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
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"regexp"
"strings"
)
//go:generate go run tools/genpackets.go
type Direction int
const (
ClientBound Direction = iota
ServerBound
)
type VarInt int32
type VarLong int64
type UInt16 uint16
type Long int64
type String string
type ByteArray []byte
type Chat struct {
Text string `json:"text"`
Extra []struct {
Text string `json:"text"`
} `json:"extra"`
}
type Packet struct {
PacketID VarInt
Data *bytes.Buffer
State GameState
Direction
}
type Serializable interface {
ToBytes() []byte
}
func NewPacket(packetID VarInt, args ...Serializable) *Packet {
p := &Packet{
PacketID: packetID,
Data: bytes.NewBuffer([]byte{}),
}
for _, arg := range args {
p.Data.Write(arg.ToBytes())
}
return p
}
func (p *Packet) String() string {
data := make([]byte, p.Data.Len())
_, _ = p.Data.Read(data)
p.Data.Write(data)
dataRepr := []string{}
for i, b := range data {
dataRepr = append(dataRepr, fmt.Sprintf("%02x", b))
if i > 40 {
dataRepr = append(dataRepr, fmt.Sprintf("... (+%d)", len(data)-i))
break
}
}
// io.R p.Data.Peek()
name := fmt.Sprintf("0x%02x", p.PacketID)
color := "160"
if directionMap, ok := PacketNames[p.State]; ok {
if packetIDMap, ok := directionMap[p.Direction]; ok {
if packetName, ok := packetIDMap[int32(p.PacketID)]; ok {
name = fmt.Sprintf("%s (0x%02x)", packetName, p.PacketID)
color = "190"
}
}
}
// if foundName, ok := PacketNamesIn[int32(p.PacketID)]; p.In && ok {
// name = foundName
// color = "190"
// }
return fmt.Sprintf(
"{Packet \u001b[38;5;%sm%s\u001b[0m data(%d)=\u001b[38;5;34m%s\u001b[0m}",
color, name, p.Data.Len(), strings.Join(dataRepr, " "),
)
}
func (v VarInt) ToBytes() []byte {
uv := uint32(v)
bytes := []byte{}
for {
if uv & ^uint32(0x7F) == 0 {
bytes = append(bytes, byte(uv))
break
}
bytes = append(bytes, byte((uv&0x7F)|0x80))
uv = uv >> 7
}
return bytes
}
func (v *VarInt) FromReader(r io.Reader) error {
*v = 0
buf := []byte{0}
for i := 0; i < 5; i++ {
_, err := r.Read(buf) // TODO
if err != nil {
return err
}
*v |= VarInt(buf[0]&0x7F) << (i * 7)
if buf[0]&0x80 == 0 {
return nil
}
}
panic("packetstream: VarInt too long")
}
func (v VarLong) ToBytes() []byte {
uv := uint64(v)
bytes := []byte{}
for {
if uv & ^uint64(0x7F) == 0 {
bytes = append(bytes, byte(uv))
break
}
bytes = append(bytes, byte((uv&0x7F)|0x80))
uv = uv >> 7
}
return bytes
}
func (v *VarLong) FromReader(r io.Reader) error {
*v = 0
buf := []byte{0}
for i := 0; i < 10; i++ {
_, err := r.Read(buf) // TODO
if err != nil {
return err
}
*v |= VarLong(buf[0]&0x7F) << (i * 7)
if buf[0]&0x80 == 0 {
return nil
}
}
panic("packetstream: VarInt too long")
}
func (u *UInt16) FromReader(r io.Reader) error {
return binary.Read(r, binary.BigEndian, u)
}
func (u UInt16) ToBytes() []byte {
buf := bytes.NewBuffer([]byte{})
binary.Write(buf, binary.BigEndian, u)
return buf.Bytes()
}
func (l *Long) FromReader(r io.Reader) error {
return binary.Read(r, binary.BigEndian, l)
}
func (l Long) ToBytes() []byte {
buf := bytes.NewBuffer([]byte{})
binary.Write(buf, binary.BigEndian, l)
return buf.Bytes()
}
func (s String) ToBytes() []byte {
return append(VarInt(len(s)).ToBytes(), []byte(s)...)
}
func (s *String) FromReader(r io.Reader) error {
var length VarInt
if err := length.FromReader(r); err != nil {
return err
}
data := make([]byte, length)
if _, err := r.Read(data); err != nil {
return err
}
*s = String(data)
return nil
}
func (b ByteArray) ToBytes() []byte {
return []byte(b)
}
func (b ByteArray) ToBytesWithSize() []byte {
return append(VarInt(len(b)).ToBytes(), b.ToBytes()...)
}
func (b *ByteArray) FromReader(r io.Reader, length int32) error {
buf := make([]byte, length)
if _, err := r.Read(buf); err != nil {
return err
}
*b = buf
return nil
}
func (b *ByteArray) FromReaderWithSize(r io.Reader) error {
var length VarInt
if err := length.FromReader(r); err != nil {
return err
}
if err := b.FromReader(r, int32(length)); err != nil {
return err
}
return nil
}
func (c *Chat) FromReader(r io.Reader) error {
var jsonLength VarInt
if err := jsonLength.FromReader(r); err != nil {
return err
}
jsonData := make([]byte, jsonLength)
if _, err := r.Read(jsonData); err != nil {
return err
}
if err := json.Unmarshal(jsonData, c); err != nil {
return err
}
return nil
}
func (c *Chat) cleanUp(s string) string {
s = regexp.MustCompile("\\xA7[\\d\\w]").ReplaceAllString(s, "")
s = strings.ReplaceAll(s, "\n", " ")
s = strings.ReplaceAll(s, "\r", "")
return s
}
func (c *Chat) String() string {
parts := []string{c.cleanUp(c.Text)}
for _, extra := range c.Extra {
parts = append(parts, c.cleanUp(extra.Text))
}
return strings.Join(parts, "")
}