-
Notifications
You must be signed in to change notification settings - Fork 6
/
csdm_util.cpp
270 lines (219 loc) · 6.05 KB
/
csdm_util.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <malloc.h>
#include <stdlib.h>
#include "amxxmodule.h"
#include "csdm_amxx.h"
#include "csdm_util.h"
#include "csdm_tasks.h"
#include "csdm_player.h"
#include "cs_offsets.h"
CHooker HookerClass;
CHooker* Hooker = &HookerClass;
CFunc* RestartRoundHook = NULL;
FuncRestartRound RestartRoundOrig = NULL;
FuncDropPlayerItem DropPlayerItem = NULL;
void *g_restartround_func = NULL;
void *g_respawn_func = NULL;
float g_last_ff_set = 0.0f;
struct patch_set
{
unsigned char *base;
int *offsets;
unsigned char **patches;
unsigned char **unpatches;
unsigned int num;
};
patch_set g_takedmg_patches;
patch_set g_pkilled_patches;
struct _p_ldr
{
const char *cod;
int offs;
};
void InitPatchControl(_p_ldr src[], patch_set *dest, unsigned int num)
{
dest->offsets = new int[num];
dest->patches = new unsigned char *[num];
dest->unpatches = new unsigned char *[num];
for (unsigned int i=0; i<num; i++)
{
dest->offsets[i] = src[i].offs;
dest->patches[i] = (unsigned char *)strdup(src[i].cod);
dest->unpatches[i] = (unsigned char *)strdup(src[i].cod);
}
dest->num = num;
}
bool InitUtilCode()
{
void* gameAddress = Hooker->memFunc->GetLibraryFromAddress( ( void* )MDLL_Spawn );
#if defined __linux__
BOOL useSymbol = TRUE;
#else
BOOL useSymbol = FALSE;
#endif
DropPlayerItem = Hooker->MemorySearch< FuncDropPlayerItem >( CSPLAYER_DROPPLAYERITEM, gameAddress, useSymbol );
g_respawn_func = Hooker->MemorySearch< void* > ( CSPLAYER_ROUNDRESPAWN , gameAddress, useSymbol );
g_restartround_func = Hooker->MemorySearch< void* > ( CSPLAYER_RESTARTROUND , gameAddress, useSymbol );
g_takedmg_patches.base = Hooker->MemorySearch< unsigned char* > ( CSPLAYER_TAKEDAMAGE , gameAddress, useSymbol );
g_pkilled_patches.base = Hooker->MemorySearch< unsigned char* > ( CSGAME_PLAYERKILLED , gameAddress, useSymbol );
RestartRoundHook = Hooker->CreateHook( g_restartround_func, ( void* )RestartRound, TRUE );
RestartRoundOrig = reinterpret_cast< FuncRestartRound >( RestartRoundHook->GetOriginal() );
_p_ldr ffa[] = CSP_TD_PATCHES;
InitPatchControl(ffa, &g_takedmg_patches, CSP_TD_PATCH_COUNT);
_p_ldr pk[] = CSG_PK_PATCHES;
InitPatchControl(pk, &g_pkilled_patches, CSG_PK_PATCH_COUNT);
return true;
}
void DoPatch(patch_set *pt)
{
size_t pt_len;
unsigned char *offs;
for (unsigned int i=0; i<pt->num; i++)
{
offs = pt->base + pt->offsets[i];
pt_len = strlen((char *)pt->patches[i]);
Hooker->memFunc->ChangeMemoryProtection( offs, pt_len, PAGE_EXECUTE_READWRITE );
memcpy(pt->unpatches[i], offs, pt_len);
memcpy(offs, pt->patches[i], pt_len);
}
}
void DoUnPatch(patch_set *pt)
{
size_t pt_len;
unsigned char *offs;
for (unsigned int i=0; i<pt->num; i++)
{
offs = pt->base + pt->offsets[i];
pt_len = strlen((char *)pt->patches[i]);
Hooker->memFunc->ChangeMemoryProtection(offs, pt_len, PAGE_EXECUTE_READWRITE);
memcpy(offs, pt->unpatches[i], pt_len);
}
}
void FFA_Disable()
{
// unpatch!
DoUnPatch(&g_takedmg_patches);
DoUnPatch(&g_pkilled_patches);
g_ffa_state = false;
//we don't care about efficiency here
CVAR_SET_FLOAT("mp_friendlyfire", g_last_ff_set);
CVAR_SET_STRING("mp_freeforall", "0");
MF_ExecuteForward(g_StateChange, CSDM_FFA_DISABLE);
}
void FFA_Enable()
{
// patch!
DoPatch(&g_takedmg_patches);
DoPatch(&g_pkilled_patches);
g_ffa_state = true;
//we don't care about efficiency here
g_last_ff_set = CVAR_GET_FLOAT("mp_friendlyfire");
CVAR_SET_FLOAT("mp_friendlyfire", 1.0f);
CVAR_SET_STRING("mp_freeforall", "1");
MF_ExecuteForward(g_StateChange, CSDM_FFA_ENABLE);
}
void InternalSpawnPlayer(edict_t *pEdict)
{
pEdict->v.deadflag = DEAD_RESPAWNABLE;
pEdict->v.flags |= FL_FROZEN;
CBasePlayer *pPlayer = (CBasePlayer *)pEdict->pvPrivateData;
#if defined( WIN32 )
typedef void ( __fastcall *RESPAWNFUNC)( CBasePlayer* );
#else
typedef void ( *RESPAWNFUNC )( CBasePlayer* );
#endif
RESPAWNFUNC rfunc = ( RESPAWNFUNC )g_respawn_func;
rfunc( pPlayer );
}
void RespawnPlayer(edict_t *pEdict)
{
Respawn *pRespawn;
int send;
if (g_PreSpawn == -1)
send = -2;
else
send = g_PreSpawn;
player_states *pPlayer = GET_PLAYER(ENTINDEX(pEdict));
//player is already respawning!
if (pPlayer->spawning)
{
return;
}
pPlayer->spawning = true;
pRespawn = Respawn::NewRespawn(pEdict, send);
g_Timer.AddTask(pRespawn, g_SpawnWaitTime);
}
void FakespawnPlayer(edict_t *pEdict)
{
int team = GetPlayerTeam(pEdict);
if (team != TEAM_T && team != TEAM_CT)
{
return;
}
int index = ENTINDEX(pEdict);
player_states *pPlayer = GET_PLAYER(index);
if (pPlayer->spawning)
return;
pPlayer->spawning = true;
if (g_PreSpawn > 0 && MF_ExecuteForward(g_PreSpawn, (cell)index, (cell)1) > 0)
{
pPlayer->spawning = false;
return;
}
SpawnHandler(index, true);
pPlayer->spawning = false;
}
edict_t *GetEdict(int index)
{
if (index < 1 || index > gpGlobals->maxClients)
return NULL;
edict_t *pEdict = MF_GetPlayerEdict(index);
if (!pEdict)
return NULL;
if (pEdict->v.flags & FL_FAKECLIENT)
{
player_states *pPlayer = GET_PLAYER(index);
pPlayer->ingame = true;
return pEdict;
}
if (pEdict->v.flags & FL_CLIENT)
return pEdict;
return NULL;
}
void print_srvconsole( const char *fmt, ...)
{
va_list argptr;
static char string[384];
va_start(argptr, fmt);
vsnprintf(string, sizeof(string) - 1, fmt, argptr);
string[sizeof(string) - 1] = '\0';
va_end(argptr);
SERVER_PRINT(string);
}
static int g_TextMsg = 0;
void print_client(edict_t *pEdict, int type, const char *fmt, ...)
{
//don't do this to bots
if (pEdict->v.flags & FL_FAKECLIENT)
return;
char buffer[255];
va_list ap;
va_start(ap, fmt);
vsnprintf(buffer, sizeof(buffer)-1, fmt, ap);
va_end(ap);
if (!g_TextMsg)
{
g_TextMsg = GET_USER_MSG_ID(PLID, "TextMsg", NULL);
if (!g_TextMsg)
return;
}
MESSAGE_BEGIN(pEdict ? MSG_ONE : MSG_BROADCAST, g_TextMsg, NULL, pEdict);
WRITE_BYTE(type);
WRITE_STRING(buffer);
MESSAGE_END();
}
bool NotifyForRemove(unsigned int owner, edict_t *ent, edict_t *box)
{
cell idx1 = (cell)ENTINDEX(ent);
cell idx2 = box ? (cell)ENTINDEX(box) : 0;
return (MF_ExecuteForward(g_RemoveWeapon, (cell)owner, idx1, idx2) == 0);
}