Skip to content

Commit 1700076

Browse files
committed
Add ability to easily unset CVars from the command line
1 parent e72b05a commit 1700076

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

Runtime/ConsoleVariables/CVarManager.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ CVar* com_configfile = nullptr;
2828
CVar* com_enableCheats = nullptr;
2929
CVar* com_cubemaps = nullptr;
3030

31-
static const std::regex cmdLineRegex(R"(\+([\w\.]+)([=])?([\/\\\s\w\.\-]+)?)");
31+
static const std::regex cmdLineRegexEnable(R"(^\+([\w\.]+)([=])?([\/\\\s\w\.\-]+)?)");
32+
static const std::regex cmdLineRegexDisable(R"(^\-([\w\.]+)([=])?([\/\\\s\w\.\-]+)?)");
33+
3234
CVarManager* CVarManager::m_instance = nullptr;
3335

3436
CVarManager::CVarManager(FileStoreManager& store, bool useBinary) : m_store(store), m_useBinary(useBinary) {
@@ -270,15 +272,15 @@ void CVarManager::parseCommandLine(const std::vector<std::string>& args) {
270272
std::string developerName(com_developer->name());
271273
CStringExtras::ToLower(developerName);
272274
for (const std::string& arg : args) {
273-
if (arg[0] != '+') {
275+
if (arg[0] != '+' && arg[0] != '-') {
274276
continue;
275277
}
276278

277279
std::smatch matches;
278280
std::string cvarName;
279281
std::string cvarValue;
280-
281-
if (std::regex_match(arg, matches, cmdLineRegex)) {
282+
bool set = false;
283+
if (std::regex_match(arg, matches, cmdLineRegexEnable)) {
282284
std::vector<std::string> realMatches;
283285
for (auto match : matches) {
284286
if (match.matched) {
@@ -291,26 +293,42 @@ void CVarManager::parseCommandLine(const std::vector<std::string>& args) {
291293
cvarName = matches[1].str();
292294
cvarValue = matches[3].str();
293295
}
296+
set = true;
297+
} else if (std::regex_match(arg, matches, cmdLineRegexDisable)) {
298+
std::vector<std::string> realMatches;
299+
for (auto match : matches) {
300+
if (match.matched) {
301+
realMatches.push_back(match);
302+
}
303+
}
304+
if (realMatches.size() == 2) {
305+
cvarName = matches[1].str();
306+
} else if (realMatches.size() == 4) {
307+
cvarName = matches[1].str();
308+
cvarValue = matches[3].str();
309+
}
310+
set = false;
294311
}
295312

296313
if (CVar* cv = findCVar(cvarName)) {
297314
if (cvarValue.empty() && cv->isBoolean()) {
298315
// We were set from the command line with an empty value, assume true
299-
cv->fromBoolean(true);
316+
cv->fromBoolean(set);
300317
} else if (!cvarValue.empty()) {
301318
cv->fromLiteralToType(cvarValue);
302319
}
303320
cv->m_wasDeserialized = true;
304321
cv->forceClearModified();
305322
CStringExtras::ToLower(cvarName);
306-
if (developerName == cvarName)
323+
if (developerName == cvarName) {
307324
/* Make sure we're not overriding developer mode when we restore */
308325
oldDeveloper = com_developer->toBoolean();
326+
}
309327
} else {
310328
/* Unable to find an existing CVar, let's defer for the time being 8 */
311329
CStringExtras::ToLower(cvarName);
312330
if (cvarValue.empty()) {
313-
cvarValue = "true";
331+
cvarValue = set ? "true" : "false";
314332
}
315333
m_deferedCVars.insert(std::make_pair<std::string, std::string>(std::move(cvarName), std::move(cvarValue)));
316334
}

0 commit comments

Comments
 (0)