This repository has been archived by the owner on Aug 16, 2024. It is now read-only.
forked from astei/anvil2slime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slime_writer.go
259 lines (226 loc) · 6.12 KB
/
slime_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
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
package main
import (
"bytes"
"encoding/binary"
"github.com/astei/anvil2slime/nbt"
"github.com/klauspost/compress/zstd"
"io"
"io/ioutil"
"sort"
)
const slimeHeader = 0xB10B
const slimeLatestVersion = 3
func slimeChunkKey(coord ChunkCoord) int64 {
return (int64(coord.Z) * 0x7fffffff) + int64(coord.X)
}
func (world *AnvilWorld) WriteAsSlime(writer io.Writer) error {
zstdWriter, err := zstd.NewWriter(ioutil.Discard)
if err != nil {
return err
}
slimeWriter := &slimeWriter{writer: writer, world: world, zstdWriter: zstdWriter}
return slimeWriter.writeWorld()
}
type slimeWriter struct {
writer io.Writer
world *AnvilWorld
zstdWriter *zstd.Encoder
}
func (w *slimeWriter) writeWorld() (err error) {
if err = w.writeHeader(); err != nil {
return
}
if err = w.writeChunks(); err != nil {
return
}
if err = w.writeTileEntities(); err != nil {
return
}
if err = w.writeEntities(); err != nil {
return
}
if err = w.writeExtra(); err != nil {
return
}
return
}
func (w *slimeWriter) writeHeader() (err error) {
minChunkXZ, width, depth := w.determineChunkBounds()
used := w.createChunkBitset(width, depth, minChunkXZ)
var header struct {
Magic uint16
Version uint8
MinX int16
MinZ int16
Width uint16
Depth uint16
}
header.Magic = slimeHeader
header.Version = slimeLatestVersion
header.MinX = int16(minChunkXZ.X)
header.MinZ = int16(minChunkXZ.Z)
header.Width = uint16(width)
header.Depth = uint16(depth)
if err = binary.Write(w.writer, binary.BigEndian, header); err != nil {
return
}
_, err = w.writer.Write(used)
return
}
func (w *slimeWriter) createChunkBitset(width int, depth int, minChunkXZ ChunkCoord) []byte {
chunkCoords := w.world.getChunkKeys()
populated := newFixedBitSet(width * depth)
for _, currentChunk := range chunkCoords {
relZ := currentChunk.Z - minChunkXZ.Z
relX := currentChunk.X - minChunkXZ.X
idx := relZ*width + relX
populated.Set(idx)
}
return populated.Bytes()
}
func (w *slimeWriter) determineChunkBounds() (minChunkXZ ChunkCoord, width int, depth int) {
// Slime uses its own order for chunks, but still requires us to determine the maximum/minimum XZ coordinates.
minX, maxX, minZ, maxZ := w.world.getMinXZ()
width = maxX - minX + 1
depth = maxZ - minZ + 1
return ChunkCoord{X: minX, Z: minZ}, width, depth
}
func (w *slimeWriter) writeChunks() (err error) {
slimeSorted := w.world.getChunkKeys()
sort.Slice(slimeSorted, func(one, two int) bool {
k1 := slimeChunkKey(slimeSorted[one])
k2 := slimeChunkKey(slimeSorted[two])
return k1 < k2
})
var out bytes.Buffer
for _, coord := range slimeSorted {
chunk := w.world.chunks[coord]
if err = w.writeChunkHeader(chunk, &out); err != nil {
return
}
for _, section := range chunk.Sections {
if err = w.writeChunkSection(section, &out); err != nil {
return
}
}
}
return w.writeZstdCompressed(&out)
}
func (w *slimeWriter) writeChunkHeader(chunk MinecraftChunk, out io.Writer) (err error) {
for _, heightEntry := range chunk.HeightMap {
if err = binary.Write(out, binary.BigEndian, int32(heightEntry)); err != nil {
return
}
}
if _, err = out.Write(chunk.Biomes); err != nil {
return
}
w.writeChunkSectionsPopulatedBitmask(chunk, out)
return
}
func (w *slimeWriter) writeChunkSectionsPopulatedBitmask(chunk MinecraftChunk, out io.Writer) {
sectionsPopulated := newFixedBitSet(16)
for _, section := range chunk.Sections {
sectionsPopulated.Set(int(section.Y))
}
_, _ = out.Write(sectionsPopulated.Bytes())
return
}
func (w *slimeWriter) writeChunkSection(section MinecraftChunkSection, out io.Writer) (err error) {
if _, err = out.Write(section.BlockLight); err != nil {
return
}
if _, err = out.Write(section.Blocks); err != nil {
return
}
if _, err = out.Write(section.Data); err != nil {
return
}
if _, err = out.Write(section.SkyLight); err != nil {
return
}
if err = binary.Write(out, binary.BigEndian, uint16(0)); err != nil {
return
}
return
}
func (w *slimeWriter) writeZstdCompressed(buf *bytes.Buffer) (err error) {
uncompressedSize := buf.Len()
var compressedOutput bytes.Buffer
w.zstdWriter.Reset(&compressedOutput)
if _, err = buf.WriteTo(w.zstdWriter); err != nil {
return
}
if err = w.zstdWriter.Close(); err != nil {
return
}
w.zstdWriter.Reset(ioutil.Discard)
if err = binary.Write(w.writer, binary.BigEndian, uint32(compressedOutput.Len())); err != nil {
return
}
if err = binary.Write(w.writer, binary.BigEndian, uint32(uncompressedSize)); err != nil {
return
}
_, err = compressedOutput.WriteTo(w.writer)
return
}
func (w *slimeWriter) writeTileEntities() (err error) {
var tileEntities []interface{}
for _, chunk := range w.world.chunks {
tileEntities = append(tileEntities, chunk.TileEntities...)
}
var compound struct {
Tiles []interface{} `nbt:"tiles"`
}
compound.Tiles = tileEntities
return w.writeCompressedNbt(compound)
}
func (w *slimeWriter) writeEntities() (err error) {
var entities []interface{}
for _, chunk := range w.world.chunks {
entities = append(entities, chunk.Entities...)
}
var compound struct {
Entities []interface{} `nbt:"entities"`
}
compound.Entities = entities
if _, err = w.writer.Write([]byte{1}); err != nil {
return
}
return w.writeCompressedNbt(compound)
}
func (w *slimeWriter) writeCompressedNbt(compound interface{}) (err error) {
var buf bytes.Buffer
if err = nbt.NewEncoder(&buf).Encode(compound); err != nil {
return
}
return w.writeZstdCompressed(&buf)
}
func (w *slimeWriter) writeExtra() (err error) {
// Write empty NBT tag compound
var empty map[string]interface{}
return w.writeCompressedNbt(empty)
}
func (world *AnvilWorld) getChunkKeys() []ChunkCoord {
var keys []ChunkCoord
for coord := range world.chunks {
keys = append(keys, coord)
}
return keys
}
func (world *AnvilWorld) getMinXZ() (minX int, maxX int, minZ int, maxZ int) {
keys := world.getChunkKeys()
sort.Slice(keys, func(one, two int) bool {
c1 := keys[one]
c2 := keys[two]
return c1.X < c2.X
})
minX = keys[0].X
maxX = keys[len(keys)-1].X
sort.Slice(keys, func(one, two int) bool {
return keys[one].Z < keys[two].Z
})
minZ = keys[0].Z
maxZ = keys[len(keys)-1].Z
return
}