-
Notifications
You must be signed in to change notification settings - Fork 3
/
settingslauncher.go
97 lines (88 loc) · 2.82 KB
/
settingslauncher.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
90
91
92
93
94
95
96
97
// QCLauncher by syncore <[email protected]> 2017
// https://github.com/syncore/qclauncher
package qclauncher
import (
"bytes"
"encoding/binary"
"encoding/gob"
"errors"
"fmt"
bolt "github.com/coreos/bbolt"
)
type LauncherSettings struct {
AutoStartQC bool
ExitOnLaunch bool
MinimizeOnLaunch bool
MinimizeToTray bool
SetAsNonSteamGame bool
}
func (s *LauncherSettings) get(ls *LauncherStore) error {
ls.checkDataFile(false)
if err := ls.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketSettings))
decerr := s.decode(b.Get([]byte(keyLauncherSettings)))
if decerr != nil {
logger.Errorw(fmt.Sprintf("%s: error decoding launcher settings from datastore during get operation", GetCaller()),
"error", decerr)
}
return nil
}); err != nil {
logger.Errorw(fmt.Sprintf("%s: error getting launcher settings from datastore", GetCaller()), "error", err)
return err
}
return nil
}
func (s *LauncherSettings) save(ls *LauncherStore) error {
return ls.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(bucketSettings))
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error creating settings bucket in datastore during save operation",
GetCaller()), "error", err)
return err
}
encoded, err := s.encode()
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error encoding launcher settings during datastore save operation", GetCaller()),
"error", err)
return err
}
if err = b.Put([]byte(keyLauncherSettings), encoded); err != nil {
logger.Errorw(fmt.Sprintf("%s: error saving encoded launcher settings to datastore", GetCaller()), "error", err)
return err
}
b, err = tx.CreateBucketIfNotExists([]byte(bucketLastUpdate))
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error creating update bucket for saving data file version to datastore", GetCaller()),
"error", err)
return err
}
dfv := make([]byte, 8)
binary.LittleEndian.PutUint64(dfv, uint64(dataFileVersion))
if err = b.Put([]byte(keyDfVer), dfv); err != nil {
logger.Errorw(fmt.Sprintf("%s: error saving data file version to datastore", GetCaller()), "error", err)
return err
}
return nil
})
}
func (s *LauncherSettings) decode(data []byte) error {
if err := gob.NewDecoder(bytes.NewBuffer(data)).Decode(&s); err != nil {
logger.Errorw(fmt.Sprintf("%s: error decoding launcher settings data", GetCaller()), "error", err)
return err
}
return nil
}
func (s *LauncherSettings) encode() ([]byte, error) {
buf := new(bytes.Buffer)
if err := gob.NewEncoder(buf).Encode(s); err != nil {
logger.Errorw(fmt.Sprintf("%s: error encoding launcher settings data", GetCaller()), "error", err)
return nil, err
}
return buf.Bytes(), nil
}
func (s *LauncherSettings) validate() error {
if s == nil {
return errors.New("Launcher setting info was not entered")
}
return nil
}