1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-27 01:25:35 +00:00

Move save game location to ~/.config/OpenDiablo2/Save (#258)

This commit is contained in:
Alex Yatskov 2019-12-17 20:20:06 -08:00 committed by Tim Sarbin
parent d5a0038125
commit 4ebe199a8e

View File

@ -5,7 +5,6 @@ import (
"log" "log"
"os" "os"
"path" "path"
"runtime"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -41,13 +40,14 @@ type GameState struct {
const GameStateVersion = uint32(2) // Update this when you make breaking changes const GameStateVersion = uint32(2) // Update this when you make breaking changes
func HasGameStates() bool { func HasGameStates() bool {
files, _ := ioutil.ReadDir(getGameBaseSavePath()) basePath, _ := getGameBaseSavePath()
files, _ := ioutil.ReadDir(basePath)
return len(files) > 0 return len(files) > 0
} }
func GetAllGameStates() []*GameState { func GetAllGameStates() []*GameState {
// TODO: Make this not crash tf out on bad files // TODO: Make this not crash tf out on bad files
basePath := getGameBaseSavePath() basePath, _ := getGameBaseSavePath()
files, _ := ioutil.ReadDir(basePath) files, _ := ioutil.ReadDir(basePath)
result := make([]*GameState, 0) result := make([]*GameState, 0)
for _, file := range files { for _, file := range files {
@ -112,30 +112,18 @@ func CreateGameState(heroName string, hero d2enum.Hero, hardcore bool) *GameStat
return result return result
} }
func getGameBaseSavePath() string { func getGameBaseSavePath() (string, error) {
if runtime.GOOS == "windows" { configDir, err := os.UserConfigDir()
appDataPath := os.Getenv("APPDATA")
basePath := path.Join(appDataPath, "OpenDiablo2", "Saves")
if err := os.MkdirAll(basePath, os.ModeDir); err != nil {
log.Panicf(err.Error())
}
return basePath
}
homeDir, err := os.UserHomeDir()
if err != nil { if err != nil {
log.Panicf(err.Error()) return "", err
} }
basePath := path.Join(homeDir, ".OpenDiablo2", "Saves")
if err := os.MkdirAll(basePath, 0755); err != nil { return path.Join(configDir, "OpenDiablo2/Saves"), nil
log.Panicf(err.Error())
}
// TODO: Is mac supposed to have a super special place for the save games?
return basePath
} }
func getFirstFreeFileName() string { func getFirstFreeFileName() string {
i := 0 i := 0
basePath := getGameBaseSavePath() basePath, _ := getGameBaseSavePath()
for { for {
filePath := path.Join(basePath, strconv.Itoa(i)+".od2") filePath := path.Join(basePath, strconv.Itoa(i)+".od2")
if _, err := os.Stat(filePath); os.IsNotExist(err) { if _, err := os.Stat(filePath); os.IsNotExist(err) {
@ -149,6 +137,9 @@ func (v *GameState) Save() {
if v.FilePath == "" { if v.FilePath == "" {
v.FilePath = getFirstFreeFileName() v.FilePath = getFirstFreeFileName()
} }
if err := os.MkdirAll(path.Dir(v.FilePath), 0755); err != nil {
log.Panic(err.Error())
}
f, err := os.Create(v.FilePath) f, err := os.Create(v.FilePath)
if err != nil { if err != nil {
log.Panicf(err.Error()) log.Panicf(err.Error())