-
Notifications
You must be signed in to change notification settings - Fork 8
/
nbt2json.go
294 lines (281 loc) · 8.23 KB
/
nbt2json.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package nbt2json
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"math"
"time"
"github.com/ghodss/yaml"
)
// NbtJson is the top-level JSON document; it is exported for reflect, and client code shouldn't use it
type NbtJson struct {
Name string `json:"name"`
Version string `json:"version"`
Nbt2JsonUrl string `json:"nbt2JsonUrl"`
ConversionTime string `json:"conversionTime,omitempty"`
Comment string `json:"comment,omitempty"`
Nbt []*json.RawMessage `json:"nbt"`
}
// NbtTag represents one NBT tag for each struct; it is exported for reflect, and client code shouldn't use it
type NbtTag struct {
TagType byte `json:"tagType"`
Name string `json:"name"`
Value interface{} `json:"value,omitempty"`
}
// NbtTagList represents an NBT tag list; it is exported for reflect, and client code shouldn't use it
type NbtTagList struct {
TagListType byte `json:"tagListType"`
List []interface{} `json:"list"`
}
// NbtLong stores a 64-bit int into two 32-bit values for json portability. ValueMost are the high 32 bits and ValueLeast are the low 32 bits.
// using uint32s to avoid Go trying to outsmart us on "negative" int32s
type NbtLong struct {
ValueLeast uint32 `json:"valueLeast"`
ValueMost uint32 `json:"valueMost"`
}
// Turns an int64 (nbt long) into a valueLeast/valueMost json pair
func longToIntPair(i int64) NbtLong {
var nbtLong NbtLong
nbtLong.ValueLeast = uint32(i & 0xffffffff)
nbtLong.ValueMost = uint32(i >> 32)
return nbtLong
}
func intPairToLong(nbtLong NbtLong) int64 {
var i int64
i = int64(nbtLong.ValueLeast) | (int64(nbtLong.ValueMost) << 32)
return i
}
// Nbt2Yaml converts uncompressed NBT byte array to YAML byte array
func Nbt2Yaml(b []byte, comment string) ([]byte, error) {
jsonOut, err := Nbt2Json(b, comment)
if err != nil {
return nil, err
}
yamlOut, err := yaml.JSONToYAML(jsonOut)
if err != nil {
return yamlOut, NbtParseError{"Error converting JSON to YAML. Oops. JSON conversion succeeded, so please report this error and use JSON instead.", err}
}
return yamlOut, nil
}
// Nbt2Json converts uncompressed NBT byte array to JSON byte array
func Nbt2Json(b []byte, comment string) ([]byte, error) {
var nbtJson NbtJson
nbtJson.Name = Name
nbtJson.Version = Version
nbtJson.Nbt2JsonUrl = Nbt2JsonUrl
nbtJson.ConversionTime = time.Now().Format(time.RFC3339)
nbtJson.Comment = comment
buf := bytes.NewReader(b)
// var nbtJson.nbt []*json.RawMessage
for buf.Len() > 0 {
element, err := getTag(buf)
if err != nil {
return nil, err
}
myTemp := json.RawMessage(element)
nbtJson.Nbt = append(nbtJson.Nbt, &myTemp)
}
jsonOut, err := json.MarshalIndent(nbtJson, "", " ")
if err != nil {
return nil, err
}
return jsonOut, nil
}
// getTag broken out form Nbt2Json to allow recursion with reader but public input is []byte
func getTag(r *bytes.Reader) ([]byte, error) {
var data NbtTag
err := binary.Read(r, byteOrder, &data.TagType)
if err != nil {
return nil, NbtParseError{"Reading TagType", err}
}
// do not try to fetch name for TagType 0 which is compound end tag
if data.TagType != 0 {
var err error
var nameLen int16
err = binary.Read(r, byteOrder, &nameLen)
if err != nil {
return nil, NbtParseError{"Reading Name length", err}
}
name := make([]byte, nameLen)
err = binary.Read(r, byteOrder, &name)
if err != nil {
return nil, NbtParseError{fmt.Sprintf("Reading Name - is UseJavaEncoding or UseBedrockEncoding set correctly? Name length decoded is %d", nameLen), err}
}
data.Name = string(name[:])
}
data.Value, err = getPayload(r, data.TagType)
if err != nil {
return nil, err
}
outJson, err := json.MarshalIndent(data, "", " ")
return outJson, err
}
// Gets the tag payload. Had to break this out from the main function to allow tag list recursion
func getPayload(r *bytes.Reader, tagType byte) (interface{}, error) {
var output interface{}
var err error
switch tagType {
case 0:
// end tag for compound; do nothing further
case 1:
var i int8
err = binary.Read(r, byteOrder, &i)
if err != nil {
return nil, NbtParseError{"Reading int8", err}
}
output = i
case 2:
var i int16
err = binary.Read(r, byteOrder, &i)
if err != nil {
return nil, NbtParseError{"Reading int16", err}
}
output = i
case 3:
var i int32
err = binary.Read(r, byteOrder, &i)
if err != nil {
return nil, NbtParseError{"Reading int32", err}
}
output = i
case 4:
var i int64
err = binary.Read(r, byteOrder, &i)
if err != nil {
return nil, NbtParseError{"Reading int64", err}
}
if longAsString {
output = fmt.Sprintf("%d", i)
} else {
output = longToIntPair(i)
}
case 5:
var f float32
err = binary.Read(r, byteOrder, &f)
if err != nil {
return nil, NbtParseError{"Reading float32", err}
}
output = f
case 6:
var f float64
err = binary.Read(r, byteOrder, &f)
if err != nil {
return nil, NbtParseError{"Reading float64", err}
}
if math.IsNaN(f) {
output = "NaN"
} else {
output = f
}
case 7:
var byteArray []int8
var oneByte int8
var numRecords int32
err := binary.Read(r, byteOrder, &numRecords)
if err != nil {
return nil, NbtParseError{"Reading byte array tag length", err}
}
for i := int32(1); i <= numRecords; i++ {
err = binary.Read(r, byteOrder, &oneByte)
if err != nil {
return nil, NbtParseError{"Reading byte in byte array tag", err}
}
byteArray = append(byteArray, oneByte)
}
output = byteArray
case 8:
var strLen int16
err := binary.Read(r, byteOrder, &strLen)
if err != nil {
return nil, NbtParseError{"Reading string tag length", err}
}
utf8String := make([]byte, strLen)
err = binary.Read(r, byteOrder, &utf8String)
if err != nil {
return nil, NbtParseError{"Reading string tag data", err}
}
output = string(utf8String[:])
case 9:
var tagList NbtTagList
err = binary.Read(r, byteOrder, &tagList.TagListType)
if err != nil {
return nil, NbtParseError{"Reading TagType", err}
}
var numRecords int32
err := binary.Read(r, byteOrder, &numRecords)
if err != nil {
return nil, NbtParseError{"Reading list tag length", err}
}
for i := int32(1); i <= numRecords; i++ {
payload, err := getPayload(r, tagList.TagListType)
if err != nil {
return nil, NbtParseError{"Reading list tag item", err}
}
tagList.List = append(tagList.List, payload)
}
output = tagList
case 10:
var compound []json.RawMessage
var tagType byte
for err = binary.Read(r, byteOrder, &tagType); tagType != 0; err = binary.Read(r, byteOrder, &tagType) {
if err != nil {
return nil, NbtParseError{"compound: reading next tag type", err}
}
_, err = r.Seek(-1, 1)
if err != nil {
return nil, NbtParseError{"seeking back one", err}
}
tag, err := getTag(r)
if err != nil {
return nil, NbtParseError{"compound: reading a child tag", err}
}
compound = append(compound, json.RawMessage(string(tag)))
}
if compound == nil {
// Explicitly give empty array else value will be null instead of []
output = []int{}
} else {
output = compound
}
case 11:
var intArray []int32
var numRecords, oneInt int32
err := binary.Read(r, byteOrder, &numRecords)
if err != nil {
return nil, NbtParseError{"Reading int array tag length", err}
}
for i := int32(1); i <= numRecords; i++ {
err := binary.Read(r, byteOrder, &oneInt)
if err != nil {
return nil, NbtParseError{"Reading int in int array tag", err}
}
intArray = append(intArray, oneInt)
}
output = intArray
case 12:
var longArray []NbtLong
var longStringArray []string
var numRecords, oneInt int64
err := binary.Read(r, byteOrder, &numRecords)
if err != nil {
return nil, NbtParseError{"Reading long array tag length", err}
}
for i := int64(1); i <= numRecords; i++ {
err := binary.Read(r, byteOrder, &oneInt)
if err != nil {
return nil, NbtParseError{"Reading long in long array tag", err}
}
longArray = append(longArray, longToIntPair(oneInt))
longStringArray = append(longStringArray, fmt.Sprintf("%d", oneInt))
}
if longAsString {
output = longStringArray
} else {
output = longArray
}
default:
return nil, NbtParseError{fmt.Sprintf("TagType %d not recognized", tagType), nil}
}
return output, nil
}