From 045f204a6bc2dcde5ab14cc4b90a72468635c009 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sat, 6 Sep 2025 22:56:10 -0400 Subject: [PATCH] [cmd] Add just-stats command This command is meant for gathering stats across many script files. Currently breaks due to an oversight in script parsing (if an inline argument list goes past EOF, everything breaks). --- Makefile | 5 ++- cmd/just-stats.go | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 cmd/just-stats.go diff --git a/Makefile b/Makefile index a4a994c..055451e 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,12 @@ .PHONY: all -all: bin/script-decode bin/sbutil +all: bin/script-decode bin/sbutil bin/just-stats bin/script-decode: cmd/script-decode.go script/*.go go build -o $@ $< bin/sbutil: cmd/sbutil.go rom/*.go go build -o $@ $< + +bin/just-stats: cmd/just-stats.go script/*.go + go build -o $@ $< diff --git a/cmd/just-stats.go b/cmd/just-stats.go new file mode 100644 index 0000000..87e2149 --- /dev/null +++ b/cmd/just-stats.go @@ -0,0 +1,85 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + "strings" + "io/fs" + + "github.com/alexflint/go-arg" + + "git.zorchenhimer.com/Zorchenhimer/go-studybox/script" +) + +type Arguments struct { + BaseDir string `arg:"positional,required"` + Output string `arg:"positional,required"` +} + +type Walker struct { + Found []string +} + +func (w *Walker) WalkFunc(path string, info fs.DirEntry, err error) error { + if info.IsDir() { + return nil + } + + if strings.HasSuffix(path, "_scriptData.dat") { + w.Found = append(w.Found, path) + } + + return nil +} + +func run(args *Arguments) error { + w := &Walker{Found: []string{}} + err := filepath.WalkDir(args.BaseDir, w.WalkFunc) + if err != nil { + return err + } + + fmt.Printf("found %d scripts\n", len(w.Found)) + + stats := make(script.Stats) + + for _, file := range w.Found { + fmt.Println(file) + scr, err := script.ParseFile(file, 0x0000) + if err != nil { + if scr != nil { + for _, token := range scr.Tokens { + fmt.Println(token.String(scr.Labels)) + } + } + return err + } + + stats.Add(scr.Stats()) + } + + outfile, err := os.Create(args.Output) + if err != nil { + return err + } + defer outfile.Close() + + _, err = stats.WriteTo(outfile) + if err != nil { + return err + } + + return nil +} + +func main() { + args := &Arguments{} + arg.MustParse(args) + + err := run(args) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +}