-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.cpp
More file actions
412 lines (397 loc) · 12.4 KB
/
Config.cpp
File metadata and controls
412 lines (397 loc) · 12.4 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include "Config.h"
#include "Logger.h"
#include <boost/process/v1/search_path.hpp>
#include <fstream>
#include <nlohmann/json.hpp>
#include <stdexcept>
#include <string_view>
namespace
{
// return value for the given key under the given JSON object if it exists, or a
// default value otherwise
// NOTE: assumes that T has a copy constructor, plus a default constructor if no
// default value is provided
template<typename T>
T GetOptionalValue(
const nlohmann::json& j, std::string_view key, const T& defaultValue = {})
{
if (j.contains(key)) { return j.at(key).template get<T>(); }
return defaultValue;
}
// assign to the given object the value for the given key under the given JSON
// object if the key exists, or assign the default value otherwise
// NOTE: assumes that T has an assignment operator, plus a default constructor
// if no default value is provided
template<typename T>
void GetOptionalValueTo(
T& dest
, const nlohmann::json& j
, std::string_view key
, const T& defaultValue = {})
{
if (j.contains(key))
{
j.at(key).get_to(dest);
}
else
{
dest = defaultValue;
}
}
// populate given parameter map with config settings under given JSON tree
// NOTES:
// - this is called recursively to walk the tree
// - tree structure is flattened by concatenating successive levels' key names
// to `path`, using `.` as a separator
// - it is expected that the JSON node that represents the initial `path`
// starting point will be passed in, and `path` will be used as its name in
// place of its actual key in order to support a custom parameter prefix
void GetParametersTo(
rustLaunchSite::Logger& logger
, rustLaunchSite::Config::ParameterMapType& pMap
, const nlohmann::json& j
, const std::string& path
)
{
// iterate over all items under j
for (const auto& [key, value] : j.items())
{
// add item's name to current level starting path to get its full path
const std::string& itemPath{path + key};
// store data values in map, or recruse into objects
using enum nlohmann::json::value_t;
switch (value.type())
{
case boolean:
{
pMap.try_emplace(itemPath, value.template get<bool>());
}
break;
case number_float:
{
pMap.try_emplace(itemPath, value.template get<double>());
}
break;
case number_integer:
case number_unsigned:
{
pMap.try_emplace(itemPath, value.template get<int>());
}
break;
case object:
{
GetParametersTo(logger, pMap, value, itemPath + ".");
}
break;
case string:
{
pMap.try_emplace(itemPath, value.template get<std::string>());
}
break;
default:
{
LOGWRN(logger, "Ignoring JSON itemPath='" << itemPath << "' with unsupported type");
}
}
}
}
}
namespace rustLaunchSite
{
Config::Config(Logger& logger, std::filesystem::path configFile)
: logger_{logger}
{
configFile.make_preferred();
LOGINF(logger_, "Loading config file: " << configFile);
// attempt to parse configFile via nlohmann/json
nlohmann::json j{};
try
{
j = nlohmann::json::parse(std::ifstream{configFile}, nullptr, true, true);
}
catch (const nlohmann::json::parse_error& e)
{
std::stringstream s;
s << e.byte;
throw std::invalid_argument(
std::string("JSON parsing exception at byte ") + s.str()
+ " of config file '" + configFile.string() + "': " + e.what()
);
}
catch (const nlohmann::json::exception& e)
{
throw std::invalid_argument(
std::string("JSON general exception while parsing config file '")
+ configFile.string() + "': " + e.what()
);
}
catch (const std::exception& e)
{
throw std::invalid_argument(
std::string("C++ general exception while parsing config file '")
+ configFile.string() + "': " + e.what()
);
}
catch (...)
{
throw std::invalid_argument(
std::string("Unknown exception while parsing config file '")
+ configFile.string() + "'"
);
}
// at this point the parse has succeeded
// grab and validate settings
try
{
// *** rustLaunchSite settings ***
const auto& jRls{j.at("rustLaunchSite")};
// just look up required settings
// if they don't exist, an exception will be thrown
// install
const auto& jRlsInstall{jRls.at("install")};
jRlsInstall.at("path").get_to(installPath_);
installPath_.make_preferred();
jRlsInstall.at("identity").get_to(installIdentity_);
// process
if (jRls.contains("process"))
{
const auto& jRlsProcess{jRls.at("process")};
GetOptionalValueTo(processAutoRestart_, jRlsProcess, "autoRestart");
if (jRlsProcess.contains("reasonPath"))
{
jRlsProcess.at("reasonPath").get_to(processReasonPath_);
processReasonPath_.make_preferred();
}
// default optional integer to zero
GetOptionalValueTo(
processShutdownDelaySeconds_, jRlsProcess, "shutdownDelaySeconds");
// collapse other possible "disable" values to zero
if (processShutdownDelaySeconds_ < 0)
{
processShutdownDelaySeconds_ = 0;
}
}
// rcon
const auto& jRlsRcon{jRls.at("rcon")};
jRlsRcon.at("password").get_to(rconPassword_);
jRlsRcon.at("ip").get_to(rconIP_);
jRlsRcon.at("port").get_to(rconPort_);
if (jRlsRcon.contains("passthrough"))
{
const auto& jRlsRconPassthrough{jRlsRcon.at("passthrough")};
GetOptionalValueTo(rconPassthroughIP_, jRlsRconPassthrough, "ip");
GetOptionalValueTo(rconPassthroughPort_, jRlsRconPassthrough, "port");
}
GetOptionalValueTo(rconLog_, jRlsRcon, "log");
// seed
if (jRls.contains("seed"))
{
const auto& jRlsSeed{jRls.at("seed")};
// string that needs to be converted to an enum
seedStrategy_ = SeedStrategy::RANDOM;
if (const auto& seedStrategy{
GetOptionalValue<std::string>(jRlsSeed, "strategy")};
seedStrategy == "fixed")
{
seedStrategy_ = SeedStrategy::FIXED;
}
else if (seedStrategy == "list")
{
seedStrategy_ = SeedStrategy::LIST;
}
else if (seedStrategy == "random")
{
seedStrategy_ = SeedStrategy::RANDOM;
}
else if (!seedStrategy.empty())
{
throw std::invalid_argument(
std::string("Invalid rustLaunchSite.seed.strategy value: ")
+ seedStrategy
);
}
// supplemental settings may be required depending on seed strategy
switch (seedStrategy_)
{
case SeedStrategy::FIXED:
{
jRlsSeed.at("fixed").get_to(seedFixed_);
}
break;
case SeedStrategy::LIST:
{
jRlsSeed.at("list").get_to(seedList_);
if (seedList_.empty())
{
throw std::invalid_argument(
"Invalid rustLaunchSite.seed.list array");
}
}
break;
case SeedStrategy::RANDOM:
{
// no supplemental settings
}
break;
}
}
// steamcmd
// prefer configured value if present
if (jRls.contains("steamcmd"))
{
const auto& jRlsSteamcmd{jRls.at("steamcmd")};
if (jRlsSteamcmd.contains("path"))
{
jRlsSteamcmd.at("path").get_to(steamcmdPath_);
if (!std::filesystem::exists(steamcmdPath_))
{
LOGWRN(logger_, "SteamCMD not found at configured path " << steamcmdPath_ << "; will attempt to get from environment");
}
}
}
// fall back to environment search (e.g. PATH)
if (!std::filesystem::exists(steamcmdPath_))
{
steamcmdPath_ =
boost::process::v1::search_path("steamcmd").generic_wstring();
}
steamcmdPath_.make_preferred();
if (std::filesystem::exists(steamcmdPath_))
{
LOGINF(logger_, "Using SteamCMD at path: " << steamcmdPath_);
}
else
{
LOGWRN(logger_, "SteamCMD not found; dependent features may not work");
}
// update
if (jRls.contains("update"))
{
const auto& jRlsUpdate{jRls.at("update")};
// server
if (jRlsUpdate.contains("server"))
{
const auto& jRlsUpdateServer{jRlsUpdate.at("server")};
GetOptionalValueTo(
updateServerOnInterval_, jRlsUpdateServer, "onInterval");
GetOptionalValueTo(
updateServerOnRelaunch_, jRlsUpdateServer, "onRelaunch");
GetOptionalValueTo(
updateServerOnStartup_, jRlsUpdateServer, "onStartup");
GetOptionalValueTo(
updateServerRetryDelaySeconds_, jRlsUpdateServer,
"updateServerRetryDelaySeconds"
);
if (updateServerRetryDelaySeconds_ < 0)
{
updateServerRetryDelaySeconds_ = 0;
}
}
// modFramework
if (jRlsUpdate.contains("modFramework"))
{
const auto& jRlsUpdateModFramework{jRlsUpdate.at("modFramework")};
// process type first, because we want to force other values to false if
// it does not resolve to a valid value
if (const auto& modFrameworkType{GetOptionalValue<std::string>(
jRlsUpdateModFramework, "type")};
"carbon" == modFrameworkType)
{
updateModFrameworkType_ = ModFrameworkType::CARBON;
}
else if ("oxide" == modFrameworkType)
{
updateModFrameworkType_ = ModFrameworkType::OXIDE;
}
else if (!modFrameworkType.empty())
{
LOGWRN(logger_, "Ignoring unsupported modFramework.type value: '" << modFrameworkType << "'");
}
if (updateModFrameworkType_ != ModFrameworkType::NONE)
{
GetOptionalValueTo(updateModFrameworkOnInterval_,
jRlsUpdateModFramework, "onInterval");
GetOptionalValueTo(updateModFrameworkOnRelaunch_,
jRlsUpdateModFramework, "onRelaunch");
GetOptionalValueTo(updateModFrameworkOnServerUpdate_,
jRlsUpdateModFramework, "onServerUpdate");
GetOptionalValueTo(updateModFrameworkOnStartup_,
jRlsUpdateModFramework, "onStartup");
GetOptionalValueTo(
updateModFrameworkRetryDelaySeconds_, jRlsUpdateModFramework,
"updateModFrameworkRetryDelaySeconds"
);
if (updateModFrameworkRetryDelaySeconds_ < 0)
{
updateModFrameworkRetryDelaySeconds_ = 0;
}
}
}
GetOptionalValueTo(updateIntervalMinutes_, jRlsUpdate, "intervalMinutes");
// enforce validity & consistency here, to simplify dependent logic
if (updateIntervalMinutes_ < 0)
{
updateIntervalMinutes_ = 0;
}
else if (updateIntervalMinutes_
&& !updateServerOnInterval_ && !updateModFrameworkOnInterval_)
{
LOGWRN(logger_, "Ignoring update.intervalMinutes value because update.server and update.modFramework onInterval are both false: '" << updateIntervalMinutes_ << "'");
updateIntervalMinutes_ = 0;
}
if (!updateIntervalMinutes_)
{
if (updateServerOnInterval_)
{
LOGWRN(logger_, "Ignoring update.server.onInterval=true because update.intervalMinutes=0");
updateServerOnInterval_ = false;
}
if (updateModFrameworkOnInterval_)
{
LOGWRN(logger_, "Ignoring update.modFramework.onInterval=true because update.intervalMinutes=0");
updateModFrameworkOnInterval_ = false;
}
}
}
// wipe
if (jRls.contains("wipe"))
{
const auto& jRlsWipe{jRls.at("wipe")};
GetOptionalValueTo(wipeOnProtocolChange_, jRlsWipe, "onProtocolChange");
GetOptionalValueTo(wipeBlueprints_, jRlsWipe, "blueprints");
}
// *** rustDedicated settings ***
if (j.contains("rustDedicated"))
{
const auto& jRd{j.at("rustDedicated")};
if (jRd.contains("minusParams"))
{
GetParametersTo(logger, minusParams_, jRd.at("minusParams"), "-");
}
if (jRd.contains("plusParams"))
{
GetParametersTo(logger, plusParams_, jRd.at("plusParams"), "+");
}
}
}
catch (const nlohmann::json::exception& e)
{
throw std::invalid_argument(
std::string("JSON general exception while processing config data: ")
+ e.what()
);
}
catch (const std::exception& e)
{
throw std::invalid_argument(
std::string("C++ general exception while processing config data: ")
+ e.what()
);
}
catch (...)
{
throw std::invalid_argument(
"Unknown exception while processing config data");
}
}
}