-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
206 lines (191 loc) · 4.76 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
196
197
198
199
200
201
202
203
204
205
206
/*
* MIT License
*
* Copyright (c) 2024 The autotiler authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package main
import (
"fmt"
"image"
"image/png"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/krylphi/autotiler/internal/unpack"
)
const (
inKey = "in"
paddingKey = "p"
exportKey = "e"
outKey = "o"
)
const (
export16 = "16"
export28 = "28"
export48 = "48"
exportAll = "all"
)
func main() {
args := parseArgs()
inFiles, ok := args[inKey]
if !ok {
log.Print("Missing input file")
os.Exit(1)
}
outFiles, outs := args[outKey]
for i, inFile := range inFiles {
inputFile := inFile
outputFile := fmt.Sprintf("%d.local.png", i)
if outs {
if len(outFiles) > i {
outputFile = outFiles[i]
}
}
err := export(args, inputFile, outputFile)
if err != nil {
log.Print(err)
os.Exit(1)
}
}
}
func export(args map[string][]string, inputFile, outputFile string) error { // todo wrap errors
imgFile, err := os.Open(inputFile)
if err != nil {
return err
}
defer imgFile.Close()
img, _, err := image.Decode(imgFile)
if err != nil {
return err
}
padding := 0
if paddings, ok := args[paddingKey]; ok {
padding, err = strconv.Atoi(paddings[0])
if err != nil {
return err
}
}
unpacker := unpack.NewUnpacker(img, 2, 3, padding)
if err := unpacker.Init(2); err != nil {
return err
}
exports, ok := args[exportKey]
var exportTypes []string
if !ok || len(exports) == 0 || exports[0] == exportAll {
exportTypes = []string{export16, export28, export48}
} else {
exportTypes = exports
}
for e := range exportTypes {
exportType := exportTypes[e]
switch exportType {
case export16:
err := produceTileset(unpacker.From6to16Terrain1, outputFile, "16x1_terrain1")
if err != nil {
return err
}
err = produceTileset(unpacker.From6to16Terrain2, outputFile, "16x1_terrain2")
if err != nil {
return err
}
case export28:
err := produceTileset(unpacker.From6to28, outputFile, "14x2")
if err != nil {
return err
}
case export48:
err := produceTileset(unpacker.From6to48Terrain1, outputFile, "12x4_terrain1")
if err != nil {
return err
}
err = produceTileset(unpacker.From6to48Terrain2, outputFile, "12x4_terrain2")
if err != nil {
return err
}
}
}
return nil
}
func parseArgs() map[string][]string {
if len(os.Args) < 2 {
log.Print(
"Usage: autotiler -in <file_in> [-o <file_out>] [-p <padding>] [-e <export_type(16,28,48,all)>]\n" +
" -e can be repeated\n")
os.Exit(1)
}
res := make(map[string][]string)
allTilesets := false
for i := 1; i < len(os.Args); i += 2 {
key := strings.TrimPrefix(os.Args[i], "-")
v, ok := res[key]
value := os.Args[i+1]
if key == exportKey && allTilesets {
continue
}
if key == exportKey && strings.EqualFold(value, exportAll) {
allTilesets = true
v = []string{"all"}
res[key] = v
continue
}
if ok {
v = append(v, value)
} else {
v = []string{value}
}
res[key] = v
}
return res
}
func produceTileset(unpackFunction func() (*image.NRGBA, error), outputPath, exportType string) error {
canvas, err := unpackFunction()
if err != nil {
return err
}
cleanPath := filepath.Clean(outputPath)
outputFile := fmt.Sprintf("%s_%s", exportType, filepath.Base(cleanPath))
outputFile = filepath.Join(filepath.Dir(cleanPath), outputFile)
file, err := os.Create(outputFile)
if err != nil {
return err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Println(err)
}
}(file)
err = png.Encode(file, canvas)
if err != nil {
return err
}
return nil
}
//nolint:unused //debug function
func printArgs(args map[string][]string) {
for key, params := range args {
for _, param := range params {
log.Printf("%s: %s\n", key, param)
}
}
}