-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
583 lines (513 loc) · 12.7 KB
/
tag.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
package gonbt
import (
"errors"
)
// TagType values
const (
TagEnd byte = iota
TagByte
TagShort
TagInt
TagLong
TagFloat
TagDouble
TagByteArray
TagString
TagList
TagCompound
TagIntArray
TagLongArray
)
// Tag interface to provide a nbt reader / writer
type Tag interface {
Read(reader Reader) error
Write(writer Writer, printInfo bool) error
}
// NewTag instance
func NewTag(tagT byte, name string) (Tag, error) {
switch tagT {
case TagEnd:
return nil, errors.New(errorEnd)
case TagByte:
return &ByteT{Name: name}, nil
case TagShort:
return &ShortT{Name: name}, nil
case TagInt:
return &IntT{Name: name}, nil
case TagLong:
return &LongT{Name: name}, nil
case TagFloat:
return &FloatT{Name: name}, nil
case TagDouble:
return &DoubleT{Name: name}, nil
case TagByteArray:
return &ByteArrayT{Name: name}, nil
case TagString:
return &StringT{Name: name}, nil
case TagList:
return &ListT{Name: name}, nil
case TagCompound:
return &CompoundT{Name: name}, nil
case TagIntArray:
return &IntArrayT{Name: name}, nil
case TagLongArray:
return &LongArrayT{Name: name}, nil
default:
return nil, errors.New(errorTag)
}
}
// TagType return the tag type from the Tag parameter
func TagType(tag Tag) (byte, error) {
switch tag.(type) {
case *ByteT:
return TagByte, nil
case *ShortT:
return TagShort, nil
case *IntT:
return TagInt, nil
case *LongT:
return TagLong, nil
case *FloatT:
return TagFloat, nil
case *DoubleT:
return TagDouble, nil
case *ByteArrayT:
return TagByteArray, nil
case *StringT:
return TagString, nil
case *ListT:
return TagList, nil
case *CompoundT:
return TagCompound, nil
case *IntArrayT:
return TagIntArray, nil
case *LongArrayT:
return TagLongArray, nil
default:
return byte('0'), errors.New(errorTag)
}
}
// EndT to end type: 0
type EndT int
// ByteT to byte type: 1
type ByteT struct {
Name string
Value byte
}
// ShortT to short type: 2
type ShortT struct {
Name string
Value int16
}
// IntT to int type: 3
type IntT struct {
Name string
Value int32
}
// LongT to long type: 4
type LongT struct {
Name string
Value int64
}
// FloatT to float type: 5
type FloatT struct {
Name string
Value float32
}
// DoubleT to double type: 6
type DoubleT struct {
Name string
Value float64
}
// ByteArrayT to byte array type: 7
type ByteArrayT struct {
Name string
Value []byte
}
// StringT to string type: 8
type StringT struct {
Name string
Value string
}
// ListT to list type: 9
type ListT struct {
Name string
Value []interface{}
}
// CompoundT to compound type: 10
type CompoundT struct {
Name string
Value map[string]interface{}
}
// IntArrayT to int array type: 11
type IntArrayT struct {
Name string
Value []int32
}
// LongArrayT to long array type: 12
type LongArrayT struct {
Name string
Value []int64
}
// 1 TAG_Byte 1 byte / 8 bits, signed <number>b or <number>B A signed integral type. Sometimes used for booleans. Full range of -(27) to (27 - 1)
// (-128 to 127)
func (t *ByteT) Read(reader Reader) error {
var err error
if t.Value, err = reader.Byte(); err != nil {
return err
}
return nil
}
func (t *ByteT) Write(writer Writer, printInfo bool) error {
var err error
if printInfo {
if err = writer.Byte(TagByte); err != nil {
return err
}
if err = writer.String(t.Name); err != nil {
return err
}
}
if err = writer.Byte(t.Value); err != nil {
return err
}
return nil
}
// 2 TAG_Short 2 bytes / 16 bits, signed, big endian <number>s or <number>S A signed integral type. Full range of -(215) to (215 - 1)
// (-32,768 to 32,767)
func (t *ShortT) Read(reader Reader) error {
var err error
if t.Value, err = reader.Short(); err != nil {
return err
}
return nil
}
func (t *ShortT) Write(writer Writer, printInfo bool) error {
var err error
if printInfo {
if err = writer.Byte(TagShort); err != nil {
return err
}
if err = writer.String(t.Name); err != nil {
return err
}
}
if err = writer.Short(t.Value); err != nil {
return err
}
return nil
}
// 3 TAG_Int 4 bytes / 32 bits, signed, big endian <number> A signed integral type. Full range of -(231) to (231 - 1)
// (-2,147,483,648 to 2,147,483,647)
func (t *IntT) Read(reader Reader) error {
var err error
if t.Value, err = reader.Int(); err != nil {
return err
}
return nil
}
func (t *IntT) Write(writer Writer, printInfo bool) error {
var err error
if printInfo {
if err = writer.Byte(TagInt); err != nil {
return err
}
if err = writer.String(t.Name); err != nil {
return err
}
}
if err = writer.Int(t.Value); err != nil {
return err
}
return nil
}
// 4 TAG_Long 8 bytes / 64 bits, signed, big endian <number>l or <number>L A signed integral type. Full range of -(263) to (263 - 1)
// (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
func (t *LongT) Read(reader Reader) error {
var err error
if t.Value, err = reader.Long(); err != nil {
return err
}
return nil
}
func (t *LongT) Write(writer Writer, printInfo bool) error {
var err error
if printInfo {
if err = writer.Byte(TagLong); err != nil {
return err
}
if err = writer.String(t.Name); err != nil {
return err
}
}
if err = writer.Long(t.Value); err != nil {
return err
}
return nil
}
// 5 TAG_Float 4 bytes / 32 bits, signed, big endian, IEEE 754-2008, binary32 <number>f or <number>F A signed floating point type. Precision varies throughout number line;
// See Single-precision floating-point format. Maximum value about 3.4*1038
func (t *FloatT) Read(reader Reader) error {
var err error
if t.Value, err = reader.Float(); err != nil {
return err
}
return nil
}
func (t *FloatT) Write(writer Writer, printInfo bool) error {
var err error
if printInfo {
if err = writer.Byte(TagFloat); err != nil {
return err
}
if err = writer.String(t.Name); err != nil {
return err
}
}
if err = writer.Float(t.Value); err != nil {
return err
}
return nil
}
// 6 TAG_Double 8 bytes / 64 bits, signed, big endian, IEEE 754-2008, binary64 <decimal number>, <number>d or <number>D A signed floating point type. Precision varies throughout number line;
// See Double-precision floating-point format. Maximum value about 1.8*10308
func (t *DoubleT) Read(reader Reader) error {
var err error
if t.Value, err = reader.Double(); err != nil {
return err
}
return nil
}
func (t *DoubleT) Write(writer Writer, printInfo bool) error {
var err error
if printInfo {
if err = writer.Byte(TagDouble); err != nil {
return err
}
if err = writer.String(t.Name); err != nil {
return err
}
}
if err = writer.Double(t.Value); err != nil {
return err
}
return nil
}
// 7 TAG_Byte_Array TAG_Int's payload size, then size TAG_Byte's payloads. [B;<byte>,<byte>,...] An array of bytes. Maximum number of elements ranges between (231 - 9) and (231 - 1) (2,147,483,639 and 2,147,483,647), depending on the specific JVM.
func (t *ByteArrayT) Read(reader Reader) error {
var err error
if t.Value, err = reader.Bytes(); err != nil {
return err
}
return nil
}
func (t *ByteArrayT) Write(writer Writer, printInfo bool) error {
var err error
if printInfo {
if err = writer.Byte(TagByteArray); err != nil {
return err
}
if err = writer.String(t.Name); err != nil {
return err
}
}
if err = writer.Bytes(t.Value); err != nil {
return err
}
return nil
}
// 8 TAG_String A TAG_Short-like, but instead unsigned[2] payload length, then a UTF-8 string resembled by length bytes. <a-zA-Z0-9 text>, "<text>" (" within needs to be escaped to \"), or '<text>' (' within needs to be escaped to \') A UTF-8 string. It has a size, rather than being null terminated. 65,535 bytes interpretable as UTF-8 (see modified UTF-8 format; most commonly-used characters are a single byte).
func (t *StringT) Read(reader Reader) error {
var err error
if t.Value, err = reader.String(); err != nil {
return err
}
return nil
}
func (t *StringT) Write(writer Writer, printInfo bool) error {
var err error
if printInfo {
if err = writer.Byte(TagString); err != nil {
return err
}
if err = writer.String(t.Name); err != nil {
return err
}
}
if err = writer.String(t.Value); err != nil {
return err
}
return nil
}
// 9 TAG_List TAG_Byte's payload tagId, then TAG_Int's payload size, then size tags' payloads, all of type tagId. [<value>,<value>,...] A list of tag payloads, without repeated tag IDs or any tag names. Due to JVM limitations and the implementation of ArrayList, the maximum number of list elements is (231 - 9), or 2,147,483,639. Also note that List and Compound tags may not be nested beyond a depth of 512.
func (t *ListT) Read(reader Reader) error {
var err error
var tagT byte
var nbr int32
// get the the tagType
if tagT, err = reader.Byte(); err != nil {
return err
}
// get number element
if nbr, err = reader.Int(); err != nil {
return err
}
for i := int32(0); i < nbr; i++ {
var elem Tag
if elem, err = NewTag(tagT, ""); err != nil {
return err
}
if err = elem.Read(reader); err != nil {
return err
}
t.Value = append(t.Value, elem)
}
return nil
}
func (t *ListT) Write(writer Writer, printInfo bool) error {
var err error
var tagT byte
var nbr int32
if printInfo {
if err = writer.Byte(TagList); err != nil {
return err
}
if err = writer.String(t.Name); err != nil {
return err
}
}
nbr = int32(len(t.Value))
if nbr > 0 {
if _, ok := t.Value[0].(Tag); !ok {
return errors.New(errorTag)
}
if tagT, err = TagType(t.Value[0].(Tag)); err != nil {
return err
}
}
if err = writer.Byte(tagT); err != nil {
return err
}
if err = writer.Int(nbr); err != nil {
return err
}
for _, t := range t.Value {
if err = t.(Tag).Write(writer, false); err != nil {
return err
}
}
return nil
}
// 10 TAG_Compound Fully formed tags, followed by a TAG_End. {<tag name>:<value>,<tag name>:<value>,...} A list of fully formed tags, including their IDs, names, and payloads. No two tags may have the same name. Unlike lists, there is no hard limit to the number of tags within a Compound (of course, there is always the implicit limit of virtual memory). Note, however, that Compound and List tags may not be nested beyond a depth of 512.
func (t *CompoundT) Read(reader Reader) error {
var err error
var tagT byte
var name string
t.Value = make(map[string]interface{})
for tagT, err = reader.Byte(); tagT != TagEnd && err == nil; tagT, err = reader.Byte() {
if name, err = reader.String(); err != nil {
return err
}
var elem Tag
if elem, err = NewTag(tagT, name); err != nil {
return err
}
if err = elem.Read(reader); err != nil {
return err
}
t.Value[name] = elem
}
if err != nil {
return err
}
return nil
}
func (t *CompoundT) Write(writer Writer, printInfo bool) error {
var err error
if printInfo {
if err = writer.Byte(TagCompound); err != nil {
return err
}
if err = writer.String(t.Name); err != nil {
return err
}
}
for key, value := range t.Value {
var tagT byte
if _, ok := value.(Tag); !ok {
return errors.New(errorTag)
}
if tagT, err = TagType(value.(Tag)); err != nil {
return err
}
if err = writer.Byte(tagT); err != nil {
return err
}
if err = writer.String(key); err != nil {
return err
}
if err = value.(Tag).Write(writer, false); err != nil {
return err
}
}
if err = writer.Byte(TagEnd); err != nil {
return err
}
return nil
}
// 11 TAG_Int_Array TAG_Int's payload size, then size TAG_Int's payloads. [I;<integer>,<integer>,...] An array of TAG_Int's payloads. Maximum number of elements ranges between (231 - 9) and (231 - 1) (2,147,483,639 and 2,147,483,647), depending on the specific JVM.
func (t *IntArrayT) Read(reader Reader) error {
var err error
if t.Value, err = reader.IntArray(); err != nil {
return err
}
return nil
}
func (t *IntArrayT) Write(writer Writer, printInfo bool) error {
var err error
if printInfo {
if err = writer.Byte(TagIntArray); err != nil {
return err
}
if err = writer.String(t.Name); err != nil {
return err
}
}
if err = writer.Int(int32(len(t.Value))); err != nil {
return err
}
for _, v := range t.Value {
if err = writer.Int(v); err != nil {
return err
}
}
return nil
}
// 12 TAG_Long_Array TAG_
func (t *LongArrayT) Read(reader Reader) error {
var err error
if t.Value, err = reader.LongArray(); err != nil {
return err
}
return nil
}
func (t *LongArrayT) Write(writer Writer, printInfo bool) error {
var err error
if printInfo {
if err = writer.Byte(TagLongArray); err != nil {
return err
}
if err = writer.String(t.Name); err != nil {
return err
}
}
if err = writer.Long(int64(len(t.Value))); err != nil {
return err
}
for _, v := range t.Value {
if err = writer.Long(v); err != nil {
return err
}
}
return nil
}