Skip to content

Commit

Permalink
Add the uniqueSaveDir option
Browse files Browse the repository at this point in the history
This option allows for the safe use of distinct filesystem snapshots of games with some cores (e.g., DosBox). Keep in mind that with this option enabled, game changes won't be saved (the unique save folder will be deleted on exit) until you explicitly call the save (or share) function. Thus, you will need files like dosbox.conf along with the games to use some default behaviors with each new game session.
  • Loading branch information
sergystepanov committed Aug 21, 2024
1 parent ddb16f8 commit f49b24b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cd/cloudretro.io/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ emulator:
logLevel: 1
cores:
list:
dos:
uniqueSaveDir: true
mame:
options:
"fbneo-diagnostic-input": "Hold Start"
nes:
scale: 2
pcsx:
altRepo: true
snes:
scale: 2
2 changes: 2 additions & 0 deletions pkg/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ emulator:
# - skip_hw_context_destroy -- don't destroy OpenGL context during Libretro core deinit.
# May help with crashes, for example, with PPSSPP.
# - skip_same_thread_save -- skip thread lock save (used with PPSSPP).
# - uniqueSaveDir (bool) -- needed only for cores (like DosBox) that persist their state into one shared file.
# This will allow for concurrent reading and saving of current states.
list:
gba:
lib: mgba_libretro
Expand Down
1 change: 1 addition & 0 deletions pkg/config/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type LibretroCoreConfig struct {
Options4rom map[string]map[string]string // <(^_^)>
Roms []string
Scale float64
UniqueSaveDir bool
UsesLibCo bool
VFR bool
Width int
Expand Down
4 changes: 4 additions & 0 deletions pkg/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ func StatSize(path string) (int64, error) {
}
return fi.Size(), nil
}

func RemoveAll(path string) error {
return os.RemoveAll(path)
}
13 changes: 13 additions & 0 deletions pkg/worker/caged/libretro/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Frontend struct {

DisableCanvasPool bool
SaveOnClose bool
UniqueSaveDir bool
}

type Device byte
Expand Down Expand Up @@ -153,6 +154,11 @@ func (f *Frontend) LoadCore(emu string) {
KbMouseSupport: conf.KbMouseSupport,
}
f.mu.Lock()
if conf.UniqueSaveDir {
f.UniqueSaveDir = true
f.nano.SetSaveDirSuffix(f.storage.MainPath())
f.log.Debug().Msgf("Using unique dir for saves: %v", f.storage.MainPath())
}
scale := 1.0
if conf.Scale > 1 {
scale = conf.Scale
Expand Down Expand Up @@ -336,6 +342,13 @@ func (f *Frontend) Close() {

f.mui.Lock()
f.nano.Close()

if f.UniqueSaveDir && !f.HasSave() {
if err := f.nano.DeleteSaveDir(); err != nil {
f.log.Error().Msgf("couldn't delete save dir: %v", err)
}
}

f.mui.Unlock()
f.log.Debug().Msgf("frontend closed")
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/worker/caged/libretro/nanoarch/nanoarch.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ func (n *Nanoarch) WaitReady() { <-n.reserved }
func (n *Nanoarch) Close() { n.Stopped.Store(true); n.reserved <- struct{}{} }
func (n *Nanoarch) SetLogger(log *logger.Logger) { n.log = log }
func (n *Nanoarch) SetVideoDebounce(t time.Duration) { n.limiter = NewLimit(t) }
func (n *Nanoarch) SetSaveDirSuffix(sx string) {
if n.cSaveDirectory != nil {
C.free(unsafe.Pointer(n.cSaveDirectory))
}
dir := C.GoString(n.cSaveDirectory) + "/" + sx
_ = os.CheckCreateDir(dir)
n.cSaveDirectory = C.CString(dir)
}
func (n *Nanoarch) DeleteSaveDir() error {
if n.cSaveDirectory == nil {
return nil
}

dir := C.GoString(n.cSaveDirectory)
return os.RemoveAll(dir)
}

func (n *Nanoarch) CoreLoad(meta Metadata) {
var err error
Expand Down
2 changes: 2 additions & 0 deletions pkg/worker/caged/libretro/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type (
Storage interface {
MainPath() string
GetSavePath() string
GetSRAMPath() string
SetMainSaveName(name string)
Expand All @@ -32,6 +33,7 @@ type (
}
)

func (s *StateStorage) MainPath() string { return s.MainSave }
func (s *StateStorage) SetMainSaveName(name string) { s.MainSave = name }
func (s *StateStorage) SetNonBlocking(v bool) { s.NonBlock = v }
func (s *StateStorage) GetSavePath() string { return filepath.Join(s.Path, s.MainSave+".dat") }
Expand Down

0 comments on commit f49b24b

Please sign in to comment.