Add CVarValueReference

This commit is contained in:
Phillip Stephens 2021-06-04 01:28:03 -07:00
parent 8f1b39c27f
commit 8c74d261c1
Signed by: Antidote
GPG Key ID: F8BEE4C83DACA60D
5 changed files with 66 additions and 12 deletions

View File

@ -405,7 +405,7 @@ public:
using delta_duration = std::chrono::duration<float, std::ratio<1>>; using delta_duration = std::chrono::duration<float, std::ratio<1>>;
realDt = std::chrono::duration_cast<delta_duration>(now - m_prevFrameTime).count(); realDt = std::chrono::duration_cast<delta_duration>(now - m_prevFrameTime).count();
if (m_cvarCommons.m_variableDt->toBoolean()) { if (m_cvarCommons.m_variableDt->toBoolean()) {
dt = std::min(realDt, 1 / 30.f); dt = std::max(realDt, 1 / 30.f);
} }
} }
m_prevFrameTime = now; m_prevFrameTime = now;

View File

@ -216,10 +216,13 @@ CStateManager::CStateManager(const std::weak_ptr<CScriptMailbox>& mailbox,
x8f0_shadowTex = g_SimplePool->GetObj("DefaultShadow"); x8f0_shadowTex = g_SimplePool->GetObj("DefaultShadow");
g_StateManager = this; g_StateManager = this;
if (sm_logScripting == nullptr) {
sm_logScripting = hecl::CVarManager::instance()->findOrMakeCVar( sm_logScripting = hecl::CVarManager::instance()->findOrMakeCVar(
"stateManager.logScripting"sv, "Prints object communication to the console", false, "stateManager.logScripting"sv, "Prints object communication to the console", false,
hecl::CVar::EFlags::ReadOnly | hecl::CVar::EFlags::Archive | hecl::CVar::EFlags::Game); hecl::CVar::EFlags::ReadOnly | hecl::CVar::EFlags::Archive | hecl::CVar::EFlags::Game);
} }
m_logScriptingReference.emplace(&m_logScripting, sm_logScripting);
}
CStateManager::~CStateManager() { CStateManager::~CStateManager() {
x88c_rumbleManager->HardStopAll(); x88c_rumbleManager->HardStopAll();
@ -1326,7 +1329,7 @@ void CStateManager::SendScriptMsg(CEntity* dest, TUniqueId src, EScriptObjectMes
return; return;
} }
if (sm_logScripting != nullptr && sm_logScripting->toBoolean()) { if (m_logScripting) {
LogModule.report(logvisor::Info, FMT_STRING("Sending '{}' to '{}' id= {}"), ScriptObjectMessageToStr(msg), LogModule.report(logvisor::Info, FMT_STRING("Sending '{}' to '{}' id= {}"), ScriptObjectMessageToStr(msg),
dest->GetName(), dest->GetUniqueId()); dest->GetName(), dest->GetUniqueId());
} }
@ -1345,7 +1348,7 @@ void CStateManager::SendScriptMsgAlways(TUniqueId dest, TUniqueId src, EScriptOb
return; return;
} }
if (sm_logScripting != nullptr && sm_logScripting->toBoolean()) { if (m_logScripting) {
LogModule.report(logvisor::Info, FMT_STRING("Sending '{}' to '{}' id= {}"), ScriptObjectMessageToStr(msg), LogModule.report(logvisor::Info, FMT_STRING("Sending '{}' to '{}' id= {}"), ScriptObjectMessageToStr(msg),
dst->GetName(), dst->GetUniqueId()); dst->GetName(), dst->GetUniqueId());
} }
@ -1415,7 +1418,7 @@ void CStateManager::FreeScriptObject(TUniqueId id) {
act->SetUseInSortedLists(false); act->SetUseInSortedLists(false);
} }
if (sm_logScripting && sm_logScripting->toBoolean()) { if (m_logScripting) {
LogModule.report(logvisor::Info, FMT_STRING("Removed '{}'"), ent->GetName()); 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()); LogModule.report(logvisor::Info, FMT_STRING("Added '{}'"), ent.GetName());
} }
} }

View File

@ -216,6 +216,8 @@ private:
bool m_warping = false; bool m_warping = false;
std::map<TEditorId, std::set<SConnection>> m_incomingConnections; std::map<TEditorId, std::set<SConnection>> m_incomingConnections;
bool m_logScripting = false;
std::optional<hecl::CVarValueReference<bool>> m_logScriptingReference;
void UpdateThermalVisor(); void UpdateThermalVisor();
static void RendererDrawCallback(void*, void*, int); static void RendererDrawCallback(void*, void*, int);

View File

@ -11,7 +11,6 @@
namespace hecl { namespace hecl {
namespace DNACVAR { namespace DNACVAR {
enum class EType : atUint8 { Boolean, Signed, Unsigned, Real, Literal, Vec2f, Vec2d, Vec3f, Vec3d, Vec4f, Vec4d }; enum class EType : atUint8 { Boolean, Signed, Unsigned, Real, Literal, Vec2f, Vec2d, Vec3f, Vec3d, Vec4f, Vec4d };
enum class EFlags { enum class EFlags {
None = 0, None = 0,
System = (1 << 0), System = (1 << 0),
@ -48,6 +47,7 @@ struct CVarContainer : public athena::io::DNA<athena::Endian::Big> {
} // namespace DNACVAR } // namespace DNACVAR
class CVarManager; class CVarManager;
class ICVarValueReference;
class CVar : protected DNACVAR::CVar { class CVar : protected DNACVAR::CVar {
friend class CVarManager; friend class CVarManager;
Delete _d; Delete _d;
@ -151,6 +151,13 @@ public:
void lock(); void lock();
void addListener(ListenerFunc func) { m_listeners.push_back(std::move(func)); } 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::string_view input) const;
bool isValidInput(std::wstring_view input) const; bool isValidInput(std::wstring_view input) const;
@ -168,6 +175,7 @@ private:
bool m_unlocked = false; bool m_unlocked = false;
bool m_wasDeserialized = false; bool m_wasDeserialized = false;
std::vector<ListenerFunc> m_listeners; std::vector<ListenerFunc> m_listeners;
std::vector<ICVarValueReference*> m_valueReferences;
bool safeToModify(EType type) const; bool safeToModify(EType type) const;
void init(EFlags flags, bool removeColor = true); void init(EFlags flags, bool removeColor = true);
}; };
@ -317,5 +325,43 @@ public:
m_cvar->lock(); 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 <typename T>
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 } // namespace hecl

View File

@ -466,6 +466,9 @@ void CVar::lock() {
void CVar::dispatch() { void CVar::dispatch() {
for (const ListenerFunc& listen : m_listeners) for (const ListenerFunc& listen : m_listeners)
listen(this); listen(this);
for (auto* ref : m_valueReferences) {
ref->updateValue();
}
} }
bool isReal(std::string_view v) { bool isReal(std::string_view v) {