-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriter.go
209 lines (186 loc) · 4.69 KB
/
writer.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
package polar
import (
"fmt"
"github.com/Tnze/go-mc/nbt"
"github.com/klauspost/compress/zstd"
)
func Write(world *World) ([]byte, error) {
var worldDataBuffer buffer
if err := writeWorld(&worldDataBuffer, world); err != nil {
return nil, err
}
var wrapBuffer buffer
if err := wrapBuffer.WriteInt32(MagicNumber); err != nil {
return nil, err
}
if err := wrapBuffer.WriteInt16(world.Version); err != nil {
return nil, err
}
if err := wrapBuffer.WriteInt8(int8(world.Compression)); err != nil {
return nil, err
}
if err := wrapBuffer.WriteVarInt(int32(worldDataBuffer.Len())); err != nil {
return nil, err
}
switch world.Compression {
case CompressionNone:
if err := wrapBuffer.WriteBytes(worldDataBuffer.Bytes()); err != nil {
return nil, err
}
case CompressionZstd:
w, _ := zstd.NewWriter(&wrapBuffer)
if _, err := w.Write(worldDataBuffer.Bytes()); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported compression type %d", world.Compression)
}
return wrapBuffer.Bytes(), nil
}
func writeWorld(b *buffer, world *World) (err error) {
if err = b.WriteInt8(world.MinSection); err != nil {
return err
}
if err = b.WriteInt8(world.MaxSection); err != nil {
return err
}
if err = b.WriteVarInt(int32(len(world.chunks))); err != nil {
return err
}
for _, chunk := range world.chunks {
if err = writeChunk(b, chunk, world.SectionCount()); err != nil {
return fmt.Errorf("failed to write chunk %d, %d: %w", chunk.X, chunk.Z, err)
}
}
return nil
}
func writeChunk(b *buffer, chunk *Chunk, sectionCount int) (err error) {
if err = b.WriteVarInt(chunk.X); err != nil {
return err
}
if err = b.WriteVarInt(chunk.Z); err != nil {
return err
}
// Section data (padded with empty if missing)
for i := 0; i < sectionCount; i++ {
var section *Section
if i < len(chunk.Sections) {
section = chunk.Sections[i]
} else {
section = &Section{}
}
if err = writeSection(b, section); err != nil {
return fmt.Errorf("failed to write section %d (abs): %w", i, err)
}
}
// Block entities
if err = b.WriteVarInt(int32(len(chunk.BlockEntities))); err != nil {
return err
}
for _, blockEntity := range chunk.BlockEntities {
if err = writeBlockEntity(b, blockEntity); err != nil {
x, y, z := blockEntity.Pos()
return fmt.Errorf("failed to write block entity %d, %d, %d: %w", x, y, z, err)
}
}
// Heightmaps todo
if err = b.WriteInt32(0); err != nil {
return err
}
// User data todo
if err = b.WriteVarInt(0); err != nil {
return err
}
return nil
}
func writeSection(b *buffer, section *Section) (err error) {
isEmpty := section.IsEmpty()
if err = b.WriteBool(isEmpty); err != nil {
return err
}
if isEmpty {
return nil
}
// Block palette
if err = b.WriteVarInt(int32(len(section.BlockPalette))); err != nil {
return err
}
for _, block := range section.BlockPalette {
if err = b.WriteString(block); err != nil {
return err
}
}
if len(section.BlockPalette) > 1 {
if err = b.WriteVarInt(int32(len(section.BlockStates))); err != nil {
return err
}
for _, state := range section.BlockStates {
if err = b.WriteUInt64(state); err != nil {
return err
}
}
}
// Biome palette
if err = b.WriteVarInt(int32(len(section.BiomePalette))); err != nil {
return err
}
for _, biome := range section.BiomePalette {
if err = b.WriteString(biome); err != nil {
return err
}
}
if len(section.BiomePalette) > 1 {
if err = b.WriteVarInt(int32(len(section.BiomeStates))); err != nil {
return err
}
for _, state := range section.BiomeStates {
if err = b.WriteUInt64(state); err != nil {
return err
}
}
}
// Light data
hasBlockLight := section.BlockLight != nil
if err = b.WriteBool(hasBlockLight); err != nil {
return err
}
if hasBlockLight {
if err = b.WriteBytes(section.BlockLight); err != nil {
return err
}
}
hasSkyLight := section.SkyLight != nil
if err = b.WriteBool(hasSkyLight); err != nil {
return err
}
if hasSkyLight {
if err = b.WriteBytes(section.SkyLight); err != nil {
return err
}
}
return nil
}
func writeBlockEntity(b *buffer, blockEntity *BlockEntity) (err error) {
if err = b.WriteInt32(blockEntity.ChunkPos); err != nil {
return err
}
hasBlockEntityId := blockEntity.ID != ""
if err = b.WriteBool(hasBlockEntityId); err != nil {
return err
}
if hasBlockEntityId {
if err = b.WriteString(blockEntity.ID); err != nil {
return err
}
}
hasBlockEntityData := blockEntity.Data != nil
if err = b.WriteBool(hasBlockEntityData); err != nil {
return err
}
if hasBlockEntityData {
if err = nbt.NewEncoder(b).Encode(blockEntity.Data, ""); err != nil {
return fmt.Errorf("failed to marshal block entity data: %w", err)
}
}
return nil
}