-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnbt.go
154 lines (118 loc) · 3.02 KB
/
nbt.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
package nbt
/*
nbt
Copyright (c) 2019 beito
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
*/
import (
"errors"
"fmt"
"strconv"
"github.com/beito123/binary"
)
const (
// IDTagEnd signifies end for compound tag
// Payload: none
IDTagEnd = iota
// IDTagByte is a signed byte (-128 to 127)
// Payload is 1byte
IDTagByte
// IDTagShort is a signed short (-32768 to 32767)
// Payload is 2 bytes
IDTagShort
// IDTagInt is a signed int (-2147483648 to 2147483647)
// Payload is 4 bytes
IDTagInt
// IDTagLong is a signed long (-9223372036854775808 to 9223372036854775807)
// Payload is 8 bytes
IDTagLong
// IDTagFloat is a signed single float (IEEE-754)
// Payload is 4 bytes
IDTagFloat
// IDTagDouble is a signed single double (IEEE-754)
// Payload is 8 bytes
IDTagDouble
// IDTagByteArray is a array of signed bytes
// Payload is 4 bytes(len of data with signed int) + len bytes
IDTagByteArray
// IDTagString is a UTF-8 string (max 32767 bytes)
// Payload is 2 bytes (len of data with short) + len bytes
IDTagString
// IDTagList is a list of nameless tags, all tags need same type.
// Payload is 1 byte(tag type with byte) + 4 bytes(len with signed int) + len bytes
IDTagList
// IDTagCompound is a list of named tags.
IDTagCompound
// IDTagIntArray is a array for int(4bytes).
IDTagIntArray
// IDTagLongArray is a array for long(8bytes).
IDTagLongArray
)
var (
// BigEndian is for MCJE, anvil and more...
BigEndian = binary.BigEndian
// LittleEndian is for MCBE leveldb format
LittleEndian = binary.LittleEndian
)
// NewStream returns new Stream
func NewStream(order binary.Order) *Stream {
return NewStreamBytes(order, []byte{})
}
// NewStreamBytes returns new Stream with bytes data
func NewStreamBytes(order binary.Order, b []byte) *Stream {
return &Stream{
Stream: binary.NewOrderStreamBytes(order, b),
}
}
// Stream is binary nbt stream
type Stream struct {
Stream *binary.OrderStream
}
// Reset resets buffer
func (s *Stream) Reset() {
s.Stream.Reset()
}
// Bytes returns stream's all buffer
func (s *Stream) Bytes() []byte {
return s.Stream.AllBytes()
}
// ReadTag reads tag from buffer
func (s *Stream) ReadTag() (Tag, error) {
errHandle := func(err error) error {
return fmt.Errorf("nbt: happened errors while it's parsing near %d Error: %s", s.Stream.Off(), err.Error())
}
id, err := s.Stream.Byte()
if err != nil {
return nil, errHandle(err)
}
tag := getTagByID(id)
if tag == nil {
return nil, errors.New("mc.nbt: invalid tag, " + strconv.Itoa(int(id)))
}
if tag.ID() == IDTagEnd {
return tag, nil
}
name, err := readString(s.Stream)
if err != nil {
return nil, errHandle(err)
}
tag.SetName(name)
err = tag.Read(s)
if err != nil {
return nil, errHandle(err)
}
return tag, nil
}
// WriteTag writes tag to buffer
func (s *Stream) WriteTag(tag Tag) error {
err := s.Stream.PutByte(tag.ID())
if err != nil {
return err
}
err = writeString(s.Stream, tag.Name())
if err != nil {
return err
}
return tag.Write(s)
}