Backed out changeset: 72798865a92b.
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 using namespace SourceHook;
36 List<ICallWrapper *> g_CallWrappers;
37 ICallWrapper *g_pRoundRespawn = NULL;
38 ICallWrapper *g_pRemoveEntity = NULL;
39 ICallWrapper *g_pWeaponGetSlot = NULL;
40 ICallWrapper *g_pDropWeapon = NULL;
41 ICallWrapper *g_pRemoveAllItems = NULL;
42 ICallWrapper *g_pGiveAmmo = NULL;
43 int g_RagdollOffset = 0;
44 int g_LifeStateOffset = 0;
45 int g_DefuserOffset = 0;
47 void DM_ProtectMemory(void *addr, int length, int prot);
49 CBaseEntity *DM_GetBaseEntity(int index)
51 edict_t *pEdict = gamehelpers->EdictOfIndex(index);
52 if (!pEdict || pEdict->IsFree())
56 IServerUnknown *pUnknown = pEdict->GetUnknown();
61 return pUnknown->GetBaseEntity();
64 CBaseEntity *DM_GetAndClearRagdoll(CBaseEntity *pEntity, int &serial)
71 unsigned char *ptr = (unsigned char *)pEntity + g_RagdollOffset;
72 CBaseHandle &ph = *(CBaseHandle *)ptr;
79 int index = ph.GetEntryIndex();
80 serial = ph.GetSerialNumber();
87 CBaseEntity *pRagdoll = DM_GetBaseEntity(index);
94 void DM_RespawnPlayer(int client)
96 dm_player_t *player = DM_GetPlayer(client);
97 if (!player || !player->pEntity)
102 if (player->respawn_timer)
104 timersys->KillTimer(player->respawn_timer);
105 player->respawn_timer = NULL;
108 CBaseEntity *pEntity = player->pEntity;
109 g_pRoundRespawn->Execute(&pEntity, NULL);
112 void DM_RemoveEntity(CBaseEntity *pEntity)
114 g_pRemoveEntity->Execute(&pEntity, NULL);
117 bool DM_IsPlayerAlive(int client)
119 dm_player_t *player = DM_GetPlayer(client);
120 if (!player || !player->pEntity || !g_LifeStateOffset)
125 if (!g_LifeStateOffset)
127 return player->pInfo->IsDead() ? false : true;
130 unsigned char *ptr = (unsigned char *)(player->pEntity) + g_LifeStateOffset;
131 return (*(char *)ptr == LIFE_ALIVE);
134 bool DM_CheckSerial(edict_t *pEdict, int serial)
136 int new_serial = (serial & ((1 << NUM_NETWORKED_EHANDLE_SERIAL_NUMBER_BITS) - 1));
137 return (pEdict->m_NetworkSerialNumber == new_serial);
140 CBaseEntity *DM_GetWeaponFromSlot(CBaseEntity *pEntity, int slot)
142 unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)];
143 *(CBaseEntity **)(&vstk[0]) = pEntity;
144 *(int *)(&vstk[sizeof(CBaseEntity *)]) = slot;
146 CBaseEntity *pWeapon = NULL;
148 g_pWeaponGetSlot->Execute(vstk, &pWeapon);
153 void DM_DropWeapon(CBaseEntity *pEntity, CBaseEntity *pWeapon)
155 unsigned char vstk[sizeof(CBaseEntity *) * 2 + sizeof(bool) * 2];
156 unsigned char *vptr = vstk;
158 *(CBaseEntity **)vptr = pEntity;
159 vptr += sizeof(CBaseEntity *);
160 *(CBaseEntity **)vptr = pWeapon;
161 vptr += sizeof(CBaseEntity *);
162 *(bool *)vptr = true;
163 vptr += sizeof(bool);
164 *(bool *)vptr = false;
165 //vptr += sizeof(bool);
167 g_pDropWeapon->Execute(vstk, NULL);
170 void DM_RemoveAllItems(CBaseEntity *pEntity, bool removeSuit)
172 unsigned char vstk[sizeof(CBaseEntity *) + sizeof(bool)];
173 unsigned char *vptr = vstk;
175 *(CBaseEntity **)vptr = pEntity;
176 vptr += sizeof(CBaseEntity *);
177 *(bool *)vptr = removeSuit;
178 //vptr += sizeof(bool);
180 g_pRemoveAllItems->Execute(vstk, NULL);
183 void DM_SetDefuseKit(CBaseEntity *pEntity, bool defuseKit)
185 if (!g_DefuserOffset)
190 *(bool *)((unsigned char *)pEntity + g_DefuserOffset) = defuseKit;
193 int DM_GiveAmmo(CBaseEntity *pEntity, int type, int count, bool noSound)
195 unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)*2 + sizeof(bool)];
196 unsigned char *vptr = vstk;
198 *(CBaseEntity **)vptr = pEntity;
199 vptr += sizeof(CBaseEntity *);
200 *(int *)vptr = count;
204 *(bool *)vptr = noSound;
205 //vptr += sizeof(bool);
208 g_pGiveAmmo->Execute(vstk, &ret);
213 size_t DM_StringToBytes(const char *str, unsigned char buffer[], size_t maxlength)
215 size_t real_bytes = 0;
216 size_t length = strlen(str);
218 for (size_t i=0; i<length; i++)
220 if (real_bytes >= maxlength)
224 buffer[real_bytes++] = (unsigned char)str[i];
232 /* Get the hex part */
235 s_byte[0] = str[i+2];
236 s_byte[1] = str[i+3];
238 /* Read it as an integer */
239 sscanf(s_byte, "%x", &r_byte);
241 buffer[real_bytes-1] = (unsigned char)r_byte;
250 void DM_ApplyPatch(void *address, int offset, const dmpatch_t *patch, dmpatch_t *restore)
252 unsigned char *addr = (unsigned char *)address + offset;
254 SourceHook::SetMemAccess(addr, 20, SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
258 for (size_t i=0; i<patch->bytes; i++)
260 restore->patch[i] = addr[i];
262 restore->bytes = patch->bytes;
265 for (size_t i=0; i<patch->bytes; i++)
267 addr[i] = patch->patch[i];
271 void DM_SetMemPatchable(void *address, size_t size)
273 SourceHook::SetMemAccess(address, (int)size, SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
276 #define GET_PROPERTY(cls, name, var) \
277 if ( (prop = gamehelpers->FindInSendTable(cls, name)) == NULL ) { \
278 snprintf(error, maxlength, "Could not find property: %s", name); \
281 var = prop->GetOffset();
283 bool InitializeUtils(char *error, size_t maxlength)
287 SourceMod::PassInfo pass[4];
290 g_pDmConf->GetMemSig("RoundRespawn", &addr);
291 g_pRoundRespawn = bintools->CreateCall(addr, CallConv_ThisCall, NULL, NULL, 0);
292 g_CallWrappers.push_back(g_pRoundRespawn);
295 g_pDmConf->GetMemSig("UTIL_Remove", &addr);
296 pass[0].flags = PASSFLAG_BYVAL;
297 pass[0].size = sizeof(CBaseEntity *);
298 pass[0].type = PassType_Basic;
299 g_pRemoveEntity = bintools->CreateCall(addr, CallConv_Cdecl, NULL, pass, 1);
300 g_CallWrappers.push_back(g_pRemoveEntity);
302 /** WEAPON_GETSLOT */
303 g_pDmConf->GetOffset("Weapon_GetSlot", &offset);
304 pass[0].flags = PASSFLAG_BYVAL;
305 pass[0].size = sizeof(int);
306 pass[0].type = PassType_Basic;
307 pass[1].flags = PASSFLAG_BYVAL;
308 pass[1].size = sizeof(CBaseEntity *);
309 pass[1].type = PassType_Basic;
310 g_pWeaponGetSlot = bintools->CreateVCall(offset, 0, 0, &pass[1], &pass[0], 1);
311 g_CallWrappers.push_back(g_pWeaponGetSlot);
314 g_pDmConf->GetMemSig("CSWeaponDrop", &addr);
315 pass[0].flags = pass[1].flags = pass[2].flags = PASSFLAG_BYVAL;
316 pass[0].type = pass[1].type = pass[2].type = PassType_Basic;
317 pass[0].size = sizeof(CBaseEntity *);
318 pass[1].size = pass[2].size = sizeof(bool);
319 g_pDropWeapon = bintools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 3);
320 g_CallWrappers.push_back(g_pDropWeapon);
322 /** REMOVEALLITEMS */
323 g_pDmConf->GetOffset("RemoveAllItems", &offset);
324 pass[0].flags = PASSFLAG_BYVAL;
325 pass[0].size = sizeof(bool);
326 pass[0].type = PassType_Basic;
327 g_pRemoveAllItems = bintools->CreateVCall(offset, 0, 0, NULL, pass, 1);
328 g_CallWrappers.push_back(g_pRemoveAllItems);
331 g_pDmConf->GetOffset("GiveAmmo", &offset);
332 pass[0].flags = pass[1].flags = pass[2].flags = pass[3].flags = PASSFLAG_BYVAL;
333 pass[0].size = pass[1].size = pass[2].size = sizeof(int);
334 pass[3].size = sizeof(bool);
335 pass[0].type = pass[1].type = pass[2].type = pass[3].type = PassType_Basic;
336 g_pGiveAmmo = bintools->CreateVCall(offset, 0, 0, &pass[3], pass, 3);
337 g_CallWrappers.push_back(g_pGiveAmmo);
340 sm_sendprop_info_t prop;
341 if(!gamehelpers->FindSendPropInfo("CCSPlayer", "m_hRagdoll", &prop))
343 snprintf(error, maxlength, "Failed to get prop info for m_hRagdoll");
346 g_RagdollOffset = prop.actual_offset;
347 if(!gamehelpers->FindSendPropInfo("CCSPlayer", "m_lifeState", &prop))
349 snprintf(error, maxlength, "Failed to get prop info for m_lifeState");
352 g_LifeStateOffset = prop.actual_offset;
353 if(!gamehelpers->FindSendPropInfo("CCSPlayer", "m_bHasDefuser", &prop))
355 snprintf(error, maxlength, "Failed to get prop info for m_bHasDefuser");
358 g_DefuserOffset = prop.actual_offset;
365 List<ICallWrapper *>::iterator iter;
366 for (iter = g_CallWrappers.begin();
367 iter != g_CallWrappers.end();
372 g_CallWrappers.clear();