I'm so sorry

lol
This commit is contained in:
Raymonf 2023-01-05 01:41:21 -05:00
parent a16ff13970
commit 50f50ac0ab
No known key found for this signature in database
GPG Key ID: 438459BF619B037A
4 changed files with 196 additions and 0 deletions

173
_cmd/bulk-bin/main.go Normal file
View File

@ -0,0 +1,173 @@
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"git.tuxpa.in/a/nori/renderer"
"gitlab.com/gfxlabs/gfximg/apng"
"golang.org/x/sync/errgroup"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"git.tuxpa.in/a/nori"
"git.tuxpa.in/a/nori/utils"
"git.tuxpa.in/a/zlog/log"
)
var (
dumpPtr = flag.Bool("d", false, "dump nri file to zip")
silentPtr = flag.Bool("s", false, "silent")
bulkDumpPtr = flag.Bool("b", false, "bulk dump nris from a folder")
jsonDumpPtr = flag.Bool("j", false, "bulk dump nri animation names from a folder")
directoryPtr = flag.String("dir", "nri", "directory to bulk dump from")
destinationPtr = flag.String("dest", "", "directory to bulk dump to")
filePtr = flag.String("f", "example.nri", "file to load")
outputPtr = flag.String("o", "output.zip", "output zip file")
)
func main() {
flag.Parse()
if *dumpPtr {
filename := *filePtr
output := *outputPtr
if _, err := os.Stat(filename); err != nil {
printIf("could not find file %s\n", filename)
return
}
dump(filename, output)
return
}
dir := *directoryPtr
dest := *destinationPtr
if dest == "" {
dest = dir
}
printIf("output: %s\n", dest)
if *bulkDumpPtr || *jsonDumpPtr {
files, err := ioutil.ReadDir(dir)
if err != nil {
printIf("%s: %s\n", dir, err)
return
}
egg := errgroup.Group{}
egg.SetLimit(12)
for _, ff := range files {
f := ff
if strings.HasSuffix(f.Name(), ".nri") {
egg.Go(func() error {
if *bulkDumpPtr {
dump_nozip(dir+"/"+f.Name(), dest)
}
if *jsonDumpPtr {
dump_anim_json(dir+"/"+f.Name(), dest)
}
return nil
})
}
}
if err := egg.Wait(); err != nil {
log.Panicf("oh fuck: %s\n", err)
return
}
}
flag.PrintDefaults()
}
func printIf(s string, args ...interface{}) {
if !*silentPtr {
fmt.Printf(s, args...)
}
}
func dump_anim_json(filename string, destination string) {
start := time.Now()
printIf("reading file '%s'\n", filename)
n, err := nori.FromFile(filename)
if err != nil {
log.Panicln("decode: %s", err)
}
names, err := nori.GetAnimationNames(n)
if err != nil {
log.Panicln("animation: %s", err)
}
json, err := json.Marshal(names)
if err != nil {
log.Panicln("could not marshal json: %s\n", err)
}
baseName := filepath.Base(strings.TrimSuffix(filename, filepath.Ext(filename)))
output := fmt.Sprintf("%s/%s.json", destination, baseName)
f, fErr := os.OpenFile(output, os.O_CREATE, 0740)
if fErr != nil {
panic(fErr)
}
defer f.Close()
if _, err := f.Write(json); err != nil {
return
}
printIf("done in %v \n", time.Now().Sub(start))
}
func dump_nozip(filename string, destination string) {
start := time.Now()
printIf("reading file '%s'\n", filename)
n, err := nori.FromFile(filename)
if err != nil {
log.Panicln("decode: %s", err)
}
// printIf("rendering %d animation(s)\n", n.AnimationCount)
animations, err := renderer.RenderAnimationsApng(n)
if err != nil {
log.Panicln("animation: %s", err)
}
baseName := filepath.Base(strings.TrimSuffix(filename, filepath.Ext(filename)))
for i, v := range animations {
output := fmt.Sprintf(destination+"/%s_%d.png", baseName, i)
// printIf("saving to %s \n", output)
f, fErr := os.OpenFile(output, os.O_CREATE, 0740)
if fErr != nil {
panic(fErr)
}
defer f.Close()
if err := apng.Encode(f, *v); err != nil {
return
}
}
//
//if err := utils.ZipApngs(out, animations); err != nil {
// log.Panicln("zipping: %s", err)
//}
printIf("done in %v \n", time.Now().Sub(start))
//os.WriteFile(output, out.Bytes(), 0740)
}
func dump(filename string, output string) {
start := time.Now()
printIf("reading file '%s'\n", filename)
n, err := nori.FromFile(filename)
if err != nil {
log.Panicln("decode: %s", err)
}
out := new(bytes.Buffer)
printIf("rendering %d animation(s)\n", n.AnimationCount)
animations, err := renderer.RenderAnimationsApng(n)
if err != nil {
log.Panicln("animation: %s", err)
}
printIf("saving to %s \n", output)
if err := utils.ZipApngs(out, animations); err != nil {
log.Panicln("zipping: %s", err)
}
printIf("done in %v \n", time.Now().Sub(start))
os.WriteFile(output, out.Bytes(), 0740)
}

View File

@ -1,6 +1,9 @@
package nori
import (
"bytes"
"fmt"
"github.com/suapapa/go_hangul/encoding/cp949"
"io"
"os"
)
@ -23,3 +26,17 @@ func Decode(r io.Reader) (*Nori, error) {
err := nori.Decode(r)
return nori, err
}
func GetAnimationNames(n *Nori) (map[string]int, error) {
names := make(map[string]int, n.AnimationCount)
for i := range n.Animations {
anim := n.Animations[i]
title, codepageError := cp949.From(anim.Title[:])
if codepageError != nil {
return nil, fmt.Errorf("couldn't convert title of animation %d", i)
}
cleanTitle := string(bytes.SplitN(title[:], []byte{0}, 2)[0])
names[cleanTitle] = i
}
return names, nil
}

2
go.mod
View File

@ -7,6 +7,7 @@ require (
github.com/disintegration/imaging v1.6.2
github.com/go-chi/chi/v5 v5.0.7
github.com/phrozen/blend v0.0.0-20210220204729-f26b6cf7a28e
github.com/suapapa/go_hangul v1.2.1
gitlab.com/gfxlabs/gfximg v0.0.5
)
@ -14,5 +15,6 @@ require (
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
)

4
go.sum
View File

@ -14,11 +14,15 @@ github.com/phrozen/blend v0.0.0-20210220204729-f26b6cf7a28e h1:r8tWFp1HMiodzOwFt
github.com/phrozen/blend v0.0.0-20210220204729-f26b6cf7a28e/go.mod h1:8LjAsvtcQgvNmMQZ/iSuduOKYgRA37KcsgPg8ZC6Krc=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/suapapa/go_hangul v1.2.1 h1:HJjhwHM2F2G0zq7uIxDWB7tFtoEq3lbjTJLJ8dH4WRE=
github.com/suapapa/go_hangul v1.2.1/go.mod h1:o5XMYtsygfiqzOViFb1W5ax+nROPYeUdh5cDGMkMDxo=
gitlab.com/gfxlabs/gfximg v0.0.5 h1:jtHE6In6axz0zGPy7YnLIZV9RFdcFEZzpdqoXb52K9s=
gitlab.com/gfxlabs/gfximg v0.0.5/go.mod h1:IAYZwCoqy3JFKwkKafragpfecp5Up6FFIzI7VvK2Zzo=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ=
golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=