-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathsavestates.go
39 lines (35 loc) · 952 Bytes
/
savestates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Package savestates takes care of serializing and unserializing the game RAM
// to the host filesystem.
package savestates
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/libretro/ludo/settings"
"github.com/libretro/ludo/state"
)
// Save the current state to the filesystem. name is the name of the
// savestate file to save to, without extension.
func Save(name string) error {
s := state.Core.SerializeSize()
bytes, err := state.Core.Serialize(s)
if err != nil {
return err
}
path := filepath.Join(settings.Current.SavestatesDirectory, name+".state")
err = os.MkdirAll(settings.Current.SavestatesDirectory, os.ModePerm)
if err != nil {
return err
}
return ioutil.WriteFile(path, bytes, 0644)
}
// Load the state from the filesystem
func Load(path string) error {
s := state.Core.SerializeSize()
bytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}
err = state.Core.Unserialize(bytes, s)
return err
}