-
Notifications
You must be signed in to change notification settings - Fork 13
/
chunk.go
245 lines (225 loc) · 4.72 KB
/
chunk.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
package nbt
import (
"compress/gzip"
"errors"
"fmt"
"io"
)
type Chunk struct {
XPos, ZPos int
Blocks []Block
}
var (
ErrListUnknown = errors.New("Lists of unknown type aren't supported")
)
func ReadChunkDat(reader io.Reader) (*Chunk, error) {
r, err := gzip.NewReader(reader)
defer r.Close()
if err != nil {
return nil, err
}
return ReadChunkNbt(r)
}
func ReadChunkNbt(reader io.Reader) (*Chunk, error) {
chunkData := new(chunkData)
chunkData.sections = make([]*sectionData, 0)
if err := chunkData.parse(NewReader(reader), false); err != nil {
return nil, err
}
chunk := &Chunk{chunkData.xPos, chunkData.zPos, nil}
if len(chunkData.sections) != 0 {
chunk.Blocks = make([]Block, 256*16*16) // Hard coded height for now. TODO: Make variable height chunks.
for _, section := range chunkData.sections {
for i, blockId := range section.blocks {
var metadata byte
if i&1 == 1 {
metadata = section.data[i/2] >> 4
} else {
metadata = section.data[i/2] & 0xf
}
// Note that the old format is XZY and the new format is YZX
x, z, y := indexToCoords(i, 16, 16)
chunk.Blocks[coordsToIndex(x, z, y+16*section.y, 16, 256)] = Block(blockId) + (Block(metadata) << 8)
}
}
} else {
if chunkData.blocks != nil && chunkData.data != nil {
chunk.Blocks = make([]Block, len(chunkData.blocks))
for i, blockId := range chunkData.blocks {
var metadata byte
if i&1 == 1 {
metadata = chunkData.data[i/2] >> 4
} else {
metadata = chunkData.data[i/2] & 0xf
}
chunk.Blocks[i] = Block(blockId) + (Block(metadata) << 8)
}
}
}
return chunk, nil
}
func indexToCoords(i, aMax, bMax int) (a, b, c int) {
a = i % aMax
b = (i / aMax) % bMax
c = i / (aMax * bMax)
return
}
func coordsToIndex(a, b, c, bMax, cMax int) int {
return c + cMax*(b+bMax*a)
}
func yzxToXzy(yzx, xMax, zMax, yMax int) int {
x := yzx % xMax
z := (yzx / xMax) % zMax
y := (yzx / (xMax * zMax)) % yMax
// yzx := x + xMax*(z + zMax*y)
xzy := y + yMax*(z+zMax*x)
return xzy
}
type chunkData struct {
xPos, zPos int
blocks []byte
data []byte
section *sectionData
sections []*sectionData
}
type sectionData struct {
y int
blocks []byte
data []byte
}
func (chunk *chunkData) parse(r *Reader, listStruct bool) error {
structDepth := 0
if listStruct {
structDepth++
}
for {
typeId, name, err := r.ReadTag()
if err != nil {
if err == io.EOF {
break
}
return err
}
switch typeId {
case TagStruct:
structDepth++
case TagStructEnd:
structDepth--
if structDepth == 0 {
return nil
}
case TagByteArray:
bytes, err := r.ReadBytes()
if err != nil {
return err
}
if name == "Blocks" {
if chunk.section != nil {
chunk.section.blocks = bytes
} else {
chunk.blocks = bytes
}
} else if name == "Data" {
if chunk.section != nil {
chunk.section.data = bytes
} else {
chunk.data = bytes
}
}
case TagIntArray:
_, err := r.ReadInts()
if err != nil {
return err
}
case TagInt8:
number, err := r.ReadInt8()
if err != nil {
return err
}
if name == "Y" {
chunk.section.y = int(number)
}
case TagInt16:
_, err := r.ReadInt16()
if err != nil {
return err
}
case TagInt32:
number, err := r.ReadInt32()
if err != nil {
return err
}
if name == "xPos" {
chunk.xPos = number
}
if name == "zPos" {
chunk.zPos = number
}
case TagInt64:
_, err := r.ReadInt64()
if err != nil {
return err
}
case TagFloat32:
_, err := r.ReadFloat32()
if err != nil {
return err
}
case TagFloat64:
_, err := r.ReadFloat64()
if err != nil {
return err
}
case TagString:
_, err := r.ReadString()
if err != nil {
return err
}
case TagList:
itemTypeId, length, err := r.ReadListHeader()
if err != nil {
return err
}
switch itemTypeId {
case TagInt8:
for i := 0; i < length; i++ {
_, err := r.ReadInt8()
if err != nil {
return err
}
}
case TagFloat32:
for i := 0; i < length; i++ {
_, err := r.ReadFloat32()
if err != nil {
return err
}
}
case TagFloat64:
for i := 0; i < length; i++ {
_, err := r.ReadFloat64()
if err != nil {
return err
}
}
case TagStruct:
for i := 0; i < length; i++ {
if name == "Sections" {
chunk.section = new(sectionData)
chunk.sections = append(chunk.sections, chunk.section)
}
err := chunk.parse(r, true)
if err != nil {
return err
}
}
default:
fmt.Printf("# %s list todo(%v) %v\n", name, itemTypeId, length)
return ErrListUnknown
}
default:
fmt.Printf("# %s todo(%d)\n", name, typeId)
}
}
return nil
}