Skip to content

Commit 7da993a

Browse files
committed
Add the uniqueSaveDir option
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.
1 parent ddb16f8 commit 7da993a

File tree

7 files changed

+40
-2
lines changed

7 files changed

+40
-2
lines changed

.github/workflows/cd/cloudretro.io/config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ emulator:
2929
logLevel: 1
3030
cores:
3131
list:
32+
dos:
33+
uniqueSaveDir: true
3234
mame:
3335
options:
3436
"fbneo-diagnostic-input": "Hold Start"
3537
nes:
3638
scale: 2
37-
pcsx:
38-
altRepo: true
3939
snes:
4040
scale: 2

pkg/config/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ emulator:
209209
# - skip_hw_context_destroy -- don't destroy OpenGL context during Libretro core deinit.
210210
# May help with crashes, for example, with PPSSPP.
211211
# - skip_same_thread_save -- skip thread lock save (used with PPSSPP).
212+
# - uniqueSaveDir (bool) -- needed only for cores (like DosBox) that persist their state into one shared file.
213+
# This will allow for concurrent reading and saving of current states.
212214
list:
213215
gba:
214216
lib: mgba_libretro

pkg/config/emulator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type LibretroCoreConfig struct {
5555
Options4rom map[string]map[string]string // <(^_^)>
5656
Roms []string
5757
Scale float64
58+
UniqueSaveDir bool
5859
UsesLibCo bool
5960
VFR bool
6061
Width int

pkg/os/os.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,7 @@ func StatSize(path string) (int64, error) {
8484
}
8585
return fi.Size(), nil
8686
}
87+
88+
func RemoveAll(path string) error {
89+
return os.RemoveAll(path)
90+
}

pkg/worker/caged/libretro/frontend.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type Frontend struct {
6666

6767
DisableCanvasPool bool
6868
SaveOnClose bool
69+
UniqueSaveDir bool
6970
}
7071

7172
type Device byte
@@ -153,6 +154,11 @@ func (f *Frontend) LoadCore(emu string) {
153154
KbMouseSupport: conf.KbMouseSupport,
154155
}
155156
f.mu.Lock()
157+
if conf.UniqueSaveDir {
158+
f.UniqueSaveDir = true
159+
f.nano.SetSaveDirSuffix(f.storage.MainPath())
160+
f.log.Debug().Msgf("Using unique dir for saves: %v", f.storage.MainPath())
161+
}
156162
scale := 1.0
157163
if conf.Scale > 1 {
158164
scale = conf.Scale
@@ -336,6 +342,13 @@ func (f *Frontend) Close() {
336342

337343
f.mui.Lock()
338344
f.nano.Close()
345+
346+
if f.UniqueSaveDir && !f.HasSave() {
347+
if err := f.nano.DeleteSaveDir(); err != nil {
348+
f.log.Error().Msgf("couldn't delete save dir: %v", err)
349+
}
350+
}
351+
339352
f.mui.Unlock()
340353
f.log.Debug().Msgf("frontend closed")
341354
}

pkg/worker/caged/libretro/nanoarch/nanoarch.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,22 @@ func (n *Nanoarch) WaitReady() { <-n.reserved }
157157
func (n *Nanoarch) Close() { n.Stopped.Store(true); n.reserved <- struct{}{} }
158158
func (n *Nanoarch) SetLogger(log *logger.Logger) { n.log = log }
159159
func (n *Nanoarch) SetVideoDebounce(t time.Duration) { n.limiter = NewLimit(t) }
160+
func (n *Nanoarch) SetSaveDirSuffix(sx string) {
161+
if n.cSaveDirectory != nil {
162+
C.free(unsafe.Pointer(n.cSaveDirectory))
163+
}
164+
dir := C.GoString(n.cSaveDirectory) + "/" + sx
165+
_ = os.CheckCreateDir(dir)
166+
n.cSaveDirectory = C.CString(dir)
167+
}
168+
func (n *Nanoarch) DeleteSaveDir() error {
169+
if n.cSaveDirectory == nil {
170+
return nil
171+
}
172+
173+
dir := C.GoString(n.cSaveDirectory)
174+
return os.RemoveAll(dir)
175+
}
160176

161177
func (n *Nanoarch) CoreLoad(meta Metadata) {
162178
var err error

pkg/worker/caged/libretro/storage.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
type (
1212
Storage interface {
13+
MainPath() string
1314
GetSavePath() string
1415
GetSRAMPath() string
1516
SetMainSaveName(name string)
@@ -32,6 +33,7 @@ type (
3233
}
3334
)
3435

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

0 commit comments

Comments
 (0)