metaforce/hecl/lib/CVarManager.cpp

323 lines
9.8 KiB
C++
Raw Normal View History

2016-03-04 23:02:44 +00:00
#include "hecl/CVarManager.hpp"
2018-01-11 09:35:27 +00:00
#include "hecl/Console.hpp"
2016-03-04 23:02:44 +00:00
#include <athena/FileWriter.hpp>
#include <athena/Utility.hpp>
#include <hecl/Runtime.hpp>
2018-04-30 03:11:37 +00:00
#include <hecl/hecl.hpp>
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
2016-03-04 23:02:44 +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;
2015-12-02 21:11:10 +00:00
2018-04-30 03:11:37 +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)
2015-12-02 21:11:10 +00:00
: m_store(store),
m_useBinary(useBinary)
{
2015-12-04 01:43:29 +00:00
m_instance = this;
2018-01-22 02:40:11 +00:00
com_configfile = newCVar("config", "File to store configuration", std::string("config"), CVar::EFlags::System);
com_developer = newCVar("developer", "Enables developer mode", false, (CVar::EFlags::System | CVar::EFlags::ReadOnly | CVar::EFlags::InternalArchivable));
com_enableCheats = newCVar("iamaweiner", "Enable cheats", false, (CVar::EFlags::System | CVar::EFlags::ReadOnly | CVar::EFlags::Hidden));
2015-12-02 21:11:10 +00:00
}
CVarManager::~CVarManager()
{
}
2018-05-20 06:12:32 +00:00
CVar* CVarManager::registerCVar(std::unique_ptr<CVar>&& cvar)
2015-12-02 21:11:10 +00:00
{
2017-11-13 06:13:53 +00:00
std::string tmp(cvar->name());
2016-03-04 23:02:44 +00:00
athena::utility::tolower(tmp);
2015-12-02 21:11:10 +00:00
if (m_cvars.find(tmp) != m_cvars.end())
2018-05-20 06:12:32 +00:00
return nullptr;
2015-12-02 21:11:10 +00:00
2018-05-20 06:12:32 +00:00
CVar* ret = cvar.get();
m_cvars[tmp] = std::move(cvar);
return ret;
2015-12-02 21:11:10 +00:00
}
2017-11-13 06:13:53 +00:00
CVar* CVarManager::findCVar(std::string_view name)
2015-12-02 21:11:10 +00:00
{
2017-11-13 06:13:53 +00:00
std::string lower(name);
athena::utility::tolower(lower);
auto search = m_cvars.find(lower);
if (search == m_cvars.end())
2015-12-02 21:11:10 +00:00
return nullptr;
2018-05-20 06:12:32 +00:00
return search->second.get();
2015-12-02 21:11:10 +00:00
}
std::vector<CVar*> CVarManager::archivedCVars() const
{
std::vector<CVar*> ret;
2018-05-20 06:12:32 +00:00
for (const auto& pair : m_cvars)
2015-12-02 21:11:10 +00:00
if (pair.second->isArchive())
2018-05-20 06:12:32 +00:00
ret.push_back(pair.second.get());
2015-12-02 21:11:10 +00:00
return ret;
}
std::vector<CVar*> CVarManager::cvars() const
{
std::vector<CVar*> ret;
2018-05-20 06:12:32 +00:00
for (const auto& pair : m_cvars)
ret.push_back(pair.second.get());
2015-12-02 21:11:10 +00:00
return ret;
}
void CVarManager::deserialize(CVar* cvar)
{
2018-01-14 07:37:00 +00:00
if (!cvar || (!cvar->isArchive() && !cvar->isInternalArchivable()))
2015-12-02 21:11:10 +00:00
return;
#if _WIN32
2018-01-22 02:40:11 +00:00
hecl::SystemString filename = hecl::SystemString(m_store.getStoreRoot()) + _S('/') + com_configfile->toWideLiteral();
2015-12-02 21:11:10 +00:00
#else
2018-01-22 02:40:11 +00:00
hecl::SystemString filename = hecl::SystemString(m_store.getStoreRoot()) + _S('/') + com_configfile->toLiteral();
2015-12-02 21:11:10 +00:00
#endif
2016-03-04 23:02:44 +00:00
hecl::Sstat st;
2015-12-02 21:11:10 +00:00
2018-01-22 02:40:11 +00:00
if (m_useBinary)
{
CVarContainer container;
2018-01-22 02:40:11 +00:00
filename += _S(".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)
{
cvar->unlock();
cvar->fromLiteralToType(tmp.m_value, true);
cvar->m_wasDeserialized = true;
cvar->lock();
}
}
}
2018-01-22 02:40:11 +00:00
}
else
{
filename += _S(".yaml");
if (hecl::Stat(filename.c_str(), &st) || !S_ISREG(st.st_mode))
return;
athena::io::FileReader reader(filename);
if (reader.isOpen())
2015-12-02 21:11:10 +00:00
{
athena::io::YAMLDocReader docReader;
if (docReader.parse(&reader))
2018-01-22 02:40:11 +00:00
{
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)
{
cvar->unlock();
cvar->fromLiteralToType(tmp->m_scalarString, true);
cvar->m_wasDeserialized = true;
cvar->lock();
}
}
2018-01-22 02:40:11 +00:00
}
2015-12-02 21:11:10 +00:00
}
}
}
void CVarManager::serialize()
{
#if _WIN32
2018-01-22 02:40:11 +00:00
hecl::SystemString filename = hecl::SystemString(m_store.getStoreRoot()) + _S('/') + com_configfile->toWideLiteral();
2015-12-02 21:11:10 +00:00
#else
2018-01-22 02:40:11 +00:00
hecl::SystemString filename = hecl::SystemString(m_store.getStoreRoot()) + _S('/') + com_configfile->toLiteral();
2015-12-02 21:11:10 +00:00
#endif
2018-01-22 02:40:11 +00:00
if (m_useBinary)
2015-12-02 21:11:10 +00:00
{
CVarContainer container;
2018-05-20 06:12:32 +00:00
for (const auto& pair : m_cvars)
if (pair.second->isArchive() || (pair.second->isInternalArchivable() &&
pair.second->wasDeserialized() && !pair.second->hasDefaultValue()))
container.cvars.push_back(*pair.second);
container.cvarCount = atUint32(container.cvars.size());
2018-01-22 02:40:11 +00:00
filename += _S(".bin");
athena::io::FileWriter writer(filename);
if (writer.isOpen())
container.write(writer);
}
else
{
filename += _S(".yaml");
athena::io::FileReader r(filename);
athena::io::YAMLDocWriter docWriter(nullptr, r.isOpen() ? &r : nullptr);
r.close();
docWriter.setStyle(athena::io::YAMLNodeStyle::Block);
2018-05-20 06:12:32 +00:00
for (const auto& pair : m_cvars)
if (pair.second->isArchive() || (pair.second->isInternalArchivable() &&
pair.second->wasDeserialized() && !pair.second->hasDefaultValue()))
docWriter.writeString(pair.second->name().data(), pair.second->toLiteral());
athena::io::FileWriter w(filename);
if (w.isOpen())
docWriter.finish(&w);
2015-12-02 21:11:10 +00:00
}
}
2015-12-04 01:43:29 +00:00
CVarManager* CVarManager::instance()
{
return m_instance;
}
2018-01-14 07:37:00 +00:00
void CVarManager::list(Console* con, const std::vector<std::string>& /*args*/)
2018-01-11 09:35:27 +00:00
{
for (const auto& cvar : m_cvars)
{
if (!cvar.second->isHidden())
con->report(Console::Level::Info, "%s: %s", cvar.first.c_str(), cvar.second->help().c_str());
}
2018-01-11 09:35:27 +00:00
}
void CVarManager::setCVar(Console* con, const std::vector<std::string> &args)
2018-01-11 09:35:27 +00:00
{
if (args.size() < 2)
{
con->report(Console::Level::Info, "Usage setCvar <cvar> <value>");
return;
}
2018-01-11 09:35:27 +00:00
std::string cvName = args[0];
athena::utility::tolower(cvName);
if (m_cvars.find(cvName) == m_cvars.end())
{
con->report(Console::Level::Error, "CVar '%s' does not exist", args[0].c_str());
return;
}
2018-05-20 06:12:32 +00:00
const auto& cv = m_cvars[cvName];
std::string value = args[1];
auto it = args.begin() + 2;
for (; it != args.end(); ++it)
value += " " + *it;
2018-01-22 02:40:11 +00:00
if (!cv->fromLiteralToType(value))
con->report(Console::Level::Warning, "Unable to set cvar '%s' to value '%s'", args[0].c_str(), value.c_str());
2018-01-11 09:35:27 +00:00
}
void CVarManager::getCVar(Console* con, const std::vector<std::string> &args)
2018-01-11 09:35:27 +00:00
{
if (args.empty())
{
con->report(Console::Level::Info, "Usage getCVar <cvar>");
return;
}
std::string cvName = args[0];
athena::utility::tolower(cvName);
if (m_cvars.find(cvName) == m_cvars.end())
{
con->report(Console::Level::Error, "CVar '%s' does not exist", args[0].c_str());
return;
}
2018-05-20 06:12:32 +00:00
const auto& cv = m_cvars[cvName];
2018-01-22 02:40:11 +00:00
con->report(Console::Level::Info, "'%s' = '%s'", cv->name().data(), cv->value().c_str());
}
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
}
bool CVarManager::restartRequired() const
{
for (const auto& cv : m_cvars)
{
if (cv.second->isModified() && cv.second->modificationRequiresRestart())
return true;
}
return false;
}
2018-04-30 03:11:37 +00:00
void CVarManager::parseCommandLine(const std::vector<SystemString>& args)
{
bool oldDeveloper = suppressDeveloper();
std::string developerName = com_developer->name().data();
2018-05-02 00:41:51 +00:00
athena::utility::tolower(developerName);
2018-04-30 03:11:37 +00:00
for (const SystemString& arg : args)
{
if (arg[0] == _S('+'))
{
std::string tmp = SystemUTF8Conv(arg).c_str();
std::smatch matches;
if (std::regex_match(tmp, matches, cmdLineRegex))
{
std::string cvarName = matches[1].str();
std::string cvarValue = matches[2].str();
if (CVar* cv = findCVar(cvarName))
{
cv->fromLiteralToType(cvarValue);
2018-05-02 00:41:51 +00:00
athena::utility::tolower(cvarName);
2018-04-30 03:11:37 +00:00
if (developerName == cvarName)
/* Make sure we're not overriding developer mode when we restore */
oldDeveloper = com_developer->toBoolean();
}
}
}
}
restoreDeveloper(oldDeveloper);
}
2015-12-02 21:11:10 +00:00
bool CVarManager::suppressDeveloper()
{
2018-01-22 02:40:11 +00:00
bool oldDeveloper = com_developer->toBoolean();
CVarUnlocker unlock(com_developer);
2018-04-30 03:11:37 +00:00
com_developer->fromBoolean(true);
2018-01-15 12:34:02 +00:00
2018-01-22 02:40:11 +00:00
return oldDeveloper;
2015-12-02 21:11:10 +00:00
}
void CVarManager::restoreDeveloper(bool oldDeveloper)
{
2018-01-22 02:40:11 +00:00
CVarUnlocker unlock(com_developer);
com_developer->fromBoolean(oldDeveloper);
2015-12-02 21:11:10 +00:00
}
}