2016-03-04 23:02:44 +00:00
|
|
|
#include "hecl/CVarManager.hpp"
|
2019-09-04 21:22:05 +00:00
|
|
|
|
2019-08-20 10:02:47 +00:00
|
|
|
#include <algorithm>
|
2015-12-02 21:11:10 +00:00
|
|
|
#include <memory>
|
2018-04-30 03:11:37 +00:00
|
|
|
#include <regex>
|
2015-12-02 21:11:10 +00:00
|
|
|
|
2019-09-04 21:22:05 +00:00
|
|
|
#include "hecl/Console.hpp"
|
|
|
|
#include "hecl/hecl.hpp"
|
|
|
|
#include "hecl/Runtime.hpp"
|
|
|
|
|
|
|
|
#include <athena/FileWriter.hpp>
|
|
|
|
#include <athena/Utility.hpp>
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
namespace hecl {
|
2015-12-02 21:11:10 +00:00
|
|
|
|
2018-01-22 02:40:11 +00:00
|
|
|
CVar* com_developer = nullptr;
|
|
|
|
CVar* com_configfile = nullptr;
|
|
|
|
CVar* com_enableCheats = nullptr;
|
2019-06-06 00:07:29 +00:00
|
|
|
CVar* com_cubemaps = nullptr;
|
2015-12-02 21:11:10 +00:00
|
|
|
|
2018-06-13 02:03:29 +00:00
|
|
|
static const std::regex cmdLineRegex("\\+([\\w\\.]+)=([\\w\\.\\-]+)");
|
2015-12-04 01:43:29 +00:00
|
|
|
CVarManager* CVarManager::m_instance = nullptr;
|
|
|
|
|
2018-01-14 07:37:00 +00:00
|
|
|
static logvisor::Module CVarLog("CVarManager");
|
2016-03-04 23:02:44 +00:00
|
|
|
CVarManager::CVarManager(hecl::Runtime::FileStoreManager& store, bool useBinary)
|
2018-12-08 05:18:42 +00:00
|
|
|
: m_store(store), m_useBinary(useBinary) {
|
|
|
|
m_instance = this;
|
2019-09-29 12:44:01 +00:00
|
|
|
com_configfile =
|
|
|
|
newCVar("config", "File to store configuration", std::string("config"),
|
|
|
|
CVar::EFlags::System | CVar::EFlags::ReadOnly | CVar::EFlags::NoDeveloper | CVar::EFlags::Hidden);
|
2018-12-08 05:18:42 +00:00
|
|
|
com_developer = newCVar("developer", "Enables developer mode", false,
|
|
|
|
(CVar::EFlags::System | CVar::EFlags::ReadOnly | CVar::EFlags::InternalArchivable));
|
2019-08-20 10:19:39 +00:00
|
|
|
com_enableCheats = newCVar(
|
|
|
|
"cheats", "Enable cheats", false,
|
|
|
|
(CVar::EFlags::System | CVar::EFlags::ReadOnly | CVar::EFlags::Hidden | CVar::EFlags::InternalArchivable));
|
|
|
|
com_cubemaps = newCVar("cubemaps", "Enable cubemaps", false,
|
|
|
|
(CVar::EFlags::Game | CVar::EFlags::ReadOnly | CVar::EFlags::InternalArchivable));
|
2015-12-02 21:11:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
CVarManager::~CVarManager() {}
|
2015-12-02 21:11:10 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
CVar* CVarManager::registerCVar(std::unique_ptr<CVar>&& cvar) {
|
|
|
|
std::string tmp(cvar->name());
|
|
|
|
athena::utility::tolower(tmp);
|
2019-08-20 09:57:56 +00:00
|
|
|
|
|
|
|
if (m_cvars.find(tmp) != m_cvars.end()) {
|
2018-12-08 05:18:42 +00:00
|
|
|
return nullptr;
|
2019-08-20 09:57:56 +00:00
|
|
|
}
|
2015-12-02 21:11:10 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
CVar* ret = cvar.get();
|
2019-08-20 09:57:56 +00:00
|
|
|
m_cvars.insert_or_assign(std::move(tmp), std::move(cvar));
|
2018-12-08 05:18:42 +00:00
|
|
|
return ret;
|
2015-12-02 21:11:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
CVar* CVarManager::findCVar(std::string_view name) {
|
|
|
|
std::string lower(name);
|
|
|
|
athena::utility::tolower(lower);
|
|
|
|
auto search = m_cvars.find(lower);
|
|
|
|
if (search == m_cvars.end())
|
|
|
|
return nullptr;
|
2015-12-02 21:11:10 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
return search->second.get();
|
2015-12-02 21:11:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
std::vector<CVar*> CVarManager::archivedCVars() const {
|
|
|
|
std::vector<CVar*> ret;
|
|
|
|
for (const auto& pair : m_cvars)
|
|
|
|
if (pair.second->isArchive())
|
|
|
|
ret.push_back(pair.second.get());
|
2015-12-02 21:11:10 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
return ret;
|
2015-12-02 21:11:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
std::vector<CVar*> CVarManager::cvars(CVar::EFlags filter) const {
|
|
|
|
std::vector<CVar*> ret;
|
|
|
|
for (const auto& pair : m_cvars)
|
2019-09-29 12:44:01 +00:00
|
|
|
if (filter == CVar::EFlags::Any || True(pair.second->flags() & filter))
|
2018-12-08 05:18:42 +00:00
|
|
|
ret.push_back(pair.second.get());
|
2015-12-02 21:11:10 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
return ret;
|
2015-12-02 21:11:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void CVarManager::deserialize(CVar* cvar) {
|
2019-09-29 12:44:01 +00:00
|
|
|
/* Make sure we're not trying to deserialize a CVar that is invalid or not exposed */
|
|
|
|
if (!cvar || (!cvar->isArchive() && !cvar->isInternalArchivable()))
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* First let's check for a deferred value */
|
|
|
|
std::string lowName = cvar->name().data();
|
|
|
|
athena::utility::tolower(lowName);
|
2019-08-20 10:19:39 +00:00
|
|
|
if (const auto iter = m_deferedCVars.find(lowName); iter != m_deferedCVars.end()) {
|
|
|
|
std::string val = std::move(iter->second);
|
2018-12-08 05:18:42 +00:00
|
|
|
m_deferedCVars.erase(lowName);
|
|
|
|
if (cvar->fromLiteralToType(val))
|
|
|
|
return;
|
|
|
|
}
|
2018-06-13 03:19:08 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
/* We were either unable to find a deferred value or got an invalid value */
|
2015-12-02 21:11:10 +00:00
|
|
|
#if _WIN32
|
2018-12-08 05:18:42 +00:00
|
|
|
hecl::SystemString filename =
|
|
|
|
hecl::SystemString(m_store.getStoreRoot()) + _SYS_STR('/') + com_configfile->toWideLiteral();
|
2015-12-02 21:11:10 +00:00
|
|
|
#else
|
2018-12-08 05:18:42 +00:00
|
|
|
hecl::SystemString filename =
|
|
|
|
hecl::SystemString(m_store.getStoreRoot()) + _SYS_STR('/') + com_configfile->toLiteral();
|
2015-12-02 21:11:10 +00:00
|
|
|
#endif
|
2018-12-08 05:18:42 +00:00
|
|
|
hecl::Sstat st;
|
|
|
|
|
|
|
|
if (m_useBinary) {
|
|
|
|
CVarContainer container;
|
|
|
|
filename += _SYS_STR(".bin");
|
|
|
|
if (hecl::Stat(filename.c_str(), &st) || !S_ISREG(st.st_mode))
|
|
|
|
return;
|
|
|
|
athena::io::FileReader reader(filename);
|
|
|
|
if (reader.isOpen())
|
|
|
|
container.read(reader);
|
|
|
|
|
|
|
|
if (container.cvars.size() > 0) {
|
|
|
|
auto serialized = std::find_if(container.cvars.begin(), container.cvars.end(),
|
|
|
|
[&cvar](const DNACVAR::CVar& c) { return c.m_name == cvar->name(); });
|
|
|
|
|
|
|
|
if (serialized != container.cvars.end()) {
|
|
|
|
DNACVAR::CVar& tmp = *serialized;
|
|
|
|
|
|
|
|
if (cvar->m_value != tmp.m_value) {
|
2019-09-29 12:44:01 +00:00
|
|
|
CVarUnlocker lc(cvar);
|
|
|
|
cvar->fromLiteralToType(tmp.m_value);
|
2018-12-08 05:18:42 +00:00
|
|
|
cvar->m_wasDeserialized = true;
|
2018-01-23 04:40:09 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
}
|
2018-01-22 02:40:11 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
} else {
|
|
|
|
filename += _SYS_STR(".yaml");
|
|
|
|
if (hecl::Stat(filename.c_str(), &st) || !S_ISREG(st.st_mode))
|
|
|
|
return;
|
|
|
|
athena::io::FileReader reader(filename);
|
|
|
|
if (reader.isOpen()) {
|
|
|
|
athena::io::YAMLDocReader docReader;
|
|
|
|
if (docReader.parse(&reader)) {
|
|
|
|
std::unique_ptr<athena::io::YAMLNode> root = docReader.releaseRootNode();
|
|
|
|
auto serialized = std::find_if(root->m_mapChildren.begin(), root->m_mapChildren.end(),
|
|
|
|
[&cvar](const auto& c) { return c.first == cvar->name(); });
|
|
|
|
|
|
|
|
if (serialized != root->m_mapChildren.end()) {
|
|
|
|
const std::unique_ptr<athena::io::YAMLNode>& tmp = serialized->second;
|
|
|
|
|
|
|
|
if (cvar->m_value != tmp->m_scalarString) {
|
2019-09-29 12:44:01 +00:00
|
|
|
CVarUnlocker lc(cvar);
|
|
|
|
cvar->fromLiteralToType(tmp->m_scalarString);
|
2018-12-08 05:18:42 +00:00
|
|
|
cvar->m_wasDeserialized = true;
|
|
|
|
}
|
2015-12-02 21:11:10 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
}
|
2015-12-02 21:11:10 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
}
|
2015-12-02 21:11:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void CVarManager::serialize() {
|
2015-12-02 21:11:10 +00:00
|
|
|
#if _WIN32
|
2018-12-08 05:18:42 +00:00
|
|
|
hecl::SystemString filename =
|
|
|
|
hecl::SystemString(m_store.getStoreRoot()) + _SYS_STR('/') + com_configfile->toWideLiteral();
|
2015-12-02 21:11:10 +00:00
|
|
|
#else
|
2018-12-08 05:18:42 +00:00
|
|
|
hecl::SystemString filename =
|
|
|
|
hecl::SystemString(m_store.getStoreRoot()) + _SYS_STR('/') + com_configfile->toLiteral();
|
2015-12-02 21:11:10 +00:00
|
|
|
#endif
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
if (m_useBinary) {
|
|
|
|
CVarContainer container;
|
2019-08-20 10:10:21 +00:00
|
|
|
for (const auto& pair : m_cvars) {
|
2019-08-20 10:29:14 +00:00
|
|
|
const auto& cvar = pair.second;
|
|
|
|
|
|
|
|
if (cvar->isArchive() || (cvar->isInternalArchivable() && cvar->wasDeserialized() && !cvar->hasDefaultValue())) {
|
|
|
|
container.cvars.push_back(*cvar);
|
2019-08-20 10:10:21 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
container.cvarCount = atUint32(container.cvars.size());
|
|
|
|
|
|
|
|
filename += _SYS_STR(".bin");
|
|
|
|
athena::io::FileWriter writer(filename);
|
|
|
|
if (writer.isOpen())
|
|
|
|
container.write(writer);
|
|
|
|
} else {
|
|
|
|
filename += _SYS_STR(".yaml");
|
|
|
|
|
|
|
|
athena::io::FileReader r(filename);
|
2019-10-01 07:23:35 +00:00
|
|
|
athena::io::YAMLDocWriter docWriter(r.isOpen() ? &r : nullptr);
|
2018-12-08 05:18:42 +00:00
|
|
|
r.close();
|
|
|
|
|
|
|
|
docWriter.setStyle(athena::io::YAMLNodeStyle::Block);
|
2019-08-20 10:10:21 +00:00
|
|
|
for (const auto& pair : m_cvars) {
|
2019-08-20 10:29:14 +00:00
|
|
|
const auto& cvar = pair.second;
|
|
|
|
|
|
|
|
if (cvar->isArchive() || (cvar->isInternalArchivable() && cvar->wasDeserialized() && !cvar->hasDefaultValue())) {
|
|
|
|
docWriter.writeString(cvar->name().data(), cvar->toLiteral());
|
2019-08-20 10:10:21 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
|
|
|
|
athena::io::FileWriter w(filename);
|
|
|
|
if (w.isOpen())
|
|
|
|
docWriter.finish(&w);
|
|
|
|
}
|
2015-12-02 21:11:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
CVarManager* CVarManager::instance() { return m_instance; }
|
2015-12-04 01:43:29 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void CVarManager::list(Console* con, const std::vector<std::string>& /*args*/) {
|
|
|
|
for (const auto& cvar : m_cvars) {
|
|
|
|
if (!cvar.second->isHidden())
|
2020-04-11 22:48:11 +00:00
|
|
|
con->report(Console::Level::Info, FMT_STRING("{}: {}"), cvar.second->name(), cvar.second->help());
|
2018-12-08 05:18:42 +00:00
|
|
|
}
|
2018-01-11 09:35:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void CVarManager::setCVar(Console* con, const std::vector<std::string>& args) {
|
|
|
|
if (args.size() < 2) {
|
2020-04-11 22:48:11 +00:00
|
|
|
con->report(Console::Level::Info, FMT_STRING("Usage setCvar <cvar> <value>"));
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cvName = args[0];
|
|
|
|
athena::utility::tolower(cvName);
|
2019-08-20 10:19:39 +00:00
|
|
|
const auto iter = m_cvars.find(cvName);
|
|
|
|
if (iter == m_cvars.end()) {
|
2020-04-11 22:48:11 +00:00
|
|
|
con->report(Console::Level::Error, FMT_STRING("CVar '{}' does not exist"), args[0]);
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-20 10:19:39 +00:00
|
|
|
const auto& cv = iter->second;
|
2018-12-08 05:18:42 +00:00
|
|
|
std::string oldVal = cv->value();
|
|
|
|
std::string value = args[1];
|
|
|
|
auto it = args.begin() + 2;
|
|
|
|
for (; it != args.end(); ++it)
|
|
|
|
value += " " + *it;
|
|
|
|
|
|
|
|
/* Check to make sure we're not redundantly assigning the value */
|
|
|
|
if (cv->value() == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!cv->fromLiteralToType(value))
|
2020-04-11 22:48:11 +00:00
|
|
|
con->report(Console::Level::Warning, FMT_STRING("Unable to set cvar '{}' to value '{}'"), cv->name(), value);
|
2018-12-08 05:18:42 +00:00
|
|
|
else
|
2020-04-11 22:48:11 +00:00
|
|
|
con->report(Console::Level::Info, FMT_STRING("Set '{}' from '{}' to '{}'"), cv->name(), oldVal, value);
|
2018-01-11 09:35:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void CVarManager::getCVar(Console* con, const std::vector<std::string>& args) {
|
|
|
|
if (args.empty()) {
|
2020-04-11 22:48:11 +00:00
|
|
|
con->report(Console::Level::Info, FMT_STRING("Usage getCVar <cvar>"));
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cvName = args[0];
|
|
|
|
athena::utility::tolower(cvName);
|
2019-08-20 10:19:39 +00:00
|
|
|
const auto iter = m_cvars.find(cvName);
|
|
|
|
if (iter == m_cvars.end()) {
|
2020-04-11 22:48:11 +00:00
|
|
|
con->report(Console::Level::Error, FMT_STRING("CVar '{}' does not exist"), args[0]);
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-20 10:19:39 +00:00
|
|
|
const auto& cv = iter->second;
|
2020-04-11 22:48:11 +00:00
|
|
|
con->report(Console::Level::Info, FMT_STRING("'{}' = '{}'"), cv->name(), cv->value());
|
2018-01-22 02:40:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void CVarManager::setDeveloperMode(bool v, bool setDeserialized) {
|
|
|
|
com_developer->unlock();
|
|
|
|
com_developer->fromBoolean(v);
|
|
|
|
if (setDeserialized)
|
|
|
|
com_developer->m_wasDeserialized = true;
|
|
|
|
com_developer->lock();
|
|
|
|
com_developer->setModified();
|
2018-01-11 09:35:27 +00:00
|
|
|
}
|
|
|
|
|
2019-01-26 23:21:30 +00:00
|
|
|
void CVarManager::setCheatsEnabled(bool v, bool setDeserialized) {
|
|
|
|
com_enableCheats->unlock();
|
|
|
|
com_enableCheats->fromBoolean(v);
|
|
|
|
if (setDeserialized)
|
|
|
|
com_enableCheats->m_wasDeserialized = true;
|
|
|
|
com_enableCheats->lock();
|
|
|
|
com_enableCheats->setModified();
|
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
bool CVarManager::restartRequired() const {
|
2019-08-20 10:02:47 +00:00
|
|
|
return std::any_of(m_cvars.cbegin(), m_cvars.cend(), [](const auto& entry) {
|
|
|
|
return entry.second->isModified() && entry.second->modificationRequiresRestart();
|
|
|
|
});
|
2018-01-11 09:35:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void CVarManager::parseCommandLine(const std::vector<SystemString>& args) {
|
|
|
|
bool oldDeveloper = suppressDeveloper();
|
2019-08-20 09:57:56 +00:00
|
|
|
std::string developerName(com_developer->name());
|
2018-12-08 05:18:42 +00:00
|
|
|
athena::utility::tolower(developerName);
|
|
|
|
for (const SystemString& arg : args) {
|
2019-08-20 09:57:56 +00:00
|
|
|
if (arg[0] != _SYS_STR('+')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string tmp(SystemUTF8Conv(arg).str());
|
|
|
|
std::smatch matches;
|
|
|
|
if (!std::regex_match(tmp, matches, cmdLineRegex)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cvarName = matches[1].str();
|
|
|
|
std::string cvarValue = matches[2].str();
|
|
|
|
if (CVar* cv = findCVar(cvarName)) {
|
|
|
|
cv->fromLiteralToType(cvarValue);
|
|
|
|
athena::utility::tolower(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 */
|
|
|
|
athena::utility::tolower(cvarName);
|
|
|
|
m_deferedCVars.insert_or_assign(std::move(cvarName), std::move(cvarValue));
|
2018-04-30 03:11:37 +00:00
|
|
|
}
|
2018-12-08 05:18:42 +00:00
|
|
|
}
|
2018-04-30 03:11:37 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
restoreDeveloper(oldDeveloper);
|
2018-04-30 03:11:37 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
bool CVarManager::suppressDeveloper() {
|
|
|
|
bool oldDeveloper = com_developer->toBoolean();
|
|
|
|
CVarUnlocker unlock(com_developer);
|
|
|
|
com_developer->fromBoolean(true);
|
2018-01-15 12:34:02 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
return oldDeveloper;
|
2015-12-02 21:11:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void CVarManager::restoreDeveloper(bool oldDeveloper) {
|
|
|
|
CVarUnlocker unlock(com_developer);
|
|
|
|
com_developer->fromBoolean(oldDeveloper);
|
2015-12-02 21:11:10 +00:00
|
|
|
}
|
2019-09-29 12:44:01 +00:00
|
|
|
void CVarManager::proc() {
|
|
|
|
for (const auto& [name, cvar] : m_cvars) {
|
|
|
|
if (cvar->isModified() && !cvar->modificationRequiresRestart()) {
|
|
|
|
cvar->dispatch();
|
2019-09-29 15:17:01 +00:00
|
|
|
// Clear the modified flag now that we've informed everyone we've changed
|
|
|
|
cvar->clearModified();
|
2019-09-29 12:44:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 21:11:10 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
} // namespace hecl
|