-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsettings.go
148 lines (138 loc) · 4.37 KB
/
settings.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// QCLauncher by syncore <[email protected]> 2017
// https://github.com/syncore/qclauncher
package qclauncher
import (
"errors"
"fmt"
"os"
)
type Configuration struct {
Core *QCCoreSettings
Experimental *QCExperimentalSettings
Launcher *LauncherSettings
Auth *TokenAuth
}
var isCollectingSettings = false
func GetConfiguration() (*Configuration, error) {
coreQCSettings := &QCCoreSettings{}
err := Get(coreQCSettings)
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error retrieving core QC configuration settings", GetCaller()), "error", err)
return nil, err
}
expQCSettings := &QCExperimentalSettings{}
err = Get(expQCSettings)
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error retrieving experimental QC configuration settings", GetCaller()), "error", err)
return nil, err
}
launcherSettings := &LauncherSettings{}
err = Get(launcherSettings)
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error retrieving launcher configuration settings", GetCaller()), "error", err)
return nil, err
}
authToken := &TokenAuth{}
err = Get(authToken)
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error retrieving auth token", GetCaller()), "error", err)
return nil, err
}
return &Configuration{
Core: coreQCSettings,
Experimental: expQCSettings,
Launcher: launcherSettings,
Auth: authToken,
}, nil
}
func GetEmptyConfiguration() *Configuration {
return &Configuration{
Core: &QCCoreSettings{},
Experimental: &QCExperimentalSettings{},
Launcher: &LauncherSettings{},
}
}
func DeleteConfiguration(removeLock bool) {
err := DeleteFile(GetDataFilePath())
if err != nil && !os.IsNotExist(err) {
logger.Error(fmt.Sprintf("%s: error deleting %s: %s", GetCaller(), DataFile, err))
Lock.Unlock()
ShowFatalErrorMsg("Error",
fmt.Sprintf("Unable to delete existing %s file during reset. Please manually delete it and then re-run QCLauncher.",
DataFile), nil)
return
}
if removeLock {
Lock.Unlock()
}
}
func configureSettings() {
if isCollectingSettings {
return
}
var err error
var cfg *Configuration
if FileExists(GetDataFilePath()) {
cfg, err = GetConfiguration()
if err != nil {
ShowErrorMsg("Error", "An error occurred when retrieving your settings. Resetting.", nil)
DeleteConfiguration(false)
cfg = GetEmptyConfiguration()
}
} else {
cfg = GetEmptyConfiguration()
}
isCollectingSettings = true
settingsWindow := newSettingsWindow(cfg, &QCLSettingsWindowOptions{CanSaveSettings: true})
settingsWindow.Run()
}
func saveConfiguration(cfg *Configuration) error {
if err := validateOnSave(cfg); err != nil {
return err
}
checkLog := fmt.Sprintf("please check the %s file for more details.", LogFile)
if err := Save(cfg.Core); err != nil {
logger.Errorw(fmt.Sprintf("%s: error saving QC core settings", GetCaller()), "error", err)
return fmt.Errorf("Unable to save QC core settings, %s", checkLog)
}
cfg.Experimental.resetBoolForZeroValues() // handle 0 values (no effect) and others
if err := Save(cfg.Experimental); err != nil {
logger.Errorw(fmt.Sprintf("%s: error saving QC experimental settings", GetCaller()), "error", err)
return fmt.Errorf("Unable to save QC experimental settings, %s", checkLog)
}
// Steam launch should be a one-time event
launchSteam := cfg.Launcher.SetAsNonSteamGame
cfg.Launcher.SetAsNonSteamGame = false
if err := Save(cfg.Launcher); err != nil {
logger.Errorw(fmt.Sprintf("%s: error saving launcher settings", GetCaller()), "error", err)
return fmt.Errorf("Unable to save launcher settings, %s", checkLog)
}
if err := updateLastCheckTime(UpdateAll, 0); err != nil {
logger.Errorw(fmt.Sprintf("%s: error setting last update check time", GetCaller()), "error", err)
return err
}
if launchSteam {
ShowInfoMsg("Launching Steam",
"Now opening Steam to add Quake Champions. When Steam loads, browse to and select your qclauncher.exe file.",
qclauncherSettingsWindow)
if err := addNonSteamGame(getSteamInstallPath()); err != nil {
ShowErrorMsg("Error", "Unable to launch Steam", qclauncherSettingsWindow)
}
}
return nil
}
func validateOnSave(cfg *Configuration) error {
if cfg == nil {
return errors.New("No settings were entered")
}
if err := cfg.Core.validate(); err != nil {
return err
}
if err := cfg.Experimental.validate(); err != nil {
return err
}
if err := cfg.Launcher.validate(); err != nil {
return err
}
return nil
}