-
Notifications
You must be signed in to change notification settings - Fork 4
/
protocol.go
207 lines (159 loc) · 3.84 KB
/
protocol.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
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"io"
)
type Handshake struct {
Version uint64
Address string
Port uint16
State uint64
}
type Status struct {
Version Version `json:"version"`
Players Players `json:"players"`
Description Description `json:"description"`
}
type Version struct {
Name string `json:"name"`
Protocol int `json:"protocol"`
}
type Players struct {
Max int `json:"max"`
Online int `json:"online"`
}
type Description struct {
Text string `json:"text"`
}
type Chat struct {
Text string `json:"text"`
}
func ReadHandshake(r *bytes.Reader) Handshake {
handshake := Handshake{}
handshake.Version, _ = binary.ReadUvarint(r)
handshake.Address = ReadString(r)
binary.Read(r, binary.BigEndian, &handshake.Port)
handshake.State, _ = binary.ReadUvarint(r)
return handshake
}
func ReadVarint(r io.Reader) (uint64, error) {
return binary.ReadUvarint(byteReader{r, make([]byte, 1)})
}
func ReadString(r *bytes.Reader) string {
length, _ := binary.ReadUvarint(r)
if length < 1 {
return ""
} else {
buf := make([]byte, length)
r.Read(buf)
return string(buf)
}
}
func WriteStatus(w io.Writer, status Status) {
b := &bytes.Buffer{}
WriteVarint(b, 0x00)
WriteJSON(b, status)
WriteVarint(w, uint64(b.Len()))
w.Write(b.Bytes())
}
func WriteLoginSuccess(w io.Writer, uuid, username string) {
b := &bytes.Buffer{}
WriteVarint(b, 0x02)
WriteString(b, uuid)
WriteString(b, username)
WriteVarint(w, uint64(b.Len()))
w.Write(b.Bytes())
}
func WriteJoinGame(w io.Writer) {
b := &bytes.Buffer{}
b.WriteByte(0)
WriteVarint(b, 0x01)
binary.Write(b, binary.BigEndian, int32(1337))
b.WriteByte(0)
binary.Write(b, binary.BigEndian, int8(0))
b.WriteByte(2)
b.WriteByte(16)
WriteString(b, "default")
b.WriteByte(0)
WriteVarint(w, uint64(b.Len()))
w.Write(b.Bytes())
}
func WriteChatMessage(w io.Writer, message string) {
b := &bytes.Buffer{}
b.WriteByte(0)
WriteVarint(b, 0x02)
WriteJSON(b, Chat{message})
b.WriteByte(0)
WriteVarint(w, uint64(b.Len()))
w.Write(b.Bytes())
}
func WriteTimeUpdate(w io.Writer, worldAge, timeOfDay int64) {
b := &bytes.Buffer{}
b.WriteByte(0)
WriteVarint(b, 0x03)
binary.Write(b, binary.BigEndian, worldAge)
binary.Write(b, binary.BigEndian, timeOfDay)
WriteVarint(w, uint64(b.Len()))
w.Write(b.Bytes())
}
func WriteSpawnPosition(w io.Writer, x, y, z int) {
b := &bytes.Buffer{}
pos := uint64(((x & 0x3FFFFFF) << 38) | ((y & 0xFFF) << 26) | (z & 0x3FFFFFF))
b.WriteByte(0)
WriteVarint(b, 0x05)
binary.Write(b, binary.BigEndian, pos)
WriteVarint(w, uint64(b.Len()))
w.Write(b.Bytes())
}
func WritePositionAndLook(w io.Writer, x, y, z float64) {
b := &bytes.Buffer{}
b.WriteByte(0)
WriteVarint(b, 0x08)
binary.Write(b, binary.BigEndian, x)
binary.Write(b, binary.BigEndian, y)
binary.Write(b, binary.BigEndian, z)
binary.Write(b, binary.BigEndian, float32(0))
binary.Write(b, binary.BigEndian, float32(0))
b.WriteByte(0)
WriteVarint(w, uint64(b.Len()))
w.Write(b.Bytes())
}
func WriteSetCompression(w io.Writer, threshold uint64) {
b := &bytes.Buffer{}
WriteVarint(b, 0x46)
WriteVarint(b, threshold)
WriteVarint(w, uint64(b.Len()))
w.Write(b.Bytes())
}
func WriteVarint(w io.Writer, i uint64) {
buf := make([]byte, 8)
n := binary.PutUvarint(buf, i)
w.Write(buf[:n])
}
func WriteSignedVarint(w io.Writer, i int64) {
buf := make([]byte, 8)
n := binary.PutVarint(buf, i)
w.Write(buf[:n])
}
func WriteString(w io.Writer, str string) {
WriteVarint(w, uint64(len(str)))
w.Write([]byte(str))
}
func WriteJSON(w io.Writer, val interface{}) {
data, _ := json.Marshal(val)
WriteVarint(w, uint64(len(data)))
w.Write(data)
}
type byteReader struct {
reader io.Reader
buf []byte
}
func (b byteReader) ReadByte() (byte, error) {
_, err := b.reader.Read(b.buf)
if err != nil {
return 0, err
}
return b.buf[0], nil
}