From bf11b6bb9e81f925a70ae82e87ed010502c32f18 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Tue, 1 Feb 2022 21:51:17 +0100 Subject: [PATCH] More cleanup --- src/Components/Modules/Script.cpp | 63 +++++++++++++++++++++---------- src/Game/Functions.cpp | 10 ----- src/Game/Functions.hpp | 2 - 3 files changed, 44 insertions(+), 31 deletions(-) diff --git a/src/Components/Modules/Script.cpp b/src/Components/Modules/Script.cpp index 11001fee..799cc9ab 100644 --- a/src/Components/Modules/Script.cpp +++ b/src/Components/Modules/Script.cpp @@ -190,11 +190,11 @@ namespace Components void Script::CompileError(unsigned int offset, const char* message, ...) { - char msgbuf[1024] = { 0 }; - va_list v; - va_start(v, message); - _vsnprintf_s(msgbuf, sizeof(msgbuf), message, v); - va_end(v); + char msgbuf[1024] = {0}; + va_list va; + va_start(va, message); + vsnprintf_s(msgbuf, sizeof(msgbuf), _TRUNCATE, message, va); + va_end(va); Game::Scr_ShutdownAllocNode(); @@ -214,7 +214,7 @@ namespace Components if (!Game::Scr_LoadScript(script.data())) { Logger::Print("Script %s encountered an error while loading. (doesn't exist?)", script.data()); - Logger::Error(Game::ERR_DROP, reinterpret_cast(0x70B810), script.data()); + Logger::Error(Game::ERR_DROP, reinterpret_cast(0x70B810), script.data()); } else { @@ -339,9 +339,9 @@ namespace Components int offset = -1; std::string file; - for (auto kv : Script::ScriptBaseProgramNum) + for (const auto& [key, value] : Script::ScriptBaseProgramNum) { - int codePos = kv.first; + int codePos = key; if (codePos > scriptPos) { @@ -356,7 +356,7 @@ namespace Components bestCodePos = codePos; - file = kv.second; + file = value; offset = scriptPos - bestCodePos; } @@ -589,7 +589,7 @@ namespace Components if (str == nullptr) { - Game::Scr_ParamError(0, "^1Exec: Illegal parameters!\n"); + Game::Scr_ParamError(0, "^1Exec: Illegal parameter!\n"); return; } @@ -605,7 +605,7 @@ namespace Components if (str == nullptr) { - Game::Scr_ParamError(i, "^1PrintConsole: Illegal parameters!\n"); + Game::Scr_ParamError(i, "^1PrintConsole: Illegal parameter!\n"); return; } @@ -616,19 +616,31 @@ namespace Components // Script Storage Funcs Script::AddFunction("StorageSet", [](Game::scr_entref_t) // gsc: StorageSet(, ); { - std::string key = Game::Scr_GetString(0); - std::string data = Game::Scr_GetString(1); + const auto* key = Game::Scr_GetString(0); + const auto* value = Game::Scr_GetString(1); - Script::ScriptStorage.insert_or_assign(key, data); + if (key == nullptr || value == nullptr) + { + Game::Scr_Error("^1StorageSet: Illegal parameters!\n"); + return; + } + + Script::ScriptStorage.insert_or_assign(key, value); }); Script::AddFunction("StorageRemove", [](Game::scr_entref_t) // gsc: StorageRemove(); { - std::string key = Game::Scr_GetString(0); + const auto* key = Game::Scr_GetString(0); + + if (key == nullptr) + { + Game::Scr_Error("^1StorageRemove: Illegal parameter!\n"); + return; + } if (!Script::ScriptStorage.count(key)) { - Game::Scr_Error(Utils::String::VA("^1StorageRemove: Store does not have key '%s'!\n", key.data())); + Game::Scr_Error(Utils::String::VA("^1StorageRemove: Store does not have key '%s'!\n", key)); return; } @@ -637,11 +649,17 @@ namespace Components Script::AddFunction("StorageGet", [](Game::scr_entref_t) // gsc: StorageGet(); { - std::string key = Game::Scr_GetString(0); + const auto* key = Game::Scr_GetString(0); + + if (key == nullptr) + { + Game::Scr_Error("^1StorageGet: Illegal parameter!\n"); + return; + } if (!Script::ScriptStorage.count(key)) { - Game::Scr_Error(Utils::String::VA("^1StorageGet: Store does not have key '%s'!\n", key.data())); + Game::Scr_Error(Utils::String::VA("^1StorageGet: Store does not have key '%s'!\n", key)); return; } @@ -651,7 +669,14 @@ namespace Components Script::AddFunction("StorageHas", [](Game::scr_entref_t) // gsc: StorageHas(); { - std::string key = Game::Scr_GetString(0); + const auto* key = Game::Scr_GetString(0); + + if (key == nullptr) + { + Game::Scr_Error("^1StorageHas: Illegal parameter!\n"); + return; + } + Game::Scr_AddBool(Script::ScriptStorage.count(key)); }); diff --git a/src/Game/Functions.cpp b/src/Game/Functions.cpp index abac4966..d0666d93 100644 --- a/src/Game/Functions.cpp +++ b/src/Game/Functions.cpp @@ -699,16 +699,6 @@ namespace Game SV_KickClient(client, reason.data()); } - void Scr_iPrintLn(int clientNum, const std::string& message) - { - Game::SV_GameSendServerCommand(clientNum, 0, Utils::String::VA("%c \"%s\"", 0x66, message.data())); - } - - void Scr_iPrintLnBold(int clientNum, const std::string& message) - { - Game::SV_GameSendServerCommand(clientNum, 0, Utils::String::VA("%c \"%s\"", 0x67, message.data())); - } - void IncInParam() { Scr_ClearOutParams(); diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index 1515869f..ac6eff8f 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -1062,8 +1062,6 @@ namespace Game void RuntimeErrorInternal(int channel, const char* codePos, unsigned int index, const char* msg); void IncInParam(); - void Scr_iPrintLn(int clientNum, const std::string& message); - void Scr_iPrintLnBold(int clientNum, const std::string& message); void Scr_NotifyId(unsigned int id, unsigned __int16 stringValue, unsigned int paramcount); void Scr_AddBool(int value);