-
Notifications
You must be signed in to change notification settings - Fork 30
/
userdata.go
89 lines (77 loc) · 2.12 KB
/
userdata.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2017 Daniel Salvadori. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/gob"
"os"
)
// The filepath of the binary file used to store the UserData instance
const USER_DATA_FILENAME string = "/gokoban-user-data"
// UserData stores all the information that persists between game sessions
type UserData struct {
MusicOn bool
MusicVol float32
SfxOn bool
SfxVol float32
LastLevel int
LastUnlockedLevel int
FullScreen bool
}
// NewUserData loads user data from file or creates a new object with default values if no file exists
func LoadOrCreateUserData() *UserData {
ud := new(UserData)
// Get user's cache directory
dataDir, err := os.UserCacheDir()
if err != nil {
panic(err)
}
// Try to read existing file
file, err := os.Open(dataDir + USER_DATA_FILENAME)
if err == nil {
decoder := gob.NewDecoder(file)
err = decoder.Decode(ud)
if err != nil {
log.Debug("Error decoding gokoban-user-data: %v", err)
}
} else {
log.Debug("Error opening gokoban-user-data: %v", err)
}
file.Close()
// If there was an error either opening or decoding the file
if err != nil {
// Set default values
ud.SfxOn = true
ud.MusicOn = true
ud.SfxVol = 0.8
ud.MusicVol = 0.5
ud.FullScreen = false
ud.LastLevel = 0
ud.LastUnlockedLevel = 0
log.Debug("Creating new user data with default values: %+v", ud)
} else {
log.Debug("Loaded user data: %+v", ud)
}
return ud
}
// Save saves the current user data to the user data file, overwriting existing/old data
func (ud *UserData) Save() {
log.Debug("Saving user data: %+v", ud)
// Get user's cache directory
dataDir, err := os.UserCacheDir()
if err != nil {
panic(err)
}
// Save user data to file
file, err := os.Create(dataDir + USER_DATA_FILENAME)
if err == nil {
encoder := gob.NewEncoder(file)
err = encoder.Encode(&ud)
if err != nil {
log.Debug("Error encoding gokoban-user-data: %v", err)
}
} else {
log.Debug("Error creating gokoban-user-data: %v", err)
}
file.Close()
}