From 36a025e1d05dd8384fd602054517ebf676150dcf Mon Sep 17 00:00:00 2001 From: Diavolo Date: Wed, 1 Sep 2021 23:19:44 +0200 Subject: [PATCH] Use game functions to safely change dvar value --- src/Components/Modules/Dvar.cpp | 21 +++++++++++---------- src/Game/Functions.cpp | 3 +++ src/Game/Functions.hpp | 9 +++++++++ src/Game/Structs.hpp | 3 +-- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/Components/Modules/Dvar.cpp b/src/Components/Modules/Dvar.cpp index a5debd80..d2d0bc93 100644 --- a/src/Components/Modules/Dvar.cpp +++ b/src/Components/Modules/Dvar.cpp @@ -86,7 +86,8 @@ namespace Components } void Dvar::Var::set(const char* string) { - if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_STRING) + assert(this->dvar->type == Game::DVAR_TYPE_STRING); + if (this->dvar) { Game::Dvar_SetString(this->dvar, string); } @@ -97,26 +98,26 @@ namespace Components } void Dvar::Var::set(int integer) { - if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_INT) + assert(this->dvar->type == Game::DVAR_TYPE_INT); + if (this->dvar) { - this->dvar->current.integer = integer; - this->dvar->latched.integer = integer; + Game::Dvar_SetInt(this->dvar, integer); } } void Dvar::Var::set(float value) { - if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT) + assert(this->dvar->type == Game::DVAR_TYPE_FLOAT); + if (this->dvar) { - this->dvar->current.value = value; - this->dvar->latched.value = value; + Game::Dvar_SetFloat(this->dvar, value); } } void Dvar::Var::set(bool enabled) { - if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_BOOL) + assert(this->dvar->type == Game::DVAR_TYPE_BOOL); + if (this->dvar) { - this->dvar->current.enabled = enabled; - this->dvar->latched.enabled = enabled; + Game::Dvar_SetBool(this->dvar, enabled); } } diff --git a/src/Game/Functions.cpp b/src/Game/Functions.cpp index ca558be0..6d3586af 100644 --- a/src/Game/Functions.cpp +++ b/src/Game/Functions.cpp @@ -280,6 +280,9 @@ namespace Game Dvar_SetFromStringByNameFromSource_t Dvar_SetFromStringByNameFromSource = Dvar_SetFromStringByNameFromSource_t(0x4FC770); Dvar_SetStringByName_t Dvar_SetStringByName = Dvar_SetStringByName_t(0x44F060); Dvar_SetString_t Dvar_SetString = Dvar_SetString_t(0x4A9580); + Dvar_SetBool_t Dvar_SetBool = Dvar_SetBool_t(0x4A9510); + Dvar_SetFloat_t Dvar_SetFloat = Dvar_SetFloat_t(0x40BB20); + Dvar_SetInt_t Dvar_SetInt = Dvar_SetInt_t(0x421DA0); SL_ConvertToString_t SL_ConvertToString = SL_ConvertToString_t(0x4EC1D0); SL_GetString_t SL_GetString = SL_GetString_t(0x4CDC10); diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index 04dbe436..dc8d7986 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -247,6 +247,15 @@ namespace Game typedef void (__cdecl * Dvar_SetString_t)(dvar_t* cvar, const char* value); extern Dvar_SetString_t Dvar_SetString; + typedef void (__cdecl * Dvar_SetBool_t)(dvar_t* cvar, bool enabled); + extern Dvar_SetBool_t Dvar_SetBool; + + typedef void (__cdecl * Dvar_SetFloat_t)(dvar_t* cvar, float value); + extern Dvar_SetFloat_t Dvar_SetFloat; + + typedef void (__cdecl * Dvar_SetInt_t)(dvar_t* cvar, int integer); + extern Dvar_SetInt_t Dvar_SetInt; + typedef void(__cdecl * Dvar_GetUnpackedColorByName_t)(const char* name, float* color); extern Dvar_GetUnpackedColorByName_t Dvar_GetUnpackedColorByName; diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index c5a96608..8e6ae687 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -90,7 +90,6 @@ namespace Game DVAR_FLAG_SERVERINFO = 0x400, //in the getstatus oob DVAR_FLAG_WRITEPROTECTED = 0x800, //write protected DVAR_FLAG_UNKNOWN1000 = 0x1000, //unknown - DVAR_FLAG_UNKNOWN1800 = 0x1800, //unknown DVAR_FLAG_READONLY = 0x2000, //read only (same as 0x800?) DVAR_FLAG_UNKNOWN4000 = 0x4000, //unknown DVAR_FLAG_UNKNOWN8000 = 0x8000, //unknown @@ -2382,7 +2381,7 @@ namespace Game const char *name; const char *description; unsigned int flags; - char type; + dvar_type type; bool modified; DvarValue current; DvarValue latched;