metaforce/hecl/lib/CVarManager.cpp

194 lines
5.1 KiB
C++
Raw Normal View History

2016-03-04 23:02:44 +00:00
#include "hecl/CVarManager.hpp"
#include "hecl/CVar.hpp"
#include <athena/FileWriter.hpp>
#include <athena/Utility.hpp>
#include <hecl/Runtime.hpp>
2015-12-02 21:11:10 +00:00
#include <memory>
2016-03-04 23:02:44 +00:00
namespace hecl
2015-12-02 21:11:10 +00:00
{
CVar* com_developer = nullptr;
CVar* com_configfile = nullptr;
CVar* com_enableCheats = nullptr;
2015-12-04 01:43:29 +00:00
CVarManager* CVarManager::m_instance = nullptr;
2016-03-04 23:02:44 +00:00
logvisor::Module CVarLog("CVarManager");
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;
2015-12-02 21:11:10 +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::Cheat | CVar::EFlags::ReadOnly));
com_enableCheats = newCVar("iamaweiner", "Enable cheats", false, (CVar::EFlags::System | CVar::EFlags::ReadOnly | CVar::EFlags::Hidden));
}
CVarManager::~CVarManager()
{
}
void CVarManager::update()
{
for (const std::pair<std::string, CVar*>& pair : m_cvars)
if (pair.second->isModified())
{
pair.second->dispatch();
pair.second->clearModified();
}
}
bool CVarManager::registerCVar(CVar* cvar)
{
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())
return false;
m_cvars[tmp] = cvar;
return true;
}
CVar* CVarManager::findCVar(std::string name)
{
2016-03-04 23:02:44 +00:00
athena::utility::tolower(name);
2015-12-02 21:11:10 +00:00
if (m_cvars.find(name) == m_cvars.end())
return nullptr;
return m_cvars[name];
}
std::vector<CVar*> CVarManager::archivedCVars() const
{
std::vector<CVar*> ret;
for (const std::pair<std::string, CVar*>& pair : m_cvars)
if (pair.second->isArchive())
ret.push_back(pair.second);
return ret;
}
std::vector<CVar*> CVarManager::cvars() const
{
std::vector<CVar*> ret;
for (const std::pair<std::string, CVar*>& pair : m_cvars)
ret.push_back(pair.second);
return ret;
}
void CVarManager::deserialize(CVar* cvar)
{
if (!cvar || !cvar->isArchive())
return;
CVarContainer container;
#if _WIN32
2016-03-04 23:02:44 +00:00
hecl::SystemString filename = m_store.getStoreRoot() + _S('/') + com_configfile->toWideLiteral();
2015-12-02 21:11:10 +00:00
#else
2016-03-04 23:02:44 +00:00
hecl::SystemString filename = 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
if (m_useBinary)
{
filename += _S(".bin");
2016-03-04 23:02:44 +00:00
if (hecl::Stat(filename.c_str(), &st) || !S_ISREG(st.st_mode))
2015-12-02 21:11:10 +00:00
return;
2016-03-04 23:02:44 +00:00
athena::io::FileReader reader(filename);
2015-12-02 21:11:10 +00:00
if (reader.isOpen())
container.read(reader);
}
else
{
filename += _S(".yaml");
2016-03-04 23:02:44 +00:00
if (hecl::Stat(filename.c_str(), &st) || !S_ISREG(st.st_mode))
2015-12-02 21:11:10 +00:00
return;
2016-03-04 23:02:44 +00:00
FILE* f = hecl::Fopen(filename.c_str(), _S("rb"));
2015-12-02 21:11:10 +00:00
if (f)
container.fromYAMLFile(f);
fclose(f);
}
if (container.cvars.size() > 0)
{
auto serialized = std::find_if(container.cvars.begin(), container.cvars.end(), [&cvar](const DNACVAR::CVar& c) -> bool
{ return c.m_name == cvar->name(); });
if (serialized != container.cvars.end())
{
DNACVAR::CVar& tmp = *serialized;
if (tmp.m_type != cvar->type())
{
2016-03-04 23:02:44 +00:00
CVarLog.report(logvisor::Error, _S("Stored type for %s does not match actual type!"), tmp.m_name.c_str());
2015-12-02 21:11:10 +00:00
return;
}
if (cvar->m_value != tmp.m_value)
{
cvar->m_value = tmp.m_value;
cvar->m_flags |= CVar::EFlags::Modified;
}
2015-12-02 21:11:10 +00:00
}
}
}
void CVarManager::serialize()
{
CVarContainer container;
for (const std::pair<std::string, CVar*>& pair : m_cvars)
if (pair.second->isArchive())
{
CVar tmp = *pair.second;
container.cvars.push_back(tmp);
}
container.cvarCount = container.cvars.size();
#if _WIN32
2016-03-04 23:02:44 +00:00
hecl::SystemString filename = m_store.getStoreRoot() + _S('/') + com_configfile->toWideLiteral();
2015-12-02 21:11:10 +00:00
#else
2016-03-04 23:02:44 +00:00
hecl::SystemString filename = m_store.getStoreRoot() + _S('/') + com_configfile->toLiteral();
2015-12-02 21:11:10 +00:00
#endif
if (m_useBinary)
{
filename += _S(".bin");
2016-03-04 23:02:44 +00:00
athena::io::FileWriter writer(filename);
2015-12-02 21:11:10 +00:00
if (writer.isOpen())
container.write(writer);
}
else
{
filename += _S(".yaml");
2016-03-04 23:02:44 +00:00
FILE* f = hecl::Fopen(filename.c_str(), _S("wb"));
2015-12-02 21:11:10 +00:00
if (f)
container.toYAMLFile(f);
fclose(f);
}
}
2015-12-04 01:43:29 +00:00
CVarManager* CVarManager::instance()
{
return m_instance;
}
2015-12-02 21:11:10 +00:00
bool CVarManager::suppressDeveloper()
{
bool oldDeveloper = com_developer->toBoolean();
CVarUnlocker unlock(com_developer);
com_developer->fromBoolean(false);
return oldDeveloper;
}
void CVarManager::restoreDeveloper(bool oldDeveloper)
{
CVarUnlocker unlock(com_developer);
com_developer->fromBoolean(oldDeveloper);
}
}