From d6c23efe687194ebfcbd1c09ef721ef0db2d2121 Mon Sep 17 00:00:00 2001 From: Edo Date: Fri, 10 Mar 2023 01:03:50 +0000 Subject: [PATCH] [Dvar]: Add argument to protect dvars (#823) --- src/Components/Modules/Dvar.cpp | 17 +++++++++++++++ src/Components/Modules/Dvar.hpp | 1 + src/Components/Modules/Party.cpp | 14 ++++++++----- src/Components/Modules/QuickPatch.cpp | 30 ++++++++++++++++++++++++--- src/Components/Modules/QuickPatch.hpp | 2 ++ 5 files changed, 56 insertions(+), 8 deletions(-) diff --git a/src/Components/Modules/Dvar.cpp b/src/Components/Modules/Dvar.cpp index 04f10714..83a591da 100644 --- a/src/Components/Modules/Dvar.cpp +++ b/src/Components/Modules/Dvar.cpp @@ -298,8 +298,25 @@ namespace Components return flag.value(); } + bool Dvar::IsSettingArchiveDvarsDisabled() + { + static std::optional flag; + + if (!flag.has_value()) + { + flag.emplace(Flags::HasFlag("protect-dvars")); + } + + return flag.value(); + } + void Dvar::DvarSetFromStringByName_Stub(const char* dvarName, const char* value) { + if (IsSettingArchiveDvarsDisabled()) + { + return; + } + // Save the dvar original value if it has the archive flag const auto* dvar = Game::Dvar_FindVar(dvarName); if (dvar && dvar->flags & Game::DVAR_ARCHIVE) diff --git a/src/Components/Modules/Dvar.hpp b/src/Components/Modules/Dvar.hpp index 9ad5215c..8e905873 100644 --- a/src/Components/Modules/Dvar.hpp +++ b/src/Components/Modules/Dvar.hpp @@ -48,6 +48,7 @@ namespace Components static void SetFromStringByNameSafeExternal(const char* dvarName, const char* string); static bool AreArchiveDvarsUnprotected(); + static bool IsSettingArchiveDvarsDisabled(); static void DvarSetFromStringByName_Stub(const char* dvarName, const char* value); static void OnRegisterVariant(Game::dvar_t* dvar); diff --git a/src/Components/Modules/Party.cpp b/src/Components/Modules/Party.cpp index a19da69a..b77a70c3 100644 --- a/src/Components/Modules/Party.cpp +++ b/src/Components/Modules/Party.cpp @@ -10,6 +10,8 @@ #include +#define CL_MOD_LOADING + namespace Components { class JoinContainer @@ -448,13 +450,13 @@ namespace Components Container.valid = false; Container.info = info; - Container.matchType = atoi(info.get("matchtype").data()); - auto securityLevel = static_cast(atoi(info.get("securityLevel").data())); + Container.matchType = std::strtol(info.get("matchtype").data(), nullptr, 10); + auto securityLevel = std::strtoul(info.get("securityLevel").data(), nullptr, 10); bool isUsermap = !info.get("usermaphash").empty(); - auto usermapHash = static_cast(atoi(info.get("usermaphash").data())); - + auto usermapHash = std::strtoul(info.get("usermaphash").data(), nullptr, 10); +#ifdef CL_MOD_LOADING std::string mod = (*Game::fs_gameDirVar)->current.string; - +#endif // set fast server stuff here so its updated when we go to download stuff if (info.get("wwwDownload") == "1"s) { @@ -497,6 +499,7 @@ namespace Components Command::Execute("closemenu popup_reconnectingtoparty"); Download::InitiateMapDownload(info.get("mapname"), info.get("isPrivate") == "1"); } +#ifdef CL_MOD_LOADING else if (!info.get("fs_game").empty() && Utils::String::ToLower(mod) != Utils::String::ToLower(info.get("fs_game"))) { Command::Execute("closemenu popup_reconnectingtoparty"); @@ -513,6 +516,7 @@ namespace Components Command::Execute("reconnect", false); } +#endif else { if (!Maps::CheckMapInstalled(Container.info.get("mapname"), true)) return; diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index 68de56bf..4eb6a71a 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -281,6 +281,27 @@ namespace Components } } + bool QuickPatch::CL_ShouldSendNotify_Hk(const char* cmd) + { + if (!cmd) + { + return false; + } + + static std::vector exceptions = + { + "vstr", + "wait", + }; + + if (std::ranges::find(exceptions, cmd) != exceptions.end()) + { + return false; + } + + return Utils::Hook::Call(0x47A640)(cmd); + } + Game::dvar_t* QuickPatch::Dvar_RegisterConMinicon(const char* dvarName, [[maybe_unused]] bool value, unsigned __int16 flags, const char* description) { #ifdef _DEBUG @@ -347,6 +368,9 @@ namespace Components Utils::Hook::Set(0x4876C6, "Successfully read stats data\n"); + // Protect players from invasive servers + Utils::Hook(0x434BD4, CL_ShouldSendNotify_Hk, HOOK_CALL).install()->quick(); // CL_CheckNotify + // Numerical ping (cg_scoreboardPingText 1) Utils::Hook::Set(0x45888E, 1); Utils::Hook::Set(0x45888C, Game::DVAR_CHEAT); @@ -597,18 +621,18 @@ namespace Components for (int i = 0; i < ARRAYSIZE(Game::MaterialTechniqueSet::techniques); ++i) { - Game::MaterialTechnique* technique = asset.techniqueSet->techniques[i]; + auto* technique = asset.techniqueSet->techniques[i]; if (technique) { // Size-check is obsolete, as the structure is dynamic buffer.align(Utils::Stream::ALIGN_4); - Game::MaterialTechnique* destTechnique = buffer.dest(); + auto* destTechnique = buffer.dest(); buffer.save(technique, 8); // Save_MaterialPassArray - Game::MaterialPass* destPasses = buffer.dest(); + auto* destPasses = buffer.dest(); buffer.saveArray(technique->passArray, technique->passCount); for (std::uint16_t j = 0; j < technique->passCount; ++j) diff --git a/src/Components/Modules/QuickPatch.hpp b/src/Components/Modules/QuickPatch.hpp index 8f18c838..fe100e03 100644 --- a/src/Components/Modules/QuickPatch.hpp +++ b/src/Components/Modules/QuickPatch.hpp @@ -34,6 +34,8 @@ namespace Components static void SND_GetAliasOffset_Stub(); + static bool CL_ShouldSendNotify_Hk(const char* cmd); + static Game::dvar_t* Dvar_RegisterConMinicon(const char* dvarName, bool value, unsigned __int16 flags, const char* description); }; }