Skip to content

Commit 9424f37

Browse files
committed
feat: Moved a bunch of functions from globals to utils
1 parent 9ea4d6c commit 9424f37

File tree

4 files changed

+132
-309
lines changed

4 files changed

+132
-309
lines changed

src/globals.cpp

Lines changed: 0 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
#include "stdafx.hpp"
99
#include "globals.hpp"
1010

11-
#include "sdk.hpp"
12-
#include "p2sm.hpp"
13-
#include "utils/loggingsystem.hpp"
14-
1511
//---------------------------------------------------------------------------------
1612
// Interfaces from the engine.
1713
//---------------------------------------------------------------------------------
@@ -24,182 +20,3 @@ IPlayerInfoManager* g_pPlayerInfoManager = nullptr;
2420
// IGameEventManager2* g_pGameEventManager_;
2521
//IServerPluginHelpers* g_pPluginHelpers = nullptr;
2622
// IFileSystem* g_pFileSystem;
27-
28-
//---------------------------------------------------------------------------------
29-
// Purpose: Get the player's entity index by their userid.
30-
//---------------------------------------------------------------------------------
31-
int UserIDToPlayerIndex(const int userid)
32-
{
33-
for (int i = 1; i <= MAX_PLAYERS; i++)
34-
{
35-
const edict_t* pEdict = nullptr;
36-
if (i >= 0 && i < g_pGlobals->maxEntities)
37-
pEdict = (g_pGlobals->pEdicts + i);
38-
39-
if (engineServer->GetPlayerUserId(pEdict) == userid)
40-
return i;
41-
}
42-
return 0; // Return 0 if the index can't be found
43-
}
44-
45-
//---------------------------------------------------------------------------------
46-
// Purpose: Get player username by their entity index.
47-
//---------------------------------------------------------------------------------
48-
const char* GetPlayerName(const int playerIndex)
49-
{
50-
if (playerIndex <= 0 || playerIndex > MAX_PLAYERS)
51-
{
52-
Log(WARNING, true, "Invalid index passed to GetPlayerName: %i! Returning ""!", playerIndex);
53-
return "";
54-
}
55-
56-
player_info_t playerInfo;
57-
if (!engineServer->GetPlayerInfo(playerIndex, &playerInfo))
58-
{
59-
Log(WARNING, true, R"(Couldn't retrieve playerInfo of player index in GetPlayerName: %i! Returning ""!)", playerIndex);
60-
return "";
61-
}
62-
63-
return playerInfo.name;
64-
}
65-
66-
//---------------------------------------------------------------------------------
67-
// Purpose: Get the account ID component of player SteamID by the player's entity index.
68-
//---------------------------------------------------------------------------------
69-
int GetSteamID(const int playerIndex)
70-
{
71-
edict_t* pEdict = nullptr;
72-
if (playerIndex >= 0 && playerIndex < MAX_PLAYERS)
73-
pEdict = (g_pGlobals->pEdicts + playerIndex);
74-
75-
if (!pEdict)
76-
return -1;
77-
78-
player_info_t playerInfo;
79-
if (!engineServer->GetPlayerInfo(playerIndex, &playerInfo))
80-
return -1;
81-
82-
const CSteamID* pSteamID = engineServer->GetClientSteamID(pEdict);
83-
if (!pSteamID || pSteamID->GetAccountID() == 0)
84-
return -1;
85-
86-
return pSteamID->GetAccountID();
87-
}
88-
89-
//---------------------------------------------------------------------------------
90-
// Purpose: Self-explanatory.
91-
//---------------------------------------------------------------------------------
92-
int GetConVarInt(const char* cvName)
93-
{
94-
const ConVar* pVar = g_pCVar->FindVar(cvName);
95-
if (!pVar)
96-
{
97-
Log(WARNING, false, R"(Could not find ConVar: "%s"! Returning ""!)", cvName);
98-
return -1;
99-
}
100-
101-
return pVar->GetInt();
102-
}
103-
104-
//---------------------------------------------------------------------------------
105-
// Purpose: Self-explanatory.
106-
//---------------------------------------------------------------------------------
107-
const char* GetConVarString(const char* cvName)
108-
{
109-
const ConVar* pVar = g_pCVar->FindVar(cvName);
110-
if (!pVar)
111-
{
112-
Log(WARNING, false, R"(Could not find ConVar: "%s"! Returning ""!)", cvName);
113-
return "";
114-
}
115-
116-
return pVar->GetString();
117-
}
118-
119-
//---------------------------------------------------------------------------------
120-
// Purpose: Self-explanatory.
121-
//---------------------------------------------------------------------------------
122-
void SetConVarInt(const char* cvName, const int newValue)
123-
{
124-
ConVar* pVar = g_pCVar->FindVar(cvName);
125-
if (!pVar)
126-
{
127-
Log(WARNING, false, "Could not set ConVar: \"%s\"!", cvName);
128-
return;
129-
}
130-
pVar->SetValue(newValue);
131-
}
132-
133-
//---------------------------------------------------------------------------------
134-
// Purpose: Self-explanatory.
135-
//---------------------------------------------------------------------------------
136-
void SetConVarString(const char* cvName, const char* newValue)
137-
{
138-
ConVar* pVar = g_pCVar->FindVar(cvName);
139-
if (!pVar)
140-
{
141-
Log(WARNING, false, R"(Could not set ConVar: "%s"!)", cvName);
142-
return;
143-
}
144-
pVar->SetValue(newValue);
145-
return;
146-
}
147-
148-
//---------------------------------------------------------------------------------
149-
// Purpose: Returns true if player is a bot.
150-
//---------------------------------------------------------------------------------
151-
bool IsBot(const int playerIndex)
152-
{
153-
player_info_t playerInfo;
154-
if (!engineServer->GetPlayerInfo(playerIndex, &playerInfo))
155-
{
156-
Log(WARNING, true, R"(Couldn't retrieve player info of player index "%i" in IsBot!)", playerIndex);
157-
return false;
158-
}
159-
160-
return playerInfo.fakeplayer;
161-
}
162-
163-
//---------------------------------------------------------------------------------
164-
// Purpose: Get the number of bots in the server.
165-
//---------------------------------------------------------------------------------
166-
int GetBotCount()
167-
{
168-
int b = 0;
169-
FOR_ALL_PLAYERS(i)
170-
{
171-
if (IsBot(i))
172-
b++;
173-
}
174-
return b;
175-
}
176-
177-
//---------------------------------------------------------------------------------
178-
// Purpose: Get the current player count on the server.
179-
//---------------------------------------------------------------------------------
180-
int CURPLAYERCOUNT()
181-
{
182-
int playerCount = 0;
183-
for (int i = 1; i <= MAX_PLAYERS; i++)
184-
{
185-
if (UTIL_PlayerByIndex(i))
186-
playerCount++;
187-
}
188-
return playerCount;
189-
}
190-
191-
//---------------------------------------------------------------------------------
192-
// Purpose: Entity index to script handle.
193-
//---------------------------------------------------------------------------------
194-
HSCRIPT INDEXHANDLE(const int iEdictNum)
195-
{
196-
edict_t* pEdict = INDEXENT(iEdictNum);
197-
if (!pEdict->GetUnknown())
198-
return nullptr;
199-
200-
CBaseEntity* pBaseEntity = pEdict->GetUnknown()->GetBaseEntity();
201-
if (!pBaseEntity)
202-
return nullptr;
203-
204-
return CBaseEntity__GetScriptInstance(pBaseEntity);
205-
}

src/globals.hpp

Lines changed: 3 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,17 @@ class CEnvProjectedTexture;
4545

4646
// Macro to iterate through all players on the server.
4747
#define FOR_ALL_PLAYERS(i) \
48-
for (int (i) = 1; (i) <= CURPLAYERCOUNT(); (i)++)
48+
for (int (i) = 1; (i) <= Utils::CurPlayerCount(); (i)++)
4949

5050
// Player team enum.
51-
enum : std::uint8_t
51+
enum PlayerTeam : std::uint8_t
5252
{
5353
TEAM_SINGLEPLAYER = 0,
5454
TEAM_SPECTATOR,
5555
TEAM_RED,
5656
TEAM_BLUE
5757
};
5858

59-
// ClientPrint msg_dest macros.
60-
enum : std::uint8_t
61-
{
62-
HUD_PRINTNOTIFY = 1, // Works same as HUD_PRINTCONSOLE
63-
HUD_PRINTCONSOLE,
64-
HUD_PRINTTALK,
65-
HUD_PRINTCENTER
66-
};
67-
6859
//---------------------------------------------------------------------------------
6960
// Public variables.
7061
//---------------------------------------------------------------------------------
@@ -83,21 +74,6 @@ extern IPlayerInfoManager* g_pPlayerInfoManager;
8374
// extern IServerPluginHelpers* g_pPluginHelpers;
8475
// extern IFileSystem* g_pFileSystem;
8576

86-
//---------------------------------------------------------------------------------
87-
// UTIL functions.
88-
//---------------------------------------------------------------------------------
89-
int UserIDToPlayerIndex(int userid);
90-
const char* GetPlayerName(int playerIndex);
91-
int GetSteamID(int playerIndex);
92-
int GetConVarInt(const char* cvName);
93-
const char* GetConVarString(const char* cvName);
94-
void SetConVarInt(const char* cvName, int newValue);
95-
void SetConVarString(const char* cvName, const char* newValue);
96-
bool IsBot(int playerIndex);
97-
int GetBotCount();
98-
int CURPLAYERCOUNT();
99-
HSCRIPT INDEXHANDLE(int iEdictNum);
100-
10177
//---------------------------------------------------------------------------------
10278
// Player recipient filter.
10379
//---------------------------------------------------------------------------------
@@ -129,99 +105,4 @@ class CPlayerFilter : public IRecipientFilter
129105
private:
130106
int recipients[256];
131107
int recipientCount;
132-
};
133-
134-
// If String Equals String helper function. Taken from utils.h.
135-
inline bool FStrEq(const char* sz1, const char* sz2)
136-
{
137-
return (V_stricmp(sz1, sz2) == 0);
138-
}
139-
140-
// If String Has Substring helper function. Taken from utils.h.
141-
inline bool FSubStr(const char* sz1, const char* search)
142-
{
143-
return (V_strstr(sz1, search));
144-
}
145-
146-
//---------------------------------------------------------------------------------
147-
// Purpose: Entity edict to entity index. Taken from utils.h.
148-
//---------------------------------------------------------------------------------
149-
inline int EDICTINDEX(const edict_t* pEdict)
150-
{
151-
if (!pEdict)
152-
return 0;
153-
const int edictIndex = pEdict - g_pGlobals->pEdicts;
154-
Assert(edictIndex < MAX_EDICTS && edictIndex >= 0);
155-
return edictIndex;
156-
}
157-
158-
//---------------------------------------------------------------------------------
159-
// Purpose: Entity to entity index.
160-
//---------------------------------------------------------------------------------
161-
inline int ENTINDEX(CBaseEntity* pEnt)
162-
{
163-
static auto ENTINDEX_= reinterpret_cast<int (__cdecl*)(CBaseEntity*)>(Memory::Scan<void*>(SERVER, "55 8B EC 8B 45 ? 85 C0 74 ? 8B 40 ? 85 C0 74 ? 8B 0D"));
164-
//static auto ENTINDEX_ = reinterpret_cast<int (__cdecl*)(CBaseEntity*)>(Memory::Scan<void*>(SERVER, "55 8B EC 8B 45 ? 85 C0 74 ? 8B 40 ? 85 C0 74 ? 8B 0D"));
165-
return ENTINDEX_(pEnt);
166-
}
167-
168-
//---------------------------------------------------------------------------------
169-
// Purpose: Entity index to entity edict. Taken from utils.h.
170-
//---------------------------------------------------------------------------------
171-
inline edict_t* INDEXENT(const int iEdictNum)
172-
{
173-
Assert(iEdictNum >= 0 && iEdictNum < MAX_EDICTS);
174-
if (g_pGlobals->pEdicts)
175-
{
176-
edict_t* pEdict = g_pGlobals->pEdicts + iEdictNum;
177-
if (pEdict->IsFree())
178-
return nullptr;
179-
return pEdict;
180-
}
181-
return nullptr;
182-
}
183-
184-
//---------------------------------------------------------------------------------
185-
// Purpose: Returns the current game directory. Ex. portal2
186-
//---------------------------------------------------------------------------------
187-
inline const char* GetGameMainDir()
188-
{
189-
return CommandLine()->ParmValue("-game", CommandLine()->ParmValue("-defaultgamedir", "portal2"));
190-
}
191-
192-
//---------------------------------------------------------------------------------
193-
// Purpose: Returns the current root game directory. Ex. Portal 2
194-
//---------------------------------------------------------------------------------
195-
inline const char* GetGameRootDir()
196-
{
197-
static char baseDir[MAX_PATH] = { 0 };
198-
const std::string fullGameDirectoryPath = engineClient->GetGameDirectory();
199-
200-
// Find last two backslashes
201-
const size_t firstSlash = fullGameDirectoryPath.find_last_of('\\');
202-
const size_t secondSlash = fullGameDirectoryPath.find_last_of('\\', firstSlash - 1);
203-
const std::string tempBaseDir = fullGameDirectoryPath.substr(secondSlash + 1, firstSlash - secondSlash - 1);
204-
V_strcpy(baseDir, tempBaseDir.c_str()); // Copy to static buffer
205-
206-
return baseDir;
207-
}
208-
209-
//---------------------------------------------------------------------------------
210-
// Purpose: Returns true if a game session is running.
211-
//---------------------------------------------------------------------------------
212-
inline bool IsGameActive()
213-
{
214-
const bool m_activeGame = **Memory::Scan<bool**>(ENGINE, "C6 05 ? ? ? ? ? C6 05 ? ? ? ? ? 0F B6 96", 2);
215-
//const bool m_activeGame = **Memory::Scan<bool**>(ENGINE, "C6 05 ? ? ? ? ? C6 05 ? ? ? ? ? 0F B6 96", 2);
216-
return m_activeGame;
217-
}
218-
219-
//---------------------------------------------------------------------------------
220-
// Purpose: Returns true if a game session is shutting down or has been shutdown.
221-
//---------------------------------------------------------------------------------
222-
inline bool IsGameShutdown()
223-
{
224-
const bool bIsGameShuttingDown = reinterpret_cast<bool(__cdecl*)()>(Memory::Scan<void*>(ENGINE, "B8 05 00 00 00 39 05"))();
225-
//const bool bIsGameShuttingDown = reinterpret_cast<bool(__cdecl*)()>(Memory::Scan<void*>(ENGINE, "B8 05 00 00 00 39 05"))();
226-
return bIsGameShuttingDown;
227-
}
108+
};

0 commit comments

Comments
 (0)