-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
195 lines (164 loc) · 4.72 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
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
package main
import (
"flag"
"github.com/Tnze/go-mc/save/region"
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
func main() {
dry := flag.Bool("dryRun", false, "Trial run that doesn't make any changes")
verbose := flag.Bool("v", false, "This option increases the amount of information you are given during the process")
mode := flag.String("mode", "perChunk", "The mode by which the inhabited time will be compared to options: \"perChunk\"/\"regionSum\"")
regionDir := flag.String("path", "", "The path of the directory with the .mca files that should be cleaned")
newPath := flag.String("newPath", "", "The path where to move the region files to")
minInhabitTime := flag.Int("minInhabitedTicks", 250, "The value that has to be passed so that a chunk will be seen as \"used\"")
flag.Parse()
println("Move Dir:", *newPath)
println("RegionDir:", *regionDir)
println("InhabitedMin:", *minInhabitTime)
println("Verbose:", *verbose)
println("Mode:", *mode)
println("Dry-Run:", *dry)
if len(*newPath) != 0 && !exists(*newPath) {
log.Fatal("The path to move the .mca files to doesn't exist")
return
}
if len(*regionDir) == 0 {
log.Fatal("No path to the region files was given, use -h to see flags and their usage")
return
} else {
absPath, err := filepath.Abs(*regionDir)
if err != nil {
log.Fatal(*regionDir, "is not a recognized path")
return
}
println(*regionDir)
regionDir = &absPath
println(absPath)
}
if !exists(*regionDir) {
log.Fatal(*regionDir, "doesn't exist")
return
}
if !strings.EqualFold(*mode, "perChunk") && !strings.EqualFold(*mode, "regionSum") {
log.Fatal(*mode, "is not a valid comparison mode, must be on of \"perChunk\" or \"regionSum\"")
return
}
err := process(*dry, *verbose, strings.EqualFold(*mode, "perChunk"), *regionDir, *newPath, int64(*minInhabitTime))
if err != nil {
log.Fatal(err)
return
}
}
func exists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
func process(dry bool, verbose bool, perChunkMode bool, path string, newPath string, minTime int64) (err error) {
moveRegions := len(newPath) != 0
regions, err := filepath.Glob(path + string(os.PathSeparator) + "r.*.*.mca")
if err != nil {
return err
}
if len(regions) == 0 {
log.Println("Couldn't find any region files in", path)
return nil
}
var regionIndex uint64 = 0
regionLoop:
for _, file := range regions {
regionIndex++
if verbose {
log.Println("Processing", file)
}
currentRegion, err := region.Open(file)
if err != nil {
if verbose {
log.Println("Couldn't open file at", file, "skipping..")
}
continue
}
var regionSum int64 = 0
var chunkMax int64 = 0
for x := 0; x < 32; x++ {
for z := 0; z < 32; z++ {
if !currentRegion.ExistSector(x, z) {
continue
}
data, err := currentRegion.ReadSector(x, z)
if err != nil {
log.Println("Couldn't read sector at x:", x, "z:", z, "from", file, "- skipping region as used..")
continue regionLoop
}
var chunk Chunk
err = chunk.Load(data)
if err != nil {
log.Println("Couldn't read chunk at x:", x, "z:", z, "from", file, "- skipping region as used..")
}
xPos := chunk.XPos + chunk.Level.XPos
zPos := chunk.ZPos + chunk.Level.ZPos
inhabitedTime := chunk.InhabitedTime + chunk.Level.InhabitedTime
if perChunkMode {
if inhabitedTime > minTime {
if verbose {
log.Println("Chunk at x:", xPos, "z:", zPos, "is", inhabitedTime, "ticks old, skipping region as used..")
}
continue regionLoop
} else if inhabitedTime > chunkMax {
chunkMax = inhabitedTime
}
} else {
regionSum += inhabitedTime
if regionSum > minTime {
if verbose {
log.Println(file, "has exceeded the minimum of inhabitant time, skipping region as used..")
}
continue regionLoop
}
}
}
}
var logStr string
logStr = "[" + strconv.FormatUint(regionIndex, 10) + "/" + strconv.Itoa(len(regions)) + "] "
if dry {
logStr = logStr + "Dry-Run "
} else if moveRegions {
logStr = logStr + "Moving "
} else {
logStr = logStr + "Deleting "
}
info, err := os.Stat(file)
if err != nil {
log.Fatal(err)
}
logStr = logStr + file
if perChunkMode {
log.Println(logStr, "- Max. ticks", chunkMax, "-", info.ModTime())
} else {
log.Println(logStr, "- Cum. ticks", regionSum, "-", info.ModTime())
}
if !dry {
if moveRegions {
err = os.Rename(file, filepath.Join(newPath, filepath.Base(file)))
if err != nil && verbose {
log.Println("Couldn't move", file)
}
} else {
err = os.Remove(file)
if err != nil && verbose {
log.Println("Couldn't delete", file)
}
}
}
}
return nil
}