From 39f2f4ff6245e459c21eb0847b64251d7a033bc7 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Thu, 3 Feb 2022 12:44:35 +0100 Subject: [PATCH 1/8] Fix gamepad vars --- src/Components/Modules/Dvar.cpp | 85 ++++++++++++++++++++---------- src/Components/Modules/Dvar.hpp | 11 ++-- src/Components/Modules/Gamepad.cpp | 8 +-- src/Components/Modules/Gamepad.hpp | 2 +- src/Game/Functions.hpp | 6 +-- 5 files changed, 72 insertions(+), 40 deletions(-) diff --git a/src/Components/Modules/Dvar.cpp b/src/Components/Modules/Dvar.cpp index 1279840b..5fa042c7 100644 --- a/src/Components/Modules/Dvar.cpp +++ b/src/Components/Modules/Dvar.cpp @@ -8,24 +8,39 @@ namespace Components Dvar::Var::Var(const std::string& dvarName) : Var() { this->dvar = Game::Dvar_FindVar(dvarName.data()); + this->dvarName = dvarName; + } - if (!this->dvar) + void Dvar::Var::registerDvar() + { + assert(!this->dvarName.empty() && this->dvar == nullptr); + + auto* dvar = Game::Dvar_FindVar(this->dvarName.data()); + + // If the dvar can't be found it will be registered as an empty string dvar + if (dvar == nullptr) { - // Quick-register the dvar - Game::Dvar_SetStringByName(dvarName.data(), ""); - this->dvar = Game::Dvar_FindVar(dvarName.data()); + this->dvar = const_cast(Game::Dvar_SetFromStringByNameFromSource(this->dvarName.data(), "", + Game::DvarSetSource::DVAR_SOURCE_INTERNAL)); + } + else + { + this->dvar = dvar; } } template <> Game::dvar_t* Dvar::Var::get() { + if (this->dvar == nullptr) + this->registerDvar(); + return this->dvar; } template <> const char* Dvar::Var::get() { if (this->dvar == nullptr) - return ""; + this->registerDvar(); if (this->dvar->type == Game::dvar_type::DVAR_TYPE_STRING || this->dvar->type == Game::dvar_type::DVAR_TYPE_ENUM) @@ -39,7 +54,10 @@ namespace Components template <> int Dvar::Var::get() { - if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_INT || this->dvar->type == Game::dvar_type::DVAR_TYPE_ENUM) + if (this->dvar == nullptr) + this->registerDvar(); + + if (this->dvar->type == Game::dvar_type::DVAR_TYPE_INT || this->dvar->type == Game::dvar_type::DVAR_TYPE_ENUM) { return this->dvar->current.integer; } @@ -49,34 +67,51 @@ namespace Components template <> unsigned int Dvar::Var::get() { - return static_cast(this->get()); - } + if (this->dvar == nullptr) + this->registerDvar(); - template <> float Dvar::Var::get() - { - if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT) + if (this->dvar->type == Game::dvar_type::DVAR_TYPE_INT) { - return this->dvar->current.value; + return this->dvar->current.unsignedInt; } return 0; } + template <> float Dvar::Var::get() + { + if (this->dvar == nullptr) + this->registerDvar(); + + if (this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT) + { + return this->dvar->current.value; + } + + return 0.f; + } + template <> float* Dvar::Var::get() { - static float val[4] = { 0 }; + static Game::vec4_t vector{ 0.f, 0.f, 0.f, 0.f }; + + if (this->dvar == nullptr) + this->registerDvar(); if (this->dvar && (this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT_2 || this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT_3 || this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT_4)) { return this->dvar->current.vector; } - return val; + return vector; } template <> bool Dvar::Var::get() { - if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_BOOL) + if (this->dvar == nullptr) + this->registerDvar(); + + if (this->dvar->type == Game::dvar_type::DVAR_TYPE_BOOL) { return this->dvar->current.enabled; } @@ -89,11 +124,6 @@ namespace Components return this->get(); } - void Dvar::Var::set(char* string) - { - this->set(const_cast(string)); - } - void Dvar::Var::set(const char* string) { assert(this->dvar->type == Game::DVAR_TYPE_STRING); @@ -242,7 +272,7 @@ namespace Components return Dvar::Register(name, username.data(), Dvar::Flag(flag | Game::dvar_flag::DVAR_FLAG_SAVED).val, description).get(); } - Game::dvar_t* Dvar::SetFromStringByNameSafeExternal(const char* dvar, const char* value) + void Dvar::SetFromStringByNameSafeExternal(const char* dvarName, const char* string) { static const char* exceptions[] = { @@ -258,18 +288,19 @@ namespace Components for (int i = 0; i < ARRAYSIZE(exceptions); ++i) { - if (Utils::String::ToLower(dvar) == Utils::String::ToLower(exceptions[i])) + if (Utils::String::ToLower(dvarName) == Utils::String::ToLower(exceptions[i])) { - return Game::Dvar_SetFromStringByName(dvar, value); + Game::Dvar_SetFromStringByNameFromSource(dvarName, string, Game::DvarSetSource::DVAR_SOURCE_INTERNAL); + return; } } - return Dvar::SetFromStringByNameExternal(dvar, value); + return Dvar::SetFromStringByNameExternal(dvarName, string); } - Game::dvar_t* Dvar::SetFromStringByNameExternal(const char* dvar, const char* value) + void Dvar::SetFromStringByNameExternal(const char* dvarName, const char* string) { - return Game::Dvar_SetFromStringByNameFromSource(dvar, value, Game::DvarSetSource::DVAR_SOURCE_EXTERNAL); + Game::Dvar_SetFromStringByNameFromSource(dvarName, string, Game::DvarSetSource::DVAR_SOURCE_EXTERNAL); } void Dvar::SaveArchiveDvar(const Game::dvar_t* var) @@ -349,7 +380,7 @@ namespace Components Utils::Hook(0x63CDB5, Dvar::SetFromStringByNameExternal, HOOK_CALL).install()->quick(); Utils::Hook(0x635E47, Dvar::SetFromStringByNameExternal, HOOK_CALL).install()->quick(); - // SetDvar + // Script_SetDvar Utils::Hook(0x63444C, Dvar::SetFromStringByNameSafeExternal, HOOK_CALL).install()->quick(); // Slider diff --git a/src/Components/Modules/Dvar.hpp b/src/Components/Modules/Dvar.hpp index b29ef1bd..5d90de11 100644 --- a/src/Components/Modules/Dvar.hpp +++ b/src/Components/Modules/Dvar.hpp @@ -18,14 +18,14 @@ namespace Components { public: Var() : dvar(nullptr) {}; - Var(const Var &obj) { this->dvar = obj.dvar; }; - Var(Game::dvar_t* _dvar) : dvar(_dvar) {}; + Var(const Var& obj) { this->dvar = obj.dvar; this->dvarName = obj.dvarName; }; + Var(Game::dvar_t* _dvar) : dvar(_dvar), dvarName(_dvar->name) {}; Var(DWORD ppdvar) : Var(*reinterpret_cast(ppdvar)) {}; Var(const std::string& dvarName); + void registerDvar(); template T get(); - void set(char* string); void set(const char* string); void set(const std::string& string); @@ -38,6 +38,7 @@ namespace Components void setRaw(bool enabled); private: + std::string dvarName; Game::dvar_t* dvar; }; @@ -58,8 +59,8 @@ namespace Components static Game::dvar_t* RegisterName(const char* name, const char* defaultVal, Game::dvar_flag flag, const char* description); - static Game::dvar_t* SetFromStringByNameExternal(const char* dvar, const char* value); - static Game::dvar_t* SetFromStringByNameSafeExternal(const char* dvar, const char* value); + static void SetFromStringByNameExternal(const char* dvar, const char* value); + static void SetFromStringByNameSafeExternal(const char* dvar, const char* value); static void SaveArchiveDvar(const Game::dvar_t* var); static void DvarSetFromStringByNameStub(const char* dvarName, const char* value); diff --git a/src/Components/Modules/Gamepad.cpp b/src/Components/Modules/Gamepad.cpp index e85e61dd..7e6f677c 100644 --- a/src/Components/Modules/Gamepad.cpp +++ b/src/Components/Modules/Gamepad.cpp @@ -1731,7 +1731,7 @@ namespace Components gpad_button_deadzone = Dvar::Register("gpad_button_deadzone", 0.13f, 0.0f, 1.0f, Game::DVAR_FLAG_NONE, "Game pad button deadzone threshhold"); gpad_button_lstick_deflect_max = Dvar::Register("gpad_button_lstick_deflect_max", 1.0f, 0.0f, 1.0f, Game::DVAR_FLAG_NONE, "Game pad maximum pad stick pressed value"); gpad_button_rstick_deflect_max = Dvar::Register("gpad_button_rstick_deflect_max", 1.0f, 0.0f, 1.0f, Game::DVAR_FLAG_NONE, "Game pad maximum pad stick pressed value"); - gpad_use_hold_time = Dvar::Register("gpad_use_hold_time", 250, 0, INT32_MAX, Game::DVAR_FLAG_NONE, "Time to hold the 'use' button on gamepads to activate use"); + gpad_use_hold_time = Dvar::Register("gpad_use_hold_time", 250, 0, std::numeric_limits::max(), Game::DVAR_FLAG_NONE, "Time to hold the 'use' button on gamepads to activate use"); gpad_lockon_enabled = Dvar::Register("gpad_lockon_enabled", true, Game::DVAR_FLAG_SAVED, "Game pad lockon aim assist enabled"); gpad_slowdown_enabled = Dvar::Register("gpad_slowdown_enabled", true, Game::DVAR_FLAG_SAVED, "Game pad slowdown aim assist enabled"); @@ -1762,10 +1762,10 @@ namespace Components aim_lockon_strength = Dvar::Var("aim_lockon_strength"); } - void Gamepad::IN_Init_Hk() + void Gamepad::CG_RegisterDvars_Hk() { // Call original method - Utils::Hook::Call(0x45D620)(); + Utils::Hook::Call(0x4F8DC0)(); InitDvars(); } @@ -1908,7 +1908,7 @@ namespace Components return; // Initialize gamepad environment - Utils::Hook(0x467C03, IN_Init_Hk, HOOK_CALL).install()->quick(); + Utils::Hook(0x4059FE, CG_RegisterDvars_Hk, HOOK_CALL).install()->quick(); // package the forward and right move components in the move buttons Utils::Hook(0x60E38D, MSG_WriteDeltaUsercmdKeyStub, HOOK_JUMP).install()->quick(); diff --git a/src/Components/Modules/Gamepad.hpp b/src/Components/Modules/Gamepad.hpp index 5c55325c..de0c16e0 100644 --- a/src/Components/Modules/Gamepad.hpp +++ b/src/Components/Modules/Gamepad.hpp @@ -187,7 +187,7 @@ namespace Components static void Scores_Toggle_f(Command::Params* params); static void InitDvars(); - static void IN_Init_Hk(); + static void CG_RegisterDvars_Hk(); static const char* GetGamePadCommand(const char* command); static int Key_GetCommandAssignmentInternal_Hk(const char* cmd, int(*keys)[2]); diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index 9e7b7132..045e2193 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -256,10 +256,10 @@ namespace Game typedef dvar_t* (__cdecl * Dvar_RegisterColor_t)(const char* name, float r, float g, float b, float a, int flags, const char* description); extern Dvar_RegisterColor_t Dvar_RegisterColor; - typedef dvar_t* (__cdecl * Dvar_SetFromStringByName_t)(const char* cvar, const char* value); + typedef void(__cdecl * Dvar_SetFromStringByName_t)(const char* dvarName, const char* string); extern Dvar_SetFromStringByName_t Dvar_SetFromStringByName; - typedef dvar_t* (__cdecl * Dvar_SetFromStringByNameFromSource_t)(const char* cvar, const char* value, DvarSetSource source); + typedef const dvar_t* (__cdecl * Dvar_SetFromStringByNameFromSource_t)(const char* dvarName, const char* string, DvarSetSource source); extern Dvar_SetFromStringByNameFromSource_t Dvar_SetFromStringByNameFromSource; typedef void (__cdecl * Dvar_SetStringByName_t)(const char* cvar, const char* value); @@ -286,7 +286,7 @@ namespace Game typedef char* (__cdecl* Dvar_InfoString_Big_t)(int typeMask); extern Dvar_InfoString_Big_t Dvar_InfoString_Big; - typedef dvar_t* (__cdecl * Dvar_SetCommand_t)(const char* name, const char* value); + typedef void(__cdecl * Dvar_SetCommand_t)(const char* dvarName, const char* string); extern Dvar_SetCommand_t Dvar_SetCommand; typedef const char* (__cdecl * Dvar_DisplayableValue_t)(const dvar_t* cvar); From 39812a93705f8e98de7b3312303c13bbc17e00f5 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Thu, 3 Feb 2022 13:08:25 +0100 Subject: [PATCH 2/8] I forgot I'm not a Java programmer --- src/Components/Modules/Dvar.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Components/Modules/Dvar.cpp b/src/Components/Modules/Dvar.cpp index 5fa042c7..057bc52b 100644 --- a/src/Components/Modules/Dvar.cpp +++ b/src/Components/Modules/Dvar.cpp @@ -15,17 +15,17 @@ namespace Components { assert(!this->dvarName.empty() && this->dvar == nullptr); - auto* dvar = Game::Dvar_FindVar(this->dvarName.data()); + auto* var = Game::Dvar_FindVar(this->dvarName.data()); // If the dvar can't be found it will be registered as an empty string dvar - if (dvar == nullptr) + if (var == nullptr) { this->dvar = const_cast(Game::Dvar_SetFromStringByNameFromSource(this->dvarName.data(), "", Game::DvarSetSource::DVAR_SOURCE_INTERNAL)); } else { - this->dvar = dvar; + this->dvar = var; } } From bc68a05b8dbfb9dd605f72ce2e341a176c731377 Mon Sep 17 00:00:00 2001 From: FutureRave Date: Sun, 6 Feb 2022 22:22:04 +0000 Subject: [PATCH 3/8] Change param name --- src/Components/Modules/Dvar.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Components/Modules/Dvar.cpp b/src/Components/Modules/Dvar.cpp index 057bc52b..fefb92f6 100644 --- a/src/Components/Modules/Dvar.cpp +++ b/src/Components/Modules/Dvar.cpp @@ -5,10 +5,10 @@ namespace Components Utils::Signal Dvar::RegistrationSignal; const char* Dvar::ArchiveDvarPath = "userraw/archivedvars.cfg"; - Dvar::Var::Var(const std::string& dvarName) : Var() + Dvar::Var::Var(const std::string& _dvarName) : Var() { - this->dvar = Game::Dvar_FindVar(dvarName.data()); - this->dvarName = dvarName; + this->dvar = Game::Dvar_FindVar(_dvarName.data()); + this->dvarName = _dvarName; } void Dvar::Var::registerDvar() From 060cb1d0a05866d6a30df319dece11c5abd285c6 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 15 Feb 2022 17:18:18 +0000 Subject: [PATCH 4/8] Bump deps/libtommath from `bea9270` to `04e9d1e` Bumps [deps/libtommath](https://github.com/libtom/libtommath) from `bea9270` to `04e9d1e`. - [Release notes](https://github.com/libtom/libtommath/releases) - [Commits](https://github.com/libtom/libtommath/compare/bea9270646303baf683f4ba2ddf0d70721f0e55d...04e9d1e7a0493910b2eb5e757d623870692ada04) --- updated-dependencies: - dependency-name: deps/libtommath dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- deps/libtommath | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/libtommath b/deps/libtommath index bea92706..04e9d1e7 160000 --- a/deps/libtommath +++ b/deps/libtommath @@ -1 +1 @@ -Subproject commit bea9270646303baf683f4ba2ddf0d70721f0e55d +Subproject commit 04e9d1e7a0493910b2eb5e757d623870692ada04 From 53ebf0d91e9feeb1bb888bddc73b3c581ae71509 Mon Sep 17 00:00:00 2001 From: FutureRave Date: Sat, 19 Feb 2022 23:06:56 +0000 Subject: [PATCH 5/8] Make pr ready to be merged --- src/Components/Modules/Dvar.cpp | 37 ++++++++++----------------------- src/Components/Modules/Dvar.hpp | 6 ++---- src/Game/Functions.hpp | 8 +++---- 3 files changed, 17 insertions(+), 34 deletions(-) diff --git a/src/Components/Modules/Dvar.cpp b/src/Components/Modules/Dvar.cpp index fefb92f6..e2b163a0 100644 --- a/src/Components/Modules/Dvar.cpp +++ b/src/Components/Modules/Dvar.cpp @@ -5,42 +5,27 @@ namespace Components Utils::Signal Dvar::RegistrationSignal; const char* Dvar::ArchiveDvarPath = "userraw/archivedvars.cfg"; - Dvar::Var::Var(const std::string& _dvarName) : Var() + Dvar::Var::Var(const std::string& dvarName) : Var() { - this->dvar = Game::Dvar_FindVar(_dvarName.data()); - this->dvarName = _dvarName; - } - - void Dvar::Var::registerDvar() - { - assert(!this->dvarName.empty() && this->dvar == nullptr); - - auto* var = Game::Dvar_FindVar(this->dvarName.data()); + this->dvar = Game::Dvar_FindVar(dvarName.data()); // If the dvar can't be found it will be registered as an empty string dvar - if (var == nullptr) + if (this->dvar == nullptr) { - this->dvar = const_cast(Game::Dvar_SetFromStringByNameFromSource(this->dvarName.data(), "", + this->dvar = const_cast(Game::Dvar_SetFromStringByNameFromSource(dvarName.data(), "", Game::DvarSetSource::DVAR_SOURCE_INTERNAL)); - } - else - { - this->dvar = var; } } template <> Game::dvar_t* Dvar::Var::get() { - if (this->dvar == nullptr) - this->registerDvar(); - return this->dvar; } template <> const char* Dvar::Var::get() { if (this->dvar == nullptr) - this->registerDvar(); + return ""; if (this->dvar->type == Game::dvar_type::DVAR_TYPE_STRING || this->dvar->type == Game::dvar_type::DVAR_TYPE_ENUM) @@ -55,7 +40,7 @@ namespace Components template <> int Dvar::Var::get() { if (this->dvar == nullptr) - this->registerDvar(); + return 0; if (this->dvar->type == Game::dvar_type::DVAR_TYPE_INT || this->dvar->type == Game::dvar_type::DVAR_TYPE_ENUM) { @@ -68,20 +53,20 @@ namespace Components template <> unsigned int Dvar::Var::get() { if (this->dvar == nullptr) - this->registerDvar(); + return 0u; if (this->dvar->type == Game::dvar_type::DVAR_TYPE_INT) { return this->dvar->current.unsignedInt; } - return 0; + return 0u; } template <> float Dvar::Var::get() { if (this->dvar == nullptr) - this->registerDvar(); + return 0.f; if (this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT) { @@ -96,7 +81,7 @@ namespace Components static Game::vec4_t vector{ 0.f, 0.f, 0.f, 0.f }; if (this->dvar == nullptr) - this->registerDvar(); + return vector; if (this->dvar && (this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT_2 || this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT_3 || this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT_4)) { @@ -109,7 +94,7 @@ namespace Components template <> bool Dvar::Var::get() { if (this->dvar == nullptr) - this->registerDvar(); + return false; if (this->dvar->type == Game::dvar_type::DVAR_TYPE_BOOL) { diff --git a/src/Components/Modules/Dvar.hpp b/src/Components/Modules/Dvar.hpp index 5d90de11..8f87eaf5 100644 --- a/src/Components/Modules/Dvar.hpp +++ b/src/Components/Modules/Dvar.hpp @@ -18,12 +18,11 @@ namespace Components { public: Var() : dvar(nullptr) {}; - Var(const Var& obj) { this->dvar = obj.dvar; this->dvarName = obj.dvarName; }; - Var(Game::dvar_t* _dvar) : dvar(_dvar), dvarName(_dvar->name) {}; + Var(const Var& obj) { this->dvar = obj.dvar; }; + Var(Game::dvar_t* _dvar) : dvar(_dvar) {}; Var(DWORD ppdvar) : Var(*reinterpret_cast(ppdvar)) {}; Var(const std::string& dvarName); - void registerDvar(); template T get(); void set(const char* string); @@ -38,7 +37,6 @@ namespace Components void setRaw(bool enabled); private: - std::string dvarName; Game::dvar_t* dvar; }; diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index 045e2193..5991519b 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -265,16 +265,16 @@ namespace Game typedef void (__cdecl * Dvar_SetStringByName_t)(const char* cvar, const char* value); extern Dvar_SetStringByName_t Dvar_SetStringByName; - typedef void (__cdecl * Dvar_SetString_t)(dvar_t* cvar, const char* value); + typedef void (__cdecl * Dvar_SetString_t)(const dvar_t* cvar, const char* value); extern Dvar_SetString_t Dvar_SetString; - typedef void (__cdecl * Dvar_SetBool_t)(dvar_t* cvar, bool enabled); + typedef void (__cdecl * Dvar_SetBool_t)(const dvar_t* cvar, bool enabled); extern Dvar_SetBool_t Dvar_SetBool; - typedef void (__cdecl * Dvar_SetFloat_t)(dvar_t* cvar, float value); + typedef void (__cdecl * Dvar_SetFloat_t)(const dvar_t* cvar, float value); extern Dvar_SetFloat_t Dvar_SetFloat; - typedef void (__cdecl * Dvar_SetInt_t)(dvar_t* cvar, int integer); + typedef void (__cdecl * Dvar_SetInt_t)(const dvar_t* cvar, int integer); extern Dvar_SetInt_t Dvar_SetInt; typedef void(__cdecl * Dvar_GetUnpackedColorByName_t)(const char* name, float* color); From 7b91b9c781895236b0b12e3168efccbe149e0461 Mon Sep 17 00:00:00 2001 From: FutureRave Date: Sat, 19 Feb 2022 23:10:35 +0000 Subject: [PATCH 6/8] make code more readable --- src/Components/Modules/Dvar.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Components/Modules/Dvar.cpp b/src/Components/Modules/Dvar.cpp index e2b163a0..d5ec5fea 100644 --- a/src/Components/Modules/Dvar.cpp +++ b/src/Components/Modules/Dvar.cpp @@ -83,7 +83,8 @@ namespace Components if (this->dvar == nullptr) return vector; - if (this->dvar && (this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT_2 || this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT_3 || this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT_4)) + if (this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT_2 || this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT_3 + || this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT_4) { return this->dvar->current.vector; } @@ -280,7 +281,7 @@ namespace Components } } - return Dvar::SetFromStringByNameExternal(dvarName, string); + Dvar::SetFromStringByNameExternal(dvarName, string); } void Dvar::SetFromStringByNameExternal(const char* dvarName, const char* string) From a5cc395b652650514eadb3ce7653ed3e3d22456b Mon Sep 17 00:00:00 2001 From: FutureRave Date: Sat, 26 Feb 2022 22:50:53 +0000 Subject: [PATCH 7/8] Refactor some utils --- src/Components/Modules/Auth.cpp | 15 ++++++++------- src/Components/Modules/Bots.cpp | 2 +- src/Components/Modules/Changelog.cpp | 2 +- src/Components/Modules/Dedicated.cpp | 4 ++-- src/Components/Modules/Maps.cpp | 2 +- src/Components/Modules/Node.cpp | 2 +- src/Components/Modules/ServerInfo.cpp | 2 +- src/Components/Modules/ServerList.cpp | 4 ++-- src/Game/Functions.hpp | 2 +- src/Utils/CSV.cpp | 2 +- src/Utils/InfoString.cpp | 25 ++++++++++++++----------- src/Utils/InfoString.hpp | 2 -- src/Utils/String.cpp | 21 +++++++-------------- src/Utils/String.hpp | 2 +- 14 files changed, 41 insertions(+), 46 deletions(-) diff --git a/src/Components/Modules/Auth.cpp b/src/Components/Modules/Auth.cpp index e090e1bb..3c06bf72 100644 --- a/src/Components/Modules/Auth.cpp +++ b/src/Components/Modules/Auth.cpp @@ -8,7 +8,8 @@ namespace Components Utils::Cryptography::Token Auth::ComputeToken; Utils::Cryptography::ECC::Key Auth::GuidKey; - std::vector Auth::BannedUids = { + std::vector Auth::BannedUids = + { 0xf4d2c30b712ac6e3, 0xf7e33c4081337fa3, 0x6f5597f103cc50e9 @@ -179,8 +180,8 @@ namespace Components Utils::InfoString infostr(params[2]); // Read the required data - std::string steamId = infostr.get("xuid"); - std::string challenge = infostr.get("challenge"); + const auto& steamId = infostr.get("xuid"); + const auto& challenge = infostr.get("challenge"); if (steamId.empty() || challenge.empty()) { @@ -189,12 +190,12 @@ namespace Components } // Parse the id - unsigned __int64 xuid = strtoull(steamId.data(), nullptr, 16); + const auto xuid = strtoull(steamId.data(), nullptr, 16); SteamID guid; guid.bits = xuid; - if (Bans::IsBanned({ guid, address.getIP() })) + if (Bans::IsBanned({guid, address.getIP()})) { Network::Send(address, "error\nEXE_ERR_BANNED_PERM"); return; @@ -223,8 +224,8 @@ namespace Components } // Verify the security level - uint32_t ourLevel = static_cast(Dvar::Var("sv_securityLevel").get()); - uint32_t userLevel = Auth::GetZeroBits(connectData.token(), connectData.publickey()); + auto ourLevel = Dvar::Var("sv_securityLevel").get(); + auto userLevel = Auth::GetZeroBits(connectData.token(), connectData.publickey()); if (userLevel < ourLevel) { diff --git a/src/Components/Modules/Bots.cpp b/src/Components/Modules/Bots.cpp index 15e36cfb..f6b5061e 100644 --- a/src/Components/Modules/Bots.cpp +++ b/src/Components/Modules/Bots.cpp @@ -96,7 +96,7 @@ namespace Components if (bots.exists()) { - std::vector names = Utils::String::Explode(bots.getBuffer(), '\n'); + std::vector names = Utils::String::Split(bots.getBuffer(), '\n'); for (auto name : names) { diff --git a/src/Components/Modules/Changelog.cpp b/src/Components/Modules/Changelog.cpp index 059ef0bb..b88513a1 100644 --- a/src/Components/Modules/Changelog.cpp +++ b/src/Components/Modules/Changelog.cpp @@ -19,7 +19,7 @@ namespace Components data = "^1Unable to get changelog."; } - Changelog::Lines = Utils::String::Explode(data, '\n'); + Changelog::Lines = Utils::String::Split(data, '\n'); for (auto& line : Changelog::Lines) { diff --git a/src/Components/Modules/Dedicated.cpp b/src/Components/Modules/Dedicated.cpp index dc856792..6414aeda 100644 --- a/src/Components/Modules/Dedicated.cpp +++ b/src/Components/Modules/Dedicated.cpp @@ -124,7 +124,7 @@ namespace Components { auto rotation = Dvar::Var("sv_mapRotation").get(); - const auto tokens = Utils::String::Explode(rotation, ' '); + const auto tokens = Utils::String::Split(rotation, ' '); std::vector> mapRotationPair; for (auto i = 0u; i < (tokens.size() - 1); i += 2) @@ -207,7 +207,7 @@ namespace Components auto rotation = Dvar::Var("sv_mapRotationCurrent").get(); - auto tokens = Utils::String::Explode(rotation, ' '); + auto tokens = Utils::String::Split(rotation, ' '); for (unsigned int i = 0; i < (tokens.size() - 1); i += 2) { diff --git a/src/Components/Modules/Maps.cpp b/src/Components/Modules/Maps.cpp index a6081034..7a91f2ef 100644 --- a/src/Components/Modules/Maps.cpp +++ b/src/Components/Modules/Maps.cpp @@ -357,7 +357,7 @@ namespace Components { if (arena->keys[j] == "dependency"s) { - return Utils::String::Explode(arena->values[j], ' '); + return Utils::String::Split(arena->values[j], ' '); } } } diff --git a/src/Components/Modules/Node.cpp b/src/Components/Modules/Node.cpp index 2d60d147..bc1af6dc 100644 --- a/src/Components/Modules/Node.cpp +++ b/src/Components/Modules/Node.cpp @@ -60,7 +60,7 @@ namespace Components std::string nodes = Utils::Cache::GetFile("/iw4/nodes.txt"); if (nodes.empty()) return; - auto nodeList = Utils::String::Explode(nodes, '\n'); + auto nodeList = Utils::String::Split(nodes, '\n'); for (auto& node : nodeList) { Utils::String::Replace(node, "\r", ""); diff --git a/src/Components/Modules/ServerInfo.cpp b/src/Components/Modules/ServerInfo.cpp index 04f08bb5..bfd9281d 100644 --- a/src/Components/Modules/ServerInfo.cpp +++ b/src/Components/Modules/ServerInfo.cpp @@ -264,7 +264,7 @@ namespace Components Dvar::Var("uiSi_ModName").set(info.get("fs_game").data() + 5); } - auto lines = Utils::String::Explode(data, '\n'); + auto lines = Utils::String::Split(data, '\n'); if (lines.size() <= 1) return; diff --git a/src/Components/Modules/ServerList.cpp b/src/Components/Modules/ServerList.cpp index 166f444f..ce43bdc7 100644 --- a/src/Components/Modules/ServerList.cpp +++ b/src/Components/Modules/ServerList.cpp @@ -539,8 +539,8 @@ namespace Components bool ServerList::CompareVersion(const std::string& version1, const std::string& version2) { - std::vector subVersions1 = Utils::String::Explode(version1, '.'); - std::vector subVersions2 = Utils::String::Explode(version2, '.'); + std::vector subVersions1 = Utils::String::Split(version1, '.'); + std::vector subVersions2 = Utils::String::Split(version2, '.'); while (subVersions1.size() >= 3) subVersions1.pop_back(); while (subVersions2.size() >= 3) subVersions2.pop_back(); diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index 9e7b7132..d6b83c52 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -283,7 +283,7 @@ namespace Game typedef dvar_t* (__cdecl * Dvar_FindVar_t)(const char *dvarName); extern Dvar_FindVar_t Dvar_FindVar; - typedef char* (__cdecl* Dvar_InfoString_Big_t)(int typeMask); + typedef char* (__cdecl * Dvar_InfoString_Big_t)(int bit); extern Dvar_InfoString_Big_t Dvar_InfoString_Big; typedef dvar_t* (__cdecl * Dvar_SetCommand_t)(const char* name, const char* value); diff --git a/src/Utils/CSV.cpp b/src/Utils/CSV.cpp index 0b0a02d1..efcfcd28 100644 --- a/src/Utils/CSV.cpp +++ b/src/Utils/CSV.cpp @@ -71,7 +71,7 @@ namespace Utils if (!buffer.empty()) { - auto rows = Utils::String::Explode(buffer, '\n'); + auto rows = Utils::String::Split(buffer, '\n'); for (auto& row : rows) { diff --git a/src/Utils/InfoString.cpp b/src/Utils/InfoString.cpp index 09126843..ba861dac 100644 --- a/src/Utils/InfoString.cpp +++ b/src/Utils/InfoString.cpp @@ -9,9 +9,10 @@ namespace Utils std::string InfoString::get(const std::string& key) { - if (this->keyValuePairs.find(key) != this->keyValuePairs.end()) + const auto value = this->keyValuePairs.find(key); + if (value != this->keyValuePairs.end()) { - return this->keyValuePairs[key]; + return value->second; } return ""; @@ -24,11 +25,13 @@ namespace Utils buffer = buffer.substr(1); } - std::vector KeyValues = Utils::String::Explode(buffer, '\\'); + auto KeyValues = Utils::String::Split(buffer, '\\'); - for (unsigned int i = 0; i < (KeyValues.size() - 1); i += 2) + for (size_t i = 0; !KeyValues.empty() && i < (KeyValues.size() - 1); i += 2) { - this->keyValuePairs[KeyValues[i]] = KeyValues[i + 1]; + const auto& key = KeyValues[i]; + const auto& value = KeyValues[i + 1]; + this->keyValuePairs[key] = value; } } @@ -36,16 +39,16 @@ namespace Utils { std::string infoString; - bool first = true; + auto first = true; - for (auto i = this->keyValuePairs.begin(); i != this->keyValuePairs.end(); ++i) + for (const auto& [key, value] : this->keyValuePairs) { if (first) first = false; else infoString.append("\\"); - infoString.append(i->first); // Key + infoString.append(key); infoString.append("\\"); - infoString.append(i->second); // Value + infoString.append(value); } return infoString; @@ -53,9 +56,9 @@ namespace Utils void InfoString::dump() { - for (auto i = this->keyValuePairs.begin(); i != this->keyValuePairs.end(); ++i) + for (const auto& [key, value] : this->keyValuePairs) { - OutputDebugStringA(Utils::String::VA("%s: %s", i->first.data(), i->second.data())); + OutputDebugStringA(Utils::String::VA("%s: %s\n", key.data(), value.data())); } } diff --git a/src/Utils/InfoString.hpp b/src/Utils/InfoString.hpp index d83edde8..f63c6714 100644 --- a/src/Utils/InfoString.hpp +++ b/src/Utils/InfoString.hpp @@ -7,11 +7,9 @@ namespace Utils public: InfoString() {}; InfoString(const std::string& buffer) : InfoString() { this->parse(buffer); }; - InfoString(const InfoString &obj) : keyValuePairs(obj.keyValuePairs) {}; void set(const std::string& key, const std::string& value); std::string get(const std::string& key); - std::string build(); void dump(); diff --git a/src/Utils/String.cpp b/src/Utils/String.cpp index ca2d0664..67d35006 100644 --- a/src/Utils/String.cpp +++ b/src/Utils/String.cpp @@ -62,25 +62,18 @@ namespace Utils return str; } - std::vector Explode(const std::string& str, char delim) + std::vector Split(const std::string& str, const char delim) { - std::vector result; - std::istringstream iss(str); + std::stringstream ss(str); + std::string item; + std::vector elems; - for (std::string token; std::getline(iss, token, delim);) + while (std::getline(ss, item, delim)) { - std::string _entry = std::move(token); - - // Remove trailing 0x0 bytes - while (_entry.size() && !_entry.back()) - { - _entry = _entry.substr(0, _entry.size() - 1); - } - - result.push_back(_entry); + elems.push_back(item); // elems.push_back(std::move(item)); // if C++11 (based on comment from S1x) } - return result; + return elems; } void Replace(std::string &string, const std::string& find, const std::string& replace) diff --git a/src/Utils/String.hpp b/src/Utils/String.hpp index 2b0ed6d1..3beed27e 100644 --- a/src/Utils/String.hpp +++ b/src/Utils/String.hpp @@ -78,7 +78,7 @@ namespace Utils std::string ToLower(std::string input); std::string ToUpper(std::string input); bool EndsWith(const std::string& haystack, const std::string& needle); - std::vector Explode(const std::string& str, char delim); + std::vector Split(const std::string& str, const char delim); void Replace(std::string &string, const std::string& find, const std::string& replace); bool StartsWith(const std::string& haystack, const std::string& needle); std::string <rim(std::string &s); From 624eba7114aa22bd8469013e159e64bc8cc7ee45 Mon Sep 17 00:00:00 2001 From: FutureRave Date: Sat, 26 Feb 2022 23:02:04 +0000 Subject: [PATCH 8/8] Auto keyword --- src/Components/Modules/Auth.cpp | 2 +- src/Components/Modules/ServerList.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Components/Modules/Auth.cpp b/src/Components/Modules/Auth.cpp index 3c06bf72..7af4580e 100644 --- a/src/Components/Modules/Auth.cpp +++ b/src/Components/Modules/Auth.cpp @@ -190,7 +190,7 @@ namespace Components } // Parse the id - const auto xuid = strtoull(steamId.data(), nullptr, 16); + const auto xuid = std::strtoull(steamId.data(), nullptr, 16); SteamID guid; guid.bits = xuid; diff --git a/src/Components/Modules/ServerList.cpp b/src/Components/Modules/ServerList.cpp index ce43bdc7..f15e09da 100644 --- a/src/Components/Modules/ServerList.cpp +++ b/src/Components/Modules/ServerList.cpp @@ -539,8 +539,8 @@ namespace Components bool ServerList::CompareVersion(const std::string& version1, const std::string& version2) { - std::vector subVersions1 = Utils::String::Split(version1, '.'); - std::vector subVersions2 = Utils::String::Split(version2, '.'); + auto subVersions1 = Utils::String::Split(version1, '.'); + auto subVersions2 = Utils::String::Split(version2, '.'); while (subVersions1.size() >= 3) subVersions1.pop_back(); while (subVersions2.size() >= 3) subVersions2.pop_back();