playback files no longer created cartridge loader directly

the information used to create the loader is storied in
the playback type rather than a created loader itself

this is more flexible and gives us better control of when
the loader is disposed of

checking of cartridge hash must now be performed explicitely
when the cartridge loader is created from the playback information
This commit is contained in:
JetSetIlly 2024-04-17 09:54:09 +01:00
parent 7ed001c4c1
commit d431974c76
4 changed files with 44 additions and 25 deletions

View file

@ -836,7 +836,10 @@ func (dbg *Debugger) StartInPlayMode(filename string) error {
return fmt.Errorf("debugger: cannot make a new recording using a playback file")
}
dbg.startPlayback(filename)
err = dbg.startPlayback(filename)
if err != nil {
return fmt.Errorf("debugger: %w", err)
}
}
if dbg.opts.Macro != "" {
@ -1289,12 +1292,26 @@ func (dbg *Debugger) endRecording() {
}
func (dbg *Debugger) startPlayback(filename string) error {
plb, err := recorder.NewPlayback(filename, dbg.opts.PlaybackCheckROM)
plb, err := recorder.NewPlayback(filename)
if err != nil {
return err
}
err = dbg.attachCartridge(plb.CartLoad)
// new cartridge loader using the information found in the playback file
cartload, err := cartridgeloader.NewLoaderFromFilename(plb.Cartridge, "AUTO")
if err != nil {
return fmt.Errorf("debugger: %w", err)
}
defer cartload.Close()
// check hash of cartridge before continuing
if dbg.opts.PlaybackCheckROM && cartload.HashSHA1 != plb.Hash {
return fmt.Errorf("playback: unexpected hash")
}
// cartload is should be passed to attachCartridge() almost immediately. the
// closure of cartload will then be handled for us
err = dbg.attachCartridge(cartload)
if err != nil {
return err
}

View file

@ -22,8 +22,6 @@ import (
"io"
"os"
"strings"
"github.com/jetsetilly/gopher2600/cartridgeloader"
)
const (
@ -104,22 +102,13 @@ func (rec *Recorder) writeHeader() error {
return nil
}
func (plb *Playback) readHeader(lines []string, checkROM bool) error {
func (plb *Playback) readHeader(lines []string) error {
if lines[lineMagicString] != magicString {
return fmt.Errorf("playback: not a valid transcript (%s)", plb.transcript)
}
var err error
plb.CartLoad, err = cartridgeloader.NewLoaderFromFilename(lines[lineCartName], "AUTO")
if err != nil {
return fmt.Errorf("playback: %w", err)
}
if checkROM && plb.CartLoad.HashSHA1 != lines[lineCartHash] {
return fmt.Errorf("playback: unexpected hash")
}
plb.Cartridge = lines[lineCartName]
plb.Hash = lines[lineCartHash]
plb.TVSpec = lines[lineTVSpec]
return nil

View file

@ -23,7 +23,6 @@ import (
"strconv"
"strings"
"github.com/jetsetilly/gopher2600/cartridgeloader"
"github.com/jetsetilly/gopher2600/digest"
"github.com/jetsetilly/gopher2600/hardware"
"github.com/jetsetilly/gopher2600/hardware/riot/ports"
@ -44,8 +43,9 @@ type playbackEntry struct {
type Playback struct {
transcript string
CartLoad cartridgeloader.Loader
TVSpec string
Cartridge string
Hash string
TVSpec string
sequence []playbackEntry
seqCt int
@ -79,7 +79,7 @@ func (plb Playback) EndFrame() (bool, error) {
// AttachToVCSInput() function) for it it to be useful.
//
// The integrityCheck flag should be true in most instances.
func NewPlayback(transcript string, checkROM bool) (*Playback, error) {
func NewPlayback(transcript string) (*Playback, error) {
var err error
plb := &Playback{
@ -104,7 +104,7 @@ func NewPlayback(transcript string, checkROM bool) (*Playback, error) {
lines := strings.Split(string(buffer), "\n")
// read header and perform validation checks
err = plb.readHeader(lines, checkROM)
err = plb.readHeader(lines)
if err != nil {
return nil, err
}

View file

@ -24,6 +24,7 @@ import (
"strings"
"time"
"github.com/jetsetilly/gopher2600/cartridgeloader"
"github.com/jetsetilly/gopher2600/database"
"github.com/jetsetilly/gopher2600/debugger/govern"
"github.com/jetsetilly/gopher2600/digest"
@ -105,7 +106,7 @@ func (reg PlaybackRegression) String() string {
func (reg *PlaybackRegression) regress(newRegression bool, output io.Writer, msg string) (_ bool, _ string, rerr error) {
output.Write([]byte(msg))
plb, err := recorder.NewPlayback(reg.Script, true)
plb, err := recorder.NewPlayback(reg.Script)
if err != nil {
return false, "", fmt.Errorf("playback: %w", err)
}
@ -137,10 +138,22 @@ func (reg *PlaybackRegression) regress(newRegression bool, output io.Writer, msg
return false, "", fmt.Errorf("playback: %w", err)
}
// new cartridge loader using the information found in the playback file
cartload, err := cartridgeloader.NewLoaderFromFilename(plb.Cartridge, "AUTO")
if err != nil {
return false, "", fmt.Errorf("playback: %w", err)
}
defer cartload.Close()
// check hash of cartridge before continuing
if cartload.HashSHA1 != plb.Hash {
return false, "", fmt.Errorf("playback: unexpected hash")
}
// not using setup.AttachCartridge. if the playback was recorded with setup
// changes the events will have been copied into the playback script and
// will be applied that way
err = vcs.AttachCartridge(plb.CartLoad, true)
err = vcs.AttachCartridge(cartload, true)
if err != nil {
return false, "", fmt.Errorf("playback: %w", err)
}
@ -191,7 +204,7 @@ func (reg *PlaybackRegression) regress(newRegression bool, output io.Writer, msg
// regressionScripts directory
if newRegression {
// create a unique filename
newScript, err := uniqueFilename("playback", plb.CartLoad.Name)
newScript, err := uniqueFilename("playback", cartload.Name)
if err != nil {
return false, "", fmt.Errorf("playback: %w", err)
}