From ed3217794e751cffad6982ef9b9acc9937616fde Mon Sep 17 00:00:00 2001 From: Edo Date: Tue, 1 Nov 2022 23:23:25 +0000 Subject: [PATCH] [Chat] Fix logic error in chat code (#547) --- src/Components/Modules/Chat.cpp | 29 +++++++++++++++++-------- src/Components/Modules/GSC/Script.cpp | 2 +- src/Components/Modules/TextRenderer.cpp | 8 +++---- src/Components/Modules/TextRenderer.hpp | 6 ++--- src/Game/Structs.hpp | 6 ++--- 5 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/Components/Modules/Chat.cpp b/src/Components/Modules/Chat.cpp index 45ee250c..ebfc6c38 100644 --- a/src/Components/Modules/Chat.cpp +++ b/src/Components/Modules/Chat.cpp @@ -29,23 +29,35 @@ namespace Components // Prevent callbacks from adding a new callback (would make the vector iterator invalid) CanAddCallback = false; - if (text[1] == '/') + // Chat messages sent through the console do not begin with \x15 + auto msgIndex = 0; + if (text[msgIndex] == '\x15') + { + msgIndex = 1; + } + + if (text[msgIndex] == '/') { SendChat = false; - text[1] = text[0]; + + if (msgIndex == 1) + { + // Overwrite / with \x15 + text[msgIndex] = text[msgIndex - 1]; + } + // Skip over the first character ++text; } if (IsMuted(player)) { SendChat = false; - Game::SV_GameSendServerCommand(player - Game::g_entities, Game::SV_CMD_CAN_IGNORE, - Utils::String::VA("%c \"You are muted\"", 0x65)); + Game::SV_GameSendServerCommand(player - Game::g_entities, Game::SV_CMD_CAN_IGNORE, Utils::String::VA("%c \"You are muted\"", 0x65)); } for (const auto& callback : SayCallbacks) { - if (!ChatCallback(player, callback.getPos(), (text + 1), mode)) + if (!ChatCallback(player, callback.getPos(), (text + msgIndex), mode)) { SendChat = false; } @@ -54,14 +66,13 @@ namespace Components if (sv_disableChat.get()) { SendChat = false; - Game::SV_GameSendServerCommand(player - Game::g_entities, Game::SV_CMD_CAN_IGNORE, - Utils::String::VA("%c \"Chat is disabled\"", 0x65)); + Game::SV_GameSendServerCommand(player - Game::g_entities, Game::SV_CMD_CAN_IGNORE, Utils::String::VA("%c \"Chat is disabled\"", 0x65)); } - TextRenderer::StripMaterialTextIcons(text, text, strlen(text) + 1); + TextRenderer::StripMaterialTextIcons(text, text, std::strlen(text) + 1); Game::Scr_AddEntity(player); - Game::Scr_AddString(text + 1); + Game::Scr_AddString(text + msgIndex); Game::Scr_NotifyLevel(Game::SL_GetString("say", 0), 2); return text; diff --git a/src/Components/Modules/GSC/Script.cpp b/src/Components/Modules/GSC/Script.cpp index a3b6e4a0..9de55849 100644 --- a/src/Components/Modules/GSC/Script.cpp +++ b/src/Components/Modules/GSC/Script.cpp @@ -441,7 +441,7 @@ namespace Components const auto* value = &Game::scrVmPub->top[-index]; - if (value->type != Game::scrParamType_t::VAR_FUNCTION) + if (value->type != Game::VAR_FUNCTION) { Game::Scr_ParamError(static_cast(index), "^1GetCodePosForParam: Expects a function as parameter!\n"); return ""; diff --git a/src/Components/Modules/TextRenderer.cpp b/src/Components/Modules/TextRenderer.cpp index 11ac4766..baad1edb 100644 --- a/src/Components/Modules/TextRenderer.cpp +++ b/src/Components/Modules/TextRenderer.cpp @@ -1260,7 +1260,7 @@ namespace Components return result; } - void TextRenderer::StripColors(const char* in, char* out, size_t max) + void TextRenderer::StripColors(const char* in, char* out, std::size_t max) { if (!in || !out) return; @@ -1287,12 +1287,12 @@ namespace Components std::string TextRenderer::StripColors(const std::string& in) { - char buffer[1024] = {0}; // 1024 is a lucky number in the engine + char buffer[1024]{}; // 1024 is a lucky number in the engine StripColors(in.data(), buffer, sizeof(buffer)); return std::string(buffer); } - void TextRenderer::StripMaterialTextIcons(const char* in, char* out, size_t max) + void TextRenderer::StripMaterialTextIcons(const char* in, char* out, std::size_t max) { if (!in || !out) return; @@ -1339,7 +1339,7 @@ namespace Components return std::string(buffer); } - void TextRenderer::StripAllTextIcons(const char* in, char* out, size_t max) + void TextRenderer::StripAllTextIcons(const char* in, char* out, std::size_t max) { if (!in || !out) return; diff --git a/src/Components/Modules/TextRenderer.hpp b/src/Components/Modules/TextRenderer.hpp index 2ffe44f2..29bfc725 100644 --- a/src/Components/Modules/TextRenderer.hpp +++ b/src/Components/Modules/TextRenderer.hpp @@ -202,11 +202,11 @@ namespace Components static void DrawText2D(const char* text, float x, float y, Game::Font_s* font, float xScale, float yScale, float sinAngle, float cosAngle, Game::GfxColor color, int maxLength, int renderFlags, int cursorPos, char cursorLetter, float padding, Game::GfxColor glowForcedColor, int fxBirthTime, int fxLetterTime, int fxDecayStartTime, int fxDecayDuration, Game::Material* fxMaterial, Game::Material* fxMaterialGlow); static int R_TextWidth_Hk(const char* text, int maxChars, Game::Font_s* font); static unsigned int ColorIndex(char index); - static void StripColors(const char* in, char* out, size_t max); + static void StripColors(const char* in, char* out, std::size_t max); static std::string StripColors(const std::string& in); - static void StripMaterialTextIcons(const char* in, char* out, size_t max); + static void StripMaterialTextIcons(const char* in, char* out, std::size_t max); static std::string StripMaterialTextIcons(const std::string& in); - static void StripAllTextIcons(const char* in, char* out, size_t max); + static void StripAllTextIcons(const char* in, char* out, std::size_t max); static std::string StripAllTextIcons(const std::string& in); static bool IsFontIcon(const char*& text, FontIconInfo& fontIcon); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 89712fba..012f57db 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -5475,7 +5475,7 @@ namespace Game char buf[1]; }; - enum scrParamType_t + enum { VAR_UNDEFINED = 0x0, VAR_BEGIN_REF = 0x1, @@ -5509,7 +5509,7 @@ namespace Game VAR_ENDON_LIST = 0x1B, }; - enum $2441F0C7E439C64E6C27842ECB570A7C + enum { FIRST_OBJECT = 0x10, FIRST_CLEARABLE_OBJECT = 0x14, @@ -5535,7 +5535,7 @@ namespace Game struct VariableValue { VariableUnion u; - scrParamType_t type; + int type; }; struct function_stack_t