Compare commits

...

3 Commits

Author SHA1 Message Date
Zorchenhimer 75e0ee2ba3
[rom] Use mode 0666 when writing rom file 2025-11-22 19:44:48 -05:00
Zorchenhimer 88e095f426
[sbutil] Use filname field in json when packing
- Use the filename provided in the json file when writing out a .studybox
  file.  By default, it will not overwrite an existing file.
- Passing --force will overwrite an existing file.
- Passing --output will override the filename in the json.
2025-11-22 19:42:10 -05:00
Zorchenhimer 6831274a25
[rom] Fix writing AUDI data
The length of the audio chunk was incorrect and did not include the
format bytes.  This was also the source of the "garbage four bytes"
that were being chopped off before.
2025-11-16 17:38:41 -05:00
4 changed files with 37 additions and 9 deletions

View File

@ -5,6 +5,8 @@ import (
"os"
"path/filepath"
"strings"
"errors"
"io/fs"
"github.com/alexflint/go-arg"
@ -17,7 +19,9 @@ type Arguments struct {
}
type ArgPack struct {
Input string `arg:"positional,required"`
Input string `arg:"positional,required"`
Force bool `arg:"--force"`
Output string `arg:"--output,-o"`
}
type ArgUnPack struct {
@ -73,9 +77,17 @@ func pack(args *ArgPack) error {
// TODO: put this in the json file?
outname := args.Input[:len(args.Input)-len(".json")]+".studybox"
fmt.Println(outname)
err = sb.Write(outname)
if args.Output != "" {
sb.Filename = args.Output
}
// outname := args.Input[:len(args.Input)-len(".json")]+".studybox"
if exists(sb.Filename) && !args.Force {
return fmt.Errorf("%s already exists or cannot be written to", sb.Filename)
}
fmt.Println(sb.Filename)
err = sb.Write(sb.Filename)
if err != nil {
return err
}
@ -117,6 +129,19 @@ func unpack(args *ArgUnPack) error {
return nil
}
func exists(filename string) bool {
_, err := os.Stat(filename)
if err == nil {
return true
}
if errors.Is(err, fs.ErrNotExist) {
return false
}
return true
}
//func main_old() {
// if len(os.Args) < 3 {
// fmt.Println("Missing command")

View File

@ -16,7 +16,7 @@ func Import(filename string) (*StudyBox, error) {
return nil, err
}
sbj := &StudyBoxJson{}
sbj := &StudyBoxJson{ Filename: filename }
err = json.Unmarshal(raw, sbj)
if err != nil {
return nil, fmt.Errorf("Unable to unmarshal json: %v", err)
@ -28,6 +28,7 @@ func Import(filename string) (*StudyBox, error) {
}
sb := &StudyBox{
Filename: sbj.Filename,
Data: &TapeData{Pages: []*Page{}},
Audio: audio,
}

View File

@ -8,6 +8,7 @@ import (
)
type StudyBox struct {
Filename string
Data *TapeData
Audio *TapeAudio
}

View File

@ -18,7 +18,7 @@ func (sb *StudyBox) Write(filename string) error {
fmt.Println("Writing to " + filename)
return os.WriteFile(filename, raw, 0777)
return os.WriteFile(filename, raw, 0666)
}
func (sb *StudyBox) rawBytes() ([]byte, error) {
@ -56,7 +56,9 @@ func (sb *StudyBox) rawBytes() ([]byte, error) {
return nil, err
}
err = binary.Write(buffer, binary.LittleEndian, uint32(len(sb.Audio.Data)))
// This length is the full size of the chunk, including the format from the
// switch below.
err = binary.Write(buffer, binary.LittleEndian, uint32(len(sb.Audio.Data)+4))
if err != nil {
return nil, err
}
@ -81,8 +83,7 @@ func (sb *StudyBox) rawBytes() ([]byte, error) {
return nil, err
}
// For some reason there's 4 extra bytes. no idea why. chomp them off.
_, err = buffer.Write(sb.Audio.Data[0 : uint32(len(sb.Audio.Data))-4])
_, err = buffer.Write(sb.Audio.Data[0 : uint32(len(sb.Audio.Data))])
if err != nil {
return nil, err
}