-
Notifications
You must be signed in to change notification settings - Fork 0
/
world_optimizer.go
340 lines (302 loc) · 7.72 KB
/
world_optimizer.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package main
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"math"
"path/filepath"
"sort"
"strings"
"mc-world-trimmer/chunk"
"github.com/Tnze/go-mc/save/region"
"github.com/dustin/go-humanize"
"github.com/spf13/afero"
)
type WorldOptimizer struct {
Source Source
AnyWorldFound bool
ComputeHeightMaps bool
ComputeLowMaps bool
}
func (o *WorldOptimizer) Process(recursive bool) error {
if recursive {
matches, _ := findWorldDirs(o.fs())
for _, m := range matches {
if err := o.checkWorldCandidate(m); err != nil {
return err
}
}
} else {
return o.checkWorldCandidate("")
}
return nil
}
func (o *WorldOptimizer) checkWorldCandidate(dir string) error {
if ok, err := afero.IsDir(o.fs(), dir); err != nil {
return err
} else if !ok {
return nil
}
readdir, err := afero.ReadDir(o.fs(), dir)
if err != nil {
return err
}
levelFound := false
regionFound := false
for _, file := range readdir {
switch file.Name() {
case "level.dat":
levelFound = true
case "region":
if file.IsDir() {
regionFound = true
}
}
}
if levelFound && regionFound {
return o.optimize(dir)
}
return nil
}
func (o *WorldOptimizer) optimize(dir string) error {
o.AnyWorldFound = true
o.log(dir, "optimize...")
if err := o.processChunks(dir); err != nil {
return err
}
if err := o.deleteUselessFiles(dir); err != nil {
return err
}
return nil
}
func (o *WorldOptimizer) deleteUselessFiles(dir string) error {
if err := o.removeDirIfExists(filepath.Join(dir, "playerdata")); err != nil {
return err
}
if err := o.removeDirIfExists(filepath.Join(dir, "stats")); err != nil {
return err
}
if err := o.removeFileIfExists(filepath.Join(dir, "level.dat_old")); err != nil {
return err
}
if err := o.removeFileIfExists(filepath.Join(dir, "session.lock")); err != nil {
return err
}
if !o.ComputeLowMaps {
if err := o.removeFileIfExists(filepath.Join(dir, "lowmap.bin")); err != nil {
return err
}
}
return nil
}
func (o *WorldOptimizer) processChunks(dir string) error {
var worldSize uint64
var newWorldSize uint64
lowmaps := make(map[ChunkPos][]byte)
regionDirPath := filepath.Join(dir, "region")
regionFiles, err := afero.ReadDir(o.fs(), regionDirPath)
if err != nil {
return err
}
for _, file := range regionFiles {
worldSize += uint64(file.Size())
path := filepath.Join(regionDirPath, file.Name())
if strings.HasSuffix(path, ".mcr") {
if exists, err := afero.Exists(o.fs(), path[:len(path)-4]+".mca"); err != nil {
return err
} else if !exists {
continue
}
if err := o.fs().Remove(path); err != nil {
return err
}
continue
}
if !strings.HasSuffix(path, ".mca") {
continue
}
open, err := o.fs().Open(path)
if err != nil {
return fmt.Errorf("%s region file read: %w", path, err)
}
rg, err := region.Load(open)
if err != nil {
return fmt.Errorf("%s region load: %w", path, err)
}
removedChunks := make(map[ChunkPos]bool)
updatedChunks := make(map[ChunkPos]*chunk.Chunk_1_8_8)
numChunks := 0
for cx := 0; cx < 32; cx++ {
for cz := 0; cz < 32; cz++ {
if !rg.ExistSector(cx, cz) {
continue
}
var c chunk.Chunk_1_8_8
if sector, err := rg.ReadSector(cx, cz); err != nil {
return fmt.Errorf("%s read sector %d,%d: %w", path, cx, cz, err)
} else if err = c.Load(sector); err != nil {
return fmt.Errorf("%s read chunk %d,%d: %w", path, cx, cz, err)
}
numChunks++
updated := false
if c.IsEmpty() {
removedChunks[ChunkPos{cx, cz}] = true
continue
} else if c.Optimize() {
if c.IsEmpty() {
removedChunks[ChunkPos{cx, cz}] = true
continue
} else {
updated = true
}
}
if o.ComputeHeightMaps && c.ComputeHeightMap() {
updated = true
}
if o.ComputeLowMaps {
lowmaps[ChunkPos{int(c.XPos), int(c.ZPos)}] = c.ComputeLowMap()
}
if updated {
updatedChunks[ChunkPos{cx, cz}] = &c
}
}
}
if len(updatedChunks) > 0 || numChunks > len(removedChunks) && len(removedChunks) > 0 {
newFile, err := o.fs().Create(path)
if err != nil {
return fmt.Errorf("%s create file: %w", path, err)
}
replace, err := region.CreateWriter(newFile)
if err != nil {
return fmt.Errorf("%s create region: %w", path, err)
}
for cx := 0; cx < 32; cx++ {
for cz := 0; cz < 32; cz++ {
if !rg.ExistSector(cx, cz) {
continue
}
if _, ok := removedChunks[ChunkPos{cx, cz}]; ok {
continue
}
if c, ok := updatedChunks[ChunkPos{cx, cz}]; ok {
if data, err := c.Save(); err != nil {
return fmt.Errorf("%s write chunk %d,%d: %w", path, cx, cz, err)
} else if err := replace.WriteSector(cx, cz, data); err != nil {
return fmt.Errorf("%s write sector %d,%d: %w", path, cx, cz, err)
}
} else {
if sector, err := rg.ReadSector(cx, cz); err != nil {
return fmt.Errorf("%s read sector %d,%d: %w", path, cx, cz, err)
} else if err := replace.WriteSector(cx, cz, sector); err != nil {
return fmt.Errorf("%s write sector %d,%d: %w", path, cx, cz, err)
}
}
}
}
if err := replace.PadToFullSector(); err != nil {
return err
}
if err := replace.Close(); err != nil {
return err
}
stat, _ := newFile.Stat()
if *verbose {
o.log(dir, file.Name(), "updated",
humanize.Bytes(uint64(file.Size())), "to", humanize.Bytes(uint64(stat.Size())),
)
}
newWorldSize += uint64(stat.Size())
_ = open.Close()
continue
}
_ = open.Close()
if numChunks == len(removedChunks) {
if *verbose {
o.log(dir, file.Name(), "removed", humanize.Bytes(uint64(file.Size())))
}
if err := o.fs().Remove(path); err != nil {
return err
}
continue
}
newWorldSize += uint64(file.Size())
}
if worldSize != newWorldSize {
o.log(
dir, "regions optimized",
humanize.Bytes(worldSize), "=>", humanize.Bytes(newWorldSize),
fmt.Sprintf("(%v%%)", math.Round(-(100-100*float64(newWorldSize)/float64(worldSize)))),
)
}
if o.ComputeLowMaps {
if err = o.saveLowMap(dir, lowmaps); err != nil {
return err
}
}
return nil
}
func (o *WorldOptimizer) saveLowMap(dir string, lowmap map[ChunkPos][]byte) error {
sorted := make([]ChunkPos, 0, len(lowmap))
for pos := range lowmap {
sorted = append(sorted, pos)
}
sort.Slice(sorted, func(i, j int) bool {
if sorted[i].Z == sorted[j].Z {
return sorted[i].X < sorted[j].X
}
return sorted[i].Z < sorted[j].Z
})
buf := make([]byte, 0, 4+len(lowmap)*(8+len(lowmap[sorted[0]])))
buf = binary.BigEndian.AppendUint32(buf, uint32(len(lowmap)))
for _, pos := range sorted {
buf = binary.BigEndian.AppendUint32(buf, uint32(pos.X))
buf = binary.BigEndian.AppendUint32(buf, uint32(pos.Z))
}
for _, pos := range sorted {
buf = append(buf, lowmap[pos]...)
}
dest := filepath.Join(dir, "lowmap.bin")
if ok, err := afero.Exists(o.fs(), dest); ok || err != nil {
if err != nil {
return err
}
file, err := afero.ReadFile(o.fs(), dest)
if err != nil {
return err
}
// no need to overwrite
if bytes.Equal(file, buf) {
return nil
}
}
return afero.WriteFile(o.fs(), dest, buf, 0644)
}
func (o *WorldOptimizer) removeDirIfExists(dir string) error {
if exists, err := afero.DirExists(o.fs(), dir); err != nil {
return err
} else if exists {
o.log(dir, "dir removed")
return o.fs().RemoveAll(dir)
}
return nil
}
func (o *WorldOptimizer) removeFileIfExists(file string) error {
if exists, err := afero.Exists(o.fs(), file); err != nil {
return err
} else if exists {
o.log(file, "removed")
return o.fs().Remove(file)
}
return nil
}
func (o *WorldOptimizer) fs() afero.Fs {
return o.Source.Fs()
}
func (o *WorldOptimizer) log(args ...string) {
log.Println(o.Source.Name(), args)
}
type ChunkPos struct {
X int
Z int
}