-
Notifications
You must be signed in to change notification settings - Fork 43
/
main.go
254 lines (204 loc) · 4.68 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
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
package main
import (
"fmt"
"image"
"image/png"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/alexflint/go-arg"
"github.com/disintegration/imaging"
)
func ifelse(c bool, a, b int) int {
if c {
return a
} else {
return b
}
}
type Frame struct {
Rect image.Rectangle
Offset image.Point
OriginalSize image.Point
Rotated int
}
func LoadImage(path string) (img image.Image, err error) {
return imaging.Open(path)
}
func SaveImage(path string, img image.Image) (err error) {
if filepath.Ext(path) == "" {
path = path + ".png"
}
os.MkdirAll(filepath.Dir(path), os.ModePerm)
imgfile, err := os.Create(path)
defer imgfile.Close()
return png.Encode(imgfile, img)
}
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
func IsFile(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return !s.IsDir()
}
func GetFiles(dir string, allow []string) []string {
allowMap := map[string]bool{}
if allow != nil {
for _, v := range allow {
allowMap[v] = true
}
}
ret := []string{}
filepath.Walk(dir, func(fpath string, f os.FileInfo, err error) error {
if f == nil || f.IsDir() {
return nil
}
ext := path.Ext(fpath)
if allowMap[ext] {
ret = append(ret, filepath.ToSlash(fpath))
}
return nil
})
return ret
}
type AtlasPart struct {
ImageFile string
Frames map[string]*Frame
}
type DumpContext struct {
FileName string
FileContent []byte
Atlases []*AtlasPart
}
func (dc *DumpContext) AppendPart() *AtlasPart {
part := &AtlasPart{}
part.Frames = map[string]*Frame{}
dc.Atlases = append(dc.Atlases, part)
return part
}
func (dc *DumpContext) dumpFrames(part *AtlasPart) error {
textureFileName := filepath.Join(filepath.Dir(dc.FileName), part.ImageFile)
textureImage, err := LoadImage(textureFileName)
if err != nil {
return fmt.Errorf("open image error:" + textureFileName)
}
outdir := filepath.Join(dc.FileName + ".out")
if !IsDir(outdir) {
err = os.MkdirAll(outdir, os.ModePerm)
if err != nil {
return err
}
}
for k, v := range part.Frames {
// fmt.Println(k)
var subImage image.Image
w, h := v.Rect.Size().X, v.Rect.Size().Y
ox, oy := v.Offset.X, v.Offset.Y
ow, oh := v.OriginalSize.X, v.OriginalSize.Y
x, y := v.Rect.Min.X, v.Rect.Min.Y
if v.Rotated == 90 {
subImage = imaging.Crop(textureImage, image.Rect(x, y, x+h, y+w))
subImage = imaging.Rotate90(subImage)
} else if v.Rotated == 270 {
subImage = imaging.Crop(textureImage, image.Rect(x, y, x+h, y+w))
subImage = imaging.Rotate270(subImage)
} else {
subImage = imaging.Crop(textureImage, image.Rect(x, y, x+w, y+h))
}
destImage := image.NewRGBA(image.Rect(0, 0, ow, oh))
newImage := imaging.Paste(destImage, subImage, image.Point{(ow-w)/2 + ox, (oh-h)/2 - oy})
savepath := path.Join(outdir, k)
if path.Ext(savepath) == "" {
savepath += ".png"
}
SaveImage(savepath, newImage)
}
return nil
}
func dumpByFileName(filename string) error {
c := DumpContext{
FileName: filename,
Atlases: []*AtlasPart{},
}
data, _ := ioutil.ReadFile(c.FileName)
c.FileContent = data
var err error
ext := path.Ext(filename)
switch ext {
case ".plist":
err = dumpPlist(&c)
case ".json":
err = dumpJson(&c)
case ".fnt":
err = dumpFnt(&c)
case ".atlas":
err = dumpSpine(&c)
default:
err = ErrNotSupportFileType
}
if err != nil {
return err
}
for _, part := range c.Atlases {
err = c.dumpFrames(part)
if err != nil {
return err
}
}
return nil
}
func main() {
var args struct {
Input string `arg:"positional"`
Ext string `arg:"-e,--ext" help:"dump ext json,plist,fnt,atlas "`
}
arg.MustParse(&args)
var ext []string
if args.Ext == "" {
ext = []string{".json", ".plist", ".fnt", ".atlas"}
} else {
ext = []string{}
arr := strings.Split(args.Ext, ",")
for _, v := range arr {
ext = append(ext, "."+v)
}
}
if args.Input == "" {
args.Input = "./"
}
// fmt.Println(ext)
allfiles := []string{}
if IsDir(args.Input) {
files := GetFiles(args.Input, ext)
allfiles = append(allfiles, files...)
} else {
allfiles = append(allfiles, args.Input)
}
fmt.Println(fmt.Sprintf("开始导出:共(%d)个", len(allfiles)))
for i, v := range allfiles {
err := dumpByFileName(v)
p := fmt.Sprintf("%d/%d", i+1, len(allfiles))
if err != nil {
if err == ErrNotSupportFileType {
} else if err == ErrNotSupportJsonType {
fmt.Println("跳过", p, v)
} else {
fmt.Println("错误", p, v, err)
}
} else {
fmt.Println("导出", p, v)
}
}
fmt.Printf("\n")
fmt.Printf("好用请给个Star,谢谢.\n")
fmt.Printf("https://github.com/qcdong2016/PlistDumper.git\n")
}