-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
178 lines (156 loc) · 3.83 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
//
// mtmapprune - Prune blocks outside a limit box in map.sqlite
//
// Copyright (C) 2017 - Auke Kok <sofar@foo-projects.org>
// Portions Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3" // MIT licensed.
"log"
"os"
"strconv"
)
// From: minetest/src/database-sqlite3.cpp
func unsigned_to_signed(i int64, max_positive int64) int64 {
if i < max_positive {
return i
}
return (i - (max_positive * 2))
}
func parse_arg(n int64) int64 {
v, err := strconv.ParseInt(os.Args[n], 0, 64)
if err != nil {
log.Fatal("Error parsing argument: ", n, ": ", err)
}
return v
}
type Limit struct {
min int64
max int64
}
func main() {
var ci, co int
if len(os.Args) < 3 {
log.Fatal("Not enough arguments: sqlite_file max_x [max_y [max_z [min_x min_y min_z]]]")
}
if len(os.Args) == 6 || len(os.Args) == 7 {
log.Fatal("Argument count must be 4 or 7, if you specify one min limit, you must specify them all")
}
f := os.Args[1]
var x Limit
var y Limit
var z Limit
x.max = parse_arg(2)
if len(os.Args) > 3 {
y.max = parse_arg(3)
if len(os.Args) > 4 {
z.max = parse_arg(4)
} else {
z.max = x.max
}
} else {
y.max = x.max
z.max = x.max
}
if len(os.Args) == 8 {
x.min = parse_arg(5)
y.min = parse_arg(6)
z.min = parse_arg(7)
} else {
if (x.max < 0) || (y.max < 0) || (z.max < 0) {
log.Fatal("Limits should be positive when passing max_x max_y max_z values")
}
x.min = -x.max
y.min = -y.max
z.min = -z.max
}
// ensure proper ordering of min, max
if x.min > x.max {
a := x.min
x.min = x.max
x.max = a
}
if y.min > y.max {
a := y.min
y.min = y.max
y.max = a
}
if z.min > z.max {
a := z.min
z.min = z.max
z.max = a
}
db, err := sql.Open("sqlite3", f)
if err != nil {
log.Fatal(err)
}
log.Println("Collecting blocks")
rows, err := db.Query("select pos from blocks")
if err != nil {
log.Fatal(err)
}
var arr []int64
for rows.Next() {
var pos int64
err = rows.Scan(&pos)
if err != nil {
log.Fatal(err)
}
arr = append(arr, int64(pos))
ci++
}
rows.Close()
log.Println("Deleting blocks")
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
stmt, err := tx.Prepare("delete from blocks where pos = ?")
if err != nil {
log.Fatal(err)
}
for _, pos := range arr {
var opos = pos
// From: minetest/src/database-sqlite3.cpp
var xi = unsigned_to_signed(pos&0xfff, 2048)
pos = (pos - xi) / 4096
var yi = unsigned_to_signed(pos&0xfff, 2048)
pos = (pos - yi) / 4096
var zi = unsigned_to_signed(pos&0xfff, 2048)
if (xi*16 > x.max) || (xi*16+15 < x.min) ||
(yi*16 > y.max) || (yi*16+15 < y.min) ||
(zi*16 > z.max) || (zi*16+15 < z.min) {
_, err = stmt.Exec(fmt.Sprintf("%v", opos))
if err != nil {
log.Fatal(err)
}
co++
}
}
tx.Commit()
log.Println("Vaccuuming database")
_, err = db.Exec("VACUUM")
if err != nil {
log.Fatal(err)
}
log.Printf("Removed %v of %v blocks (limits: [%v, %v, %v]-[%v, %v, %v])\n",
co, ci, x.min, y.min, z.min, x.max, y.max, z.max)
defer db.Close()
}