From 8c74d261c16d14cca992af2069e4a8e3571a0011 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Fri, 4 Jun 2021 01:28:03 -0700 Subject: [PATCH] Add CVarValueReference --- Runtime/CMain.cpp | 2 +- Runtime/CStateManager.cpp | 17 +++++++----- Runtime/CStateManager.hpp | 2 ++ hecl/include/hecl/CVar.hpp | 54 +++++++++++++++++++++++++++++++++++--- hecl/lib/CVar.cpp | 3 +++ 5 files changed, 66 insertions(+), 12 deletions(-) diff --git a/Runtime/CMain.cpp b/Runtime/CMain.cpp index 6fbd35d03..304986231 100644 --- a/Runtime/CMain.cpp +++ b/Runtime/CMain.cpp @@ -405,7 +405,7 @@ public: using delta_duration = std::chrono::duration>; realDt = std::chrono::duration_cast(now - m_prevFrameTime).count(); if (m_cvarCommons.m_variableDt->toBoolean()) { - dt = std::min(realDt, 1 / 30.f); + dt = std::max(realDt, 1 / 30.f); } } m_prevFrameTime = now; diff --git a/Runtime/CStateManager.cpp b/Runtime/CStateManager.cpp index 605fe1e46..2ec29130a 100644 --- a/Runtime/CStateManager.cpp +++ b/Runtime/CStateManager.cpp @@ -216,9 +216,12 @@ CStateManager::CStateManager(const std::weak_ptr& mailbox, x8f0_shadowTex = g_SimplePool->GetObj("DefaultShadow"); g_StateManager = this; - sm_logScripting = hecl::CVarManager::instance()->findOrMakeCVar( - "stateManager.logScripting"sv, "Prints object communication to the console", false, - hecl::CVar::EFlags::ReadOnly | hecl::CVar::EFlags::Archive | hecl::CVar::EFlags::Game); + if (sm_logScripting == nullptr) { + sm_logScripting = hecl::CVarManager::instance()->findOrMakeCVar( + "stateManager.logScripting"sv, "Prints object communication to the console", false, + hecl::CVar::EFlags::ReadOnly | hecl::CVar::EFlags::Archive | hecl::CVar::EFlags::Game); + } + m_logScriptingReference.emplace(&m_logScripting, sm_logScripting); } CStateManager::~CStateManager() { @@ -1326,7 +1329,7 @@ void CStateManager::SendScriptMsg(CEntity* dest, TUniqueId src, EScriptObjectMes return; } - if (sm_logScripting != nullptr && sm_logScripting->toBoolean()) { + if (m_logScripting) { LogModule.report(logvisor::Info, FMT_STRING("Sending '{}' to '{}' id= {}"), ScriptObjectMessageToStr(msg), dest->GetName(), dest->GetUniqueId()); } @@ -1345,7 +1348,7 @@ void CStateManager::SendScriptMsgAlways(TUniqueId dest, TUniqueId src, EScriptOb return; } - if (sm_logScripting != nullptr && sm_logScripting->toBoolean()) { + if (m_logScripting) { LogModule.report(logvisor::Info, FMT_STRING("Sending '{}' to '{}' id= {}"), ScriptObjectMessageToStr(msg), dst->GetName(), dst->GetUniqueId()); } @@ -1415,7 +1418,7 @@ void CStateManager::FreeScriptObject(TUniqueId id) { act->SetUseInSortedLists(false); } - if (sm_logScripting && sm_logScripting->toBoolean()) { + if (m_logScripting) { LogModule.report(logvisor::Info, FMT_STRING("Removed '{}'"), ent->GetName()); } } @@ -2779,7 +2782,7 @@ void CStateManager::AddObject(CEntity& ent) { } } - if (sm_logScripting != nullptr && sm_logScripting->toBoolean()) { + if (m_logScripting) { LogModule.report(logvisor::Info, FMT_STRING("Added '{}'"), ent.GetName()); } } diff --git a/Runtime/CStateManager.hpp b/Runtime/CStateManager.hpp index 1fb1c9f8b..a4214f200 100644 --- a/Runtime/CStateManager.hpp +++ b/Runtime/CStateManager.hpp @@ -216,6 +216,8 @@ private: bool m_warping = false; std::map> m_incomingConnections; + bool m_logScripting = false; + std::optional> m_logScriptingReference; void UpdateThermalVisor(); static void RendererDrawCallback(void*, void*, int); diff --git a/hecl/include/hecl/CVar.hpp b/hecl/include/hecl/CVar.hpp index 67fc742c5..d362f62e8 100644 --- a/hecl/include/hecl/CVar.hpp +++ b/hecl/include/hecl/CVar.hpp @@ -11,7 +11,6 @@ namespace hecl { namespace DNACVAR { enum class EType : atUint8 { Boolean, Signed, Unsigned, Real, Literal, Vec2f, Vec2d, Vec3f, Vec3d, Vec4f, Vec4d }; - enum class EFlags { None = 0, System = (1 << 0), @@ -48,6 +47,7 @@ struct CVarContainer : public athena::io::DNA { } // namespace DNACVAR class CVarManager; +class ICVarValueReference; class CVar : protected DNACVAR::CVar { friend class CVarManager; Delete _d; @@ -151,6 +151,13 @@ public: void lock(); void addListener(ListenerFunc func) { m_listeners.push_back(std::move(func)); } + void addVariableReference(ICVarValueReference* v) { m_valueReferences.push_back(v); } + void removeVariableReference(ICVarValueReference* v) { + auto it = std::find(m_valueReferences.begin(), m_valueReferences.end(), v); + if (it != m_valueReferences.end()) { + m_valueReferences.erase(it); + } + } bool isValidInput(std::string_view input) const; bool isValidInput(std::wstring_view input) const; @@ -168,6 +175,7 @@ private: bool m_unlocked = false; bool m_wasDeserialized = false; std::vector m_listeners; + std::vector m_valueReferences; bool safeToModify(EType type) const; void init(EFlags flags, bool removeColor = true); }; @@ -216,9 +224,9 @@ inline bool CVar::toValue(double& value) const { } template <> inline bool CVar::toValue(float& value) const { - bool isValid = false; - value = static_cast(toReal(&isValid)); - return isValid; + bool isValid = false; + value = static_cast(toReal(&isValid)); + return isValid; } template <> inline bool CVar::toValue(bool& value) const { @@ -317,5 +325,43 @@ public: m_cvar->lock(); } }; +class ICVarValueReference { +protected: + CVar* m_cvar = nullptr; +public: + ICVarValueReference() = default; + explicit ICVarValueReference(CVar* cv) : m_cvar(cv) { + if (m_cvar != nullptr) { + m_cvar->addVariableReference(this); + } + } + virtual ~ICVarValueReference() { + if (m_cvar != nullptr) { + m_cvar->removeVariableReference(this); + } + m_cvar = nullptr; + } + virtual void updateValue() = 0; +}; + +template +class CVarValueReference : public ICVarValueReference { + T* m_valueRef = nullptr; + +public: + CVarValueReference() = default; + explicit CVarValueReference(T* t, CVar* cv) : ICVarValueReference(cv) { + m_valueRef = t; + if (m_valueRef && m_cvar) { + m_cvar->toValue(*m_valueRef); + } + } + + void updateValue() override { + if (m_valueRef != nullptr && m_cvar->isModified()) { + m_cvar->toValue(*m_valueRef); + } + } +}; } // namespace hecl diff --git a/hecl/lib/CVar.cpp b/hecl/lib/CVar.cpp index 5b9a958e3..130a98964 100644 --- a/hecl/lib/CVar.cpp +++ b/hecl/lib/CVar.cpp @@ -466,6 +466,9 @@ void CVar::lock() { void CVar::dispatch() { for (const ListenerFunc& listen : m_listeners) listen(this); + for (auto* ref : m_valueReferences) { + ref->updateValue(); + } } bool isReal(std::string_view v) {