From ea9c7a7d00babc25f4b6581605c562268a13f2db Mon Sep 17 00:00:00 2001 From: notkoen <45914779+notkoen@users.noreply.github.com> Date: Fri, 7 Nov 2025 15:34:39 -0800 Subject: [PATCH 1/3] Convert adminsystem vector to std::vector --- src/adminsystem.cpp | 18 +++++++++--------- src/adminsystem.h | 3 +-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/adminsystem.cpp b/src/adminsystem.cpp index 20d902480..87c116eb1 100644 --- a/src/adminsystem.cpp +++ b/src/adminsystem.cpp @@ -1529,7 +1529,7 @@ void CAdminSystem::AddOrUpdateAdmin(uint64 iSteamID, uint64 iFlags, int iAdminIm bool CAdminSystem::LoadInfractions() { - m_vecInfractions.PurgeAndDeleteElements(); + m_vecInfractions.clear(); KeyValues* pKV = new KeyValues("infractions"); KeyValues::AutoDelete autoDelete(pKV); @@ -1590,7 +1590,7 @@ void CAdminSystem::SaveInfractions() KeyValues* pSubKey; KeyValues::AutoDelete autoDelete(pKV); - FOR_EACH_VEC(m_vecInfractions, i) + for (int i = 0; i < m_vecInfractions.size(); i++) { time_t timestamp = m_vecInfractions[i]->GetTimestamp(); if (timestamp != 0 && timestamp < std::time(0)) @@ -1618,7 +1618,7 @@ void CAdminSystem::SaveInfractions() void CAdminSystem::AddInfraction(CInfractionBase* infraction) { - m_vecInfractions.AddToTail(infraction); + m_vecInfractions.push_back(infraction); } // This function can run at least twice when a player connects: Immediately upon client connection, and also upon getting authenticated by steam. @@ -1626,7 +1626,7 @@ void CAdminSystem::AddInfraction(CInfractionBase* infraction) // This returns false only when called from ClientConnect and the player is banned in order to reject them. bool CAdminSystem::ApplyInfractions(ZEPlayer* player) { - FOR_EACH_VEC(m_vecInfractions, i) + for (int i = m_vecInfractions.size() - 1; i >= 0; i--) { // Because this can run without the player being authenticated, and the fact that we're applying a ban/mute here, // we can immediately just use the steamid we got from the connecting player. @@ -1642,7 +1642,7 @@ bool CAdminSystem::ApplyInfractions(ZEPlayer* player) time_t timestamp = m_vecInfractions[i]->GetTimestamp(); if (timestamp != 0 && timestamp <= std::time(0)) { - m_vecInfractions.Remove(i); + m_vecInfractions.erase(m_vecInfractions.begin() + i); continue; } @@ -1658,12 +1658,12 @@ bool CAdminSystem::ApplyInfractions(ZEPlayer* player) bool CAdminSystem::FindAndRemoveInfraction(ZEPlayer* player, CInfractionBase::EInfractionType type) { - FOR_EACH_VEC_BACK(m_vecInfractions, i) + for (int i = m_vecInfractions.size() - 1; i >= 0; i--) { if (m_vecInfractions[i]->GetSteamId64() == player->GetSteamId64() && m_vecInfractions[i]->GetType() == type) { m_vecInfractions[i]->UndoInfraction(player); - m_vecInfractions.Remove(i); + m_vecInfractions.erase(m_vecInfractions.begin() + i); return true; } @@ -1674,11 +1674,11 @@ bool CAdminSystem::FindAndRemoveInfraction(ZEPlayer* player, CInfractionBase::EI bool CAdminSystem::FindAndRemoveInfractionSteamId64(uint64 steamid64, CInfractionBase::EInfractionType type) { - FOR_EACH_VEC_BACK(m_vecInfractions, i) + for (int i = m_vecInfractions.size() - 1; i >= 0; i--) { if (m_vecInfractions[i]->GetSteamId64() == steamid64 && m_vecInfractions[i]->GetType() == type) { - m_vecInfractions.Remove(i); + m_vecInfractions.erase(m_vecInfractions.begin() + i); return true; } diff --git a/src/adminsystem.h b/src/adminsystem.h index e691f1d59..150d0abab 100644 --- a/src/adminsystem.h +++ b/src/adminsystem.h @@ -20,7 +20,6 @@ #pragma once #include "platform.h" #include "playermanager.h" -#include "utlvector.h" #include // clang-format off @@ -196,7 +195,7 @@ class CAdminSystem private: std::map m_mapAdminGroups; std::map m_mapAdmins; - CUtlVector m_vecInfractions; + std::vector m_vecInfractions; // Implemented as a circular buffer. std::tuple m_rgDCPly[20]; From fbab71d81a088ae940ac64f9e291cfa178b3bbd8 Mon Sep 17 00:00:00 2001 From: notkoen <45914779+notkoen@users.noreply.github.com> Date: Fri, 7 Nov 2025 15:41:19 -0800 Subject: [PATCH 2/3] Convert discord vector to std::vector --- src/discord.cpp | 6 +++--- src/discord.h | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/discord.cpp b/src/discord.cpp index d241278d3..5a0cb31e2 100644 --- a/src/discord.cpp +++ b/src/discord.cpp @@ -40,7 +40,7 @@ void DiscordHttpCallback(HTTPRequestHandle request, json response) void CDiscordBotManager::PostDiscordMessage(const char* sDiscordBotName, const char* sMessage) { - FOR_EACH_VEC(m_vecDiscordBots, i) + for (int i = 0; i < m_vecDiscordBots.size(); i++) { CDiscordBot bot = m_vecDiscordBots[i]; @@ -74,7 +74,7 @@ void CDiscordBot::PostMessage(const char* sMessage) bool CDiscordBotManager::LoadDiscordBotsConfig() { - m_vecDiscordBots.Purge(); + m_vecDiscordBots.clear(); KeyValues* pKV = new KeyValues("discordbots"); KeyValues::AutoDelete autoDelete(pKV); @@ -105,7 +105,7 @@ bool CDiscordBotManager::LoadDiscordBotsConfig() ConMsg("Loaded DiscordBot config %s\n", bot.GetName()); ConMsg(" - Webhook URL: %s\n", bot.GetWebhookUrl()); ConMsg(" - Avatar URL: %s\n", bot.GetAvatarUrl()); - m_vecDiscordBots.AddToTail(bot); + m_vecDiscordBots.push_back(bot); } return true; diff --git a/src/discord.h b/src/discord.h index 2a5d580aa..23e02d2f1 100644 --- a/src/discord.h +++ b/src/discord.h @@ -18,7 +18,6 @@ */ #include "httpmanager.h" -#include "utlvector.h" class CDiscordBot { @@ -55,7 +54,7 @@ class CDiscordBotManager bool LoadDiscordBotsConfig(); private: - CUtlVector m_vecDiscordBots; + std::vector m_vecDiscordBots; }; extern CDiscordBotManager* g_pDiscordBotManager; \ No newline at end of file From 57bdaa48594cb70912b0eaba468661fbf2cd435b Mon Sep 17 00:00:00 2001 From: Vauff Date: Sun, 1 Mar 2026 22:47:10 -0500 Subject: [PATCH 3/3] Address review comments & update HTTP manager API std::string params allow for more flexibility, since both C & C++ strings can be passed in --- src/adminsystem.cpp | 26 +++++++++++++------------- src/adminsystem.h | 4 ++-- src/discord.cpp | 34 ++++++++++++++++------------------ src/discord.h | 26 +++++++++++++------------- src/httpmanager.cpp | 41 ++++++++++++++++++----------------------- src/httpmanager.h | 12 ++++++------ 6 files changed, 68 insertions(+), 75 deletions(-) diff --git a/src/adminsystem.cpp b/src/adminsystem.cpp index 87c116eb1..efbe0d274 100644 --- a/src/adminsystem.cpp +++ b/src/adminsystem.cpp @@ -1568,13 +1568,13 @@ bool CAdminSystem::LoadInfractions() switch (iType) { case CInfractionBase::Ban: - AddInfraction(new CBanInfraction(iEndTime, iSteamId, true)); + AddInfraction(std::make_shared(iEndTime, iSteamId, true)); break; case CInfractionBase::Mute: - AddInfraction(new CMuteInfraction(iEndTime, iSteamId, true)); + AddInfraction(std::make_shared(iEndTime, iSteamId, true)); break; case CInfractionBase::Gag: - AddInfraction(new CGagInfraction(iEndTime, iSteamId, true)); + AddInfraction(std::make_shared(iEndTime, iSteamId, true)); break; default: Warning("Invalid infraction type %d\n", iType); @@ -1616,9 +1616,9 @@ void CAdminSystem::SaveInfractions() Warning("Failed to save infractions to %s\n", szPath); } -void CAdminSystem::AddInfraction(CInfractionBase* infraction) +void CAdminSystem::AddInfraction(std::shared_ptr pInfraction) { - m_vecInfractions.push_back(infraction); + m_vecInfractions.push_back(pInfraction); } // This function can run at least twice when a player connects: Immediately upon client connection, and also upon getting authenticated by steam. @@ -1961,31 +1961,31 @@ void ParseInfraction(const CCommand& args, CCSPlayerController* pAdmin, bool bAd g_pAdminSystem->FindAndRemoveInfraction(zpTarget, infType); else { - CInfractionBase* infraction; + std::shared_ptr pInfraction; switch (infType) { case CInfractionBase::Mute: - infraction = new CMuteInfraction(iDuration, zpTarget->GetSteamId64()); + pInfraction = std::make_shared(iDuration, zpTarget->GetSteamId64()); break; case CInfractionBase::Gag: - infraction = new CGagInfraction(iDuration, zpTarget->GetSteamId64()); + pInfraction = std::make_shared(iDuration, zpTarget->GetSteamId64()); break; case CInfractionBase::Ban: - infraction = new CBanInfraction(iDuration, zpTarget->GetSteamId64()); + pInfraction = std::make_shared(iDuration, zpTarget->GetSteamId64()); break; case CInfractionBase::Eban: - infraction = new CEbanInfraction(iDuration, zpTarget->GetSteamId64()); + pInfraction = std::make_shared(iDuration, zpTarget->GetSteamId64()); break; default: - // This should never be reached, since we it means we are trying to apply an unimplemented block type + // This should never be reached, since it means we are trying to apply an unimplemented block type ClientPrint(pAdmin, HUD_PRINTTALK, CHAT_PREFIX "Improper block type... Send to a dev with the command used."); return; } // We're overwriting the infraction, so remove the previous one first g_pAdminSystem->FindAndRemoveInfraction(zpTarget, infType); - g_pAdminSystem->AddInfraction(infraction); - infraction->ApplyInfraction(zpTarget); + g_pAdminSystem->AddInfraction(pInfraction); + pInfraction->ApplyInfraction(zpTarget); } if (iNumClients == 1 || (bAdding && iDuration == 0)) diff --git a/src/adminsystem.h b/src/adminsystem.h index 150d0abab..fd899dbf2 100644 --- a/src/adminsystem.h +++ b/src/adminsystem.h @@ -178,7 +178,7 @@ class CAdminSystem bool LoadAdmins(); void AddOrUpdateAdmin(uint64 iSteamID, uint64 iFlags = 0, int iAdminImmunity = 0); bool LoadInfractions(); - void AddInfraction(CInfractionBase*); + void AddInfraction(std::shared_ptr pInfraction); void SaveInfractions(); bool ApplyInfractions(ZEPlayer* player); bool FindAndRemoveInfraction(ZEPlayer* player, CInfractionBase::EInfractionType type); @@ -195,7 +195,7 @@ class CAdminSystem private: std::map m_mapAdminGroups; std::map m_mapAdmins; - std::vector m_vecInfractions; + std::vector> m_vecInfractions; // Implemented as a circular buffer. std::tuple m_rgDCPly[20]; diff --git a/src/discord.cpp b/src/discord.cpp index 5a0cb31e2..3a0fc4a3f 100644 --- a/src/discord.cpp +++ b/src/discord.cpp @@ -38,38 +38,36 @@ void DiscordHttpCallback(HTTPRequestHandle request, json response) Message("Discord post received response: %s\n", response.dump().c_str()); } -void CDiscordBotManager::PostDiscordMessage(const char* sDiscordBotName, const char* sMessage) +void CDiscordBotManager::PostDiscordMessage(const char* pszDiscordBotName, const char* pszMessage) { - for (int i = 0; i < m_vecDiscordBots.size(); i++) + for (auto pBot : m_vecDiscordBots) { - CDiscordBot bot = m_vecDiscordBots[i]; - if (g_cvarDebugDiscordRequests.Get()) - Message("The bot at %i is %s with %s webhook and %s avatar.\n", i, bot.GetName(), bot.GetWebhookUrl(), bot.GetAvatarUrl()); + Message("The bot is %s with %s webhook and %s avatar.\n", pBot->GetName(), pBot->GetWebhookUrl(), pBot->GetAvatarUrl()); - if (!V_stricmp(sDiscordBotName, bot.GetName())) - bot.PostMessage(sMessage); + if (!V_stricmp(pszDiscordBotName, pBot->GetName())) + pBot->PostMessage(pszMessage); } } -void CDiscordBot::PostMessage(const char* sMessage) +void CDiscordBot::PostMessage(std::string strMessage) { json jRequestBody; // Fill up the Json fields - jRequestBody["content"] = sMessage; + jRequestBody["content"] = strMessage; if (m_bOverrideName) - jRequestBody["username"] = m_pszName; + jRequestBody["username"] = m_strName; - if (V_strcmp(m_pszAvatarUrl, "")) - jRequestBody["avatar_url"] = m_pszAvatarUrl; + if (V_strcmp(GetAvatarUrl(), "")) + jRequestBody["avatar_url"] = m_strAvatarUrl; // Send the request std::string sRequestBody = jRequestBody.dump(); if (g_cvarDebugDiscordRequests.Get()) Message("Sending '%s' to %s.\n", sRequestBody.c_str(), GetWebhookUrl()); - g_HTTPManager.Post(m_pszWebhookUrl, sRequestBody.c_str(), &DiscordHttpCallback); + g_HTTPManager.Post(m_strWebhookUrl, sRequestBody, &DiscordHttpCallback); } bool CDiscordBotManager::LoadDiscordBotsConfig() @@ -101,11 +99,11 @@ bool CDiscordBotManager::LoadDiscordBotsConfig() pszAvatarUrl = ""; // We just append the bots as-is - CDiscordBot bot = CDiscordBot(pszName, pszWebhookUrl, pszAvatarUrl, bOverrideName); - ConMsg("Loaded DiscordBot config %s\n", bot.GetName()); - ConMsg(" - Webhook URL: %s\n", bot.GetWebhookUrl()); - ConMsg(" - Avatar URL: %s\n", bot.GetAvatarUrl()); - m_vecDiscordBots.push_back(bot); + std::shared_ptr pBot = std::make_shared(pszName, pszWebhookUrl, pszAvatarUrl, bOverrideName); + ConMsg("Loaded DiscordBot config %s\n", pBot->GetName()); + ConMsg(" - Webhook URL: %s\n", pBot->GetWebhookUrl()); + ConMsg(" - Avatar URL: %s\n", pBot->GetAvatarUrl()); + m_vecDiscordBots.push_back(pBot); } return true; diff --git a/src/discord.h b/src/discord.h index 23e02d2f1..ea97ad969 100644 --- a/src/discord.h +++ b/src/discord.h @@ -22,23 +22,23 @@ class CDiscordBot { public: - CDiscordBot(const char* pszName, const char* pszWebhookUrl, const char* pszAvatarUrl, bool bOverrideName) + CDiscordBot(std::string strName, std::string strWebhookUrl, std::string strAvatarUrl, bool bOverrideName) { - V_strcpy(m_pszName, pszName); - V_strcpy(m_pszWebhookUrl, pszWebhookUrl); - V_strcpy(m_pszAvatarUrl, pszAvatarUrl); + m_strName = strName; + m_strWebhookUrl = strWebhookUrl; + m_strAvatarUrl = strAvatarUrl; m_bOverrideName = bOverrideName; } - const char* GetName() { return (const char*)m_pszName; }; - const char* GetWebhookUrl() { return m_pszWebhookUrl; }; - const char* GetAvatarUrl() { return m_pszAvatarUrl; }; - void PostMessage(const char* sMessage); + const char* GetName() { return m_strName.c_str(); }; + const char* GetWebhookUrl() { return m_strWebhookUrl.c_str(); }; + const char* GetAvatarUrl() { return m_strAvatarUrl.c_str(); }; + void PostMessage(std::string strMessage); private: - char m_pszName[32]; - char m_pszWebhookUrl[256]; - char m_pszAvatarUrl[256]; + std::string m_strName; + std::string m_strWebhookUrl; + std::string m_strAvatarUrl; bool m_bOverrideName; }; @@ -50,11 +50,11 @@ class CDiscordBotManager LoadDiscordBotsConfig(); } - void PostDiscordMessage(const char* sDiscordBotName, const char* sMessage); + void PostDiscordMessage(const char* pszDiscordBotName, const char* pszMessage); bool LoadDiscordBotsConfig(); private: - std::vector m_vecDiscordBots; + std::vector> m_vecDiscordBots; }; extern CDiscordBotManager* g_pDiscordBotManager; \ No newline at end of file diff --git a/src/httpmanager.cpp b/src/httpmanager.cpp index 1e61e7f4d..29fc31435 100644 --- a/src/httpmanager.cpp +++ b/src/httpmanager.cpp @@ -107,37 +107,37 @@ void HTTPManager::TrackedRequest::OnHTTPRequestCompleted(HTTPRequestCompleted_t* delete this; } -void HTTPManager::Get(const char* pszUrl, CompletedCallback callbackCompleted, +void HTTPManager::Get(std::string strUrl, CompletedCallback callbackCompleted, ErrorCallback callbackError, std::vector* headers) { - GenerateRequest(k_EHTTPMethodGET, pszUrl, "", callbackCompleted, callbackError, headers); + GenerateRequest(k_EHTTPMethodGET, strUrl, "", callbackCompleted, callbackError, headers); } -void HTTPManager::Post(const char* pszUrl, const char* pszText, CompletedCallback callbackCompleted, +void HTTPManager::Post(std::string strUrl, std::string strText, CompletedCallback callbackCompleted, ErrorCallback callbackError, std::vector* headers) { - GenerateRequest(k_EHTTPMethodPOST, pszUrl, pszText, callbackCompleted, callbackError, headers); + GenerateRequest(k_EHTTPMethodPOST, strUrl, strText, callbackCompleted, callbackError, headers); } -void HTTPManager::Put(const char* pszUrl, const char* pszText, CompletedCallback callbackCompleted, +void HTTPManager::Put(std::string strUrl, std::string strText, CompletedCallback callbackCompleted, ErrorCallback callbackError, std::vector* headers) { - GenerateRequest(k_EHTTPMethodPUT, pszUrl, pszText, callbackCompleted, callbackError, headers); + GenerateRequest(k_EHTTPMethodPUT, strUrl, strText, callbackCompleted, callbackError, headers); } -void HTTPManager::Patch(const char* pszUrl, const char* pszText, CompletedCallback callbackCompleted, +void HTTPManager::Patch(std::string strUrl, std::string strText, CompletedCallback callbackCompleted, ErrorCallback callbackError, std::vector* headers) { - GenerateRequest(k_EHTTPMethodPATCH, pszUrl, pszText, callbackCompleted, callbackError, headers); + GenerateRequest(k_EHTTPMethodPATCH, strUrl, strText, callbackCompleted, callbackError, headers); } -void HTTPManager::Delete(const char* pszUrl, const char* pszText, CompletedCallback callbackCompleted, +void HTTPManager::Delete(std::string strUrl, std::string strText, CompletedCallback callbackCompleted, ErrorCallback callbackError, std::vector* headers) { - GenerateRequest(k_EHTTPMethodDELETE, pszUrl, pszText, callbackCompleted, callbackError, headers); + GenerateRequest(k_EHTTPMethodDELETE, strUrl, strText, callbackCompleted, callbackError, headers); } -void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const char* pszText, +void HTTPManager::GenerateRequest(EHTTPMethod method, std::string strUrl, std::string strText, CompletedCallback callbackCompleted, ErrorCallback callbackError, std::vector* headers) { @@ -147,24 +147,19 @@ void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const return; } - // Message("Sending HTTP:\n%s\n", pszText); - auto hReq = GetSteamHTTP()->CreateHTTPRequest(method, pszUrl); - int size = strlen(pszText); - // Message("HTTP request: %p\n", hReq); +#ifdef _DEBUG + Message("Sending HTTP:\n%s\n", strText.c_str()); +#endif + + HTTPRequestHandle hReq = GetSteamHTTP()->CreateHTTPRequest(method, strUrl.c_str()); bool shouldHaveBody = method == k_EHTTPMethodPOST || method == k_EHTTPMethodPATCH || method == k_EHTTPMethodPUT || method == k_EHTTPMethodDELETE; - if (shouldHaveBody && !GetSteamHTTP()->SetHTTPRequestRawPostBody(hReq, "application/json", (uint8*)pszText, size)) - { - // Message("Failed to SetHTTPRequestRawPostBody\n"); + if (shouldHaveBody && !GetSteamHTTP()->SetHTTPRequestRawPostBody(hReq, "application/json", (uint8*)(strText.c_str()), strText.length())) return; - } - - // Prevent HTTP error 411 (probably not necessary?) - // g_http->SetHTTPRequestHeaderValue(hReq, "Content-Length", std::to_string(size).c_str()); if (headers != nullptr) for (HTTPHeader header : *headers) @@ -173,5 +168,5 @@ void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const SteamAPICall_t hCall; GetSteamHTTP()->SendHTTPRequest(hReq, &hCall); - new TrackedRequest(hReq, hCall, pszUrl, pszText, callbackCompleted, callbackError); + new TrackedRequest(hReq, hCall, strUrl, strText, callbackCompleted, callbackError); } diff --git a/src/httpmanager.h b/src/httpmanager.h index 0e6826e29..7a8024b4b 100644 --- a/src/httpmanager.h +++ b/src/httpmanager.h @@ -59,15 +59,15 @@ class HTTPHeader class HTTPManager { public: - void Get(const char* pszUrl, CompletedCallback callbackCompleted, + void Get(std::string strUrl, CompletedCallback callbackCompleted, ErrorCallback callbackError = nullptr, std::vector* headers = nullptr); - void Post(const char* pszUrl, const char* pszText, CompletedCallback callbackCompleted, + void Post(std::string strUrl, std::string strText, CompletedCallback callbackCompleted, ErrorCallback callbackError = nullptr, std::vector* headers = nullptr); - void Put(const char* pszUrl, const char* pszText, CompletedCallback callbackCompleted, + void Put(std::string strUrl, std::string strText, CompletedCallback callbackCompleted, ErrorCallback callbackError = nullptr, std::vector* headers = nullptr); - void Patch(const char* pszUrl, const char* pszText, CompletedCallback callbackCompleted, + void Patch(std::string strUrl, std::string strText, CompletedCallback callbackCompleted, ErrorCallback callbackError = nullptr, std::vector* headers = nullptr); - void Delete(const char* pszUrl, const char* pszText, CompletedCallback callbackCompleted, + void Delete(std::string strUrl, std::string strText, CompletedCallback callbackCompleted, ErrorCallback callbackError = nullptr, std::vector* headers = nullptr); bool HasAnyPendingRequests() const { return m_PendingRequests.size() > 0; } @@ -94,7 +94,7 @@ class HTTPManager private: std::vector m_PendingRequests; - void GenerateRequest(EHTTPMethod method, const char* pszUrl, const char* pszText, + void GenerateRequest(EHTTPMethod method, std::string strUrl, std::string strText, CompletedCallback callbackCompleted, ErrorCallback callbackError, std::vector* headers); };