-
Notifications
You must be signed in to change notification settings - Fork 1
/
nbt.go
275 lines (249 loc) · 6.01 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
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
// Package nbt provides an implementation of Minecraft's
// NBT data format.
package nbt
//go:generate go run typegen/main.go -in type.tmp -out typegen.go End Byte Short Int Long Float Double ByteArray String List Compound IntArray LongArray
import (
"fmt"
"io"
"strconv"
)
// Type represents the types of tags available.
type Type uint8
// We define tags for the various NBT tag types.
const (
TypeEnd Type = iota
TypeByte
TypeShort
TypeInt
TypeLong
TypeFloat
TypeDouble
TypeByteArray
TypeString
TypeList
TypeCompound
TypeIntArray
TypeLongArray
TypeMax
)
// A Tag is one of several concrete types which represent NBT "payloads",
// because it turns out that's the correct conceptual entity to think of
// as a "tag".
type Tag interface {
Type() Type
store(w io.Writer) error
}
type End struct{}
type Byte int8
type Short int16
type Int int32
type Long int64
type Float float32
type Double float64
type ByteArray []int8
type String string
// List is a pain to work with; see tag.tmp for the template code, and
// typegen.go for the fancy generated-code implementations with type
// switches.
type List struct {
Contents Type
data interface{}
}
type Compound map[String]Tag
type IntArray []Int
type LongArray []Long
// You never actually have to make an End to put in a List Of End objects,
// so we check the interface thing here for consistency.
var _ Tag = End{}
// String() makes End objects printable.
func (x End) String() string {
return ""
}
// String() makes Byte objects printable.
func (x Byte) String() string {
return fmt.Sprintf("%q", byte(x))
}
// String() makes Short objects printable.
func (x Short) String() string {
return strconv.FormatInt(int64(x), 10)
}
// String() makes Int objects printable.
func (x Int) String() string {
return strconv.FormatInt(int64(x), 10)
}
// String() makes Long objects printable.
func (x Long) String() string {
return strconv.FormatInt(int64(x), 10)
}
// String() makes Float objects printable.
func (x Float) String() string {
return strconv.FormatFloat(float64(x), 'f', -1, 32)
}
// String() makes Double objects printable.
func (x Double) String() string {
return strconv.FormatFloat(float64(x), 'f', -1, 64)
}
// String() makes ByteArray objects printable.
func (x ByteArray) String() string {
return fmt.Sprintf("%v [%d elements]", x.Type(), len(x))
}
// String() makes String objects printable.
func (x String) String() string {
return string(x)
}
// String() makes List objects printable.
func (x List) String() string {
return fmt.Sprintf("list[%d elements] of %v", x.Length(), x.Contents)
}
// String() makes Compound objects printable.
func (x Compound) String() string {
return fmt.Sprintf("%v [%d elements]", x.Type(), len(x))
}
// String() makes IntArray objects printable.
func (x IntArray) String() string {
return fmt.Sprintf("%v [%d elements]", x.Type(), len(x))
}
// String() makes LongArray objects printable.
func (x LongArray) String() string {
return fmt.Sprintf("%v [%d elements]", x.Type(), len(x))
}
// PrintIndented pretty-prints the given Tag.
func PrintIndented(w io.Writer, t Tag) {
printIndented(w, t, nil, 0)
}
// printIndented tries to print the given tag,
func printIndented(w io.Writer, p Tag, prefix interface{}, indent int) {
fmt.Fprintf(w, "%*s", indent*2, "")
switch v := prefix.(type) {
case nil:
// do nothing with a nil prefix
case string:
fmt.Fprintf(w, "%s: ", v)
case int:
fmt.Fprintf(w, "[%d]: ", v)
default:
fmt.Fprintf(w, "%s: ", v)
}
defer fmt.Fprintln(w) // newline after this
switch x := p.(type) {
default:
fmt.Fprintf(w, "[unknown tag %v]", p.Type())
case End:
fmt.Fprintf(w, "}")
case Byte:
fmt.Fprintf(w, "%q", x)
case Short:
fmt.Fprintf(w, "%d", x)
case Int:
fmt.Fprintf(w, "%d", x)
case Long:
fmt.Fprintf(w, "%d", x)
case Float:
fmt.Fprintf(w, "%f", x)
case Double:
fmt.Fprintf(w, "%f", x)
case String:
fmt.Fprintf(w, "%s", x)
case ByteArray:
fmt.Fprintf(w, "[%d item byte]", len(x))
case IntArray:
fmt.Fprintf(w, "[%d item int]", len(x))
case LongArray:
fmt.Fprintf(w, "[%d item long]", len(x))
case List:
length := x.Length()
fmt.Fprintf(w, "[%d %v list] {", length, x.Contents)
if length != 0 {
fmt.Fprintf(w, "\n")
x.Iterate(func(i int, t Tag) error { printIndented(w, p, i, indent+1); return nil })
}
fmt.Fprintf(w, "%*s}", indent*2, "")
case Compound:
fmt.Fprintf(w, "compound [%d elements] {\n", len(x))
for k, v := range x {
printIndented(w, v, k, indent+1)
}
fmt.Fprintf(w, "%*s}", indent*2, "")
}
}
func TagLength(t Tag) int {
switch tag := t.(type) {
case ByteArray:
return len(tag)
case IntArray:
return len(tag)
case LongArray:
return len(tag)
case Compound:
return len(tag)
case List:
return tag.Length()
}
return 0
}
// Element obtains the element t[idx], where idx is a string for a
// Compound element, or an int for Array or List types.
func TagElement(t Tag, idx interface{}) (out Tag, ok bool) {
if t == nil {
return nil, false
}
switch tag := t.(type) {
case Compound:
sidx, ok := idx.(String)
if !ok {
// allow plain Go strings
str, sok := idx.(string)
if !sok {
return nil, false
}
sidx = String(str)
}
pay, ok := tag[sidx]
return pay, ok
case List:
idx, ok := idx.(int)
if !ok {
return nil, false
}
data, ok := tag.Element(idx)
return data, ok
case ByteArray:
idx, ok := idx.(int)
if !ok {
return nil, false
}
if idx >= 0 && idx < len(tag) {
return Byte(tag[idx]), true
}
return nil, false
case IntArray:
idx, ok := idx.(int)
if !ok {
return nil, false
}
if idx >= 0 && idx < len(tag) {
return tag[idx], true
}
return nil, false
case LongArray:
idx, ok := idx.(int)
if !ok {
return nil, false
}
if idx >= 0 && idx < len(tag) {
return tag[idx], true
}
return nil, false
default:
return nil, false
}
}
// HasElements indicates whether an item conceptually has sub-elements.
func TagHasElements(t Tag) bool {
switch t.Type() {
case TypeCompound, TypeList, TypeByteArray, TypeIntArray, TypeLongArray:
return true
default:
return false
}
}