-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (137 loc) · 3.33 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/spf13/afero"
)
var overwrite = flag.Bool("o", false, "Overwrite original world")
var suffix = flag.String("s", "_opt", "Suffix for optimized worlds")
var dryRun = flag.Bool("dry", false, "Dry run (no changes on disk)")
var verbose = flag.Bool("v", false, "Verbose logging")
var recursive = flag.Bool("r", false, "Recursive search for worlds")
var heightMap = flag.Bool("hm", false, "Recalculate height maps")
var lowMap = flag.Bool("lm", false, "Compute low maps")
var foundAny = false
func main() {
flag.Usage = func() {
w := flag.CommandLine.Output()
base := filepath.Base(os.Args[0])
fmt.Fprintln(w, "Usage:")
fmt.Fprintln(w, " ", base, "[options] path")
fmt.Fprintln(w, "Examples:")
fmt.Fprintln(w, " ", base, "-r -dry .minecraft/saves")
fmt.Fprintln(w, " ", base, "-r -o .")
fmt.Fprintln(w, " ", base, "-o world.zip")
fmt.Fprintln(w, "Options:")
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() == 0 {
flag.Usage()
return
}
path := strings.Join(flag.Args(), " ")
if strings.HasSuffix(path, ".zip") {
process(NewZipSource(path), true)
return
}
if *recursive {
// Find plain directories
abspath, err := filepath.Abs(path)
if err != nil {
log.Fatalln(err)
}
fs := afero.NewBasePathFs(afero.NewOsFs(), abspath)
plainDirs, err := findWorldDirs(fs)
if err != nil {
log.Fatalln(err)
}
dirsDone := make(map[string]bool)
for _, dir := range plainDirs {
fullPath := filepath.Join(path, dir)
if dirsDone[fullPath] {
continue
}
if strings.HasSuffix(dir, *suffix) {
log.Println("Skip", dir, "as optimized")
continue
}
dirsDone[fullPath] = true
process(NewDirSource(fullPath), false)
}
// Find zip files
zipFiles, err := findZipFiles(fs)
if err != nil {
log.Fatalln(err)
}
for _, file := range zipFiles {
if strings.HasSuffix(file, *suffix+".zip") {
log.Println("Skip", file, "as optimized")
continue
}
process(NewZipSource(filepath.Join(path, file)), true)
}
} else {
process(NewDirSource(path), false)
}
if !foundAny {
log.Println("No worlds found in", path)
}
}
func process(source Source, recursive bool) {
optimizer := &WorldOptimizer{
Source: source,
ComputeHeightMaps: *heightMap,
ComputeLowMaps: *lowMap,
}
if err := optimizer.Process(recursive); err != nil {
log.Fatalln(err)
}
if optimizer.AnyWorldFound {
foundAny = true
}
if !*dryRun {
if err := source.Save(); err != nil {
log.Fatalln(err)
}
}
if err := source.Close(); err != nil {
log.Fatalln(err)
}
}
func findZipFiles(fs afero.Fs) ([]string, error) {
var files []string
err := afero.Walk(fs, "", func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() && f.Name() == ".git" {
return filepath.SkipDir
}
if strings.HasSuffix(path, ".zip") {
files = append(files, path)
}
return nil
})
return files, err
}
func findWorldDirs(fs afero.Fs) ([]string, error) {
var files []string
err := afero.Walk(fs, "", func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() && f.Name() == ".git" {
return filepath.SkipDir
}
if filepath.Base(path) == "region" && f.IsDir() {
files = append(files, filepath.Dir(path))
}
return nil
})
return files, err
}