mlp-server-tools/tools/genpics/main.go

132 lines
3.2 KiB
Go
Raw Normal View History

2019-05-21 22:34:21 +00:00
package main // import "git.fromouter.space/mcg/mlp-server-tools/tools/genpics"
2019-05-15 13:45:28 +00:00
import (
"archive/zip"
"encoding/json"
"flag"
"fmt"
"image"
"io"
"os"
"path/filepath"
"strings"
"image/jpeg"
_ "image/jpeg"
_ "image/png"
)
type cardMap map[string]string
func main() {
packfolder := flag.String("packdir", "octgn-data", "Path to folder containing .o8c files")
cardmap := flag.String("mapfile", filepath.Join("octgn-data", "cardmap.json"), "Path to card id map")
outfolder := flag.String("out", filepath.Join("images", "cards"), "Path where to put the extracted/renamed images")
flag.Parse()
// Read and parse card map
mapfile, err := os.Open(*cardmap)
checkErr(err, "Could not read card map file")
var cmap cardMap
checkErr(json.NewDecoder(mapfile).Decode(&cmap), "Could not decode card map file")
mapfile.Close()
// Create dir tree
checkErr(os.MkdirAll(filepath.Join(*outfolder, "unknown"), 0755), "Could not create output folder(s)")
checkErr(filepath.Walk(*packfolder, func(path string, info os.FileInfo, err error) error {
// Skip all non-OCTGN image packs
if strings.ToLower(filepath.Ext(path)) != ".o8c" {
return nil
}
// Open as ZIP
packfile, err := zip.OpenReader(path)
if err != nil {
return err
}
defer packfile.Close()
fmt.Printf("Processing %s:\n", path)
for _, file := range packfile.File {
// Skip folders
if file.FileInfo().IsDir() {
continue
}
// Get filename and conver it, if possible
base := filepath.Base(file.Name)
pieces := strings.Split(base, ".")
translated, ok := cmap[pieces[0]]
if ok {
// Convert boosted card format
if pieces[1] == "Mane Character Boosted" {
translated += "b"
pieces[1] = pieces[2]
}
fmt.Printf(" Found %s -> %s\n", base, translated)
// If file is jpg/jpeg, copy as it is
ext := strings.ToLower(filepath.Ext(base))
if ext == ".jpg" || ext == ".jpeg" {
err := copyFile(file, filepath.Join(*outfolder, translated+".jpg"))
if err != nil {
return err
}
} else {
// Non-JPG, needs to be converted
infile, err := file.Open()
if err != nil {
return err
}
defer infile.Close()
img, _, err := image.Decode(infile)
if err != nil {
return err
}
outfile, err := os.Create(filepath.Join(*outfolder, translated+".jpg"))
if err != nil {
return err
}
defer outfile.Close()
err = jpeg.Encode(outfile, img, &jpeg.Options{Quality: 90})
if err != nil {
return err
}
}
} else {
fmt.Printf(" Unknown %s\n", base)
err := copyFile(file, filepath.Join(*outfolder, "unknown", base))
if err != nil {
return err
}
}
}
return nil
}), "Error while looking up for archives")
}
func copyFile(in *zip.File, outpath string) error {
infile, err := in.Open()
if err != nil {
return err
}
defer infile.Close()
outfile, err := os.Create(outpath)
if err != nil {
return err
}
defer outfile.Close()
_, err = io.Copy(outfile, infile)
return err
}
func checkErr(err error, errfmt string, args ...interface{}) {
if err != nil {
fmt.Fprintf(os.Stderr, "[FATAL ERROR] "+errfmt+":\n "+err.Error(), args...)
os.Exit(1)
}
}