[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).
This commit is contained in:
Zorchenhimer 2025-09-06 22:56:10 -04:00
parent db2e5b5f87
commit 045f204a6b
Signed by: Zorchenhimer
GPG Key ID: 70A1AB767AAB9C20
2 changed files with 89 additions and 1 deletions

View File

@ -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 $@ $<

85
cmd/just-stats.go Normal file
View File

@ -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)
}
}