Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller fetch refactor #74

Merged
merged 4 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk
21 changes: 10 additions & 11 deletions src/adminsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ CON_COMMAND_CHAT_FLAGS(ban, "ban a player", ADMFLAG_BAN)
ClientPrint(player, HUD_PRINTTALK, CHAT_PREFIX"Invalid duration.");
return;
}

CBasePlayerController* pTarget = (CBasePlayerController*)g_pEntitySystem->GetBaseEntity((CEntityIndex)(pSlot[0] + 1));
CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlot[0]);

if (!pTarget)
return;
Expand Down Expand Up @@ -199,7 +198,7 @@ CON_COMMAND_CHAT_FLAGS(mute, "mutes a player", ADMFLAG_CHAT)

for (int i = 0; i < iNumClients; i++)
{
CBasePlayerController* pTarget = (CBasePlayerController*)g_pEntitySystem->GetBaseEntity((CEntityIndex)(pSlot[i] + 1));
CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlot[i]);

if (!pTarget)
continue;
Expand Down Expand Up @@ -252,7 +251,7 @@ CON_COMMAND_CHAT_FLAGS(unmute, "unmutes a player", ADMFLAG_CHAT)

for (int i = 0; i < iNumClients; i++)
{
CBasePlayerController *pTarget = (CBasePlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(pSlot[i] + 1));
CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlot[i]);

if (!pTarget)
continue;
Expand Down Expand Up @@ -318,7 +317,7 @@ CON_COMMAND_CHAT_FLAGS(gag, "gag a player", ADMFLAG_CHAT)

for (int i = 0; i < iNumClients; i++)
{
CBasePlayerController *pTarget = (CBasePlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(pSlot[i] + 1));
CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlot[i]);

if (!pTarget)
continue;
Expand Down Expand Up @@ -373,7 +372,7 @@ CON_COMMAND_CHAT_FLAGS(ungag, "ungags a player", ADMFLAG_CHAT)

for (int i = 0; i < iNumClients; i++)
{
CBasePlayerController *pTarget = (CBasePlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(pSlot[i] + 1));
CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlot[i]);

if (!pTarget)
continue;
Expand Down Expand Up @@ -422,7 +421,7 @@ CON_COMMAND_CHAT_FLAGS(kick, "kick a player", ADMFLAG_KICK)

for (int i = 0; i < iNumClients; i++)
{
CBasePlayerController* pTarget = (CBasePlayerController*)g_pEntitySystem->GetBaseEntity((CEntityIndex)(pSlot[i] + 1));
CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlot[i]);

if (!pTarget)
continue;
Expand Down Expand Up @@ -459,7 +458,7 @@ CON_COMMAND_CHAT_FLAGS(slay, "slay a player", ADMFLAG_SLAY)

for (int i = 0; i < iNumClients; i++)
{
CBasePlayerController *pTarget = (CBasePlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(pSlots[i] + 1));
CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlots[i]);

if (!pTarget)
continue;
Expand Down Expand Up @@ -505,7 +504,7 @@ CON_COMMAND_CHAT_FLAGS(goto, "teleport to a player", ADMFLAG_SLAY)

for (int i = 0; i < iNumClients; i++)
{
CBasePlayerController *pTarget = (CBasePlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(pSlots[i] + 1));
CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlots[i]);

if (!pTarget)
continue;
Expand Down Expand Up @@ -545,7 +544,7 @@ CON_COMMAND_CHAT_FLAGS(bring, "bring a player", ADMFLAG_SLAY)

for (int i = 0; i < iNumClients; i++)
{
CBasePlayerController *pTarget = (CBasePlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(pSlots[i] + 1));
CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlots[i]);

if (!pTarget)
continue;
Expand Down Expand Up @@ -598,7 +597,7 @@ CON_COMMAND_CHAT_FLAGS(setteam, "set a player's team", ADMFLAG_SLAY)

for (int i = 0; i < iNumClients; i++)
{
CCSPlayerController *pTarget = (CCSPlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(pSlots[i] + 1));
CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlots[i]);

if (!pTarget)
continue;
Expand Down
14 changes: 7 additions & 7 deletions src/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,16 +369,16 @@ CON_COMMAND_CHAT(message, "message someone")
// Note that the engine will treat this as a player slot number, not an entity index
int uid = atoi(args[1]);

CBasePlayerController *target = (CBasePlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(uid + 1));
CCSPlayerController* pTarget = CCSPlayerController::FromSlot(uid);

if (!target)
if (!pTarget)
return;

// skipping the id and space, it's dumb but w/e
const char *pMessage = args.ArgS() + V_strlen(args[1]) + 1;

char buf[256];
V_snprintf(buf, sizeof(buf), CHAT_PREFIX"Private message from %s to %s: \5%s", player->GetPlayerName(), target->GetPlayerName(), pMessage);
V_snprintf(buf, sizeof(buf), CHAT_PREFIX"Private message from %s to %s: \5%s", player->GetPlayerName(), pTarget->GetPlayerName(), pMessage);

CSingleRecipientFilter filter(uid);

Expand Down Expand Up @@ -428,7 +428,7 @@ CON_COMMAND_CHAT(test_target, "test string targetting")

for (int i = 0; i < iNumClients; i++)
{
CBasePlayerController* pTarget = (CBasePlayerController*)g_pEntitySystem->GetBaseEntity((CEntityIndex)(pSlots[i] + 1));
CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlots[i]);

if (!pTarget)
continue;
Expand Down Expand Up @@ -501,7 +501,7 @@ CON_COMMAND_CHAT(setcollisiongroup, "set a player's collision group")

for (int i = 0; i < iNumClients; i++)
{
CBasePlayerController *pTarget = (CBasePlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(pSlots[i] + 1));
CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlots[i]);

if (!pTarget)
continue;
Expand All @@ -526,7 +526,7 @@ CON_COMMAND_CHAT(setsolidtype, "set a player's solid type")

for (int i = 0; i < iNumClients; i++)
{
CBasePlayerController *pTarget = (CBasePlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(pSlots[i] + 1));
CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlots[i]);

if (!pTarget)
continue;
Expand All @@ -550,7 +550,7 @@ CON_COMMAND_CHAT(setinteraction, "set a player's interaction flags")

for (int i = 0; i < iNumClients; i++)
{
CBasePlayerController *pTarget = (CBasePlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(pSlots[i] + 1));
CCSPlayerController* pTarget = CCSPlayerController::FromSlot(pSlots[i]);

if (!pTarget)
continue;
Expand Down
17 changes: 16 additions & 1 deletion src/cs2_sdk/entity/ccsplayercontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

#include "cbaseplayercontroller.h"
#include "services.h"
#include "../playermanager.h"

extern CEntitySystem* g_pEntitySystem;

class CCSPlayerController : public CBasePlayerController
{
Expand All @@ -31,7 +34,19 @@ class CCSPlayerController : public CBasePlayerController
SCHEMA_FIELD(CCSPlayerController_ActionTrackingServices*, m_pActionTrackingServices)
SCHEMA_FIELD(bool, m_bPawnIsAlive);

static CCSPlayerController* FromPawn(CCSPlayerPawn* pawn) { return (CCSPlayerController*)pawn->m_hController().Get(); }
static CCSPlayerController* FromPawn(CCSPlayerPawn* pawn) {
return (CCSPlayerController*)pawn->m_hController().Get();
}

static CCSPlayerController* FromSlot(CPlayerSlot slot)
{
return (CCSPlayerController*)g_pEntitySystem->GetBaseEntity(CEntityIndex(slot.Get() + 1));
}

ZEPlayer* GetZEPlayer()
{
return g_playerManager->GetPlayer(GetPlayerSlot());
}

void ChangeTeam(int iTeam)
{
Expand Down
8 changes: 4 additions & 4 deletions src/cs2fixes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ void CS2Fixes::Hook_CheckTransmit(CCheckTransmitInfo **ppInfoList, int infoCount
// though this is probably part of the client class that contains the CCheckTransmitInfo
int iPlayerSlot = (int)*((uint8 *)pInfo + 560);

auto pSelfController = (CCSPlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(iPlayerSlot + 1));
CCSPlayerController* pSelfController = CCSPlayerController::FromSlot(iPlayerSlot);

if (!pSelfController || !pSelfController->IsConnected() || !pSelfController->m_bPawnIsAlive)
continue;
Expand All @@ -483,12 +483,12 @@ void CS2Fixes::Hook_CheckTransmit(CCheckTransmitInfo **ppInfoList, int infoCount
if (!pSelfZEPlayer)
continue;

for (int i = 1; i <= gpGlobals->maxClients; i++)
for (int i = 0; i < gpGlobals->maxClients; i++)
{
if (!pSelfZEPlayer->ShouldBlockTransmit(i - 1))
if (!pSelfZEPlayer->ShouldBlockTransmit(i))
continue;

auto pController = (CBasePlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)i);
CCSPlayerController* pController = CCSPlayerController::FromSlot(i);

if (!pController)
continue;
Expand Down
6 changes: 3 additions & 3 deletions src/detours.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ void FASTCALL Detour_UTIL_SayText2Filter(
const char *param4)
{
#ifdef _DEBUG
int entindex = filter.GetRecipientIndex(0).Get() + 1;
CCSPlayerController *target = (CCSPlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)entindex);
CPlayerSlot slot = filter.GetRecipientIndex(0);
CCSPlayerController* target = CCSPlayerController::FromSlot(slot);

if (target)
Message("Chat from %s to %s: %s\n", param1, target->GetPlayerName(), param2);
Expand All @@ -182,7 +182,7 @@ void FASTCALL Detour_UTIL_SayText2Filter(

void FASTCALL Detour_Host_Say(CCSPlayerController *pController, CCommand &args, bool teamonly, int unk1, const char *unk2)
{
bool bGagged = pController && g_playerManager->GetPlayer(pController->GetPlayerSlot())->IsGagged();
bool bGagged = pController && pController->GetZEPlayer()->IsGagged();

if (!bGagged && *args[1] != '/')
{
Expand Down
14 changes: 7 additions & 7 deletions src/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ GAME_EVENT_F(round_prestart)
if (!g_bForceCT)
return;

for (int i = 1; i <= gpGlobals->maxClients; i++)
for (int i = 0; i < gpGlobals->maxClients; i++)
{
CCSPlayerController *pController = (CCSPlayerController *)g_pEntitySystem->GetBaseEntity(CEntityIndex(i));
CCSPlayerController* pController = CCSPlayerController::FromSlot(i);

// Only do this for Ts, ignore CTs and specs
if (!pController || pController->m_iTeamNum() != CS_TEAM_T)
Expand Down Expand Up @@ -140,14 +140,14 @@ GAME_EVENT_F(player_spawn)

GAME_EVENT_F(player_hurt)
{
CBasePlayerController *pAttacker = (CBasePlayerController*)pEvent->GetPlayerController("attacker");
CBasePlayerController *pVictim = (CBasePlayerController*)pEvent->GetPlayerController("userid");
CCSPlayerController *pAttacker = (CCSPlayerController*)pEvent->GetPlayerController("attacker");
CCSPlayerController *pVictim = (CCSPlayerController*)pEvent->GetPlayerController("userid");

// Ignore Ts/zombies and CTs hurting themselves
if (!pAttacker || pAttacker->m_iTeamNum() != CS_TEAM_CT || pAttacker == pVictim)
return;

ZEPlayer *pPlayer = g_playerManager->GetPlayer(pAttacker->GetPlayerSlot());
ZEPlayer* pPlayer = pAttacker->GetZEPlayer();

if (!pPlayer)
return;
Expand Down Expand Up @@ -179,7 +179,7 @@ GAME_EVENT_F(round_end)
if (!pPlayer || pPlayer->GetTotalDamage() == 0)
continue;

CCSPlayerController* pController = (CCSPlayerController*)g_pEntitySystem->GetBaseEntity(CEntityIndex(pPlayer->GetPlayerSlot().Get() + 1));
CCSPlayerController* pController = CCSPlayerController::FromSlot(pPlayer->GetPlayerSlot());

if(!pController)
continue;
Expand All @@ -202,7 +202,7 @@ GAME_EVENT_F(round_end)
for (int i = 0; i < MIN(sortedPlayers.Count(), 5); i++)
{
ZEPlayer* pPlayer = sortedPlayers[i];
CCSPlayerController* pController = (CCSPlayerController*)g_pEntitySystem->GetBaseEntity(CEntityIndex(pPlayer->GetPlayerSlot().Get() + 1));
CCSPlayerController* pController = CCSPlayerController::FromSlot(pPlayer->GetPlayerSlot());

ClientPrintAll(HUD_PRINTTALK, " %c%i. %s \x01- \x07%i DMG", colorMap[MIN(i, 3)], i + 1, pController->GetPlayerName(), pPlayer->GetTotalDamage());
pPlayer->SetTotalDamage(0);
Expand Down
12 changes: 6 additions & 6 deletions src/playermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void CPlayerManager::OnLateLoad()
{
for (int i = 0; i < gpGlobals->maxClients; i++)
{
CCSPlayerController *pController = (CCSPlayerController *)g_pEntitySystem->GetBaseEntity(CEntityIndex(i + 1));
CCSPlayerController* pController = CCSPlayerController::FromSlot(i);

if (!pController || !pController->IsController() || !pController->IsConnected())
continue;
Expand Down Expand Up @@ -171,7 +171,7 @@ void CPlayerManager::CheckHideDistances()
if (!hideDistance)
continue;

auto pController = (CCSPlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(i + 1));
CCSPlayerController* pController = CCSPlayerController::FromSlot(i);

if (!pController)
continue;
Expand All @@ -189,7 +189,7 @@ void CPlayerManager::CheckHideDistances()
if (j == i)
continue;

auto pTargetController = (CCSPlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(j + 1));
CCSPlayerController* pTargetController = CCSPlayerController::FromSlot(j);

if (pTargetController)
{
Expand Down Expand Up @@ -245,7 +245,7 @@ ETargetType CPlayerManager::TargetPlayerString(int iCommandClient, const char* t
if (m_vecPlayers[i] == nullptr)
continue;

CBasePlayerController* player = (CBasePlayerController*)g_pEntitySystem->GetBaseEntity((CEntityIndex)(i + 1));
CCSPlayerController* player = CCSPlayerController::FromSlot(i);

if (!player || !player->IsController())
continue;
Expand All @@ -270,7 +270,7 @@ ETargetType CPlayerManager::TargetPlayerString(int iCommandClient, const char* t
if (m_vecPlayers[slot] == nullptr)
continue;

CBasePlayerController* player = (CBasePlayerController*)g_pEntitySystem->GetBaseEntity((CEntityIndex)(slot + 1));
CCSPlayerController* player = CCSPlayerController::FromSlot(slot);

if (!player)
continue;
Expand Down Expand Up @@ -298,7 +298,7 @@ ETargetType CPlayerManager::TargetPlayerString(int iCommandClient, const char* t
if (m_vecPlayers[i] == nullptr)
continue;

CBasePlayerController* player = (CBasePlayerController*)g_pEntitySystem->GetBaseEntity((CEntityIndex)(i + 1));
CCSPlayerController* player = CCSPlayerController::FromSlot(i);

if (!player || !player->IsController())
continue;
Expand Down