diff --git a/Runtime/CVar.cpp b/Runtime/CVar.cpp index fb8256503..76be851fb 100755 --- a/Runtime/CVar.cpp +++ b/Runtime/CVar.cpp @@ -216,7 +216,7 @@ int CVar::toInteger(bool* isValid) const const std::string CVar::toLiteral(bool* isValid) const { - if (m_type != EType::Literal /*&& (com_developer && com_developer->toBoolean())*/) + if (m_type != EType::Literal && (com_developer && com_developer->toBoolean())) { if (isValid != nullptr) *isValid = false; @@ -230,7 +230,7 @@ const std::string CVar::toLiteral(bool* isValid) const const std::wstring CVar::toWideLiteral(bool* isValid) const { - if (m_type != EType::Literal /*&& (com_developer && com_developer->toBoolean())*/) + if (m_type != EType::Literal && (com_developer && com_developer->toBoolean())) { if (isValid != nullptr) *isValid = false; @@ -244,13 +244,15 @@ const std::wstring CVar::toWideLiteral(bool* isValid) const bool CVar::fromColor(const Zeus::CColor& val) { - if (isCheat()) + if (isCheat() && (com_developer && !com_developer->toBoolean() && !com_enableCheats->toBoolean())) + return false; + else if (isCheat()) return false; if (m_type != EType::Color) return false; - if (isReadOnly()) + if (isReadOnly() && (com_developer && !com_developer->toBoolean())) return false; m_value.assign(CBasics::Stringize("%i %i %i %i", unsigned(val.r), unsigned(val.g), unsigned(val.b), unsigned(val.a))); @@ -260,13 +262,15 @@ bool CVar::fromColor(const Zeus::CColor& val) bool CVar::fromFloat(float val) { - if (isCheat() /*&& (!com_developer->toBoolean() && !com_enableCheats->toBoolean())*/) + if (isCheat() && (com_developer && !com_developer->toBoolean() && !com_enableCheats->toBoolean())) + return false; + else if (isCheat()) return false; if (m_type != EType::Float) return false; - if (isReadOnly() /*&& (com_developer && !com_developer->toBoolean())*/) + if (isReadOnly() && (com_developer && !com_developer->toBoolean())) return false; m_value.assign(CBasics::Stringize("%f", val)); @@ -276,13 +280,15 @@ bool CVar::fromFloat(float val) bool CVar::fromBoolean(bool val) { - if (isCheat() /*&& (!com_developer->toBoolean() && !com_enableCheats->toBoolean())*/) + if (isCheat() && (com_developer && !com_developer->toBoolean() && !com_enableCheats->toBoolean())) + return false; + else if (isCheat()) return false; if (m_type != EType::Boolean) return false; - if (isReadOnly() /*&& (com_developer && !com_developer->toBoolean())*/) + if (isReadOnly() && (com_developer && !com_developer->toBoolean())) return false; if (val) @@ -296,13 +302,15 @@ bool CVar::fromBoolean(bool val) bool CVar::fromInteger(int val) { - if (isCheat() /*&& (!com_developer->toBoolean() && !com_enableCheats->toBoolean())*/) + if (isCheat() && (com_developer && !com_developer->toBoolean() && !com_enableCheats->toBoolean())) + return false; + else if (isCheat()) return false; if (m_type != EType::Integer) return false; - if (isReadOnly() /*&& (com_developer && !com_developer->toBoolean())*/) + if (isReadOnly() && (com_developer && !com_developer->toBoolean())) return false; m_value = CBasics::Stringize("%i", val); @@ -312,13 +320,15 @@ bool CVar::fromInteger(int val) bool CVar::fromLiteral(const std::string& val) { - if (isCheat() /*&& (!com_developer->toBoolean() && !com_enableCheats->toBoolean())*/) + if (isCheat() && (com_developer && !com_developer->toBoolean() && !com_enableCheats->toBoolean())) + return false; + else if (isCheat()) return false; if (m_type != EType::Literal) return false; - if (isReadOnly() /*&& (com_developer && !com_developer->toBoolean())*/) + if (isReadOnly() && (com_developer && !com_developer->toBoolean())) return false; m_value.assign(val); @@ -328,13 +338,15 @@ bool CVar::fromLiteral(const std::string& val) bool CVar::fromLiteral(const std::wstring& val) { - if (isCheat() /*&& (!com_developer->toBoolean() && !com_enableCheats->toBoolean())*/) + if (isCheat() && (com_developer && !com_developer->toBoolean() && !com_enableCheats->toBoolean())) + return false; + else if (isCheat()) return false; if (m_type != EType::Literal) return false; - if (isReadOnly() /*&& (com_developer && !com_developer->toBoolean())*/) + if (isReadOnly() && (com_developer && !com_developer->toBoolean())) return false; m_value.assign(HECL::WideToUTF8(val)); @@ -380,5 +392,11 @@ void CVar::lock() clearModified(); } } + +void CVar::dispatch() +{ + for (const ListenerFunc& listen : m_listeners) + listen(this); +} } diff --git a/Runtime/CVar.hpp b/Runtime/CVar.hpp index 9e529fecb..b2549f999 100755 --- a/Runtime/CVar.hpp +++ b/Runtime/CVar.hpp @@ -2,6 +2,7 @@ #define CVAR_HPP #include +#include #include "CColor.hpp" #include "DataSpec/DNACommon/CVar.hpp" @@ -14,6 +15,8 @@ class CVar : protected DNACVAR::CVar friend class CVarManager; public: + typedef std::function ListenerFunc; + using EType = DNACVAR::EType; using EFlags = DNACVAR::EFlags; @@ -46,7 +49,6 @@ public: bool isBoolean() const { return m_type == EType::Boolean; } bool isInteger() const { return m_type == EType::Integer; } bool isLiteral() const { return m_type == EType::Literal; } - bool isBinding() const { return m_type == EType::Bind; } bool isColor() const { return m_type == EType::Color; } bool isModified() const; bool isReadOnly() const; @@ -73,13 +75,19 @@ public: * \see unlock */ void lock(); + + void addListener(ListenerFunc func) { m_listeners.push_back(func); } + private: + void dispatch(); std::string m_help; std::string m_defaultValue; EFlags m_flags; bool m_allowedWrite; CVarManager& m_mgr; + + std::vector m_listeners; }; diff --git a/Runtime/CVarManager.cpp b/Runtime/CVarManager.cpp index 144ba63ce..4a3fee8ed 100755 --- a/Runtime/CVarManager.cpp +++ b/Runtime/CVarManager.cpp @@ -20,7 +20,7 @@ CVarManager::CVarManager(HECL::Runtime::FileStoreManager& store, bool useBinary) { 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::Archive | CVar::EFlags::ReadOnly | CVar::EFlags::Hidden)); + com_enableCheats = newCVar("iamaweiner", "Enable cheats", false, (CVar::EFlags::System | CVar::EFlags::ReadOnly | CVar::EFlags::Hidden)); r_clearColor = newCVar("r_clearcolor", "Sets the clear color for the frame buffer", Zeus::CColor{Zeus::Comp8(255), 255, 255}, (CVar::EFlags::System | CVar::EFlags::Archive)); } @@ -28,6 +28,16 @@ CVarManager::~CVarManager() { } +void CVarManager::update() +{ + for (const std::pair pair : m_cvars) + if (pair.second->isModified()) + { + pair.second->dispatch(); + pair.second->clearModified(); + } +} + bool CVarManager::registerCVar(CVar* cvar) { std::string tmp = cvar->name(); diff --git a/Runtime/CVarManager.hpp b/Runtime/CVarManager.hpp index 5e3c7ef53..e8f04d22d 100755 --- a/Runtime/CVarManager.hpp +++ b/Runtime/CVarManager.hpp @@ -24,7 +24,8 @@ class CVarManager CVar* ret(new CVar(name, value, help, flags, *this)); if (registerCVar(ret)) { - deserialize(ret); + if (ret->isArchive()) + deserialize(ret); return ret; } delete ret; @@ -37,6 +38,7 @@ public: CVarManager(HECL::Runtime::FileStoreManager& store, bool useBinary = false); ~CVarManager(); + void update(); CVar* newCVar(const std::string& name, const std::string& help, const Zeus::CColor& value, CVar::EFlags flags) { return _newCVar(name, help, value, flags); } CVar* newCVar(const std::string& name, const std::string& help, const std::string& value, CVar::EFlags flags) @@ -50,7 +52,7 @@ public: bool registerCVar(CVar* cvar); - CVar*findCVar(const std::string& name); + CVar* findCVar(const std::string& name); std::vector archivedCVars() const; std::vector cvars() const;