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"
33 #if defined PLATFORM_LINUX
35 #define PAGE_SIZE 4096
36 #define ALIGN(ar) ((long)ar & ~(PAGE_SIZE-1))
37 #define PAGE_EXECUTE_READWRITE PROT_READ|PROT_WRITE|PROT_EXEC
40 using namespace SourceHook;
42 List<ICallWrapper *> g_CallWrappers;
43 ICallWrapper *g_pRoundRespawn = NULL;
44 ICallWrapper *g_pRemoveEntity = NULL;
45 ICallWrapper *g_pWeaponGetSlot = NULL;
46 ICallWrapper *g_pDropWeapon = NULL;
47 ICallWrapper *g_pRemoveAllItems = NULL;
48 ICallWrapper *g_pGiveAmmo = NULL;
49 int g_RagdollOffset = 0;
50 int g_LifeStateOffset = 0;
51 int g_DefuserOffset = 0;
53 void DM_ProtectMemory(void *addr, int length, int prot);
55 CBaseEntity *DM_GetBaseEntity(int index)
57 edict_t *pEdict = engine->PEntityOfEntIndex(index);
58 if (!pEdict || pEdict->IsFree())
62 IServerUnknown *pUnknown = pEdict->GetUnknown();
67 return pUnknown->GetBaseEntity();
70 CBaseEntity *DM_GetAndClearRagdoll(CBaseEntity *pEntity, int &serial)
77 unsigned char *ptr = (unsigned char *)pEntity + g_RagdollOffset;
78 CBaseHandle &ph = *(CBaseHandle *)ptr;
85 int index = ph.GetEntryIndex();
86 serial = ph.GetSerialNumber();
93 CBaseEntity *pRagdoll = DM_GetBaseEntity(index);
100 void DM_RespawnPlayer(int client)
102 dm_player_t *player = DM_GetPlayer(client);
103 if (!player || !player->pEntity)
107 CBaseEntity *pEntity = player->pEntity;
108 g_pRoundRespawn->Execute(&pEntity, NULL);
111 void DM_RemoveEntity(CBaseEntity *pEntity)
113 g_pRemoveEntity->Execute(&pEntity, NULL);
116 bool DM_IsPlayerAlive(int client)
118 dm_player_t *player = DM_GetPlayer(client);
119 if (!player || !player->pEntity || !g_LifeStateOffset)
124 if (!g_LifeStateOffset)
126 return player->pInfo->IsDead() ? false : true;
129 unsigned char *ptr = (unsigned char *)(player->pEntity) + g_LifeStateOffset;
130 return (*(char *)ptr == LIFE_ALIVE);
133 bool DM_CheckSerial(edict_t *pEdict, int serial)
135 int new_serial = (serial & (1 << NUM_NETWORKED_EHANDLE_SERIAL_NUMBER_BITS) - 1);
136 return (pEdict->m_NetworkSerialNumber == new_serial);
139 CBaseEntity *DM_GetWeaponFromSlot(CBaseEntity *pEntity, int slot)
141 unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)];
142 *(CBaseEntity **)(&vstk[0]) = pEntity;
143 *(int *)(&vstk[sizeof(CBaseEntity *)]) = slot;
145 CBaseEntity *pWeapon = NULL;
147 g_pWeaponGetSlot->Execute(vstk, &pWeapon);
152 void DM_DropWeapon(CBaseEntity *pEntity, CBaseEntity *pWeapon)
154 unsigned char vstk[sizeof(CBaseEntity *) * 2 + sizeof(bool) * 2];
155 unsigned char *vptr = vstk;
157 *(CBaseEntity **)vptr = pEntity;
158 vptr += sizeof(CBaseEntity *);
159 *(CBaseEntity **)vptr = pWeapon;
160 vptr += sizeof(CBaseEntity *);
161 *(bool *)vptr = true;
162 vptr += sizeof(bool);
163 *(bool *)vptr = false;
164 //vptr += sizeof(bool);
166 g_pDropWeapon->Execute(vstk, NULL);
169 void DM_RemoveAllItems(CBaseEntity *pEntity, bool removeSuit)
171 unsigned char vstk[sizeof(CBaseEntity *) + sizeof(bool)];
172 unsigned char *vptr = vstk;
174 *(CBaseEntity **)vptr = pEntity;
175 vptr += sizeof(CBaseEntity *);
176 *(bool *)vptr = removeSuit;
177 //vptr += sizeof(bool);
179 g_pRemoveAllItems->Execute(vstk, NULL);
182 void DM_SetDefuseKit(CBaseEntity *pEntity, bool defuseKit)
184 if (!g_DefuserOffset)
189 *(bool *)((unsigned char *)pEntity + g_DefuserOffset) = defuseKit;
192 int DM_GiveAmmo(CBaseEntity *pEntity, int type, int count, bool noSound)
194 unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)*2 + sizeof(bool)];
195 unsigned char *vptr = vstk;
197 *(CBaseEntity **)vptr = pEntity;
198 vptr += sizeof(CBaseEntity *);
199 *(int *)vptr = count;
203 *(bool *)vptr = noSound;
204 //vptr += sizeof(bool);
207 g_pGiveAmmo->Execute(vstk, &ret);
212 size_t DM_StringToBytes(const char *str, unsigned char buffer[], size_t maxlength)
214 size_t real_bytes = 0;
215 size_t length = strlen(str);
217 for (size_t i=0; i<length; i++)
219 if (real_bytes >= maxlength)
223 buffer[real_bytes++] = (unsigned char)str[i];
231 /* Get the hex part */
234 s_byte[0] = str[i+2];
235 s_byte[1] = str[i+3];
237 /* Read it as an integer */
238 sscanf(s_byte, "%x", &r_byte);
240 buffer[real_bytes-1] = (unsigned char)r_byte;
249 void DM_ApplyPatch(void *address, int offset, const dmpatch_t *patch, dmpatch_t *restore)
251 DM_ProtectMemory(address, 20, PAGE_EXECUTE_READWRITE);
253 unsigned char *addr = (unsigned char *)address + offset;
256 for (size_t i=0; i<patch->bytes; i++)
258 restore->patch[i] = addr[i];
260 restore->bytes = patch->bytes;
263 for (size_t i=0; i<patch->bytes; i++)
265 addr[i] = patch->patch[i];
269 void DM_SetMemPatchable(void *address, size_t size)
271 DM_ProtectMemory(address, (int)size, PAGE_EXECUTE_READWRITE);
274 void DM_ProtectMemory(void *addr, int length, int prot)
276 #if defined PLATFORM_LINUX
277 void *addr2 = (void *)ALIGN(addr);
278 mprotect(addr2, sysconf(_SC_PAGESIZE), prot);
279 #elif defined PLATFORM_WINDOWS
281 VirtualProtect(addr, length, prot, &old_prot);
285 #define GET_PROPERTY(cls, name, var) \
286 if ( (prop = gamehelpers->FindInSendTable(cls, name)) == NULL ) { \
287 snprintf(error, maxlength, "Could not find property: %s", name); \
290 var = prop->GetOffset();
292 bool InitializeUtils(char *error, size_t maxlength)
299 g_pDmConf->GetMemSig("RoundRespawn", &addr);
300 g_pRoundRespawn = bintools->CreateCall(addr, CallConv_ThisCall, NULL, NULL, 0);
301 g_CallWrappers.push_back(g_pRoundRespawn);
304 g_pDmConf->GetMemSig("UTIL_Remove", &addr);
305 pass[0].flags = PASSFLAG_BYVAL;
306 pass[0].size = sizeof(CBaseEntity *);
307 pass[0].type = PassType_Basic;
308 g_pRemoveEntity = bintools->CreateCall(addr, CallConv_Cdecl, NULL, pass, 1);
309 g_CallWrappers.push_back(g_pRemoveEntity);
311 /** WEAPON_GETSLOT */
312 g_pDmConf->GetOffset("Weapon_GetSlot", &offset);
313 pass[0].flags = PASSFLAG_BYVAL;
314 pass[0].size = sizeof(int);
315 pass[0].type = PassType_Basic;
316 pass[1].flags = PASSFLAG_BYVAL;
317 pass[1].size = sizeof(CBaseEntity *);
318 pass[1].type = PassType_Basic;
319 g_pWeaponGetSlot = bintools->CreateVCall(offset, 0, 0, &pass[1], &pass[0], 1);
320 g_CallWrappers.push_back(g_pWeaponGetSlot);
323 g_pDmConf->GetMemSig("CSWeaponDrop", &addr);
324 pass[0].flags = pass[1].flags = pass[2].flags = PASSFLAG_BYVAL;
325 pass[0].type = pass[1].type = pass[2].type = PassType_Basic;
326 pass[0].size = sizeof(CBaseEntity *);
327 pass[1].size = pass[2].size = sizeof(bool);
328 g_pDropWeapon = bintools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 3);
329 g_CallWrappers.push_back(g_pDropWeapon);
331 /** REMOVEALLITEMS */
332 g_pDmConf->GetOffset("RemoveAllItems", &offset);
333 pass[0].flags = PASSFLAG_BYVAL;
334 pass[0].size = sizeof(bool);
335 pass[0].type = PassType_Basic;
336 g_pRemoveAllItems = bintools->CreateVCall(offset, 0, 0, NULL, pass, 1);
337 g_CallWrappers.push_back(g_pRemoveAllItems);
340 g_pDmConf->GetOffset("GiveAmmo", &offset);
341 pass[0].flags = pass[1].flags = pass[2].flags = pass[3].flags = PASSFLAG_BYVAL;
342 pass[0].size = pass[1].size = pass[2].size = sizeof(int);
343 pass[3].size = sizeof(bool);
344 pass[0].type = pass[1].type = pass[2].type = pass[3].type = PassType_Basic;
345 g_pGiveAmmo = bintools->CreateVCall(offset, 0, 0, &pass[3], pass, 3);
346 g_CallWrappers.push_back(g_pGiveAmmo);
350 GET_PROPERTY("CCSPlayer", "m_hRagdoll", g_RagdollOffset);
351 GET_PROPERTY("CCSPlayer", "m_lifeState", g_LifeStateOffset);
352 GET_PROPERTY("CCSPlayer", "m_bHasDefuser", g_DefuserOffset);
359 List<ICallWrapper *>::iterator iter;
360 for (iter = g_CallWrappers.begin();
361 iter != g_CallWrappers.end();
366 g_CallWrappers.clear();