-
Notifications
You must be signed in to change notification settings - Fork 0
/
stitcher.go
271 lines (236 loc) · 6.8 KB
/
stitcher.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
package MinimapStitcher
import (
"fmt"
"runtime"
"os"
"log"
"sync"
"io/ioutil"
"strings"
"strconv"
"image/draw"
"image"
"image/png"
"encoding/json"
)
const (
TILE_SIZE = 512
)
type reportCallbackMessage map[string]string
type reportCallback func(reportCallbackMessage)
type reportCallbackWrapper func(string, reportCallbackMessage)
func Stitch(sourceDirectory string, destinationDirectory string) {
var callback = func(message reportCallbackMessage) {
json, _ := json.Marshal(message)
line := append(json, "\r\n"...)
os.Stdout.Write(line)
}
var wg sync.WaitGroup;
tasks := make(chan [5]string);
setupWaitGroup(wg, tasks, callback);
listMapsFound(callback, sourceDirectory);
addMapsToWaitGroup(tasks, sourceDirectory, destinationDirectory);
wg.Wait()
}
func listMapsFound(callback reportCallback, sourceDirectory string) {
files, _ := ioutil.ReadDir(sourceDirectory)
for _, f := range files {
fd, err := os.Open(sourceDirectory + f.Name())
if err != nil {
fmt.Println(err)
return
}
fi, err := fd.Stat()
if err != nil {
fmt.Println(err)
return
}
mode := fi.Mode()
if mode.IsDir() {
if f.Name() != "WMO" {
message := make(map [string]string)
message["minimap"] = f.Name()
message["type"] = "found"
callback(message)
}
}
defer fd.Close()
}
}
func setupWaitGroup(wg sync.WaitGroup, tasks chan [5]string, callback reportCallback, ) {
for i := 0; i < runtime.NumCPU(); i++ {
wg.Add(1)
go func() {
for arguments := range tasks {
message := make(map [string]string)
message["minimap"] = arguments[4]
message["tile"] = "0"
message["tiles"] = "0"
var callbackWrapper = func(messageText string, extras reportCallbackMessage) {
message["type"] = messageText
if _, ok := extras["tile"]; ok {
message["tile"] = extras["tile"]
}
if _, ok := extras["tiles"]; ok {
message["tiles"] = extras["tiles"]
}
callback(message)
}
callbackWrapper("start_compile", make(map [string]string))
compileMinimap(callbackWrapper, tasks, arguments[0], arguments[1], arguments[2], arguments[3])
callbackWrapper("complete_compile", make(map [string]string))
}
wg.Done()
return
}()
}
}
func addMapsToWaitGroup(tasks chan [5]string, sourceDirectory string, destinationDirectory string) {
files, _ := ioutil.ReadDir(sourceDirectory)
for _, f := range files {
fd, err := os.Open(sourceDirectory + f.Name())
if err != nil {
fmt.Println(err)
return
}
fi, err := fd.Stat()
if err != nil {
fmt.Println(err)
return
}
mode := fi.Mode()
if mode.IsDir() {
if f.Name() != "WMO" {
var array [5]string
array[0] = destinationDirectory + f.Name()
array[1] = sourceDirectory + f.Name()
array[2] = f.Name()
array[3] = "false"
array[4] = f.Name()
tasks <- array
}
}
defer fd.Close()
}
}
func compileMinimap(callback reportCallbackWrapper, tasks chan [5]string, resultFileName string, sourceDirectory string, minimapName string, noLiquidString string) {
var falseString = "false"
var foundNoLiquid = false
var tiles = make(map[string]string);
files, _ := ioutil.ReadDir(sourceDirectory)
for _, f := range files {
var fullFileName = sourceDirectory + "/" + f.Name()
if strings.Contains(f.Name(), ".png") {
var fName = strings.TrimRight(f.Name(), ".png")
var fNameNoLiquid = strings.Contains(fName, "noLiquid")
if fNameNoLiquid && noLiquidString == falseString {
foundNoLiquid = true
} else if !fNameNoLiquid && noLiquidString == falseString {
tiles[strings.TrimLeft(fName, "map")] = fullFileName
} else if fNameNoLiquid && noLiquidString != falseString {
tiles[strings.TrimLeft(fName, "noLiquid_map")] = fullFileName
} else if !fNameNoLiquid && noLiquidString != falseString {
var trimmerFName = strings.TrimLeft(fName, "map")
if _, ok := tiles[trimmerFName]; !ok {
tiles[trimmerFName] = fullFileName
}
}
}
}
if foundNoLiquid && noLiquidString == falseString {
var array [5]string
array[0] = resultFileName + "NoLiquid"
array[1] = sourceDirectory
array[2] = minimapName
array[3] = "true"
array[4] = minimapName + "NoLiquid"
go func() { tasks <- array }()
}
callback("start_build", make(map [string]string))
buildMinimap(callback, resultFileName, tiles)
callback("start_build", make(map [string]string))
}
func buildMinimap(callback reportCallbackWrapper, resultFileName string, tiles map[string]string) {
callback("calculate_minimap_size", make(map [string]string))
var hc, lc, hr, lr = calculateMinimapSize(tiles)
callback("calculate_minimap_tileplacement", make(map [string]string))
var files, width, height = calculateMinimapTilePlacement(tiles, hc, lc, hr, lr)
createMinimapImage(callback, resultFileName, width, height, files)
}
func calculateMinimapSize(tiles map[string]string) (hc, lc, hr, lr int) {
hc = 1000
lc = 0
hr = 1000
lr = 0
for tile, _ := range tiles {
var tileParts = strings.Split(tile, "_")
var col, _ = strconv.Atoi(tileParts[0])
var row, _ = strconv.Atoi(tileParts[1])
if hc > col {
hc = col
}
if lc < col {
lc = col
}
if hr > row {
hr = row
}
if lr < row {
lr = row
}
}
return
}
func calculateMinimapTilePlacement(tiles map[string]string, hc int, lc int, hr int, lr int) (map[string]string, int, int) {
var files = make(map[string]string)
var width = 0;
var height = 0;
for i := hc; i < lc; i++ {
width += TILE_SIZE
for j := hr; j < lr; j++ {
if i == hc {
height += TILE_SIZE
}
var si = strconv.Itoa(i)
var sj = strconv.Itoa(j)
var oi = strconv.Itoa((i - hc) * TILE_SIZE)
var oj = strconv.Itoa((j - hr) * TILE_SIZE)
if _, ok := tiles[si + "_" + sj]; ok {
files[oi + "_" + oj] = tiles[si + "_" + sj]
}
}
}
return files, width, height
}
func createMinimapImage(callback reportCallbackWrapper, resultFileName string, width int, height int, files map[string]string) {
extras := make(map [string]string)
extras["tiles"] = strconv.Itoa(len(files))
callback("start_stitch", extras)
m := image.NewRGBA(image.Rect(0, 0, width, height))
for coords, fileName := range files {
extras := make(map [string]string)
extras["tile"] = coords
callback("stitch_tile", extras)
var coordParts = strings.Split(coords, "_")
var x, _ = strconv.Atoi(coordParts[0])
var y, _ = strconv.Atoi(coordParts[1])
applyTileToImage(m, fileName, x, y)
}
toimg, _ := os.Create(resultFileName + ".png")
png.Encode(toimg, m)
defer toimg.Close()
callback("finish_stitch", make(map [string]string))
}
func applyTileToImage(m *image.RGBA, fileName string, x int, y int) {
tile, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
defer tile.Close()
tileImage, err := png.Decode(tile)
if err != nil {
fmt.Printf("%v", fileName)
log.Fatal(err)
}
draw.Draw(m, image.Rect(x, y, x + TILE_SIZE, y + TILE_SIZE), tileImage, image.Point{0,0}, draw.Src)
}