From d6506b7430b6e5e4fd3c80fa094cb6d314404762 Mon Sep 17 00:00:00 2001 From: Frozen-H2O <43626458+Frozen-H2O@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:03:14 -0700 Subject: [PATCH] Add console printing modes and anti-spam --- src/adminsystem.cpp | 20 ++++++++++++++++--- src/detours.cpp | 45 +++++++++++++++++++++++++++++++++++-------- src/playermanager.cpp | 16 +++++++++------ src/playermanager.h | 8 ++++---- 4 files changed, 68 insertions(+), 21 deletions(-) diff --git a/src/adminsystem.cpp b/src/adminsystem.cpp index c29731e2..aa8f6c79 100644 --- a/src/adminsystem.cpp +++ b/src/adminsystem.cpp @@ -1449,9 +1449,23 @@ CON_COMMAND_CHAT_FLAGS(bw, "- Toggle button watch display", ADMFLAG_GENERIC) return; } - zpPlayer->ToggleButtonWatch(); - ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "You have%s\1 button watch.", - zpPlayer->IsWatchingButtons() ? "\x04 enabled" : "\x02 disabled"); + zpPlayer->CycleButtonWatch(); + + switch (zpPlayer->GetButtonWatchMode()) + { + case 0: + ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "You have\x02 disabled\1 button watch."); + break; + case 1: + ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "You have\x04 enabled\1 button watch in chat."); + break; + case 2: + ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "You have\x04 enabled\1 button watch in console."); + break; + case 3: + ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX "You have\x04 enabled\1 button watch in chat and console."); + break; + } } CAdminSystem::CAdminSystem() diff --git a/src/detours.cpp b/src/detours.cpp index 526a62b5..962c3932 100644 --- a/src/detours.cpp +++ b/src/detours.cpp @@ -439,17 +439,27 @@ bool FASTCALL Detour_CEntityIdentity_AcceptInput(CEntityIdentity* pThis, CUtlSym return CEntityIdentity_AcceptInput(pThis, pInputName, pActivator, pCaller, value, nOutputID); } - +std::map mapRecentEnts; void FASTCALL Detour_CEntityIOOutput_FireOutputInternal(CEntityIOOutput* pThis, CEntityInstance* pActivator, CEntityInstance* pCaller, CVariant* value, float flDelay) { - if (!V_stricmp(pThis->m_pDesc->m_pName, "OnPressed") && ((CBaseEntity*)pActivator)->IsPawn()) + if (!V_stricmp(pThis->m_pDesc->m_pName, "OnPressed") && ((CBaseEntity*)pActivator)->IsPawn() && !mapRecentEnts.contains(pCaller->GetEntityIndex().Get())) { - std::string strMessage = CCSPlayerController::FromPawn(static_cast(pActivator))->GetPlayerName(); - strMessage = strMessage + "\1 pressed button \x0C" + std::to_string(pCaller->GetEntityIndex().Get()) + " "; - strMessage.append(((CBaseEntity*)pCaller)->GetName()); + CCSPlayerController* ccsPlayer = CCSPlayerController::FromPawn(static_cast(pActivator)); + std::string strPlayerName = ccsPlayer->GetPlayerName(); + + ZEPlayer* zpPlayer = ccsPlayer->GetZEPlayer(); + std::string strPlayerID = ""; + if (zpPlayer && !zpPlayer->IsFakeClient()) + { + strPlayerID = std::to_string(zpPlayer->IsAuthenticated() ? zpPlayer->GetSteamId64() : zpPlayer->GetUnauthenticatedSteamId64()); + strPlayerID = "(" + strPlayerID + ")"; + } + + std::string strButton = std::to_string(pCaller->GetEntityIndex().Get()) + " " + + std::string(((CBaseEntity*)pCaller)->GetName()); // ClientPrint doesn't work when called directly in here for some reason, so use in a timer instead - new CTimer(0.0f, false, false, [strMessage]() + new CTimer(0.0f, false, false, [strPlayerName, strButton, strPlayerID]() { for (int i = 0; i < gpGlobals->maxClients; i++) { @@ -458,12 +468,31 @@ void FASTCALL Detour_CEntityIOOutput_FireOutputInternal(CEntityIOOutput* pThis, continue; ZEPlayer* zpPlayer = ccsPlayer->GetZEPlayer(); - if (zpPlayer && zpPlayer->IsWatchingButtons()) - ClientPrint(ccsPlayer, HUD_PRINTTALK, " \x02[BW]\x0C %s\1", strMessage.c_str()); + if (!zpPlayer) + continue; + + if (zpPlayer->GetButtonWatchMode() % 2 == 1) + ClientPrint(ccsPlayer, HUD_PRINTTALK, " \x02[BW]\x0C %s\1 pressed button \x0C%s\1", strPlayerName.c_str(), strButton.c_str()); + if (zpPlayer->GetButtonWatchMode() >= 2) + { + ClientPrint(ccsPlayer, HUD_PRINTCONSOLE, "------------------------------------ [ButtonWatch] ------------------------------------"); + ClientPrint(ccsPlayer, HUD_PRINTCONSOLE, "Player: %s %s", strPlayerName.c_str(), strPlayerID.c_str()); + ClientPrint(ccsPlayer, HUD_PRINTCONSOLE, "Button: %s", strButton.c_str()); + ClientPrint(ccsPlayer, HUD_PRINTCONSOLE, "---------------------------------------------------------------------------------------"); + } } return -1.0f; }); + + // Prevent the same button from spamming more than once every 5 seconds + int iIndex = pCaller->GetEntityIndex().Get(); + mapRecentEnts[iIndex] = true; + new CTimer(5.0f, true, true, [iIndex]() + { + mapRecentEnts.erase(iIndex); + return -1.0f; + }); } } diff --git a/src/playermanager.cpp b/src/playermanager.cpp index 3ed9fe4d..85f52c38 100644 --- a/src/playermanager.cpp +++ b/src/playermanager.cpp @@ -490,17 +490,21 @@ void ZEPlayer::EndGlow() addresses::UTIL_Remove(pModelParent); } -void ZEPlayer::ToggleButtonWatch() +void ZEPlayer::CycleButtonWatch() { - m_bIsWatchingButton = !m_bIsWatchingButton; - g_pUserPreferencesSystem->SetPreferenceBool(m_slot.Get(), BUTTON_WATCH_PREF_KEY_NAME, m_bIsWatchingButton); + m_bIsWatchingButton = (m_bIsWatchingButton + 1) % 4; + g_pUserPreferencesSystem->SetPreferenceInt(m_slot.Get(), BUTTON_WATCH_PREF_KEY_NAME, m_bIsWatchingButton); } -bool ZEPlayer::IsWatchingButtons() +// 0: Off +// 1: Chat +// 2: Console +// 3: Chat + Console +int ZEPlayer::GetButtonWatchMode() { if (!IsAdminFlagSet(ADMFLAG_GENERIC) || IsFakeClient()) - return false; - return g_pUserPreferencesSystem->GetPreferenceBool(m_slot.Get(), BUTTON_WATCH_PREF_KEY_NAME, m_bIsWatchingButton); + return 0; + return g_pUserPreferencesSystem->GetPreferenceInt(m_slot.Get(), BUTTON_WATCH_PREF_KEY_NAME, m_bIsWatchingButton); } void CPlayerManager::OnBotConnected(CPlayerSlot slot) diff --git a/src/playermanager.h b/src/playermanager.h index a9ad179f..99809c19 100644 --- a/src/playermanager.h +++ b/src/playermanager.h @@ -123,7 +123,7 @@ class ZEPlayer m_flMaxSpeed = 1.f; m_iLastInputs = IN_NONE; m_iLastInputTime = std::time(0); - m_bIsWatchingButton = g_pUserPreferencesSystem->GetPreferenceBool(m_slot.Get(), BUTTON_WATCH_PREF_KEY_NAME, false); + m_bIsWatchingButton = g_pUserPreferencesSystem->GetPreferenceInt(m_slot.Get(), BUTTON_WATCH_PREF_KEY_NAME, false); } ~ZEPlayer() @@ -178,7 +178,7 @@ class ZEPlayer void SetLastInputs(uint64 iLastInputs) { m_iLastInputs = iLastInputs; } void UpdateLastInputTime() { m_iLastInputTime = std::time(0); } void SetMaxSpeed(float flMaxSpeed) { m_flMaxSpeed = flMaxSpeed; } - void ToggleButtonWatch(); + void CycleButtonWatch(); bool IsMuted() { return m_bMuted; } bool IsGagged() { return m_bGagged; } @@ -212,7 +212,7 @@ class ZEPlayer float GetMaxSpeed() { return m_flMaxSpeed; } uint64 GetLastInputs() { return m_iLastInputs; } std::time_t GetLastInputTime() { return m_iLastInputTime; } - bool IsWatchingButtons(); + int GetButtonWatchMode(); void OnSpawn(); void OnAuthenticated(); @@ -266,7 +266,7 @@ class ZEPlayer float m_flMaxSpeed; uint64 m_iLastInputs; std::time_t m_iLastInputTime; - bool m_bIsWatchingButton; + int m_bIsWatchingButton; }; class CPlayerManager