Skip to content

Commit

Permalink
Add console printing modes and anti-spam
Browse files Browse the repository at this point in the history
  • Loading branch information
Frozen-H2O committed Jul 23, 2024
1 parent 29820fa commit d6506b7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
20 changes: 17 additions & 3 deletions src/adminsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
45 changes: 37 additions & 8 deletions src/detours.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,17 +439,27 @@ bool FASTCALL Detour_CEntityIdentity_AcceptInput(CEntityIdentity* pThis, CUtlSym
return CEntityIdentity_AcceptInput(pThis, pInputName, pActivator, pCaller, value, nOutputID);
}


std::map <int, bool> 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<CCSPlayerPawn*>(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<CCSPlayerPawn*>(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++)
{
Expand All @@ -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;
});
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/playermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/playermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d6506b7

Please sign in to comment.