-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmc_type.go
199 lines (167 loc) · 4.04 KB
/
mc_type.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
// Based on https://github.com/realDragonium/Ultraviolet/blob/main/mc/type.go
package main
import (
"encoding/binary"
"errors"
"io"
)
var (
ErrMcVarIntSize = errors.New("McVarInt is too big")
)
// A Field is both FieldEncoder and FieldDecoder
type Field interface {
FieldEncoder
FieldDecoder
}
// A FieldEncoder can be encode as minecraft protocol used.
type FieldEncoder interface {
Encode() []byte
}
// A FieldDecoder can Decode from minecraft protocol
type FieldDecoder interface {
Decode(r DecodeReader) error
}
// DecodeReader is both io.Reader and io.McByteReader
type DecodeReader interface {
io.ByteReader
io.Reader
}
type (
// McByte is signed 8-bit integer, two's complement
McByte int8
// McUnsignedShort is unsigned 16-bit integer
McUnsignedShort uint16
// McLong is signed 64-bit integer, two's complement
McLong int64
// McString is sequence of Unicode scalar values with a max length of 32767
McString string
// McChat is encoded as a McString with max length of 262144.
McChat = McString
// McVarInt is variable-length data encoding a two's complement signed 32-bit integer
McVarInt int32
// UUID is encoded as an unsigned 128-bit integer
McUUID [16]byte
)
// ReadNMcBytes read N bytes from bytes.Reader
func ReadNBytes(r DecodeReader, n int) ([]byte, error) {
buf := make([]byte, n)
if _, err := io.ReadFull(r, buf); err != nil {
return buf, err
}
return buf, nil
}
///////////////////////////////////////////////////////////////////////////////
// Encode a McString
func (s McString) Encode() []byte {
byteMcString := []byte(s)
var bb []byte
bb = append(bb, McVarInt(len(byteMcString)).Encode()...) // len
bb = append(bb, byteMcString...) // data
return bb
}
// Decode a McString
func (s *McString) Decode(r DecodeReader) error {
var l McVarInt // McString length
if err := l.Decode(r); err != nil {
return err
}
bb, err := ReadNBytes(r, int(l))
if err != nil {
return err
}
*s = McString(bb)
return nil
}
///////////////////////////////////////////////////////////////////////////////
// Encode a McByte
func (b McByte) Encode() []byte {
return []byte{byte(b)}
}
// Decode a McByte
func (b *McByte) Decode(r DecodeReader) error {
v, err := r.ReadByte()
if err != nil {
return err
}
*b = McByte(v)
return nil
}
///////////////////////////////////////////////////////////////////////////////
// Encode a Unsigned Short
func (us McUnsignedShort) Encode() []byte {
n := uint16(us)
return []byte{
byte(n >> 8),
byte(n),
}
}
// Decode a McUnsignedShort
func (us *McUnsignedShort) Decode(r DecodeReader) error {
bb, err := ReadNBytes(r, 2)
if err != nil {
return err
}
*us = McUnsignedShort(int16(bb[0])<<8 | int16(bb[1]))
return nil
}
///////////////////////////////////////////////////////////////////////////////
// Encode a McLong
func (l McLong) Encode() []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(l))
return buf
}
// Decode a McLong
func (l *McLong) Decode(r DecodeReader) error {
buf, err := ReadNBytes(r, 8)
if err != nil {
return err
}
*l = McLong(binary.BigEndian.Uint64(buf))
return nil
}
///////////////////////////////////////////////////////////////////////////////
// Encode a McVarInt
func (v McVarInt) Encode() []byte {
num := uint32(v)
var bb []byte
for {
b := num & 0x7F
num >>= 7
if num != 0 {
b |= 0x80
}
bb = append(bb, byte(b))
if num == 0 {
break
}
}
return bb
}
// Decode a McVarInt
func (v *McVarInt) Decode(r DecodeReader) error {
var n uint32
for i := 0; ; i++ {
sec, err := r.ReadByte()
if err != nil {
return err
}
n |= uint32(sec&0x7F) << uint32(7*i)
if i >= 5 {
return ErrMcVarIntSize
} else if sec&0x80 == 0 {
break
}
}
*v = McVarInt(n)
return nil
}
///////////////////////////////////////////////////////////////////////////////
func (u McUUID) Encode() []byte {
return u[:]
}
// Decode читает 16 байт из reader и устанавливает значение UInt128.
func (u *McUUID) Decode(r DecodeReader) error {
_, err := io.ReadFull(r, u[:])
return err
}