From 17000766feb8dc328a391ab8f84ebc9a59f059c4 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Tue, 15 Apr 2025 18:48:56 -0700 Subject: [PATCH] Add ability to easily unset CVars from the command line --- Runtime/ConsoleVariables/CVarManager.cpp | 32 ++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/Runtime/ConsoleVariables/CVarManager.cpp b/Runtime/ConsoleVariables/CVarManager.cpp index 750e9b505..2b794cfe7 100644 --- a/Runtime/ConsoleVariables/CVarManager.cpp +++ b/Runtime/ConsoleVariables/CVarManager.cpp @@ -28,7 +28,9 @@ CVar* com_configfile = nullptr; CVar* com_enableCheats = nullptr; CVar* com_cubemaps = nullptr; -static const std::regex cmdLineRegex(R"(\+([\w\.]+)([=])?([\/\\\s\w\.\-]+)?)"); +static const std::regex cmdLineRegexEnable(R"(^\+([\w\.]+)([=])?([\/\\\s\w\.\-]+)?)"); +static const std::regex cmdLineRegexDisable(R"(^\-([\w\.]+)([=])?([\/\\\s\w\.\-]+)?)"); + CVarManager* CVarManager::m_instance = nullptr; CVarManager::CVarManager(FileStoreManager& store, bool useBinary) : m_store(store), m_useBinary(useBinary) { @@ -270,15 +272,15 @@ void CVarManager::parseCommandLine(const std::vector& args) { std::string developerName(com_developer->name()); CStringExtras::ToLower(developerName); for (const std::string& arg : args) { - if (arg[0] != '+') { + if (arg[0] != '+' && arg[0] != '-') { continue; } std::smatch matches; std::string cvarName; std::string cvarValue; - - if (std::regex_match(arg, matches, cmdLineRegex)) { + bool set = false; + if (std::regex_match(arg, matches, cmdLineRegexEnable)) { std::vector realMatches; for (auto match : matches) { if (match.matched) { @@ -291,26 +293,42 @@ void CVarManager::parseCommandLine(const std::vector& args) { cvarName = matches[1].str(); cvarValue = matches[3].str(); } + set = true; + } else if (std::regex_match(arg, matches, cmdLineRegexDisable)) { + std::vector realMatches; + for (auto match : matches) { + if (match.matched) { + realMatches.push_back(match); + } + } + if (realMatches.size() == 2) { + cvarName = matches[1].str(); + } else if (realMatches.size() == 4) { + cvarName = matches[1].str(); + cvarValue = matches[3].str(); + } + set = false; } if (CVar* cv = findCVar(cvarName)) { if (cvarValue.empty() && cv->isBoolean()) { // We were set from the command line with an empty value, assume true - cv->fromBoolean(true); + cv->fromBoolean(set); } else if (!cvarValue.empty()) { cv->fromLiteralToType(cvarValue); } cv->m_wasDeserialized = true; cv->forceClearModified(); CStringExtras::ToLower(cvarName); - if (developerName == cvarName) + if (developerName == cvarName) { /* Make sure we're not overriding developer mode when we restore */ oldDeveloper = com_developer->toBoolean(); + } } else { /* Unable to find an existing CVar, let's defer for the time being 8 */ CStringExtras::ToLower(cvarName); if (cvarValue.empty()) { - cvarValue = "true"; + cvarValue = set ? "true" : "false"; } m_deferedCVars.insert(std::make_pair(std::move(cvarName), std::move(cvarValue))); }