-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump.go
More file actions
71 lines (61 loc) · 1.5 KB
/
Copy pathdump.go
File metadata and controls
71 lines (61 loc) · 1.5 KB
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
package binpack
import (
"fmt"
"os"
"path/filepath"
)
// DumpFragments writes every sprite to dir as its own PNG, named after the key
// it would get in the map. Renaming a fragment and joining the directory again
// is what turns a numbered cut into a meaningful name.
func DumpFragments(dir string, sprites []Sprite) error {
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
for _, s := range sprites {
path := filepath.Join(dir, s.Name+".png")
if err := SavePNG(path, s.Img); err != nil {
return fmt.Errorf("dump %s: %w", s.Name, err)
}
}
return nil
}
// CountPNGs reports how many PNGs sit directly in dir. A missing directory
// counts as zero.
func CountPNGs(dir string) (int, error) {
entries, err := os.ReadDir(dir)
if os.IsNotExist(err) {
return 0, nil
}
if err != nil {
return 0, err
}
n := 0
for _, e := range entries {
if !e.IsDir() && isPNG(e.Name()) {
n++
}
}
return n, nil
}
// CheckFragmentsDir rejects an output directory that holds any of the input
// files, which would scatter fragments among the sheets they were cut from and
// feed them back in on the next run.
func CheckFragmentsDir(dir string, files []string) error {
target, err := filepath.Abs(dir)
if err != nil {
return err
}
for _, f := range files {
abs, err := filepath.Abs(f)
if err != nil {
return err
}
if filepath.Dir(abs) == target {
return fmt.Errorf(
"-outFolder %s holds input %s: split into an empty directory instead",
dir, f,
)
}
}
return nil
}