-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.go
169 lines (136 loc) · 3.86 KB
/
config.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
package main
import (
"errors"
"fmt"
"os"
"path"
"strconv"
"strings"
"github.com/andygrunwald/vdf"
"github.com/leighmacdonald/bd/platform"
"github.com/leighmacdonald/steamid/v4/steamid"
)
func getLocalConfigPath(steamRoot string, steamID steamid.SteamID) (string, error) {
if !steamID.Valid() { //nolint:nestif
userDataRoot := path.Join(steamRoot, "userdata")
// Attempt to use the id found in the userdata if only one exists
entries, err := os.ReadDir(userDataRoot)
if err != nil {
return "", errors.Join(err, errSteamUserData)
}
dirCount := 0
// List the userdata folder to find potential steamid. If there is more than one steamid looking
// directory, then error out as we don't know which is the correct.
for _, entry := range entries {
if entry.IsDir() {
sidInt, errParse := strconv.ParseInt(entry.Name(), 10, 32)
if errParse != nil {
continue
}
maybeSteamID := steamid.New(sidInt)
if maybeSteamID.Valid() {
steamID = maybeSteamID
}
dirCount++
if dirCount == 2 {
return "", errors.Join(err, errSteamUserDataGuess)
}
}
}
}
configPath := path.Join(steamRoot, "userdata", fmt.Sprintf("%d", steamID.AccountID), "config", "localconfig.vdf")
if !platform.Exists(configPath) {
return "", errPathNotExist
}
return configPath, nil
}
func getUserLaunchArgs(steamRoot string, steamID steamid.SteamID) ([]string, error) {
localConfigPath, errConfigPath := getLocalConfigPath(steamRoot, steamID)
if errConfigPath != nil {
return nil, errors.Join(errConfigPath, errSteamLocalConfig)
}
openVDF, errOpen := os.Open(localConfigPath)
if errOpen != nil {
return nil, errors.Join(errOpen, platform.ErrVDFOpen)
}
newParser := vdf.NewParser(openVDF)
result, errParse := newParser.Parse()
if errParse != nil {
return nil, errors.Join(errOpen, platform.ErrVDFParse)
}
var (
castOk bool
found bool
launchOpts []string
pathKeys = []string{"UserLocalConfigStore", "Software", "Valve", "sTeam", "apps", "440"}
)
for index, key := range pathKeys {
// Find a matching existing key using case-insensitive match since casing can vary
csKey := key
for k := range result {
if strings.EqualFold(k, key) {
csKey = k
break
}
}
result, castOk = result[csKey].(map[string]any)
if !castOk {
return nil, errors.Join(errOpen, fmt.Errorf("%w: %s", platform.ErrVDFKey, key))
}
if index == len(pathKeys)-1 {
launchStr, launchStrOk := result["LaunchOptions"].(string)
if !launchStrOk {
return nil, fmt.Errorf("%w: %s", platform.ErrVDFValue, "LaunchOptions")
}
launchOpts = strings.Split(launchStr, " ")
found = true
}
}
if !found {
return nil, errGetLaunchOptions
}
return launchOpts, nil
}
func getLaunchArgs(rconPass string, rconPort uint16, steamRoot string, steamID steamid.SteamID) ([]string, error) {
userArgs, errUserArgs := getUserLaunchArgs(steamRoot, steamID)
if errUserArgs != nil {
return nil, errors.Join(errUserArgs, errSteamLaunchArgs)
}
bdArgs := []string{
"-game", "tf",
// "-noreactlogin", // needed for vac to load as of late 2022?
"-steam",
"-secure",
"-usercon",
"+ip", "0.0.0.0",
"+sv_rcon_whitelist_address", "127.0.0.1",
"+sv_quota_stringcmdspersecond", "1000000",
"+rcon_password", rconPass,
"+hostport", fmt.Sprintf("%d", rconPort),
"+net_start",
"+con_timestamp", "1",
"-rpt", // Same as having -condebug, -conclearlog, and -console enabled
"-g15",
}
var full []string //nolint:prealloc
for _, arg := range append(bdArgs, userArgs...) {
arg = strings.Trim(arg, " ")
if !strings.HasSuffix(arg, "-") || strings.HasPrefix(arg, "+") {
full = append(full, arg)
continue
}
alreadyKnown := false
for _, known := range full {
if known == arg {
// duplicate arg
alreadyKnown = true
break
}
}
if alreadyKnown {
continue
}
full = append(full, arg)
}
return full, nil
}