-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasstes.go
346 lines (307 loc) · 7.54 KB
/
asstes.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package utils
import (
"fmt"
"image"
"io"
"math"
"os"
"path/filepath"
"strings"
"github.com/chai2010/webp"
"github.com/disintegration/imaging"
"howett.net/plist"
)
func CreateFaces(src string, to string, base string) error {
srcP := filepath.Join(src, "data/com.nintendo.zaba/files/assets/Common/Face")
if _, err := os.Stat(srcP); os.IsNotExist(err) {
return fmt.Errorf("源文件夹不存在: %s", srcP)
}
toP := filepath.Join(to, "update/faces")
baseP := ""
if base != "" {
baseP = filepath.Join(base, `faces`)
}
dirCreated := false
shouldCopy := false
entries, err := os.ReadDir(srcP)
if err != nil {
return err
}
for _, e := range entries {
if e.IsDir() {
dir := e.Name()
targetFile := filepath.Join(toP, fmt.Sprintf("%s.webp", dir))
if baseP == "" {
shouldCopy = true
} else {
baseFile := filepath.Join(baseP, fmt.Sprintf("%s.webp", dir))
if Exist(baseFile) {
shouldCopy = false
} else {
shouldCopy = true
}
}
if shouldCopy {
if !dirCreated {
err = os.MkdirAll(toP, 0750)
if err != nil {
return err
}
dirCreated = true
}
srcFile := filepath.Join(srcP, dir, "Face_FC.png")
if _, err := os.Stat(srcFile); os.IsNotExist(err) {
// ? 有的文件夹没有Face_FC.png文件,也许最好从加载好的结构体中直接读取?
// return fmt.Errorf("源文件不存在: %s", srcFile)
continue
}
r, err := os.Open(srcFile)
if err != nil {
return err
}
w, err := os.Create(targetFile)
if err != nil {
return err
}
_, err = io.Copy(w, r)
if err != nil {
return err
}
}
}
}
return nil
}
func CreateIcons(src string, to string, base string) error {
srcP := filepath.Join(src, "data/com.nintendo.zaba/files/assets/Common/UI")
if !Exist(srcP) {
return fmt.Errorf("源文件夹不存在: %s", srcP)
}
toP := filepath.Join(to, "update/icons")
baseP := ""
if base != "" {
baseP = filepath.Join(base, `icons`)
}
dirCreated := false
compare := image.Rect(19, 19, 57, 57)
writeConf := webp.Options{Lossless: false, Quality: 80.0}
for srcId := 1; srcId < 100; srcId++ {
srcFile := filepath.Join(srcP, fmt.Sprintf("Skill_Passive%d.png", srcId))
if !Exist(srcFile) {
break
}
m, err := DecodeWebp(srcFile)
if err != nil {
return err
}
for col := 0; col < 13; col++ {
for row := 0; row < 13; row++ {
iconId := (srcId-1)*169 + col*13 + row
croped := imaging.Crop(*m, image.Rect(row*76, col*76, (row+1)*76, (col+1)*76))
isNUll, err := IsNullPic(croped, compare)
if err != nil {
return err
}
if !isNUll {
// 如果不给基准路径或该路径文件不存在,直接写入
// 否则比较是否为相同图片后操作
baseFile := filepath.Join(baseP, fmt.Sprintf("%d.webp", iconId))
if baseP != "" && Exist(baseFile) {
basePic, err := DecodeWebp(baseFile)
if err != nil {
return err
}
isSame, err := IsSamePic(*basePic, croped, compare)
if err != nil {
return err
}
if !isSame {
if !dirCreated {
err = os.MkdirAll(toP, 0755)
if err != nil {
return err
}
dirCreated = true
}
toFile := filepath.Join(toP, fmt.Sprintf("%d.webp", iconId))
err = WriteWebp(toFile, croped, &writeConf)
if err != nil {
return err
}
}
} else {
if !dirCreated {
err = os.MkdirAll(toP, 0755)
if err != nil {
return err
}
dirCreated = true
}
toFile := filepath.Join(toP, fmt.Sprintf("%d.webp", iconId))
err = WriteWebp(toFile, croped, &writeConf)
if err != nil {
return err
}
}
}
}
}
}
return nil
}
func CreateStatus(src string, to string, base string) error {
srcP := filepath.Join(src, "data/com.nintendo.zaba/files/assets/Common/UI")
if !Exist(srcP) {
return fmt.Errorf("源文件夹不存在: %s", srcP)
}
toP := filepath.Join(to, "update")
baseP := ""
if base != "" {
baseP = base
}
writeConf := webp.Options{Lossless: false, Quality: 80.0}
f, err := os.Open(filepath.Join(srcP, "Status.plist"))
if err != nil {
return err
}
by, err := io.ReadAll(f)
if err != nil {
return err
}
pList := make(map[string]any)
_, err = plist.Unmarshal(by, &pList)
if err != nil {
return err
}
tasks := []string{"Move", "Weapon"}
for _, t := range tasks {
dirCreated := false
srcImg, err := DecodeWebp(filepath.Join(srcP, "Status.png"))
if err != nil {
return err
}
frames, ok := pList["frames"].(map[string]any)
if !ok {
return fmt.Errorf("plist 数据解析错误")
}
frame, ok := frames[fmt.Sprintf("Icon_%s.png", t)].(map[string]any)
if !ok {
return fmt.Errorf("plist 数据解析错误")
}
crop, err := CropAsset(frame, *srcImg)
if err != nil {
return err
}
s := crop.Bounds().Size()
width := s.X
height := s.Y
for i := 0; i < int(math.Round(float64(width)/float64(height))); i++ {
target := filepath.Join(toP, strings.ToLower(t), fmt.Sprintf("%d.webp", i))
baseFile := filepath.Join(baseP, strings.ToLower(t), fmt.Sprintf("%d.webp", i))
if baseP == "" || !Exist(baseFile) {
if !dirCreated {
err = os.MkdirAll(filepath.Dir(target), 0755)
if err != nil {
return err
}
dirCreated = true
}
out := imaging.Crop(crop, image.Rect(i*height, 0, i*height+height, height))
WriteWebp(target, out, &writeConf)
}
}
}
return nil
}
func CreateBlessing(src string, to string, base string) error {
srcP := filepath.Join(src, "data/com.nintendo.zaba/files/assets/Common/UI")
if !Exist(srcP) {
return fmt.Errorf("源文件夹不存在: %s", srcP)
}
toP := filepath.Join(to, "update/blessing")
baseP := ""
if base != "" {
baseP = filepath.Join(base, `blessing`)
}
dirCreated := false
writeConf := webp.Options{Lossless: false, Quality: 80.0}
tasks := []string{
"Icon_SeasonNone.png",
"Icon_BlessingFireS.png",
"Icon_BlessingWaterS.png",
"Icon_BlessingWindS.png",
"Icon_BlessingEarthS.png",
"Icon_BlessingLightS.png",
"Icon_BlessingDarkS.png",
"Icon_BlessingHeavenS.png",
"Icon_BlessingLogicS.png",
}
for i, t := range tasks {
f, err := os.Open(filepath.Join(srcP, "Blessing.plist"))
if err != nil {
return err
}
by, err := io.ReadAll(f)
if err != nil {
return err
}
pList := make(map[string]any)
_, err = plist.Unmarshal(by, &pList)
if err != nil {
return err
}
srcImg, err := DecodeWebp(filepath.Join(srcP, "Blessing.png"))
if err != nil {
return err
}
frames, ok := pList["frames"].(map[string]any)
if !ok {
return fmt.Errorf("plist 数据解析错误")
}
frame, ok := frames[t].(map[string]any)
if !ok {
return fmt.Errorf("plist 数据解析错误")
}
cropped, err := CropAsset(frame, *srcImg)
if err != nil {
return err
}
baseFile := filepath.Join(baseP, fmt.Sprintf("%d.webp", i))
if !Exist(baseFile) {
target := filepath.Join(toP, fmt.Sprintf("%d.webp", i))
if !Exist(target) {
if !dirCreated {
err = os.MkdirAll(toP, 0755)
if err != nil {
return err
}
dirCreated = true
}
err = WriteWebp(target, cropped, &writeConf)
if err != nil {
return err
}
}
}
}
return nil
}
func CreateAssets(src string, to string, base string) error {
err := CreateFaces(src, to, base)
if err != nil {
return err
}
err = CreateIcons(src, to, base)
if err != nil {
return err
}
err = CreateStatus(src, to, base)
if err != nil {
return err
}
err = CreateBlessing(src, to, base)
if err != nil {
return err
}
return nil
}