From 0068b76d9827f5d51a59e7db2a2741b1bccefdcb Mon Sep 17 00:00:00 2001 From: FutureRave Date: Tue, 15 Mar 2022 22:49:58 +0000 Subject: [PATCH] Fix a typo in stub & clean-up some things --- src/Components/Modules/Bots.cpp | 6 ++-- src/Components/Modules/Bots.hpp | 2 +- src/Components/Modules/ClientCommand.cpp | 7 ++-- src/Components/Modules/Download.cpp | 4 +-- src/Components/Modules/Script.cpp | 39 ++++++++++++---------- src/Components/Modules/ScriptExtension.cpp | 24 ++++++------- src/Components/Modules/ScriptExtension.hpp | 1 + src/Game/Functions.hpp | 2 +- 8 files changed, 44 insertions(+), 41 deletions(-) diff --git a/src/Components/Modules/Bots.cpp b/src/Components/Modules/Bots.cpp index cdab519a..36841f67 100644 --- a/src/Components/Modules/Bots.cpp +++ b/src/Components/Modules/Bots.cpp @@ -264,7 +264,7 @@ namespace Components } constexpr auto SV_BotUserMove = 0x626E50; - __declspec(naked) void Bots::SV_UpdateBots_Hk() + __declspec(naked) void Bots::SV_BotUserMove_Hk() { __asm { @@ -302,8 +302,8 @@ namespace Components // Intercept sprintf for the connect string Utils::Hook(0x48ADAB, Bots::BuildConnectString, HOOK_CALL).install()->quick(); - Utils::Hook(0x627021, Bots::SV_UpdateBots_Hk, HOOK_CALL).install()->quick(); - Utils::Hook(0x627241, Bots::SV_UpdateBots_Hk, HOOK_CALL).install()->quick(); + Utils::Hook(0x627021, Bots::SV_BotUserMove_Hk, HOOK_CALL).install()->quick(); + Utils::Hook(0x627241, Bots::SV_BotUserMove_Hk, HOOK_CALL).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 e6a6ee22..dcbadc6c 100644 --- a/src/Components/Modules/Bots.hpp +++ b/src/Components/Modules/Bots.hpp @@ -32,6 +32,6 @@ namespace Components static void AddMethods(); static void BotAiAction(Game::client_t* cl); - static void SV_UpdateBots_Hk(); + static void SV_BotUserMove_Hk(); }; } diff --git a/src/Components/Modules/ClientCommand.cpp b/src/Components/Modules/ClientCommand.cpp index 6ad47a0e..eab176f8 100644 --- a/src/Components/Modules/ClientCommand.cpp +++ b/src/Components/Modules/ClientCommand.cpp @@ -28,10 +28,11 @@ namespace Components bool ClientCommand::CallbackHandler(Game::gentity_s* ent, const char* cmd) { const auto command = Utils::String::ToLower(cmd); + const auto got = ClientCommand::FunctionMap.find(command); - if (ClientCommand::FunctionMap.find(command) != ClientCommand::FunctionMap.end()) + if (got != ClientCommand::FunctionMap.end()) { - ClientCommand::FunctionMap[command](ent); + got->second(ent); return true; } @@ -42,7 +43,7 @@ namespace Components { const auto command = Utils::String::ToLower(name); - ClientCommand::FunctionMap[command] = callback; + ClientCommand::FunctionMap[command] = std::move(callback); } void ClientCommand::ClientCommandStub(const int clientNum) diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index 95b96574..39682107 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -966,7 +966,7 @@ namespace Components Script::AddFunction("HttpGet", [](Game::scr_entref_t) { - if (!Dedicated::IsEnabled() && !Flags::HasFlag("scriptablehttp")) + if (!Flags::HasFlag("scriptablehttp")) return; const auto* url = Game::Scr_GetString(0); @@ -987,7 +987,7 @@ namespace Components Script::AddFunction("HttpCancel", [](Game::scr_entref_t) { - if (!Dedicated::IsEnabled() && !Flags::HasFlag("scriptablehttp")) + if (!Flags::HasFlag("scriptablehttp")) return; const auto object = Game::Scr_GetObject(0); diff --git a/src/Components/Modules/Script.cpp b/src/Components/Modules/Script.cpp index 3520065e..acb5ed9e 100644 --- a/src/Components/Modules/Script.cpp +++ b/src/Components/Modules/Script.cpp @@ -60,18 +60,16 @@ namespace Components if (Game::scrVmPub->debugCode || Game::scrVarPub->developer_script) { Game::RuntimeErrorInternal(23, codePos, index, msg); - - if (!Game::scrVmPub->terminal_error) - return; } else { Logger::Print(23, "%s\n", msg); - // Let's not throw error unless we have to - if (Game::scrVmPub->abort_on_error && !Game::scrVmPub->terminal_error) - return; } + // Let's not throw error unless we have to + if (!Game::scrVmPub->abort_on_error) + return; + if (dialogMessage == nullptr) dialogMessage = ""; @@ -222,7 +220,7 @@ namespace Components } Logger::Print("Finding script handle %s::%s...\n", script.data(), label.data()); - int handle = Game::Scr_GetFunctionHandle(script.data(), label.data()); + const auto handle = Game::Scr_GetFunctionHandle(script.data(), label.data()); if (handle) { Logger::Print("Script handle %s::%s loaded successfully.\n", script.data(), label.data()); @@ -235,7 +233,7 @@ namespace Components void Script::LoadGameType() { - for (auto handle : Script::ScriptHandles) + for (const auto& handle : Script::ScriptHandles) { Game::Scr_FreeThread(Game::Scr_ExecThread(handle, 0)); } @@ -247,7 +245,7 @@ namespace Components { Script::ScriptHandles.clear(); - auto list = FileSystem::GetFileList("scripts/", "gsc"); + const auto list = FileSystem::GetFileList("scripts/", "gsc"); for (auto file : list) { @@ -258,8 +256,12 @@ namespace Components file = file.substr(0, file.size() - 4); } - int handle = Script::LoadScriptAndLabel(file, "init"); - if (handle) Script::ScriptHandles.push_back(handle); + auto handle = Script::LoadScriptAndLabel(file, "init"); + + if (handle) + { + Script::ScriptHandles.push_back(handle); + } else { handle = Script::LoadScriptAndLabel(file, "main"); @@ -339,7 +341,7 @@ namespace Components for (const auto& [key, value] : Script::ScriptBaseProgramNum) { - int codePos = key; + const auto codePos = key; if (codePos > scriptPos) { @@ -403,7 +405,7 @@ namespace Components void Script::OnVMShutdown(Utils::Slot callback) { Script::ScriptBaseProgramNum.clear(); - Script::VMShutdownSignal.connect(callback); + Script::VMShutdownSignal.connect(std::move(callback)); } void Script::ScrShutdownSystemStub(int num) @@ -460,7 +462,7 @@ namespace Components { if (what[0] == '\0' || with[0] == '\0') { - Logger::Print("Warning: Invalid paramters passed to ReplacedFunctions\n"); + Logger::Print("Warning: Invalid parameters passed to ReplacedFunctions\n"); return; } @@ -600,7 +602,7 @@ namespace Components } }); - // Script Storage Funcs + // Script Storage Functions Script::AddFunction("StorageSet", [](Game::scr_entref_t) // gsc: StorageSet(, ); { const auto* key = Game::Scr_GetString(0); @@ -664,7 +666,7 @@ namespace Components return; } - Game::Scr_AddBool(Script::ScriptStorage.count(key)); + Game::Scr_AddBool(static_cast(Script::ScriptStorage.count(key))); // Until C++17 }); Script::AddFunction("StorageClear", [](Game::scr_entref_t) // gsc: StorageClear(); @@ -719,11 +721,12 @@ namespace Components if (!Game::SV_Loaded()) return; - int nowMs = Game::Sys_Milliseconds(); + const auto nowMs = Game::Sys_Milliseconds(); if (Script::LastFrameTime != -1) { - int timeTaken = static_cast((nowMs - Script::LastFrameTime) * Dvar::Var("timescale").get()); + const auto timeScale = Dvar::Var("timescale").get(); + const auto timeTaken = static_cast((nowMs - Script::LastFrameTime) * timeScale); if (timeTaken >= 500) Logger::Print(23, "Hitch warning: %i msec frame time\n", timeTaken); diff --git a/src/Components/Modules/ScriptExtension.cpp b/src/Components/Modules/ScriptExtension.cpp index 9de9a586..66cd38ff 100644 --- a/src/Components/Modules/ScriptExtension.cpp +++ b/src/Components/Modules/ScriptExtension.cpp @@ -2,7 +2,7 @@ namespace Components { - static const char* queryStrings[] = { R"(..)", R"(../)", R"(..\)" }; + const char* ScriptExtension::QueryStrings[] = { R"(..)", R"(../)", R"(..\)" }; void ScriptExtension::AddFunctions() { @@ -26,16 +26,16 @@ namespace Components return; } - for (auto i = 0u; i < ARRAYSIZE(queryStrings); ++i) + for (auto i = 0u; i < ARRAYSIZE(ScriptExtension::QueryStrings); ++i) { - if (std::strstr(path, queryStrings[i]) != nullptr) + if (std::strstr(path, ScriptExtension::QueryStrings[i]) != nullptr) { Logger::Print("^1FileWrite: directory traversal is not allowed!\n"); return; } } - if (mode != "append"s && mode != "write"s) + if (mode != "append" && mode != "write") { Logger::Print("^3FileWrite: mode not defined or was wrong, defaulting to 'write'\n"); mode = "write"; @@ -61,9 +61,9 @@ namespace Components return; } - for (auto i = 0u; i < ARRAYSIZE(queryStrings); ++i) + for (auto i = 0u; i < ARRAYSIZE(ScriptExtension::QueryStrings); ++i) { - if (std::strstr(path, queryStrings[i]) != nullptr) + if (std::strstr(path, ScriptExtension::QueryStrings[i]) != nullptr) { Logger::Print("^1FileRead: directory traversal is not allowed!\n"); return; @@ -89,9 +89,9 @@ namespace Components return; } - for (auto i = 0u; i < ARRAYSIZE(queryStrings); ++i) + for (auto i = 0u; i < ARRAYSIZE(ScriptExtension::QueryStrings); ++i) { - if (std::strstr(path, queryStrings[i]) != nullptr) + if (std::strstr(path, ScriptExtension::QueryStrings[i]) != nullptr) { Logger::Print("^1FileExists: directory traversal is not allowed!\n"); return; @@ -111,9 +111,9 @@ namespace Components return; } - for (auto i = 0u; i < ARRAYSIZE(queryStrings); ++i) + for (auto i = 0u; i < ARRAYSIZE(ScriptExtension::QueryStrings); ++i) { - if (std::strstr(path, queryStrings[i]) != nullptr) + if (std::strstr(path, ScriptExtension::QueryStrings[i]) != nullptr) { Logger::Print("^1fileRemove: directory traversal is not allowed!\n"); return; @@ -137,9 +137,7 @@ namespace Components std::string ip = Game::NET_AdrToString(client->netchan.remoteAddress); - const auto pos = ip.find_first_of(":"); - - if (pos != std::string::npos) + if (const auto pos = ip.find_first_of(":"); pos != std::string::npos) ip.erase(ip.begin() + pos, ip.end()); // Erase port Game::Scr_AddString(ip.data()); diff --git a/src/Components/Modules/ScriptExtension.hpp b/src/Components/Modules/ScriptExtension.hpp index 2c64e89a..84eef531 100644 --- a/src/Components/Modules/ScriptExtension.hpp +++ b/src/Components/Modules/ScriptExtension.hpp @@ -8,6 +8,7 @@ namespace Components ScriptExtension(); private: + static const char* QueryStrings[]; static void AddFunctions(); static void AddMethods(); diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index ac4a336d..6fa0dfd9 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -681,7 +681,7 @@ namespace Game typedef unsigned int(__cdecl * Scr_GetNumParam_t)(); extern Scr_GetNumParam_t Scr_GetNumParam; - typedef int(__cdecl * Scr_GetFunctionHandle_t)(const char*, const char*); + typedef int(__cdecl * Scr_GetFunctionHandle_t)(const char* filename, const char* name); extern Scr_GetFunctionHandle_t Scr_GetFunctionHandle; typedef int(__cdecl * Scr_ExecThread_t)(int, int);