MCPcopy Create free account
hub / github.com/ZeppelinMC/Zeppelin / Decode

Method Decode

protocol/nbt/decoder.go:123–168  ·  view source on GitHub ↗

Decode will decode the nbt file into v and return the root name of the file

(v any)

Source from the content-addressed store, hash-verified

121
122// Decode will decode the nbt file into v and return the root name of the file
123func (d *Decoder) Decode(v any) (rootName string, err error) {
124 val := reflect.ValueOf(v)
125 if val.Kind() != reflect.Pointer {
126 return "", fmt.Errorf("Decode expects a pointer")
127 }
128 val = val.Elem()
129
130 typeId, err := d.readByte()
131 if err != nil {
132 return "", err
133 }
134 if typeId != Compound {
135 return "", fmt.Errorf("expected a compound first element")
136 }
137
138 if !d.dontReadRootCompoundName {
139 rootName, err = d.readString()
140 if err != nil {
141 return
142 }
143 }
144
145 switch val.Kind() {
146 case reflect.Struct:
147 m := generateMap(val)
148 defer func() {
149 clear(m)
150 valueMap.Put(m)
151 }()
152
153 if err := d.decodeCompoundStruct(m); err != nil {
154 return "", err
155 }
156 case reflect.Map:
157 if val.IsNil() {
158 val.Set(reflect.MakeMap(val.Type()))
159 }
160 if err := d.decodeCompoundMap(val); err != nil {
161 return rootName, err
162 }
163 default:
164 return rootName, fmt.Errorf("Decode expects a pointer of struct/map, not %s", val.Type())
165 }
166
167 return
168}
169
170func (d *Decoder) decodeCompoundMap(_map reflect.Value) error {
171 var typeId [1]byte

Callers 1

NBTMethod · 0.95

Calls 7

readByteMethod · 0.95
readStringMethod · 0.95
decodeCompoundStructMethod · 0.95
decodeCompoundMapMethod · 0.95
generateMapFunction · 0.85
SetMethod · 0.45
TypeMethod · 0.45

Tested by

no test coverage detected