-
Notifications
You must be signed in to change notification settings - Fork 0
/
zoomreduce.go
140 lines (124 loc) · 2.91 KB
/
zoomreduce.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"github.com/voxelbrain/goptions"
"io"
"log"
"os"
"path/filepath"
"strings"
)
const (
VERSION = "0.1.1"
CONFIG_FILE = "overviewerConfig.js"
)
var (
options = struct {
goptions.Help `goptions:"-h, --help"`
goptions.Verbs
List struct {
} `goptions:"list"`
Remove struct {
Num int `goptions:"-n, --num, description='Number of zoom levels to remove'"`
Worlds []string `goptions:"-w, --world, description='Include world folder in operation'"`
} `goptions:"remove"`
}{}
)
func init() {
goptions.ParseAndFail(&options)
}
func main() {
config, err := parseConfig()
if err != nil {
log.Fatalf("Could not parse config: %s", err)
}
switch options.Verbs {
case "list":
fmt.Println("Worlds:")
for _, worldpath := range config.Worlds() {
world := config.World(worldpath)
fmt.Printf("\t%s (Depth: %d)\n", worldpath, world.ZoomLevels())
}
case "remove":
for _, worldname := range options.Remove.Worlds {
world := config.World(worldname)
newzoom := world.ZoomLevels() - options.Remove.Num
filepath.Walk(world.Path(), func(path string, fi os.FileInfo, err error) error {
if fi.IsDir() {
return nil
}
pe := strings.Split(path, "/")
zoomLevel := len(pe)
if zoomLevel > newzoom {
log.Printf("Removing %s...", path)
err := os.Remove(path)
if err != nil {
log.Printf("Could not delete %s: %s", path, err)
}
}
return nil
})
world.SetZoomLevels(newzoom)
}
f, err := os.Create(CONFIG_FILE)
if err != nil {
log.Fatalf("Could not open config file %s for writing: %s", CONFIG_FILE, err)
}
defer f.Close()
io.WriteString(f, "var overviewerConfig = ")
enc := json.NewEncoder(f)
enc.Encode(config)
default:
goptions.PrintHelp()
return
}
}
type Config map[string]interface{}
func (c Config) Worlds() []string {
worlds := c["tilesets"].([]interface{})
r := make([]string, 0, len(worlds))
for _, world := range worlds {
r = append(r, world.(map[string]interface{})["path"].(string))
}
return r
}
type World map[string]interface{}
func (c Config) World(worldpath string) World {
for _, tileset := range c["tilesets"].([]interface{}) {
ts := tileset.(map[string]interface{})
if ts["path"].(string) == worldpath {
return ts
}
}
panic("Invalid world name")
}
func (w World) Path() string {
return w["path"].(string)
}
func (w World) ZoomLevels() int {
return int(w["maxZoom"].(float64))
}
func (w World) SetZoomLevels(zl int) {
w["maxZoom"] = float64(zl)
}
func parseConfig() (Config, error) {
f, err := os.Open(CONFIG_FILE)
if err != nil {
log.Fatalf("Could not open overviewerConfig.js: %s", err)
}
defer f.Close()
br := bufio.NewReader(f)
for r := rune(' '); r != rune('{'); {
var err error
r, _, err = br.ReadRune()
if err != nil {
return nil, err
}
}
br.UnreadRune()
c := make(Config)
dec := json.NewDecoder(br)
return c, dec.Decode(&c)
}