diff --git a/build/cows.tar.gz b/build/cows.tar.gz index 4031e201..78ac3c25 100644 Binary files a/build/cows.tar.gz and b/build/cows.tar.gz differ diff --git a/go.mod b/go.mod index ae44c1bc..2855c6e2 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/tmck-code/pokesay -go 1.20 +go 1.23 require ( github.com/fatih/color v1.18.0 diff --git a/src/bin/convert/png_convert.go b/src/bin/convert/png_convert.go index 14d0aec3..c22dee96 100644 --- a/src/bin/convert/png_convert.go +++ b/src/bin/convert/png_convert.go @@ -5,6 +5,8 @@ import ( "flag" "fmt" "os" + "path/filepath" + "strings" "github.com/tmck-code/pokesay/src/bin" "github.com/tmck-code/pokesay/src/pokedex" @@ -52,9 +54,44 @@ func main() { fmt.Println("Converting PNGs -> cowfiles") pbar := bin.NewProgressBar(len(fpaths)) + + allData := make([]string, 0, len(fpaths)) + nDuplicates, nFailures := 0, 0 + for _, f := range fpaths { - pokedex.ConvertPngToCow(args.FromDir, f, args.ToDir, args.Padding) + data, err := pokedex.ConvertPngToCow(args.FromDir, f, args.ToDir, args.Padding) + if err != nil { + nFailures++ + continue + } + + // check if this cawfile is a duplicate of one that has already been written + found := false + for _, existingData := range allData { + if existingData == data { + found = true + break + } + } + if found { + fmt.Println("Skipping duplicate data for", f) + nDuplicates++ + pbar.Add(1) + continue + } + allData = append(allData, data) + + destDirpath := filepath.Join( + args.ToDir, + // strip the root "source dirpath" from the source path + // e.g. fpath: /a/b/c.txt sourceDir: /a/ -> b/c.txt + filepath.Dir(strings.ReplaceAll(f, args.FromDir, "")), + ) + destFpath := filepath.Join(destDirpath, strings.ReplaceAll(filepath.Base(f), ".png", ".cow")) + + pokedex.WriteToCowfile(data, destDirpath, destFpath) pbar.Add(1) } - fmt.Println("Finished converting", len(fpaths), "pokesprite PNGs", "-> cowfiles") + fmt.Println("Finished converting", len(fpaths), "pokesprite PNGs -> cowfiles") + fmt.Println("(skipped", nDuplicates, "duplicates and", nFailures, "failures)") } diff --git a/src/pokedex/convert.go b/src/pokedex/convert.go index 41d2b798..6ce84729 100644 --- a/src/pokedex/convert.go +++ b/src/pokedex/convert.go @@ -107,15 +107,7 @@ func padLeft(cowfile []byte, n int) []string { return converted } -func ConvertPngToCow(sourceDirpath string, sourceFpath string, destDirpath string, extraPadding int) { - destDir := filepath.Join( - destDirpath, - // strip the root "source dirpath" from the source path - // e.g. fpath: /a/b/c.txt sourceDir: /a/ -> b/c.txt - filepath.Dir(strings.ReplaceAll(sourceFpath, sourceDirpath, "")), - ) - // Ensure that the destination dir exists - os.MkdirAll(destDir, 0755) +func ConvertPngToCow(sourceDirpath string, sourceFpath string, destDirpath string, extraPadding int) (string, error) { // Trim the whitespace from the edges of the images. This helps with the conversion autoCrop(sourceFpath) @@ -125,20 +117,24 @@ func ConvertPngToCow(sourceDirpath string, sourceFpath string, destDirpath strin if len(converted) == 0 { failures = append(failures, sourceFpath) - return + return "", fmt.Errorf("failed to convert %s", sourceFpath) } + final := stripEmptyLines(padLeft(converted, extraPadding)) + return strings.Join(final, "\n") + COLOUR_RESET, nil +} + +func WriteToCowfile(data string, destDirpath string, destFpath string) { + // Ensure that the destination dir exists + os.MkdirAll(destDirpath, 0755) - destFpath := filepath.Join(destDir, strings.ReplaceAll(filepath.Base(sourceFpath), ".png", ".cow")) ostream, err := os.Create(destFpath) Check(err) defer ostream.Close() writer := bufio.NewWriter(ostream) - final := stripEmptyLines(padLeft(converted, extraPadding)) - - // Join all of the lines back together, add colour reset sequence at the end - _, err = writer.WriteString(strings.Join(final, "\n") + COLOUR_RESET) + _, err = writer.WriteString(data) Check(err) - writer.Flush() + err = writer.Flush() + Check(err) }