Merge pull request #9 from lioncash/cvar

CVarManager: Minor changes
This commit is contained in:
Phillip Stephens 2019-08-20 06:56:24 -07:00 committed by GitHub
commit 138ab07089
1 changed files with 57 additions and 47 deletions

View File

@ -4,6 +4,7 @@
#include <athena/Utility.hpp> #include <athena/Utility.hpp>
#include <hecl/Runtime.hpp> #include <hecl/Runtime.hpp>
#include <hecl/hecl.hpp> #include <hecl/hecl.hpp>
#include <algorithm>
#include <memory> #include <memory>
#include <regex> #include <regex>
@ -24,11 +25,10 @@ CVarManager::CVarManager(hecl::Runtime::FileStoreManager& store, bool useBinary)
com_configfile = newCVar("config", "File to store configuration", std::string("config"), CVar::EFlags::System); com_configfile = newCVar("config", "File to store configuration", std::string("config"), CVar::EFlags::System);
com_developer = newCVar("developer", "Enables developer mode", false, com_developer = newCVar("developer", "Enables developer mode", false,
(CVar::EFlags::System | CVar::EFlags::ReadOnly | CVar::EFlags::InternalArchivable)); (CVar::EFlags::System | CVar::EFlags::ReadOnly | CVar::EFlags::InternalArchivable));
com_enableCheats = com_enableCheats = newCVar(
newCVar("cheats", "Enable cheats", false, "cheats", "Enable cheats", false,
(CVar::EFlags::System | CVar::EFlags::ReadOnly | CVar::EFlags::Hidden | CVar::EFlags::InternalArchivable)); (CVar::EFlags::System | CVar::EFlags::ReadOnly | CVar::EFlags::Hidden | CVar::EFlags::InternalArchivable));
com_cubemaps = com_cubemaps = newCVar("cubemaps", "Enable cubemaps", false,
newCVar("cubemaps", "Enable cubemaps", false,
(CVar::EFlags::Game | CVar::EFlags::ReadOnly | CVar::EFlags::InternalArchivable)); (CVar::EFlags::Game | CVar::EFlags::ReadOnly | CVar::EFlags::InternalArchivable));
} }
@ -37,11 +37,13 @@ CVarManager::~CVarManager() {}
CVar* CVarManager::registerCVar(std::unique_ptr<CVar>&& cvar) { CVar* CVarManager::registerCVar(std::unique_ptr<CVar>&& cvar) {
std::string tmp(cvar->name()); std::string tmp(cvar->name());
athena::utility::tolower(tmp); athena::utility::tolower(tmp);
if (m_cvars.find(tmp) != m_cvars.end())
if (m_cvars.find(tmp) != m_cvars.end()) {
return nullptr; return nullptr;
}
CVar* ret = cvar.get(); CVar* ret = cvar.get();
m_cvars[tmp] = std::move(cvar); m_cvars.insert_or_assign(std::move(tmp), std::move(cvar));
return ret; return ret;
} }
@ -80,8 +82,8 @@ void CVarManager::deserialize(CVar* cvar) {
/* First let's check for a deferred value */ /* First let's check for a deferred value */
std::string lowName = cvar->name().data(); std::string lowName = cvar->name().data();
athena::utility::tolower(lowName); athena::utility::tolower(lowName);
if (m_deferedCVars.find(lowName) != m_deferedCVars.end()) { if (const auto iter = m_deferedCVars.find(lowName); iter != m_deferedCVars.end()) {
std::string val = m_deferedCVars[lowName]; std::string val = std::move(iter->second);
m_deferedCVars.erase(lowName); m_deferedCVars.erase(lowName);
if (cvar->fromLiteralToType(val)) if (cvar->fromLiteralToType(val))
return; return;
@ -162,10 +164,13 @@ void CVarManager::serialize() {
if (m_useBinary) { if (m_useBinary) {
CVarContainer container; CVarContainer container;
for (const auto& pair : m_cvars) for (const auto& pair : m_cvars) {
if (pair.second->isArchive() || const auto& cvar = pair.second;
(pair.second->isInternalArchivable() && pair.second->wasDeserialized() && !pair.second->hasDefaultValue()))
container.cvars.push_back(*pair.second); if (cvar->isArchive() || (cvar->isInternalArchivable() && cvar->wasDeserialized() && !cvar->hasDefaultValue())) {
container.cvars.push_back(*cvar);
}
}
container.cvarCount = atUint32(container.cvars.size()); container.cvarCount = atUint32(container.cvars.size());
filename += _SYS_STR(".bin"); filename += _SYS_STR(".bin");
@ -180,10 +185,13 @@ void CVarManager::serialize() {
r.close(); r.close();
docWriter.setStyle(athena::io::YAMLNodeStyle::Block); docWriter.setStyle(athena::io::YAMLNodeStyle::Block);
for (const auto& pair : m_cvars) for (const auto& pair : m_cvars) {
if (pair.second->isArchive() || const auto& cvar = pair.second;
(pair.second->isInternalArchivable() && pair.second->wasDeserialized() && !pair.second->hasDefaultValue()))
docWriter.writeString(pair.second->name().data(), pair.second->toLiteral()); if (cvar->isArchive() || (cvar->isInternalArchivable() && cvar->wasDeserialized() && !cvar->hasDefaultValue())) {
docWriter.writeString(cvar->name().data(), cvar->toLiteral());
}
}
athena::io::FileWriter w(filename); athena::io::FileWriter w(filename);
if (w.isOpen()) if (w.isOpen())
@ -208,12 +216,13 @@ void CVarManager::setCVar(Console* con, const std::vector<std::string>& args) {
std::string cvName = args[0]; std::string cvName = args[0];
athena::utility::tolower(cvName); athena::utility::tolower(cvName);
if (m_cvars.find(cvName) == m_cvars.end()) { const auto iter = m_cvars.find(cvName);
if (iter == m_cvars.end()) {
con->report(Console::Level::Error, fmt("CVar '{}' does not exist"), args[0]); con->report(Console::Level::Error, fmt("CVar '{}' does not exist"), args[0]);
return; return;
} }
const auto& cv = m_cvars[cvName]; const auto& cv = iter->second;
std::string oldVal = cv->value(); std::string oldVal = cv->value();
std::string value = args[1]; std::string value = args[1];
auto it = args.begin() + 2; auto it = args.begin() + 2;
@ -238,12 +247,13 @@ void CVarManager::getCVar(Console* con, const std::vector<std::string>& args) {
std::string cvName = args[0]; std::string cvName = args[0];
athena::utility::tolower(cvName); athena::utility::tolower(cvName);
if (m_cvars.find(cvName) == m_cvars.end()) { const auto iter = m_cvars.find(cvName);
if (iter == m_cvars.end()) {
con->report(Console::Level::Error, fmt("CVar '{}' does not exist"), args[0]); con->report(Console::Level::Error, fmt("CVar '{}' does not exist"), args[0]);
return; return;
} }
const auto& cv = m_cvars[cvName]; const auto& cv = iter->second;
con->report(Console::Level::Info, fmt("'{}' = '{}'"), cv->name(), cv->value()); con->report(Console::Level::Info, fmt("'{}' = '{}'"), cv->name(), cv->value());
} }
@ -266,24 +276,26 @@ void CVarManager::setCheatsEnabled(bool v, bool setDeserialized) {
} }
bool CVarManager::restartRequired() const { bool CVarManager::restartRequired() const {
for (const auto& cv : m_cvars) { return std::any_of(m_cvars.cbegin(), m_cvars.cend(), [](const auto& entry) {
if (cv.second->isModified() && cv.second->modificationRequiresRestart()) return entry.second->isModified() && entry.second->modificationRequiresRestart();
return true; });
}
return false;
} }
void CVarManager::parseCommandLine(const std::vector<SystemString>& args) { void CVarManager::parseCommandLine(const std::vector<SystemString>& args) {
bool oldDeveloper = suppressDeveloper(); bool oldDeveloper = suppressDeveloper();
std::string developerName = com_developer->name().data(); std::string developerName(com_developer->name());
athena::utility::tolower(developerName); athena::utility::tolower(developerName);
for (const SystemString& arg : args) { for (const SystemString& arg : args) {
if (arg[0] == _SYS_STR('+')) { if (arg[0] != _SYS_STR('+')) {
std::string tmp = SystemUTF8Conv(arg).c_str(); continue;
}
const std::string tmp(SystemUTF8Conv(arg).str());
std::smatch matches; std::smatch matches;
if (std::regex_match(tmp, matches, cmdLineRegex)) { if (!std::regex_match(tmp, matches, cmdLineRegex)) {
continue;
}
std::string cvarName = matches[1].str(); std::string cvarName = matches[1].str();
std::string cvarValue = matches[2].str(); std::string cvarValue = matches[2].str();
if (CVar* cv = findCVar(cvarName)) { if (CVar* cv = findCVar(cvarName)) {
@ -295,9 +307,7 @@ void CVarManager::parseCommandLine(const std::vector<SystemString>& args) {
} else { } else {
/* Unable to find an existing CVar, let's defer for the time being 8 */ /* Unable to find an existing CVar, let's defer for the time being 8 */
athena::utility::tolower(cvarName); athena::utility::tolower(cvarName);
m_deferedCVars[cvarName] = cvarValue; m_deferedCVars.insert_or_assign(std::move(cvarName), std::move(cvarValue));
}
}
} }
} }