-
Notifications
You must be signed in to change notification settings - Fork 8
/
json2nbt.go
363 lines (353 loc) · 12.6 KB
/
json2nbt.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package nbt2json
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"math"
"strconv"
"github.com/ghodss/yaml"
)
// Yaml2Nbt converts JSON byte array to uncompressed NBT byte array
func Yaml2Nbt(b []byte) ([]byte, error) {
myJson, err := yaml.YAMLToJSON(b)
if err != nil {
return nil, JsonParseError{"Error converting YAML to JSON", err}
}
nbtOut, err := Json2Nbt(myJson)
if err != nil {
return nbtOut, err
}
return nbtOut, nil
}
// Json2Nbt converts JSON byte array to uncompressed NBT byte array
func Json2Nbt(b []byte) ([]byte, error) {
nbtOut := new(bytes.Buffer)
var nbtJsonData NbtJson
var nbtTag interface{}
var nbtArray []interface{}
var err error
err = json.Unmarshal(b, &nbtJsonData)
if err != nil {
return nil, JsonParseError{"Error parsing JSON input. Is input JSON-formatted?", err}
}
temp, err := json.Marshal(nbtJsonData.Nbt)
if err != nil {
return nil, JsonParseError{"Error marshalling nbt: json.RawMessage", err}
}
err = json.Unmarshal(temp, &nbtArray)
if err != nil {
return nil, JsonParseError{"Error unmarshalling nbt: value", err}
}
if len(nbtArray) == 0 {
return nil, JsonParseError{"JSON input has no top-level value named nbt. JSON-encoded nbt data should be in an array { \"nbt\": [ <HERE> ] }", nil}
}
for _, nbtTag = range nbtArray {
err = writeTag(nbtOut, nbtTag)
if err != nil {
return nil, err
}
}
return nbtOut.Bytes(), nil
}
func writeTag(w io.Writer, myMap interface{}) error {
var err error
// TODO: This is panic-exiting when passed a string or null tagType instead of returning error
if m, ok := myMap.(map[string]interface{}); ok {
if tagType, ok := m["tagType"].(float64); ok {
if tagType == 0 {
// not expecting a 0 tag, but if it occurs just ignore it
return nil
}
err = binary.Write(w, byteOrder, byte(tagType))
if err != nil {
return JsonParseError{"Error writing tagType" + string(byte(tagType)), err}
}
if name, ok := m["name"].(string); ok {
err = binary.Write(w, byteOrder, int16(len(name)))
if err != nil {
return JsonParseError{"Error writing name length", err}
}
err = binary.Write(w, byteOrder, []byte(name))
if err != nil {
return JsonParseError{"Error converting name", err}
}
} else {
return JsonParseError{fmt.Sprintf("name field '%v' not a string", m["name"]), err}
}
err = writePayload(w, m, tagType)
if err != nil {
return err
}
} else {
return JsonParseError{fmt.Sprintf("tagType '%v' is not an integer", m["tagType"]), err}
}
} else {
return JsonParseError{"writeTag: myMap is not map[string]interface{}", err}
}
return err
}
func writePayload(w io.Writer, m map[string]interface{}, tagType float64) error {
var err error
switch tagType {
case 1:
if i, ok := m["value"].(float64); ok {
if i < math.MinInt8 || i > math.MaxInt8 {
return JsonParseError{fmt.Sprintf("%v is out of range for tag 1 - Byte", i), nil}
}
err = binary.Write(w, byteOrder, int8(i))
if err != nil {
return JsonParseError{"Error writing byte payload", err}
}
} else {
return JsonParseError{fmt.Sprintf("Tag 1 Byte value field '%v' not an integer", m["value"]), err}
}
case 2:
if i, ok := m["value"].(float64); ok {
if i < math.MinInt16 || i > math.MaxInt16 {
return JsonParseError{fmt.Sprintf("%v is out of range for tag 2 - Short", i), nil}
}
err = binary.Write(w, byteOrder, int16(i))
if err != nil {
return JsonParseError{"Error writing short payload", err}
}
} else {
return JsonParseError{fmt.Sprintf("Tag 2 Short value field '%v' not an integer", m["value"]), err}
}
case 3:
if i, ok := m["value"].(float64); ok {
if i < math.MinInt32 || i > math.MaxInt32 {
return JsonParseError{fmt.Sprintf("%v is out of range for tag 3 - Int", i), nil}
}
err = binary.Write(w, byteOrder, int32(i))
if err != nil {
return JsonParseError{"Error writing int32 payload", err}
}
} else {
return JsonParseError{fmt.Sprintf("Tag 3 Int value field '%v' not an integer", m["value"]), err}
}
case 4:
if int64Map, ok := m["value"].(map[string]interface{}); ok {
var nbtLong NbtLong
var vl, vm float64
if vl, ok = int64Map["valueLeast"].(float64); !ok {
return JsonParseError{fmt.Sprintf("Error reading valueLeast of '%v'", int64Map["valueLeast"]), nil}
}
nbtLong.ValueLeast = uint32(vl)
if vm, ok = int64Map["valueMost"].(float64); !ok {
return JsonParseError{fmt.Sprintf("Error reading valueMost of '%v'", int64Map["valueMost"]), nil}
}
nbtLong.ValueMost = uint32(vm)
err = binary.Write(w, byteOrder, int64(intPairToLong(nbtLong)))
if err != nil {
return JsonParseError{"Error writing int64 (from uint32 pair) payload:", err}
}
} else if int64String, ok := m["value"].(string); ok {
i, err := strconv.ParseInt(int64String, 10, 64)
if err != nil {
return JsonParseError{"Error converting long as string payload:", err}
}
err = binary.Write(w, byteOrder, i)
if err != nil {
return JsonParseError{"Error writing int64 (from string) payload:", err}
}
if err != nil {
return JsonParseError{fmt.Sprintf("Tag 4 Long value string field '%s' not an integer", int64String), err}
}
} else {
return JsonParseError{fmt.Sprintf("Tag 4 Long value field '%v' not an object", m["value"]), err}
}
case 5:
if f, ok := m["value"].(float64); ok {
// Comparing to smallest/max values is causing false errors as the nbt value comes out right even if this comparison doesn't
// Instead, will check for positive/negative infinity. Not sure what happens if f is too small for float32, but is likely edge case
// if f != 0 && (math.Abs(f) < math.SmallestNonzeroFloat32 || math.Abs(f) > math.MaxFloat32) {
if math.IsInf(float64(float32(f)), 0) {
return JsonParseError{fmt.Sprintf("%g is out of range for tag 5 - Float", f), nil}
}
err = binary.Write(w, byteOrder, float32(f))
if err != nil {
return JsonParseError{"Error writing float32 payload", err}
}
} else {
// If NaN is valid for double, maybe it's valid for float?
// return JsonParseError{fmt.Sprintf("Tag 5 Float value field '%v' not a number", m["value"]), err}
f = math.NaN()
err = binary.Write(w, byteOrder, float32(f))
if err != nil {
return JsonParseError{"Error writing float64 payload", err}
}
}
case 6:
if f, ok := m["value"].(float64); ok {
err = binary.Write(w, byteOrder, f)
if err != nil {
return JsonParseError{"Error writing float64 payload", err}
}
} else {
// Apparently NaN is a valid value in Minecraft for double?
// return JsonParseError{fmt.Sprintf("Tag 6 Double value field '%v' not a number", m["value"]), err}
f = math.NaN()
err = binary.Write(w, byteOrder, f)
if err != nil {
return JsonParseError{"Error writing float64 payload", err}
}
}
case 7:
if values, ok := m["value"].([]interface{}); ok {
err = binary.Write(w, byteOrder, int32(len(values)))
if err != nil {
return JsonParseError{"Error writing byte array length", err}
}
for _, value := range values {
if i, ok := value.(float64); ok {
if i < math.MinInt8 || i > math.MaxInt8 {
return JsonParseError{fmt.Sprintf("%v is out of range for Byte in tag 7 - Byte Array", i), nil}
}
err = binary.Write(w, byteOrder, int8(i))
if err != nil {
return JsonParseError{"Error writing element of byte array", err}
}
} else {
return JsonParseError{fmt.Sprintf("Tag 7 Byte Array element value field '%v' not an integer", m["value"]), err}
}
}
} else {
return JsonParseError{fmt.Sprintf("Tag 7 Byte Array value field '%v' not an array", m["value"]), err}
}
case 8:
if s, ok := m["value"].(string); ok {
err = binary.Write(w, byteOrder, int16(len([]byte(s))))
if err != nil {
return JsonParseError{"Error writing string length", err}
}
err = binary.Write(w, byteOrder, []byte(s))
if err != nil {
return JsonParseError{"Error writing string payload", err}
}
} else {
return JsonParseError{fmt.Sprintf("Tag 8 String value field '%v' not a string", m["value"]), err}
}
case 9:
// important: tagListType needs to be in scope to be passed to writePayload
// := were keeping it in a lower scope and zeroing it out.
var tagListType float64
if listMap, ok := m["value"].(map[string]interface{}); ok {
if tagListType, ok = listMap["tagListType"].(float64); ok {
err = binary.Write(w, byteOrder, byte(tagListType))
if err != nil {
return JsonParseError{"While writing tag 9 list type", err}
}
}
if values, ok := listMap["list"].([]interface{}); ok {
err = binary.Write(w, byteOrder, int32(len(values)))
if err != nil {
return JsonParseError{"While writing tag 9 list size", err}
}
for _, value := range values {
fakeTag := make(map[string]interface{})
fakeTag["value"] = value
err = writePayload(w, fakeTag, tagListType)
if err != nil {
return JsonParseError{"While writing tag 9 list of type " + strconv.Itoa(int(tagListType)), err}
}
}
} else if listMap["list"] == nil {
// NBT lists can be null / nil and therefore aren't represented as an array in JSON
err = binary.Write(w, byteOrder, int32(0))
if err != nil {
return JsonParseError{"While writing tag 9 list null size", err}
}
return nil
} else {
return JsonParseError{fmt.Sprintf("Tag 9 List's value field '%v' not an array or null", listMap["list"]), err}
}
} else {
return JsonParseError{fmt.Sprintf("Tag 9 List value field '%v' not an object", m["value"]), err}
}
case 10:
if values, ok := m["value"].([]interface{}); ok {
for _, value := range values {
err = writeTag(w, value)
if err != nil {
return JsonParseError{"While writing Compound tags", err}
}
}
// write the end tag which is just a single byte 0
err = binary.Write(w, byteOrder, byte(0))
if err != nil {
return JsonParseError{"Writing End tag", err}
}
} else {
return JsonParseError{fmt.Sprintf("Tag 10 Compound value field '%v' not an array", m["value"]), err}
}
case 11:
if values, ok := m["value"].([]interface{}); ok {
err = binary.Write(w, byteOrder, int32(len(values)))
if err != nil {
return JsonParseError{"Error writing int32 array length", err}
}
for _, value := range values {
if i, ok := value.(float64); ok {
if i < math.MinInt32 || i > math.MaxInt32 {
return JsonParseError{fmt.Sprintf("%v is out of range for Int in tag 11 - Int Array", i), nil}
}
err = binary.Write(w, byteOrder, int32(i))
if err != nil {
return JsonParseError{"Error writing element of int32 array", err}
}
} else {
return JsonParseError{fmt.Sprintf("Tag 11 Int Array element value field '%v' not an integer", value), err}
}
}
} else {
return JsonParseError{fmt.Sprintf("Tag Int Array value field '%v' not an array", m["value"]), err}
}
case 12:
if values, ok := m["value"].([]interface{}); ok {
err = binary.Write(w, byteOrder, int64(len(values)))
if err != nil {
return JsonParseError{"Error writing int64 array length", err}
}
for _, value := range values {
if int64Map, ok := value.(map[string]interface{}); ok {
var nbtLong NbtLong
var vl, vm float64
if vl, ok = int64Map["valueLeast"].(float64); !ok {
return JsonParseError{fmt.Sprintf("Error reading valueLeast of '%v'", int64Map["valueLeast"]), nil}
}
nbtLong.ValueLeast = uint32(vl)
if vm, ok = int64Map["valueMost"].(float64); !ok {
return JsonParseError{fmt.Sprintf("Error reading valueMost of '%v'", int64Map["valueMost"]), nil}
}
nbtLong.ValueMost = uint32(vm)
// if i, ok := value.(float64); ok {
err = binary.Write(w, byteOrder, int64(intPairToLong(nbtLong)))
if err != nil {
return JsonParseError{"Error writing element of int64 array", err}
}
} else if int64String, ok := value.(string); ok {
i, err := strconv.ParseInt(int64String, 10, 64)
if err != nil {
return JsonParseError{"Error converting long array element as string payload:", err}
}
err = binary.Write(w, byteOrder, i)
if err != nil {
return JsonParseError{"Error writing int64 array element (from string) payload", err}
}
if err != nil {
return JsonParseError{fmt.Sprintf("Tag 4 Long Array element value string field '%s' not an integer", int64String), err}
}
} else {
return JsonParseError{fmt.Sprintf("Tag Long Array element value field '%v' not an object", value), err}
}
}
} else {
return JsonParseError{fmt.Sprintf("Tag 12 Long Array element value field '%v' not an array", m["value"]), err}
}
default:
return JsonParseError{fmt.Sprintf("tagType '%v' is not recognized", tagType), err}
}
return err
}