-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
222 lines (194 loc) · 3.92 KB
/
parse.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
package nbtreader
import (
"encoding/binary"
"fmt"
"io"
"math"
)
func parseType(r io.Reader, tagType TagType) (NbtTag, error) {
var tag NbtTag
switch tagType {
case Tag_Byte:
tag = Byte(0)
case Tag_Short:
tag = Short(0)
case Tag_Int:
tag = Int(0)
case Tag_Long:
tag = Long(0)
case Tag_Float:
tag = Float(0)
case Tag_Double:
tag = Double(0)
case Tag_Byte_Array:
tag = ByteArray{}
case Tag_String:
tag = String("")
case Tag_List:
tag = List{}
case Tag_Compound:
tag = Compound{}
case Tag_Int_Array:
tag = IntArray{}
case Tag_Long_Array:
tag = LongArray{}
default:
return nil, fmt.Errorf("unknown type 0x%02x", byte(tagType))
}
return tag.parse(r)
}
func (t Byte) parse(r io.Reader) (NbtTag, error) {
return popByte(r)
}
func (t Short) parse(r io.Reader) (NbtTag, error) {
return popShort(r)
}
func (t Int) parse(r io.Reader) (NbtTag, error) {
return popInt(r)
}
func (t Long) parse(r io.Reader) (NbtTag, error) {
return popLong(r)
}
func (t Float) parse(r io.Reader) (NbtTag, error) {
return popFloat(r)
}
func (t Double) parse(r io.Reader) (NbtTag, error) {
return popDouble(r)
}
func (t ByteArray) parse(r io.Reader) (NbtTag, error) {
itemCap, err := popInt(r)
if err != nil {
return t, err
}
t = make([]Byte, itemCap)
for i, item := range t {
item, err = popByte(r)
if err != nil {
return t, err
}
t[i] = item
}
return t, nil
}
func (t String) parse(r io.Reader) (NbtTag, error) {
return popString(r)
}
func (t List) parse(r io.Reader) (NbtTag, error) {
i, err := popByte(r)
if err != nil {
return t, err
}
t.TagType = TagType(i)
itemCap, err := popInt(r)
if err != nil {
return t, err
}
if t.TagType == Tag_End && itemCap > 0 {
return t, fmt.Errorf("list cannot be of type TAG_END")
}
t.Elements = make([]NbtTag, itemCap)
for i, entry := range t.Elements {
entry, err = parseType(r, t.TagType)
if err != nil {
return t, err
}
t.Elements[i] = entry
}
return t, nil
}
func (t Compound) parse(r io.Reader) (NbtTag, error) {
index := 0
for {
var i Byte
var err error
i, err = popByte(r)
if err != nil {
return t, err
}
tagType := TagType(i)
if tagType == Tag_End {
return t, nil
}
var key String
var child NbtTag
key, err = popString(r)
child, err = parseType(r, tagType)
if err != nil {
return t, err
}
t[key] = struct {
Index int
Value NbtTag
}{Index: index, Value: child}
index++
}
}
func (t IntArray) parse(r io.Reader) (NbtTag, error) {
itemCap, err := popInt(r)
if err != nil {
return t, err
}
t = make([]Int, itemCap)
for i, item := range t {
item, err = popInt(r)
if err != nil {
return t, err
}
t[i] = item
}
return t, nil
}
func (t LongArray) parse(r io.Reader) (NbtTag, error) {
itemCap, err := popInt(r)
if err != nil {
return t, err
}
t = make([]Long, itemCap)
for i, item := range t {
item, err = popLong(r)
if err != nil {
return t, err
}
t[i] = item
}
return t, nil
}
func popByte(r io.Reader) (Byte, error) {
var buf [1]byte
_, err := io.ReadFull(r, buf[:])
return Byte(buf[0]), err
}
func popShort(r io.Reader) (Short, error) {
var buf [2]byte
_, err := io.ReadFull(r, buf[:])
return Short(binary.BigEndian.Uint16(buf[:])), err
}
func popInt(r io.Reader) (Int, error) {
var buf [4]byte
_, err := io.ReadFull(r, buf[:])
return Int(binary.BigEndian.Uint32(buf[:])), err
}
func popLong(r io.Reader) (Long, error) {
var buf [8]byte
_, err := io.ReadFull(r, buf[:])
return Long(binary.BigEndian.Uint64(buf[:])), err
}
func popFloat(r io.Reader) (Float, error) {
i, err := popInt(r)
f := Float(math.Float32frombits(uint32(i)))
return f, err
}
func popDouble(r io.Reader) (Double, error) {
l, err := popLong(r)
d := Double(math.Float64frombits(uint64(l)))
return d, err
}
func popString(r io.Reader) (String, error) {
lenName, err := popShort(r)
if err != nil {
return "", err
}
p := make([]byte, lenName)
_, err = io.ReadFull(r, p)
return String(p), err
}