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_POSIX
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 = gamehelpers->EdictOfIndex(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)
108 if (player->respawn_timer)
110 timersys->KillTimer(player->respawn_timer);
111 player->respawn_timer = NULL;
114 CBaseEntity *pEntity = player->pEntity;
115 g_pRoundRespawn->Execute(&pEntity, NULL);
118 void DM_RemoveEntity(CBaseEntity *pEntity)
120 g_pRemoveEntity->Execute(&pEntity, NULL);
123 bool DM_IsPlayerAlive(int client)
125 dm_player_t *player = DM_GetPlayer(client);
126 if (!player || !player->pEntity || !g_LifeStateOffset)
131 if (!g_LifeStateOffset)
133 return player->pInfo->IsDead() ? false : true;
136 unsigned char *ptr = (unsigned char *)(player->pEntity) + g_LifeStateOffset;
137 return (*(char *)ptr == LIFE_ALIVE);
140 bool DM_CheckSerial(edict_t *pEdict, int serial)
142 int new_serial = (serial & ((1 << NUM_NETWORKED_EHANDLE_SERIAL_NUMBER_BITS) - 1));
143 return (pEdict->m_NetworkSerialNumber == new_serial);
146 CBaseEntity *DM_GetWeaponFromSlot(CBaseEntity *pEntity, int slot)
148 unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)];
149 *(CBaseEntity **)(&vstk[0]) = pEntity;
150 *(int *)(&vstk[sizeof(CBaseEntity *)]) = slot;
152 CBaseEntity *pWeapon = NULL;
154 g_pWeaponGetSlot->Execute(vstk, &pWeapon);
159 void DM_DropWeapon(CBaseEntity *pEntity, CBaseEntity *pWeapon)
161 unsigned char vstk[sizeof(CBaseEntity *) * 2 + sizeof(bool) * 2];
162 unsigned char *vptr = vstk;
164 *(CBaseEntity **)vptr = pEntity;
165 vptr += sizeof(CBaseEntity *);
166 *(CBaseEntity **)vptr = pWeapon;
167 vptr += sizeof(CBaseEntity *);
168 *(bool *)vptr = true;
169 vptr += sizeof(bool);
170 *(bool *)vptr = false;
171 //vptr += sizeof(bool);
173 g_pDropWeapon->Execute(vstk, NULL);
176 void DM_RemoveAllItems(CBaseEntity *pEntity, bool removeSuit)
178 unsigned char vstk[sizeof(CBaseEntity *) + sizeof(bool)];
179 unsigned char *vptr = vstk;
181 *(CBaseEntity **)vptr = pEntity;
182 vptr += sizeof(CBaseEntity *);
183 *(bool *)vptr = removeSuit;
184 //vptr += sizeof(bool);
186 g_pRemoveAllItems->Execute(vstk, NULL);
189 void DM_SetDefuseKit(CBaseEntity *pEntity, bool defuseKit)
191 if (!g_DefuserOffset)
196 *(bool *)((unsigned char *)pEntity + g_DefuserOffset) = defuseKit;
199 int DM_GiveAmmo(CBaseEntity *pEntity, int type, int count, bool noSound)
201 unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)*2 + sizeof(bool)];
202 unsigned char *vptr = vstk;
204 *(CBaseEntity **)vptr = pEntity;
205 vptr += sizeof(CBaseEntity *);
206 *(int *)vptr = count;
210 *(bool *)vptr = noSound;
211 //vptr += sizeof(bool);
214 g_pGiveAmmo->Execute(vstk, &ret);
219 size_t DM_StringToBytes(const char *str, unsigned char buffer[], size_t maxlength)
221 size_t real_bytes = 0;
222 size_t length = strlen(str);
224 for (size_t i=0; i<length; i++)
226 if (real_bytes >= maxlength)
230 buffer[real_bytes++] = (unsigned char)str[i];
238 /* Get the hex part */
241 s_byte[0] = str[i+2];
242 s_byte[1] = str[i+3];
244 /* Read it as an integer */
245 sscanf(s_byte, "%x", &r_byte);
247 buffer[real_bytes-1] = (unsigned char)r_byte;
256 void DM_ApplyPatch(void *address, int offset, const dmpatch_t *patch, dmpatch_t *restore)
258 unsigned char *addr = (unsigned char *)address + offset;
260 DM_ProtectMemory(addr, 20, PAGE_EXECUTE_READWRITE);
264 for (size_t i=0; i<patch->bytes; i++)
266 restore->patch[i] = addr[i];
268 restore->bytes = patch->bytes;
271 for (size_t i=0; i<patch->bytes; i++)
273 addr[i] = patch->patch[i];
277 void DM_SetMemPatchable(void *address, size_t size)
279 DM_ProtectMemory(address, (int)size, PAGE_EXECUTE_READWRITE);
282 void DM_ProtectMemory(void *addr, int length, int prot)
284 #if defined PLATFORM_POSIX
285 void *addr2 = (void *)ALIGN(addr);
286 mprotect(addr2, sysconf(_SC_PAGESIZE), prot);
287 #elif defined PLATFORM_WINDOWS
289 VirtualProtect(addr, length, prot, &old_prot);
293 #define GET_PROPERTY(cls, name, var) \
294 if ( (prop = gamehelpers->FindInSendTable(cls, name)) == NULL ) { \
295 snprintf(error, maxlength, "Could not find property: %s", name); \
298 var = prop->GetOffset();
300 bool InitializeUtils(char *error, size_t maxlength)
304 SourceMod::PassInfo pass[4];
307 g_pDmConf->GetMemSig("RoundRespawn", &addr);
308 g_pRoundRespawn = bintools->CreateCall(addr, CallConv_ThisCall, NULL, NULL, 0);
309 g_CallWrappers.push_back(g_pRoundRespawn);
312 g_pDmConf->GetMemSig("UTIL_Remove", &addr);
313 pass[0].flags = PASSFLAG_BYVAL;
314 pass[0].size = sizeof(CBaseEntity *);
315 pass[0].type = PassType_Basic;
316 g_pRemoveEntity = bintools->CreateCall(addr, CallConv_Cdecl, NULL, pass, 1);
317 g_CallWrappers.push_back(g_pRemoveEntity);
319 /** WEAPON_GETSLOT */
320 g_pDmConf->GetOffset("Weapon_GetSlot", &offset);
321 pass[0].flags = PASSFLAG_BYVAL;
322 pass[0].size = sizeof(int);
323 pass[0].type = PassType_Basic;
324 pass[1].flags = PASSFLAG_BYVAL;
325 pass[1].size = sizeof(CBaseEntity *);
326 pass[1].type = PassType_Basic;
327 g_pWeaponGetSlot = bintools->CreateVCall(offset, 0, 0, &pass[1], &pass[0], 1);
328 g_CallWrappers.push_back(g_pWeaponGetSlot);
331 g_pDmConf->GetMemSig("CSWeaponDrop", &addr);
332 pass[0].flags = pass[1].flags = pass[2].flags = PASSFLAG_BYVAL;
333 pass[0].type = pass[1].type = pass[2].type = PassType_Basic;
334 pass[0].size = sizeof(CBaseEntity *);
335 pass[1].size = pass[2].size = sizeof(bool);
336 g_pDropWeapon = bintools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 3);
337 g_CallWrappers.push_back(g_pDropWeapon);
339 /** REMOVEALLITEMS */
340 g_pDmConf->GetOffset("RemoveAllItems", &offset);
341 pass[0].flags = PASSFLAG_BYVAL;
342 pass[0].size = sizeof(bool);
343 pass[0].type = PassType_Basic;
344 g_pRemoveAllItems = bintools->CreateVCall(offset, 0, 0, NULL, pass, 1);
345 g_CallWrappers.push_back(g_pRemoveAllItems);
348 g_pDmConf->GetOffset("GiveAmmo", &offset);
349 pass[0].flags = pass[1].flags = pass[2].flags = pass[3].flags = PASSFLAG_BYVAL;
350 pass[0].size = pass[1].size = pass[2].size = sizeof(int);
351 pass[3].size = sizeof(bool);
352 pass[0].type = pass[1].type = pass[2].type = pass[3].type = PassType_Basic;
353 g_pGiveAmmo = bintools->CreateVCall(offset, 0, 0, &pass[3], pass, 3);
354 g_CallWrappers.push_back(g_pGiveAmmo);
358 GET_PROPERTY("CCSPlayer", "m_hRagdoll", g_RagdollOffset);
359 GET_PROPERTY("CCSPlayer", "m_lifeState", g_LifeStateOffset);
360 GET_PROPERTY("CCSPlayer", "m_bHasDefuser", g_DefuserOffset);
367 List<ICallWrapper *>::iterator iter;
368 for (iter = g_CallWrappers.begin();
369 iter != g_CallWrappers.end();
374 g_CallWrappers.clear();