3 * ===============================================================
4 * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC.
5 * By David "BAILOPAN" Anderson
7 * ===============================================================
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
27 #include "cssdm_headers.h"
28 #include "cssdm_players.h"
29 #include "cssdm_utils.h"
30 #include "cssdm_includesdk.h"
32 #include <sh_memory.h>
34 #if defined PLATFORM_POSIX
36 #define PAGE_SIZE 4096
37 #define ALIGN(ar) ((long)ar & ~(PAGE_SIZE-1))
38 #define PAGE_EXECUTE_READWRITE PROT_READ|PROT_WRITE|PROT_EXEC
41 using namespace SourceHook;
43 List<ICallWrapper *> g_CallWrappers;
44 ICallWrapper *g_pRoundRespawn = NULL;
45 ICallWrapper *g_pRemoveEntity = NULL;
46 ICallWrapper *g_pWeaponGetSlot = NULL;
47 ICallWrapper *g_pDropWeapon = NULL;
48 ICallWrapper *g_pRemoveAllItems = NULL;
49 ICallWrapper *g_pGiveAmmo = NULL;
50 int g_RagdollOffset = 0;
51 int g_LifeStateOffset = 0;
52 int g_DefuserOffset = 0;
54 void DM_ProtectMemory(void *addr, int length, int prot);
56 CBaseEntity *DM_GetBaseEntity(int index)
58 edict_t *pEdict = gamehelpers->EdictOfIndex(index);
59 if (!pEdict || pEdict->IsFree())
63 IServerUnknown *pUnknown = pEdict->GetUnknown();
68 return pUnknown->GetBaseEntity();
71 CBaseEntity *DM_GetAndClearRagdoll(CBaseEntity *pEntity, int &serial)
78 unsigned char *ptr = (unsigned char *)pEntity + g_RagdollOffset;
79 CBaseHandle &ph = *(CBaseHandle *)ptr;
86 int index = ph.GetEntryIndex();
87 serial = ph.GetSerialNumber();
94 CBaseEntity *pRagdoll = DM_GetBaseEntity(index);
101 void DM_RespawnPlayer(int client)
103 dm_player_t *player = DM_GetPlayer(client);
104 if (!player || !player->pEntity)
109 if (player->respawn_timer)
111 timersys->KillTimer(player->respawn_timer);
112 player->respawn_timer = NULL;
115 CBaseEntity *pEntity = player->pEntity;
116 g_pRoundRespawn->Execute(&pEntity, NULL);
119 void DM_RemoveEntity(CBaseEntity *pEntity)
121 g_pRemoveEntity->Execute(&pEntity, NULL);
124 bool DM_IsPlayerAlive(int client)
126 dm_player_t *player = DM_GetPlayer(client);
127 if (!player || !player->pEntity || !g_LifeStateOffset)
132 if (!g_LifeStateOffset)
134 return player->pInfo->IsDead() ? false : true;
137 unsigned char *ptr = (unsigned char *)(player->pEntity) + g_LifeStateOffset;
138 return (*(char *)ptr == LIFE_ALIVE);
141 bool DM_CheckSerial(edict_t *pEdict, int serial)
143 int new_serial = (serial & ((1 << NUM_NETWORKED_EHANDLE_SERIAL_NUMBER_BITS) - 1));
144 return (pEdict->m_NetworkSerialNumber == new_serial);
147 CBaseEntity *DM_GetWeaponFromSlot(CBaseEntity *pEntity, int slot)
149 unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)];
150 *(CBaseEntity **)(&vstk[0]) = pEntity;
151 *(int *)(&vstk[sizeof(CBaseEntity *)]) = slot;
153 CBaseEntity *pWeapon = NULL;
155 g_pWeaponGetSlot->Execute(vstk, &pWeapon);
160 void DM_DropWeapon(CBaseEntity *pEntity, CBaseEntity *pWeapon)
162 unsigned char vstk[sizeof(CBaseEntity *) * 2 + sizeof(bool) * 2];
163 unsigned char *vptr = vstk;
165 *(CBaseEntity **)vptr = pEntity;
166 vptr += sizeof(CBaseEntity *);
167 *(CBaseEntity **)vptr = pWeapon;
168 vptr += sizeof(CBaseEntity *);
169 *(bool *)vptr = true;
170 vptr += sizeof(bool);
171 *(bool *)vptr = false;
172 //vptr += sizeof(bool);
174 g_pDropWeapon->Execute(vstk, NULL);
177 void DM_RemoveAllItems(CBaseEntity *pEntity, bool removeSuit)
179 unsigned char vstk[sizeof(CBaseEntity *) + sizeof(bool)];
180 unsigned char *vptr = vstk;
182 *(CBaseEntity **)vptr = pEntity;
183 vptr += sizeof(CBaseEntity *);
184 *(bool *)vptr = removeSuit;
185 //vptr += sizeof(bool);
187 g_pRemoveAllItems->Execute(vstk, NULL);
190 void DM_SetDefuseKit(CBaseEntity *pEntity, bool defuseKit)
192 if (!g_DefuserOffset)
197 *(bool *)((unsigned char *)pEntity + g_DefuserOffset) = defuseKit;
200 int DM_GiveAmmo(CBaseEntity *pEntity, int type, int count, bool noSound)
202 unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)*2 + sizeof(bool)];
203 unsigned char *vptr = vstk;
205 *(CBaseEntity **)vptr = pEntity;
206 vptr += sizeof(CBaseEntity *);
207 *(int *)vptr = count;
211 *(bool *)vptr = noSound;
212 //vptr += sizeof(bool);
215 g_pGiveAmmo->Execute(vstk, &ret);
220 size_t DM_StringToBytes(const char *str, unsigned char buffer[], size_t maxlength)
222 size_t real_bytes = 0;
223 size_t length = strlen(str);
225 for (size_t i=0; i<length; i++)
227 if (real_bytes >= maxlength)
231 buffer[real_bytes++] = (unsigned char)str[i];
239 /* Get the hex part */
242 s_byte[0] = str[i+2];
243 s_byte[1] = str[i+3];
245 /* Read it as an integer */
246 sscanf(s_byte, "%x", &r_byte);
248 buffer[real_bytes-1] = (unsigned char)r_byte;
257 void DM_ApplyPatch(void *address, int offset, const dmpatch_t *patch, dmpatch_t *restore)
259 unsigned char *addr = (unsigned char *)address + offset;
261 DM_ProtectMemory(addr, 20, PAGE_EXECUTE_READWRITE);
265 for (size_t i=0; i<patch->bytes; i++)
267 restore->patch[i] = addr[i];
269 restore->bytes = patch->bytes;
272 for (size_t i=0; i<patch->bytes; i++)
274 addr[i] = patch->patch[i];
278 void DM_SetMemPatchable(void *address, size_t size)
280 //DM_ProtectMemory(address, (int)size, PAGE_EXECUTE_READWRITE);
281 SourceHook::SetMemAccess(address, (int)size, SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
284 void DM_ProtectMemory(void *addr, int length, int prot)
286 #if defined PLATFORM_POSIX
287 void *addr2 = (void *)ALIGN(addr);
288 mprotect(addr2, sysconf(_SC_PAGESIZE), prot);
289 #elif defined PLATFORM_WINDOWS
291 VirtualProtect(addr, length, prot, &old_prot);
295 #define GET_PROPERTY(cls, name, var) \
296 if ( (prop = gamehelpers->FindInSendTable(cls, name)) == NULL ) { \
297 snprintf(error, maxlength, "Could not find property: %s", name); \
300 var = prop->GetOffset();
302 bool InitializeUtils(char *error, size_t maxlength)
306 SourceMod::PassInfo pass[4];
309 g_pDmConf->GetMemSig("RoundRespawn", &addr);
310 g_pRoundRespawn = bintools->CreateCall(addr, CallConv_ThisCall, NULL, NULL, 0);
311 g_CallWrappers.push_back(g_pRoundRespawn);
314 g_pDmConf->GetMemSig("UTIL_Remove", &addr);
315 pass[0].flags = PASSFLAG_BYVAL;
316 pass[0].size = sizeof(CBaseEntity *);
317 pass[0].type = PassType_Basic;
318 g_pRemoveEntity = bintools->CreateCall(addr, CallConv_Cdecl, NULL, pass, 1);
319 g_CallWrappers.push_back(g_pRemoveEntity);
321 /** WEAPON_GETSLOT */
322 g_pDmConf->GetOffset("Weapon_GetSlot", &offset);
323 pass[0].flags = PASSFLAG_BYVAL;
324 pass[0].size = sizeof(int);
325 pass[0].type = PassType_Basic;
326 pass[1].flags = PASSFLAG_BYVAL;
327 pass[1].size = sizeof(CBaseEntity *);
328 pass[1].type = PassType_Basic;
329 g_pWeaponGetSlot = bintools->CreateVCall(offset, 0, 0, &pass[1], &pass[0], 1);
330 g_CallWrappers.push_back(g_pWeaponGetSlot);
333 g_pDmConf->GetMemSig("CSWeaponDrop", &addr);
334 pass[0].flags = pass[1].flags = pass[2].flags = PASSFLAG_BYVAL;
335 pass[0].type = pass[1].type = pass[2].type = PassType_Basic;
336 pass[0].size = sizeof(CBaseEntity *);
337 pass[1].size = pass[2].size = sizeof(bool);
338 g_pDropWeapon = bintools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 3);
339 g_CallWrappers.push_back(g_pDropWeapon);
341 /** REMOVEALLITEMS */
342 g_pDmConf->GetOffset("RemoveAllItems", &offset);
343 pass[0].flags = PASSFLAG_BYVAL;
344 pass[0].size = sizeof(bool);
345 pass[0].type = PassType_Basic;
346 g_pRemoveAllItems = bintools->CreateVCall(offset, 0, 0, NULL, pass, 1);
347 g_CallWrappers.push_back(g_pRemoveAllItems);
350 g_pDmConf->GetOffset("GiveAmmo", &offset);
351 pass[0].flags = pass[1].flags = pass[2].flags = pass[3].flags = PASSFLAG_BYVAL;
352 pass[0].size = pass[1].size = pass[2].size = sizeof(int);
353 pass[3].size = sizeof(bool);
354 pass[0].type = pass[1].type = pass[2].type = pass[3].type = PassType_Basic;
355 g_pGiveAmmo = bintools->CreateVCall(offset, 0, 0, &pass[3], pass, 3);
356 g_CallWrappers.push_back(g_pGiveAmmo);
359 sm_sendprop_info_t prop;
360 if(!gamehelpers->FindSendPropInfo("CCSPlayer", "m_hRagdoll", &prop))
362 snprintf(error, maxlength, "Failed to get prop info for m_hRagdoll");
365 g_RagdollOffset = prop.actual_offset;
366 if(!gamehelpers->FindSendPropInfo("CCSPlayer", "m_lifeState", &prop))
368 snprintf(error, maxlength, "Failed to get prop info for m_lifeState");
371 g_LifeStateOffset = prop.actual_offset;
372 if(!gamehelpers->FindSendPropInfo("CCSPlayer", "m_bHasDefuser", &prop))
374 snprintf(error, maxlength, "Failed to get prop info for m_bHasDefuser");
377 g_DefuserOffset = prop.actual_offset;
384 List<ICallWrapper *>::iterator iter;
385 for (iter = g_CallWrappers.begin();
386 iter != g_CallWrappers.end();
391 g_CallWrappers.clear();