-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobals.cpp
204 lines (175 loc) · 5.77 KB
/
globals.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//===========================================================================//
//
// Author: Orsell
// Purpose: Global functions & variables used repeatedly throughout the plugin
//
//===========================================================================//
#include <string>
#include "globals.hpp"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//---------------------------------------------------------------------------------
// Purpose: Logging for the plugin by adding a prefix and line break.
// Max character limit of 1024 characters.
// level: 0 = Msg/DevMsg, 1 = Warning/DevWarning
//---------------------------------------------------------------------------------
void P2MMLog(int level, bool dev, const char* pMsgFormat, ...)
{
va_list argptr;
char szFormattedText[1024];
va_start(argptr, pMsgFormat);
V_vsnprintf(szFormattedText, sizeof(szFormattedText), pMsgFormat, argptr);
va_end(argptr);
char completeMsg[1024];
V_snprintf(completeMsg, sizeof(completeMsg), "(P2:MM PLUGIN): %s\n", szFormattedText);
if (dev && !p2mm_developer.GetBool())
{
return;
}
switch (level)
{
case 0:
ConColorMsg(P2MM_PLUGIN_CONSOLE_COLOR, completeMsg);
return;
case 1:
Warning(completeMsg);
return;
default:
Warning("(P2:MM PLUGIN): P2MMLog level set outside of 0-1, \"%i\", defaulting to ConColorMsg().\n", level);
ConColorMsg(P2MM_PLUGIN_CONSOLE_COLOR, completeMsg);
return;
}
}
void ReplacePattern(std::string target_module, std::string patternBytes, std::string replace_with)
{
void* addr = Memory::Scanner::Scan<void*>(Memory::Modules::Get(target_module), patternBytes);
if (!addr)
{
P2MMLog(1, false, "Failed to replace pattern!");
return;
}
std::vector<uint8_t> replace;
std::istringstream patternStream(replace_with);
std::string patternByte;
while (patternStream >> patternByte)
{
replace.push_back((uint8_t)std::stoul(patternByte, nullptr, 16));
}
DWORD oldprotect = 0;
DWORD newprotect = PAGE_EXECUTE_READWRITE;
VirtualProtect(addr, replace.size(), newprotect, &oldprotect);
memcpy_s(addr, replace.size(), replace.data(), replace.size());
VirtualProtect(addr, replace.size(), oldprotect, &newprotect);
}
//---------------------------------------------------------------------------------
// Purpose: Gets player entity index by userid.
//---------------------------------------------------------------------------------
int GFunc::UserIDToPlayerIndex(int userid)
{
for (int i = 1; i <= gpGlobals->maxClients; i++)
{
edict_t* pEdict = NULL;
if (i >= 0 && i < gpGlobals->maxEntities)
{
pEdict = (edict_t*)(gpGlobals->pEdicts + i);
}
if (engineServer->GetPlayerUserId(pEdict) == userid)
{
return i;
}
}
return NULL; // Return NULL if the index can't be found
}
// Get the script scope of a entity, thanks to Nullderef/Vista for this.
HSCRIPT GFunc::GetScriptScope(CBaseEntity* entity)
{
if (entity == NULL)
{
return NULL;
}
return reinterpret_cast<HSCRIPT>(reinterpret_cast<uintptr_t>(entity) + 0x33c);
}
//---------------------------------------------------------------------------------
// Purpose: Gets player base class by player entity index. Thanks to Nanoman2525 for this.
//---------------------------------------------------------------------------------
CBasePlayer* GFunc::PlayerIndexToPlayer(int playerIndex)
{
#ifdef _WIN32
static auto _PlayerIndexToPlayer = reinterpret_cast<CBasePlayer* (__cdecl*)(int)>(Memory::Scanner::Scan<void*>(Memory::Modules::Get("server"), "55 8B EC 8B 4D 08 33 C0 85 C9 7E 30"));
return _PlayerIndexToPlayer(playerIndex);
#else // Linux support TODO
return NULL;
#endif
}
//---------------------------------------------------------------------------------
// Purpose: Gets player username by index.
//---------------------------------------------------------------------------------
const char* GFunc::GetPlayerName(int index)
{
if (index <= 0)
{
return "";
}
player_info_t playerinfo;
if (!engineServer->GetPlayerInfo(index, &playerinfo))
{
return "";
}
return playerinfo.name;
}
//---------------------------------------------------------------------------------
// Purpose: Gets the account ID component of player SteamID by index.
//---------------------------------------------------------------------------------
int GFunc::GetSteamID(int index)
{
edict_t* pEdict = NULL;
if (index >= 0 && index < gpGlobals->maxEntities)
{
pEdict = (edict_t*)(gpGlobals->pEdicts + index);
}
if (!pEdict)
{
return -1;
}
player_info_t playerinfo;
if (!engineServer->GetPlayerInfo(index, &playerinfo))
{
return -1;
}
const CSteamID* pSteamID = engineServer->GetClientSteamID(pEdict);
if (!pSteamID || pSteamID->GetAccountID() == 0)
{
return -1;
}
return pSteamID->GetAccountID();
}
void GFunc::RemoveEntity(CBaseEntity* pEntity)
{
reinterpret_cast<void (*)(void*)>(Memory::Scanner::Scan<void*>(Memory::Modules::Get("server"), "55 8B EC 57 8B 7D 08 85 FF 74 72"))(reinterpret_cast<IServerEntity*>(pEntity)->GetNetworkable());
}
//---------------------------------------------------------------------------------
// Purpose: Self-explanatory.
//---------------------------------------------------------------------------------
int GFunc::GetConVarInt(const char* cvname)
{
ConVar* pVar = g_pCVar->FindVar(cvname);
if (!pVar)
{
P2MMLog(1, false, "Could not find ConVar: \"%s\"! Returning -1!", cvname);
return -1;
}
return pVar->GetInt();
}
//---------------------------------------------------------------------------------
// Purpose: Self-explanatory.
//---------------------------------------------------------------------------------
const char* GFunc::GetConVarString(const char* cvname)
{
ConVar* pVar = g_pCVar->FindVar(cvname);
if (!pVar)
{
P2MMLog(1, false, "Could not find ConVar: \"%s\"! Returning \"\"!", cvname);
return "";
}
return pVar->GetString();
}