preferences for the comparison emulation can be added on the command line

added commandline stack functions to the prefs package
This commit is contained in:
JetSetIlly 2021-12-01 17:29:49 +00:00
parent 89191d9a5a
commit dd706b392b
7 changed files with 243 additions and 10 deletions

View file

@ -43,6 +43,7 @@ import (
"github.com/jetsetilly/gopher2600/hardware/television"
"github.com/jetsetilly/gopher2600/hardware/television/coords"
"github.com/jetsetilly/gopher2600/logger"
"github.com/jetsetilly/gopher2600/prefs"
"github.com/jetsetilly/gopher2600/reflection"
"github.com/jetsetilly/gopher2600/reflection/counter"
"github.com/jetsetilly/gopher2600/rewind"
@ -573,7 +574,7 @@ func (dbg *Debugger) end() {
}
// Starts the main emulation sequence.
func (dbg *Debugger) Start(mode emulation.Mode, initScript string, cartload cartridgeloader.Loader, comparisonROM string) error {
func (dbg *Debugger) Start(mode emulation.Mode, initScript string, cartload cartridgeloader.Loader, comparisonROM string, comparisonPrefs string) error {
// do not allow comparison emulation inside the debugger. it's far too
// complicated running two emulations that must be synced in the debugger
// loop
@ -581,11 +582,20 @@ func (dbg *Debugger) Start(mode emulation.Mode, initScript string, cartload cart
return curated.Errorf("debugger: cannot run comparison emulation inside the debugger")
}
// add any bespoke comparision prefs
prefs.PushCommandLineStack(comparisonPrefs)
err := dbg.addComparisonEmulation(comparisonROM)
if err != nil {
return curated.Errorf("debugger: %v", err)
}
// check use of comparison prefs
comparisonPrefs = prefs.PopCommandLineStack()
if comparisonPrefs != "" {
logger.Logf("debugger", "%s unused for comparison emulation", comparisonPrefs)
}
defer dbg.end()
err = dbg.start(mode, initScript, cartload)
if err != nil {

View file

@ -156,7 +156,7 @@ func TestDebugger_withNonExistantInitScript(t *testing.T) {
go trm.testSequence()
err = dbg.Start(emulation.ModeDebugger, "non_existent_script", cartridgeloader.Loader{}, "")
err = dbg.Start(emulation.ModeDebugger, "non_existent_script", cartridgeloader.Loader{}, "", "")
if err != nil {
t.Fatalf(err.Error())
}
@ -179,7 +179,7 @@ func TestDebugger(t *testing.T) {
go trm.testSequence()
err = dbg.Start(emulation.ModeDebugger, "", cartridgeloader.Loader{}, "")
err = dbg.Start(emulation.ModeDebugger, "", cartridgeloader.Loader{}, "", "")
if err != nil {
t.Fatalf(err.Error())
}

View file

@ -425,9 +425,11 @@ func emulate(emulationMode emulation.Mode, md *modalflag.Modes, sync *mainSync)
log := md.AddBool("log", false, "echo debugging log to stdout")
// some arguments are mode specific
var comparison *string
var comparisonROM *string
var comparisonPrefs *string
if emulationMode == emulation.ModePlay {
comparison = md.AddString("comparison", "", "ROM to run in parallel for comparison")
comparisonROM = md.AddString("comparisonROM", "", "ROM to run in parallel for comparison")
comparisonPrefs = md.AddString("comparisonPrefs", "", "preferences for comparison emulation")
}
stats := &[]bool{false}[0]
@ -538,11 +540,17 @@ func emulate(emulationMode emulation.Mode, md *modalflag.Modes, sync *mainSync)
// check if comparison was defined and dereference if it was, otherwise
// comp is just the empty string
var comp string
if comparison != nil {
comp = *comparison
if comparisonROM != nil {
comp = *comparisonROM
}
err := dbg.Start(emulationMode, *initScript, cartload, comp)
// same for compPrefs
var compPrefs string
if comparisonPrefs != nil {
compPrefs = *comparisonPrefs
}
err := dbg.Start(emulationMode, *initScript, cartload, comp, compPrefs)
if err != nil {
return err
}

102
prefs/commandline.go Normal file
View file

@ -0,0 +1,102 @@
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package prefs
import (
"fmt"
"sort"
"strings"
)
var commandLineStack []map[string]Value
func init() {
commandLineStack = make([]map[string]Value, 0)
}
// SizeCommandLineStack returns the number of groups that have been added with
// AddCommanLineGroup().
func SizeCommandLineStack() int {
return len(commandLineStack)
}
// PopCommandLineStack forgets the most recent group added by
// AddCommandLineGroup().
//
// Returns the "unused" preferences of the stack entry.
func PopCommandLineStack() string {
if len(commandLineStack) == 0 {
return ""
}
// get top of stack
popped := commandLineStack[len(commandLineStack)-1]
// remove the top of the stack
commandLineStack = commandLineStack[:len(commandLineStack)-1]
// rebuild the prefs string from the remaining entries from the old stack top
keys := make([]string, 0, len(popped))
for key := range popped {
keys = append(keys, key)
}
sort.Strings(keys)
s := strings.Builder{}
for _, key := range keys {
s.WriteString(fmt.Sprintf("%s::%v; ", key, popped[key]))
}
// return prefs string
return strings.TrimSuffix(s.String(), "; ")
}
// PushCommandLineStack parses a command line and adds it as a new group.
func PushCommandLineStack(prefs string) {
commandLineStack = append(commandLineStack, make(map[string]Value))
cl := commandLineStack[len(commandLineStack)-1]
// divide prefs string into individual key/value pairs
o := strings.Split(prefs, ";")
for _, p := range o {
// split key/value
kv := strings.Split(p, "::")
// add to top of stack
if len(kv) == 2 {
cl[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
}
}
}
// GetCommandLinePref value from current group. The value is deleted when it is returned.
func GetCommandLinePref(key string) (bool, Value) {
if len(commandLineStack) == 0 {
return false, nil
}
// top of stack
cl := commandLineStack[len(commandLineStack)-1]
// return value for key if present. delete that entry.
if v, ok := cl[key]; ok {
delete(cl, key)
return true, v
}
return false, nil
}

70
prefs/commandline_test.go Normal file
View file

@ -0,0 +1,70 @@
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package prefs_test
import (
"testing"
"github.com/jetsetilly/gopher2600/prefs"
"github.com/jetsetilly/gopher2600/test"
)
func TestCommandLineStackValues(t *testing.T) {
// empty on start
test.Equate(t, prefs.PopCommandLineStack(), "")
// single value
prefs.PushCommandLineStack("foo::bar")
test.Equate(t, prefs.PopCommandLineStack(), "foo::bar")
// single value but with additional space
prefs.PushCommandLineStack(" foo:: bar ")
test.Equate(t, prefs.PopCommandLineStack(), "foo::bar")
// more than one key/value in the prefs string. remaining string will
// will be sorted
prefs.PushCommandLineStack("foo::bar; baz::qux")
test.Equate(t, prefs.PopCommandLineStack(), "baz::qux; foo::bar")
// check invalid prefs string
prefs.PushCommandLineStack("foo_bar")
test.Equate(t, prefs.PopCommandLineStack(), "")
// check (partically) invalid prefs string
prefs.PushCommandLineStack("foo_bar;baz::qux")
test.Equate(t, prefs.PopCommandLineStack(), "baz::qux")
// get prefs value that doesn't exist after pushing a parially invalid prefs string
prefs.PushCommandLineStack("foo::bar;baz_qux")
ok, _ := prefs.GetCommandLinePref("baz")
test.ExpectedFailure(t, ok)
test.Equate(t, prefs.PopCommandLineStack(), "foo::bar")
}
func TestCommandLineStack(t *testing.T) {
// empty on start
test.Equate(t, prefs.PopCommandLineStack(), "")
// single value
prefs.PushCommandLineStack("foo::bar")
// add another command line group
prefs.PushCommandLineStack("baz::qux")
test.Equate(t, prefs.PopCommandLineStack(), "baz::qux")
// first group still exists
test.Equate(t, prefs.PopCommandLineStack(), "foo::bar")
}

View file

@ -151,7 +151,7 @@ func (dsk *Disk) Save() (rerr error) {
return curated.Errorf("prefs: %v", err)
}
if n != len(WarningBoilerPlate)+1 {
return curated.Errorf("prefs: %v", "incorrect number of characters writtent to file")
return curated.Errorf("prefs: %v", "incorrect number of characters written to file")
}
// write entries (combination of old and live entries) to disk
@ -161,7 +161,7 @@ func (dsk *Disk) Save() (rerr error) {
return curated.Errorf("prefs: %v", err)
}
if n != len(s) {
return curated.Errorf("prefs: %v", "incorrect number of characters writtent to file")
return curated.Errorf("prefs: %v", "incorrect number of characters written to file")
}
return nil
@ -176,6 +176,13 @@ func (dsk *Disk) Load(saveOnFirstUse bool) error {
return err
}
// override loaded values
for k := range dsk.entries {
if ok, v := GetCommandLinePref(k); ok {
dsk.entries[k].Set(v)
}
}
// if the number of entries loaded by the load() function is not equal to
// then number of entries in this Disk instance then we can say that a new
// preference value has been added since the last save to disk. if

View file

@ -48,6 +48,42 @@
// Set() should be used with care *if* SetHookPost() or SetHookPre() has been
// set for that value.
//
//Command Line Support
//
// The *CommandLine*() functions are designed to help with the overriding of
// disk values with a value given on the command line. These values are added
// as a group with AddCommandLineGroup(). For example
//
// AddCommandLineGroup("foo::bar; baz::qux")
//
// (see below for more detail about the format of the prefs string)
//
// These values are then looked up when preferences are first loaded by an
// instance of the Disk type. The command line value will be used instead of
// the saved value for the first load only. If Load() is never called then the
// command line value will not be used -- this shouldn't be an issue because it
// is good practice for Load() to always be called after the a set of prefs
// values have been added with the Add() function
//
// Commandline groups can be nested. Subsequent calls to AddCommandLineGroup()
// will hide the previous group until EndCommandLineGroup() is called.
//
// The prefs string used with AddCommandLineGroup() mirror the format of the
// preferences file except that entries are separated by semi-colons instead of
// newlines.
//
// For example, if the preferences file has the following entries:
//
// a.b.c :: 100
// d.e.f.g :: false
// h.i :: wibble
//
// A valid string to use with AddCommandLineGroup() might be:
//
// a.b.c::100; h.i::wibble
//
// Leading and trailing spaces around the key and value are stripped.
//
//Note
//
// While saved preference files are stored in UTF-8 it is not a good idea for