From c9d53065535a4de3ae369ec07f272515770b978a Mon Sep 17 00:00:00 2001 From: FutureRave Date: Mon, 21 Mar 2022 18:55:35 +0000 Subject: [PATCH] Address review --- src/Components/Modules/Bots.cpp | 77 +++++++++++++++------- src/Components/Modules/Bots.hpp | 4 +- src/Components/Modules/Dvar.cpp | 1 - src/Components/Modules/ScriptExtension.cpp | 29 +++++++- src/Components/Modules/ScriptExtension.hpp | 1 + src/Game/Functions.cpp | 17 +++++ src/Game/Functions.hpp | 13 +++- 7 files changed, 111 insertions(+), 31 deletions(-) diff --git a/src/Components/Modules/Bots.cpp b/src/Components/Modules/Bots.cpp index 36841f67..884c3c10 100644 --- a/src/Components/Modules/Bots.cpp +++ b/src/Components/Modules/Bots.cpp @@ -2,7 +2,6 @@ namespace Components { - Game::dvar_t* Bots::TestClientsActivate; std::vector Bots::BotNames; struct BotMovementInfo @@ -11,6 +10,7 @@ namespace Components int8_t forward; int8_t right; uint16_t weapon; + bool active; }; static BotMovementInfo g_botai[18]; @@ -36,7 +36,8 @@ namespace Components { "leanright", Game::usercmdButtonBits::CMD_BUTTON_LEAN_RIGHT }, { "ads", Game::usercmdButtonBits::CMD_BUTTON_ADS }, { "holdbreath", Game::usercmdButtonBits::CMD_BUTTON_BREATH }, - { "use", Game::usercmdButtonBits::CMD_BUTTON_USE_RELOAD | Game::usercmdButtonBits::CMD_BUTTON_ACTIVATE }, + { "usereload", Game::usercmdButtonBits::CMD_BUTTON_USE_RELOAD }, + { "activate", Game::usercmdButtonBits::CMD_BUTTON_ACTIVATE }, { "0", Bots::NUM_0 }, { "1", Bots::NUM_1 }, { "2", Bots::NUM_2 }, @@ -51,7 +52,7 @@ namespace Components int Bots::BuildConnectString(char* buffer, const char* connectString, int num, int, int protocol, int checksum, int statVer, int statStuff, int port) { - static auto botId = 0; + static size_t botId = 0; const char* botName; if (Bots::BotNames.empty()) @@ -156,6 +157,7 @@ namespace Components g_botai[entref.entnum] = {0}; g_botai[entref.entnum].weapon = 1; + g_botai[entref.entnum].active = false; }); Script::AddFunction("BotWeapon", [](Game::scr_entref_t entref) // Usage: BotWeapon(); @@ -179,6 +181,7 @@ namespace Components const auto weapId = Game::G_GetWeaponIndexForName(weapon); g_botai[entref.entnum].weapon = static_cast(weapId); + g_botai[entref.entnum].active = true; }); Script::AddFunction("BotAction", [](Game::scr_entref_t entref) // Usage: BotAction(); @@ -216,6 +219,7 @@ namespace Components else g_botai[entref.entnum].buttons &= ~(BotActions[i].key); + g_botai[entref.entnum].active = true; return; } @@ -241,6 +245,7 @@ namespace Components g_botai[entref.entnum].forward = static_cast(forwardInt); g_botai[entref.entnum].right = static_cast(rightInt); + g_botai[entref.entnum].active = true; }); } @@ -251,16 +256,23 @@ namespace Components const auto entnum = cl->gentity->s.number; - Game::usercmd_s ucmd = {0}; + // Keep test client functionality + if (!g_botai[entnum].active) + { + Game::SV_BotUserMove(cl); + return; + } - ucmd.serverTime = *Game::svs_time; + Game::usercmd_s userCmd = {0}; - ucmd.buttons = g_botai[entnum].buttons; - ucmd.forwardmove = g_botai[entnum].forward; - ucmd.rightmove = g_botai[entnum].right; - ucmd.weapon = g_botai[entnum].weapon; + userCmd.serverTime = *Game::svs_time; - Game::SV_ClientThink(cl, &ucmd); + userCmd.buttons = g_botai[entnum].buttons; + userCmd.forwardmove = g_botai[entnum].forward; + userCmd.rightmove = g_botai[entnum].right; + userCmd.weapon = g_botai[entnum].weapon; + + Game::SV_ClientThink(cl, &userCmd); } constexpr auto SV_BotUserMove = 0x626E50; @@ -268,13 +280,6 @@ namespace Components { __asm { - push eax - mov eax, Bots::TestClientsActivate - cmp byte ptr [eax + 0x10], 0x1 - pop eax - - jz enableBots - pushad push edi @@ -282,20 +287,42 @@ namespace Components add esp, 4 popad - ret + } + } - enableBots: - call SV_BotUserMove - ret + void Bots::G_SelectWeaponIndex(int clientNum, int iWeaponIndex) + { + if (g_botai[clientNum].active) + { + g_botai[clientNum].weapon = static_cast(iWeaponIndex); + } + } + + __declspec(naked) void Bots::G_SelectWeaponIndex_Hk() + { + __asm + { + pushad + + push [esp + 0x20 + 0x8] + push [esp + 0x20 + 0x8] + call Bots::G_SelectWeaponIndex + add esp, 0x8 + + popad + + // Code skipped by hook + mov eax, [esp + 0x8] + push eax + + push 0x441B85 + retn } } Bots::Bots() { - Bots::TestClientsActivate = Game::Dvar_RegisterBool("testClients_activate", true, - Game::dvar_flag::DVAR_FLAG_NONE, "Testclients will retain their native functionality."); - // Replace connect string Utils::Hook::Set(0x48ADA6, "connect bot%d \"\\cg_predictItems\\1\\cl_anonymous\\0\\color\\4\\head\\default\\model\\multi\\snaps\\20\\rate\\5000\\name\\%s\\protocol\\%d\\checksum\\%d\\statver\\%d %u\\qport\\%d\""); @@ -305,6 +332,8 @@ namespace Components Utils::Hook(0x627021, Bots::SV_BotUserMove_Hk, HOOK_CALL).install()->quick(); Utils::Hook(0x627241, Bots::SV_BotUserMove_Hk, HOOK_CALL).install()->quick(); + Utils::Hook(0x441B80, Bots::G_SelectWeaponIndex_Hk, HOOK_JUMP).install()->quick(); + // Zero the bot command array for (auto i = 0u; i < std::extent_v; i++) { diff --git a/src/Components/Modules/Bots.hpp b/src/Components/Modules/Bots.hpp index dcbadc6c..ccd1a6ef 100644 --- a/src/Components/Modules/Bots.hpp +++ b/src/Components/Modules/Bots.hpp @@ -22,7 +22,6 @@ namespace Components }; private: - static Game::dvar_t* TestClientsActivate; static std::vector BotNames; static int BuildConnectString(char* buffer, const char* connectString, int num, int, int protocol, int checksum, int statVer, int statStuff, int port); @@ -33,5 +32,8 @@ namespace Components static void BotAiAction(Game::client_t* cl); static void SV_BotUserMove_Hk(); + + static void G_SelectWeaponIndex(int clientNum, int iWeaponIndex); + static void G_SelectWeaponIndex_Hk(); }; } diff --git a/src/Components/Modules/Dvar.cpp b/src/Components/Modules/Dvar.cpp index 5e6f8a60..7c2d2087 100644 --- a/src/Components/Modules/Dvar.cpp +++ b/src/Components/Modules/Dvar.cpp @@ -385,7 +385,6 @@ namespace Components Utils::Hook(0x59386A, Dvar::DvarSetFromStringByNameStub, HOOK_CALL).install()->quick(); // If the game closed abruptly, the dvars would not have been restored - Dvar::OnInit([] { Dvar::ResetDvarsValue(); diff --git a/src/Components/Modules/ScriptExtension.cpp b/src/Components/Modules/ScriptExtension.cpp index e47041e9..512c4304 100644 --- a/src/Components/Modules/ScriptExtension.cpp +++ b/src/Components/Modules/ScriptExtension.cpp @@ -7,7 +7,6 @@ namespace Components void ScriptExtension::AddFunctions() { //File functions - Script::AddFunction("FileWrite", [](Game::scr_entref_t) // gsc: FileWrite(, , ) { const auto* path = Game::Scr_GetString(0); @@ -115,7 +114,7 @@ namespace Components { if (std::strstr(path, ScriptExtension::QueryStrings[i]) != nullptr) { - Logger::Print("^1fileRemove: directory traversal is not allowed!\n"); + Logger::Print("^1FileRemove: directory traversal is not allowed!\n"); return; } } @@ -152,9 +151,35 @@ namespace Components }); } + void ScriptExtension::Scr_TableLookupIStringByRow() + { + if (Game::Scr_GetNumParam() < 3) + { + Game::Scr_Error("USAGE: tableLookupIStringByRow( filename, rowNum, returnValueColumnNum )\n"); + return; + } + + const auto* fileName = Game::Scr_GetString(0); + const auto rowNum = Game::Scr_GetInt(1); + const auto returnValueColumnNum = Game::Scr_GetInt(2); + + const auto* table = Game::DB_FindXAssetHeader(Game::ASSET_TYPE_STRINGTABLE, fileName).stringTable; + + if (table == nullptr) + { + Game::Scr_ParamError(0, Utils::String::VA("%s does not exist\n", fileName)); + return; + } + + const auto* value = Game::StringTable_GetColumnValueForRow(table, rowNum, returnValueColumnNum); + Game::Scr_AddIString(value); + } + ScriptExtension::ScriptExtension() { ScriptExtension::AddFunctions(); ScriptExtension::AddMethods(); + // Correct builtin function pointer + Utils::Hook::Set(0x79A90C, ScriptExtension::Scr_TableLookupIStringByRow); } } diff --git a/src/Components/Modules/ScriptExtension.hpp b/src/Components/Modules/ScriptExtension.hpp index 84eef531..6b050159 100644 --- a/src/Components/Modules/ScriptExtension.hpp +++ b/src/Components/Modules/ScriptExtension.hpp @@ -12,5 +12,6 @@ namespace Components static void AddFunctions(); static void AddMethods(); + static void Scr_TableLookupIStringByRow(); }; } diff --git a/src/Game/Functions.cpp b/src/Game/Functions.cpp index e8c10b00..47d87d44 100644 --- a/src/Game/Functions.cpp +++ b/src/Game/Functions.cpp @@ -271,6 +271,7 @@ namespace Game Scr_AddEntity_t Scr_AddEntity = Scr_AddEntity_t(0x4BFB40); Scr_AddString_t Scr_AddString = Scr_AddString_t(0x412310); + Scr_AddIString_t Scr_AddIString = Scr_AddIString_t(0x455F20); Scr_AddInt_t Scr_AddInt = Scr_AddInt_t(0x41D7D0); Scr_AddFloat_t Scr_AddFloat = Scr_AddFloat_t(0x61E860); Scr_AddObject_t Scr_AddObject = Scr_AddObject_t(0x430F40); @@ -320,6 +321,7 @@ namespace Game Steam_JoinLobby_t Steam_JoinLobby = Steam_JoinLobby_t(0x49CF70); StringTable_Lookup_t StringTable_Lookup = StringTable_Lookup_t(0x42F0E0); + StringTable_GetColumnValueForRow_t StringTable_GetColumnValueForRow = StringTable_GetColumnValueForRow_t(0x4F2C80); StringTable_HashString_t StringTable_HashString = StringTable_HashString_t(0x475EB0); SV_AddTestClient_t SV_AddTestClient = SV_AddTestClient_t(0x48AD30); @@ -1591,5 +1593,20 @@ namespace Game } } + constexpr auto SV_BotUserMove_Addr = 0x626E50; + __declspec(naked) void SV_BotUserMove(client_t* /*client*/) + { + __asm + { + pushad + + mov edi, [esp + 0x20 + 0x4] + call SV_BotUserMove_Addr + + popad + ret + } + } + #pragma optimize("", on) } diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index fadf46f1..65a4e72f 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -639,12 +639,15 @@ namespace Game typedef void(__cdecl * RemoveRefToObject_t)(unsigned int id); extern RemoveRefToObject_t RemoveRefToObject; - typedef void(__cdecl * Scr_AddEntity_t)(gentity_s const*); + typedef void(__cdecl * Scr_AddEntity_t)(const gentity_s* ent); extern Scr_AddEntity_t Scr_AddEntity; - typedef void(__cdecl * Scr_AddString_t)(const char* str); + typedef void(__cdecl * Scr_AddString_t)(const char* value); extern Scr_AddString_t Scr_AddString; + typedef void(__cdecl * Scr_AddIString_t)(const char* value); + extern Scr_AddIString_t Scr_AddIString; + typedef void(__cdecl * Scr_AddInt_t)(int num); extern Scr_AddInt_t Scr_AddInt; @@ -759,9 +762,12 @@ namespace Game typedef void(__cdecl * Steam_JoinLobby_t)(SteamID, char); extern Steam_JoinLobby_t Steam_JoinLobby; - typedef const char*(__cdecl * StringTable_Lookup_t)(StringTable *table, const int comparisonColumn, const char *value, const int valueColumn); + typedef const char*(__cdecl * StringTable_Lookup_t)(const StringTable *table, const int comparisonColumn, const char *value, const int valueColumn); extern StringTable_Lookup_t StringTable_Lookup; + typedef const char* (__cdecl * StringTable_GetColumnValueForRow_t)(const StringTable* table, int, int column); + extern StringTable_GetColumnValueForRow_t StringTable_GetColumnValueForRow; + typedef int(__cdecl * StringTable_HashString_t)(const char* string); extern StringTable_HashString_t StringTable_HashString; @@ -1108,6 +1114,7 @@ namespace Game void SV_KickClient(client_t* client, const char* reason); void SV_KickClientError(client_t* client, const std::string& reason); + void SV_BotUserMove(client_t* client); void RuntimeErrorInternal(int channel, const char* codePos, unsigned int index, const char* msg); void IncInParam();