-
Notifications
You must be signed in to change notification settings - Fork 11
/
cssdm_utils.cpp
399 lines (338 loc) · 10.1 KB
/
cssdm_utils.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
* vim: set ts=4 :
* ===============================================================
* CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC.
* By David "BAILOPAN" Anderson
* All rights reserved.
* ===============================================================
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* Version: $Id$
*/
#include "cssdm_headers.h"
#include "cssdm_players.h"
#include "cssdm_utils.h"
#include "cssdm_includesdk.h"
#include <sh_list.h>
#include <sh_memory.h>
using namespace SourceHook;
List<ICallWrapper *> g_CallWrappers;
ICallWrapper *g_pRoundRespawn = NULL;
ICallWrapper *g_pWeaponGetSlot = NULL;
ICallWrapper *g_pDropWeapon = NULL;
ICallWrapper *g_pRemoveAllItems = NULL;
ICallWrapper *g_pGiveAmmo = NULL;
int g_RagdollOffset = 0;
int g_LifeStateOffset = 0;
int g_DefuserOffset = 0;
void DM_ProtectMemory(void *addr, int length, int prot);
CBaseEntity *DM_GetBaseEntity(int index)
{
edict_t *pEdict = gamehelpers->EdictOfIndex(index);
if (!pEdict || pEdict->IsFree())
{
return NULL;
}
IServerUnknown *pUnknown = pEdict->GetUnknown();
if (!pUnknown)
{
return NULL;
}
return pUnknown->GetBaseEntity();
}
CBaseEntity *DM_GetAndClearRagdoll(CBaseEntity *pEntity, int &serial)
{
if (!g_RagdollOffset)
{
return NULL;
}
unsigned char *ptr = (unsigned char *)pEntity + g_RagdollOffset;
CBaseHandle &ph = *(CBaseHandle *)ptr;
if (!ph.IsValid())
{
return NULL;
}
int index = ph.GetEntryIndex();
serial = ph.GetSerialNumber();
if (!index)
{
return NULL;
}
CBaseEntity *pRagdoll = DM_GetBaseEntity(index);
ph.Set(NULL);
return pRagdoll;
}
void DM_RespawnPlayer(int client)
{
dm_player_t *player = DM_GetPlayer(client);
if (!player || !player->pEntity)
{
return;
}
if (player->respawn_timer)
{
timersys->KillTimer(player->respawn_timer);
player->respawn_timer = NULL;
}
CBaseEntity *pEntity = player->pEntity;
g_pRoundRespawn->Execute(&pEntity, NULL);
}
class variant_t
{
public:
union
{
bool bVal;
string_t iszVal;
int iVal;
float flVal;
float vecVal[3];
color32 rgbaVal;
};
CBaseHandle eVal; // this can't be in the union because it has a constructor.
fieldtype_t fieldType;
};
struct inputdata_t
{
CBaseEntity *pActivator; // The entity that initially caused this chain of output events.
CBaseEntity *pCaller; // The entity that fired this particular output.
variant_t value; // The data parameter for this output.
int nOutputID; // The unique ID of the output that was fired.
};
void DM_RemoveEntity(CBaseEntity *pEntity)
{
datamap_t *pMap = gamehelpers->GetDataMap(pEntity);
if(!pMap)
return;
typedescription_t *pDesc = gamehelpers->FindInDataMap(pMap, "InputKill");
if(!pDesc)
return;
inputdata_t data;
(pEntity->*pDesc->inputFunc)(data);
}
bool DM_IsPlayerAlive(int client)
{
dm_player_t *player = DM_GetPlayer(client);
if (!player || !player->pEntity || !g_LifeStateOffset)
{
return false;
}
if (!g_LifeStateOffset)
{
return player->pInfo->IsDead() ? false : true;
}
unsigned char *ptr = (unsigned char *)(player->pEntity) + g_LifeStateOffset;
return (*(char *)ptr == LIFE_ALIVE);
}
bool DM_CheckSerial(edict_t *pEdict, int serial)
{
int new_serial = (serial & ((1 << NUM_NETWORKED_EHANDLE_SERIAL_NUMBER_BITS) - 1));
return (pEdict->m_NetworkSerialNumber == new_serial);
}
CBaseEntity *DM_GetWeaponFromSlot(CBaseEntity *pEntity, int slot)
{
unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)];
*(CBaseEntity **)(&vstk[0]) = pEntity;
*(int *)(&vstk[sizeof(CBaseEntity *)]) = slot;
CBaseEntity *pWeapon = NULL;
g_pWeaponGetSlot->Execute(vstk, &pWeapon);
return pWeapon;
}
void DM_DropWeapon(CBaseEntity *pEntity, CBaseEntity *pWeapon)
{
unsigned char vstk[sizeof(CBaseEntity *) * 2 + sizeof(bool) * 2];
unsigned char *vptr = vstk;
*(CBaseEntity **)vptr = pEntity;
vptr += sizeof(CBaseEntity *);
*(CBaseEntity **)vptr = pWeapon;
vptr += sizeof(CBaseEntity *);
*(bool *)vptr = true;
vptr += sizeof(bool);
*(bool *)vptr = false;
//vptr += sizeof(bool);
g_pDropWeapon->Execute(vstk, NULL);
}
void DM_RemoveAllItems(CBaseEntity *pEntity, bool removeSuit)
{
unsigned char vstk[sizeof(CBaseEntity *) + sizeof(bool)];
unsigned char *vptr = vstk;
*(CBaseEntity **)vptr = pEntity;
vptr += sizeof(CBaseEntity *);
*(bool *)vptr = removeSuit;
//vptr += sizeof(bool);
g_pRemoveAllItems->Execute(vstk, NULL);
}
void DM_SetDefuseKit(CBaseEntity *pEntity, bool defuseKit)
{
if (!g_DefuserOffset)
{
return;
}
*(bool *)((unsigned char *)pEntity + g_DefuserOffset) = defuseKit;
}
int DM_GiveAmmo(CBaseEntity *pEntity, int type, int count, bool noSound)
{
unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)*2 + sizeof(bool)];
unsigned char *vptr = vstk;
*(CBaseEntity **)vptr = pEntity;
vptr += sizeof(CBaseEntity *);
*(int *)vptr = count;
vptr += sizeof(int);
*(int *)vptr = type;
vptr += sizeof(int);
*(bool *)vptr = noSound;
//vptr += sizeof(bool);
int ret;
g_pGiveAmmo->Execute(vstk, &ret);
return ret;
}
size_t DM_StringToBytes(const char *str, unsigned char buffer[], size_t maxlength)
{
size_t real_bytes = 0;
size_t length = strlen(str);
for (size_t i=0; i<length; i++)
{
if (real_bytes >= maxlength)
{
break;
}
buffer[real_bytes++] = (unsigned char)str[i];
if (str[i] == '\\'
&& str[i+1] == 'x')
{
if (i + 3 >= length)
{
continue;
}
/* Get the hex part */
char s_byte[3];
int r_byte;
s_byte[0] = str[i+2];
s_byte[1] = str[i+3];
s_byte[2] = '\n';
/* Read it as an integer */
sscanf(s_byte, "%x", &r_byte);
/* Save the value */
buffer[real_bytes-1] = (unsigned char)r_byte;
/* Adjust index */
i += 3;
}
}
return real_bytes;
}
void DM_ApplyPatch(void *address, int offset, const dmpatch_t *patch, dmpatch_t *restore)
{
unsigned char *addr = (unsigned char *)address + offset;
SourceHook::SetMemAccess(addr, 20, SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
if (restore)
{
for (size_t i=0; i<patch->bytes; i++)
{
restore->patch[i] = addr[i];
}
restore->bytes = patch->bytes;
}
for (size_t i=0; i<patch->bytes; i++)
{
addr[i] = patch->patch[i];
}
}
void DM_SetMemPatchable(void *address, size_t size)
{
SourceHook::SetMemAccess(address, (int)size, SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
}
#define GET_PROPERTY(cls, name, var) \
if ( (prop = gamehelpers->FindInSendTable(cls, name)) == NULL ) { \
snprintf(error, maxlength, "Could not find property: %s", name); \
return false; \
} \
var = prop->GetOffset();
bool InitializeUtils(char *error, size_t maxlength)
{
void *addr;
int offset;
SourceMod::PassInfo pass[4];
/** ROUNDRESPAWN */
g_pDmConf->GetMemSig("RoundRespawn", &addr);
g_pRoundRespawn = bintools->CreateCall(addr, CallConv_ThisCall, NULL, NULL, 0);
g_CallWrappers.push_back(g_pRoundRespawn);
/** WEAPON_GETSLOT */
g_pDmConf->GetOffset("Weapon_GetSlot", &offset);
pass[0].flags = PASSFLAG_BYVAL;
pass[0].size = sizeof(int);
pass[0].type = PassType_Basic;
pass[1].flags = PASSFLAG_BYVAL;
pass[1].size = sizeof(CBaseEntity *);
pass[1].type = PassType_Basic;
g_pWeaponGetSlot = bintools->CreateVCall(offset, 0, 0, &pass[1], &pass[0], 1);
g_CallWrappers.push_back(g_pWeaponGetSlot);
/** CSWEAPONDROP */
g_pDmConf->GetMemSig("CSWeaponDrop", &addr);
pass[0].flags = pass[1].flags = pass[2].flags = PASSFLAG_BYVAL;
pass[0].type = pass[1].type = pass[2].type = PassType_Basic;
pass[0].size = sizeof(CBaseEntity *);
pass[1].size = pass[2].size = sizeof(bool);
g_pDropWeapon = bintools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 3);
g_CallWrappers.push_back(g_pDropWeapon);
/** REMOVEALLITEMS */
g_pDmConf->GetOffset("RemoveAllItems", &offset);
pass[0].flags = PASSFLAG_BYVAL;
pass[0].size = sizeof(bool);
pass[0].type = PassType_Basic;
g_pRemoveAllItems = bintools->CreateVCall(offset, 0, 0, NULL, pass, 1);
g_CallWrappers.push_back(g_pRemoveAllItems);
/** GIVEAMMO */
g_pDmConf->GetOffset("GiveAmmo", &offset);
pass[0].flags = pass[1].flags = pass[2].flags = pass[3].flags = PASSFLAG_BYVAL;
pass[0].size = pass[1].size = pass[2].size = sizeof(int);
pass[3].size = sizeof(bool);
pass[0].type = pass[1].type = pass[2].type = pass[3].type = PassType_Basic;
g_pGiveAmmo = bintools->CreateVCall(offset, 0, 0, &pass[3], pass, 3);
g_CallWrappers.push_back(g_pGiveAmmo);
/** PROPERTIES */
sm_sendprop_info_t prop;
if(!gamehelpers->FindSendPropInfo("CCSPlayer", "m_hRagdoll", &prop))
{
snprintf(error, maxlength, "Failed to get prop info for m_hRagdoll");
return false;
}
g_RagdollOffset = prop.actual_offset;
if(!gamehelpers->FindSendPropInfo("CCSPlayer", "m_lifeState", &prop))
{
snprintf(error, maxlength, "Failed to get prop info for m_lifeState");
return false;
}
g_LifeStateOffset = prop.actual_offset;
if(!gamehelpers->FindSendPropInfo("CCSPlayer", "m_bHasDefuser", &prop))
{
snprintf(error, maxlength, "Failed to get prop info for m_bHasDefuser");
return false;
}
g_DefuserOffset = prop.actual_offset;
return true;
}
void ShutdownUtils()
{
List<ICallWrapper *>::iterator iter;
for (iter = g_CallWrappers.begin();
iter != g_CallWrappers.end();
iter++)
{
(*iter)->Destroy();
}
g_CallWrappers.clear();
}