-
Notifications
You must be signed in to change notification settings - Fork 97
/
dump.go
167 lines (157 loc) · 4.77 KB
/
dump.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
package nbt
import (
"fmt"
"reflect"
"strconv"
"strings"
)
// Dump returns a human readable decoded version of a serialised slice of NBT encoded using the encoding that
// is passed.
// Types are printed using the names present in the doc.go file and nested tags are indented using a single
// tab.
// Due to the nature of NBT, TAG_Compounds will not be printed in the same order. A different result is to be
// expected every time Dump is called, due to the random ordering.
//
// If the serialised NBT data passed is not parsable using the encoding that was passed, an error is returned
// and the resulting string will always be empty.
func Dump(data []byte, encoding Encoding) (string, error) {
var m map[string]interface{}
if err := UnmarshalEncoding(data, &m, encoding); err != nil {
return "", fmt.Errorf("error decoding NBT: %v", err)
}
s := &dumpState{}
return s.encodeTagType(m) + "(" + s.encodeTagValue(m) + ")", nil
}
// dumpState is used to keep track of values used during a single dump operations. A new one is created upon
// every call to Dump().
type dumpState struct {
// currentIndent specifies the amount of tabs that should be present in front of tags in the dump upon
// writing. The value is increased every time a compound or list tag is opened, and reduced every time
// a compound or list tag is closed.
currentIndent int
}
// indent returns the indentation required for the current nesting level. It is increased every time a list
// or compound tag is opened, and reduced when it is closed.
func (s *dumpState) indent() string {
return strings.Repeat(" ", s.currentIndent)
}
// encodeTagType encodes the type of the value passed to an NBT tag name. The way these are translated can be
// found in the doc.go file.
func (s *dumpState) encodeTagType(val interface{}) string {
if val == nil {
return "nil"
}
switch val.(type) {
case byte:
return "TAG_Byte"
case int16:
return "TAG_Short"
case int32:
return "TAG_Int"
case int64:
return "TAG_Long"
case float32:
return "TAG_Float"
case float64:
return "TAG_Double"
case string:
return "TAG_String"
}
t := reflect.TypeOf(val)
switch t.Kind() {
case reflect.Map:
return "TAG_Compound"
case reflect.Slice:
elemType := reflect.New(t.Elem()).Elem().Interface()
v := reflect.ValueOf(val)
if v.Len() != 0 && elemType == nil {
elemType = v.Index(0).Elem().Interface()
}
return "TAG_List<" + s.encodeTagType(elemType) + ">"
case reflect.Array:
switch t.Elem().Kind() {
case reflect.Uint8, reflect.Bool:
return "TAG_ByteArray"
case reflect.Int32:
return "TAG_IntArray"
case reflect.Int64:
return "TAG_LongArray"
}
}
panic("should not happen")
}
// encodeTagValue encodes a value passed to a format in which they are displayed in the dump string.
// encodeTagValue operates recursively: If lists or compounds are nested, encodeTagValue will include all
// nested tags.
func (s *dumpState) encodeTagValue(val interface{}) string {
const hextable = "0123456789abcdef"
switch v := val.(type) {
case byte:
return "0x" + string([]byte{hextable[v>>4], hextable[v&0x0f]})
case int16:
return strconv.Itoa(int(v))
case int32:
return strconv.Itoa(int(v))
case int64:
return strconv.FormatInt(v, 10)
case float32:
return strconv.FormatFloat(float64(v), 'g', -1, 32)
case float64:
return strconv.FormatFloat(v, 'g', -1, 64)
case string:
return v
}
t := reflect.TypeOf(val)
reflVal := reflect.ValueOf(val)
switch t.Kind() {
case reflect.Map:
b := strings.Builder{}
b.WriteString("{\n")
for _, k := range reflVal.MapKeys() {
v := reflVal.MapIndex(k)
actualVal := v.Interface()
s.currentIndent++
b.WriteString(fmt.Sprintf("%v'%v': %v(%v),\n", s.indent(), k.String(), s.encodeTagType(actualVal), s.encodeTagValue(actualVal)))
s.currentIndent--
}
b.WriteString(s.indent() + "}")
return b.String()
case reflect.Slice:
b := strings.Builder{}
b.WriteString("{\n")
for i := 0; i < reflVal.Len(); i++ {
v := reflVal.Index(i)
actualVal := v.Interface()
s.currentIndent++
b.WriteString(fmt.Sprintf("%v%v,\n", s.indent(), s.encodeTagValue(actualVal)))
s.currentIndent--
}
b.WriteString(s.indent() + "}")
return b.String()
case reflect.Array:
switch t.Elem().Kind() {
case reflect.Uint8:
b := strings.Builder{}
for i := 0; i < reflVal.Len(); i++ {
v := reflVal.Index(i).Uint()
b.WriteString("0x")
b.WriteString(string([]byte{hextable[v>>4], hextable[v&0x0f]}))
if i != reflVal.Len()-1 {
b.WriteByte(' ')
}
}
return b.String()
case reflect.Int32, reflect.Int64:
b := strings.Builder{}
for i := 0; i < reflVal.Len(); i++ {
v := reflVal.Index(i).Int()
b.WriteString(strconv.FormatInt(v, 10))
if i != reflVal.Len()-1 {
b.WriteByte(' ')
}
}
return b.String()
}
}
panic("should not happen")
}