-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlaunch.go
207 lines (196 loc) · 7.26 KB
/
launch.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// QCLauncher by syncore <[email protected]> 2017
// https://github.com/syncore/qclauncher
package qclauncher
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"reflect"
"strconv"
"strings"
"syscall"
)
const (
QCExe = "QuakeChampions.exe"
defArgs = `--startup --set /Config/GAME_CONFIG/bethesdaGameCode "%GAMECODE%" --set /Config/GAME_CONFIG/bethesdaLoginEnabled 1 --set /Config/Bethesda/Language "%LANGUAGE%" --set /Config/GAME_CONFIG/bethesdaEndpointUrl "https://services.bethesda.net/agora_beam/"`
qcEntitlmentID = 48329
qcProjectID = 11
qcDefaultBranchIdentifier = "Default"
gameCodeTempl = "%GAMECODE%"
langTempl = "%LANGUAGE%"
defLang = "en"
)
func Launch() error {
running, _, _, _, namepids, err := IsProcessRunning(QCExe)
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error enumerating running processes to see if QC is already running", GetCaller()),
"error", err)
ShowErrorMsg("Error", "Unable to enumerate processes to determine if Quake Champions is already running", nil)
return err
}
if running {
if willClose := ShowQCRunningMsg(namepids[QCExe]); !willClose {
return &alreadyRunningError{emsg: "Quake Champions is already running, cannot start."}
}
}
cfg, err := GetConfiguration()
if err != nil {
logger.Errorw(fmt.Sprintf("%s: fatal error: unable to load data file during launch", GetCaller()), "error", err)
ShowFatalErrorMsg("Error", fmt.Sprintf("Could not read your %s file. Cannot start.", DataFile), nil)
return err
}
lc := newLauncherClient(defTimeout)
lc.checkServerStatus()
if err = CheckUpdate(ConfEnforceHash, UpdateQC); IsErrHashMismatch(err) {
return &hashMismatchError{emsg: err.Error()}
}
err = lc.authenticate(cfg)
if err != nil {
return err
}
var projectID, branchID int
if UseEntitlementAPI {
entitlementInfo, eerr := lc.getEntitlementInfo()
if eerr != nil {
logger.Errorw(fmt.Sprintf("%s: getEntitlementInfo error", GetCaller()), "error", eerr, "data", entitlementInfo)
return eerr
}
logger.Debugw("Entitlement info", "entitlementInfo", entitlementInfo)
projectID, branchID, _, err = getProjectBranchBuildIdentifiers(entitlementInfo)
if err != nil {
logger.Errorw(fmt.Sprintf("%s: getBuildBranchIdentifiers error", GetCaller()), "error", err)
return err
}
} else {
buildInfo, berr := lc.getBuildInfo()
if berr != nil {
logger.Errorw(fmt.Sprintf("%s: getBuildInfo error", GetCaller()), "error", berr, "data", buildInfo)
return berr
}
logger.Debugw("Build info", "buildInfo", buildInfo)
projectID, branchID = buildInfo.Projects[0].ID, buildInfo.Branches[0].ID
}
branchInfo, err := lc.getBranchInfo(projectID, branchID)
if err != nil {
logger.Errorw(fmt.Sprintf("%s: getBranchInfo error", GetCaller()), "error", err, "data", branchInfo)
return err
}
logger.Debugw("Branch info", "branchInfo", branchInfo)
launchArgs, err := lc.getLaunchArgs(projectID)
if err != nil {
logger.Errorw(fmt.Sprintf("%s: getLaunchArgs error", GetCaller()), "error", err, "data", launchArgs)
return err
}
exArgs := launchArgs.extractLaunchArgs(branchInfo.LaunchinfoList[0], cfg.Core.Language)
logger.Debugw("Launch args", "launchArgs", launchArgs)
logger.Debugw("Extracted launch args", "exArgs", exArgs)
gameCode, err := lc.getGameCode(projectID)
if err != nil {
logger.Errorw(fmt.Sprintf("%s: getGameCode error", GetCaller()), "error", err, "data", gameCode)
return err
}
logger.Debugw("Game code", "gameCode.GameCode", gameCode.Gamecode)
baseArgs := strings.Replace(exArgs, gameCodeTempl, gameCode.Gamecode, -1)
return runQC(cfg, baseArgs)
}
func Exit(code int) {
Lock.Unlock()
os.Exit(code)
}
func (r *LaunchArgsResponse) extractLaunchArgs(liKey int, language string) string {
fallback := strings.Replace(defArgs, langTempl, defLang, -1)
if r == nil {
logger.Info("extractLaunchArgs: launch args was nil, using fallback arguments")
return fallback
}
if (LaunchInfo{}) == r.LaunchinfoSet {
logger.Info("extractLaunchArgs: launch info set had default struct values, using fallback arguments")
return fallback
}
if (LaunchInfoItem{}) == r.LaunchinfoSet.Default {
logger.Info("extractLaunchArgs: default launch info item had default struct values, using fallback arguments")
return fallback
}
likstr := strconv.Itoa(liKey)
val := reflect.ValueOf(r.LaunchinfoSet)
found := false
for i := 0; i < val.Type().NumField(); i++ {
v := val.Type().Field(i).Tag.Get("json")
if v != likstr {
continue
}
found = true
}
if found {
v := r.LaunchinfoSet.Default.LaunchArgs
v = strings.Replace(v, "\\", "", -1)
v = strings.Replace(v, langTempl, language, -1)
return v
}
logger.Infof("extractLaunchArgs: launch info key (%d) passed in had no match, using fallback arguments", liKey)
return fallback
}
func formatUnexpectedResponse(event string) error {
// Formatting for general errors that occur during launcher client actions (displayed in msg box)
return fmt.Errorf("Received an unexpected response when %s", event)
}
func buildArgs(cfg *Configuration, baseArgs string) string {
largs := []string{baseArgs}
if ConfAppendCustomArgs != "" {
largs = append(largs, ConfAppendCustomArgs)
}
// Keep support for cmd-line (shortcut) Max FPS argument for backwards compatibility w/ previous version
// If the "maxfps" cmd-line argument is specified, it takes precedence over the value configured in the UI
if ConfMaxFPS != 0 {
largs = append(largs, fmt.Sprintf("--set /Config/CONFIG/maxFpsValue %d", ConfMaxFPS))
} else if cfg.Experimental.UseMaxFPSLimit {
largs = append(largs, fmt.Sprintf("--set /Config/CONFIG/maxFpsValue %d", cfg.Experimental.MaxFPSLimit))
}
if cfg.Experimental.UseMaxFPSLimitMinimized {
largs = append(largs, fmt.Sprintf("--set /Config/CONFIG/maxFpsValueMinimized %d", cfg.Experimental.MaxFPSLimitMinimized))
}
if cfg.Experimental.UseFPSSmoothing {
largs = append(largs, fmt.Sprintf("--set /Config/CONFIG/enableFpsSmooth 1"))
}
return strings.Join(largs, " ")
}
func runQC(cfg *Configuration, baseArgs string) error {
qc := exec.Command(cfg.Core.FilePath)
qc.Dir = filepath.Dir(cfg.Core.FilePath)
a := buildArgs(cfg, baseArgs)
logger.Debugf("Final arguments: %s", a)
// Handle arg quote-escaping manually (see golang issue #15566)
qc.SysProcAttr = &syscall.SysProcAttr{
HideWindow: false,
CmdLine: fmt.Sprintf(` %s`, a),
CreationFlags: 0,
}
logger.Debug("Launching....")
if err := qc.Start(); err != nil {
logger.Errorw(fmt.Sprintf("%s: error starting QC", GetCaller()), "error", err)
return err
}
handlePostLaunch(cfg)
return nil
}
func getProjectBranchBuildIdentifiers(r *EntitlementInfoResponse) (projectID int, buildID int, branchID int, err error) {
for _, v := range r.Branches {
if v.Project == qcProjectID && strings.EqualFold(v.Name, qcDefaultBranchIdentifier) {
return v.Project, v.ID, v.Build, nil
}
}
return 0, 0, 0, fmt.Errorf("QC build/branch identifiers were not found")
}
func handlePostLaunch(cfg *Configuration) {
if cfg.Launcher.ExitOnLaunch {
exitFromUI()
return
} else if cfg.Launcher.MinimizeOnLaunch {
if cfg.Launcher.MinimizeToTray && qclauncherMainWindow.TrayIcon.Visible() {
// Already minimized to tray and being launched from tray context menu item; do nothing
return
}
qclauncherMainWindow.minimize(true)
}
}