cssdm_utils.cpp
changeset 0 67dcd379a2d5
child 2 bf8615181357
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/cssdm_utils.cpp	Sun Jun 24 21:58:09 2007 +0000
     1.3 @@ -0,0 +1,367 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ===============================================================
     1.7 + * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC. 
     1.8 + * By David "BAILOPAN" Anderson
     1.9 + * All rights reserved.
    1.10 + * ===============================================================
    1.11 + *
    1.12 + * This program is free software; you can redistribute it and/or modify
    1.13 + * it under the terms of the GNU General Public License as published by
    1.14 + * the Free Software Foundation; either version 2 of the License, or (at
    1.15 + * your option) any later version.
    1.16 + * 
    1.17 + * This program is distributed in the hope that it will be useful, but
    1.18 + * WITHOUT ANY WARRANTY; without even the implied warranty of
    1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.20 + * General Public License for more details.
    1.21 + * 
    1.22 + * You should have received a copy of the GNU General Public License
    1.23 + * along with this program; see the file COPYING; if not, write to the
    1.24 + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
    1.25 + * MA 02110-1301 USA
    1.26 + * 
    1.27 + * Version: $Id$
    1.28 + */
    1.29 +
    1.30 +#include "cssdm_headers.h"
    1.31 +#include "cssdm_players.h"
    1.32 +#include "cssdm_utils.h"
    1.33 +#include "cssdm_includesdk.h"
    1.34 +#include <sh_list.h>
    1.35 +
    1.36 +#if defined PLATFORM_LINUX
    1.37 +#include <sys/mman.h>
    1.38 +#define	PAGE_SIZE	4096
    1.39 +#define ALIGN(ar) ((long)ar & ~(PAGE_SIZE-1))
    1.40 +#define	PAGE_EXECUTE_READWRITE	PROT_READ|PROT_WRITE|PROT_EXEC
    1.41 +#endif
    1.42 +
    1.43 +using namespace SourceHook;
    1.44 +
    1.45 +List<ICallWrapper *> g_CallWrappers;
    1.46 +ICallWrapper *g_pRoundRespawn = NULL;
    1.47 +ICallWrapper *g_pRemoveEntity = NULL;
    1.48 +ICallWrapper *g_pWeaponGetSlot = NULL;
    1.49 +ICallWrapper *g_pDropWeapon = NULL;
    1.50 +ICallWrapper *g_pRemoveAllItems = NULL;
    1.51 +ICallWrapper *g_pGiveAmmo = NULL;
    1.52 +int g_RagdollOffset = 0;
    1.53 +int g_LifeStateOffset = 0;
    1.54 +int g_DefuserOffset = 0;
    1.55 +
    1.56 +void DM_ProtectMemory(void *addr, int length, int prot);
    1.57 +
    1.58 +CBaseEntity *DM_GetBaseEntity(int index)
    1.59 +{
    1.60 +	edict_t *pEdict = engine->PEntityOfEntIndex(index);
    1.61 +	if (!pEdict || pEdict->IsFree())
    1.62 +	{
    1.63 +		return NULL;
    1.64 +	}
    1.65 +	IServerUnknown *pUnknown = pEdict->GetUnknown();
    1.66 +	if (!pUnknown)
    1.67 +	{
    1.68 +		return NULL;
    1.69 +	}
    1.70 +	return pUnknown->GetBaseEntity();
    1.71 +}
    1.72 +
    1.73 +CBaseEntity *DM_GetAndClearRagdoll(CBaseEntity *pEntity, int &serial)
    1.74 +{
    1.75 +	if (!g_RagdollOffset)
    1.76 +	{
    1.77 +		return NULL;
    1.78 +	}
    1.79 +
    1.80 +	unsigned char *ptr = (unsigned char *)pEntity + g_RagdollOffset;
    1.81 +	CBaseHandle &ph = *(CBaseHandle *)ptr;
    1.82 +
    1.83 +	if (!ph.IsValid())
    1.84 +	{
    1.85 +		return NULL;
    1.86 +	}
    1.87 +
    1.88 +	int index = ph.GetEntryIndex();
    1.89 +	serial = ph.GetSerialNumber();
    1.90 +
    1.91 +	if (!index)
    1.92 +	{
    1.93 +		return NULL;
    1.94 +	}
    1.95 +
    1.96 +	CBaseEntity *pRagdoll = DM_GetBaseEntity(index);
    1.97 +
    1.98 +	ph.Set(NULL);
    1.99 +
   1.100 +	return pRagdoll;
   1.101 +}
   1.102 +
   1.103 +void DM_RespawnPlayer(int client)
   1.104 +{
   1.105 +	dm_player_t *player = DM_GetPlayer(client);
   1.106 +	if (!player || !player->pEntity)
   1.107 +	{
   1.108 +		return;
   1.109 +	}
   1.110 +	CBaseEntity *pEntity = player->pEntity;
   1.111 +	g_pRoundRespawn->Execute(&pEntity, NULL);
   1.112 +}
   1.113 +
   1.114 +void DM_RemoveEntity(CBaseEntity *pEntity)
   1.115 +{
   1.116 +	g_pRemoveEntity->Execute(&pEntity, NULL);
   1.117 +}
   1.118 +
   1.119 +bool DM_IsPlayerAlive(int client)
   1.120 +{
   1.121 +	dm_player_t *player = DM_GetPlayer(client);
   1.122 +	if (!player || !player->pEntity || !g_LifeStateOffset)
   1.123 +	{
   1.124 +		return false;
   1.125 +	}
   1.126 +
   1.127 +	if (!g_LifeStateOffset)
   1.128 +	{
   1.129 +		return player->pInfo->IsDead() ? false : true;
   1.130 +	}
   1.131 +
   1.132 +	unsigned char *ptr = (unsigned char *)(player->pEntity) + g_LifeStateOffset;
   1.133 +	return (*(char *)ptr == LIFE_ALIVE);
   1.134 +}
   1.135 +
   1.136 +bool DM_CheckSerial(edict_t *pEdict, int serial)
   1.137 +{
   1.138 +	int new_serial = (serial & (1 << NUM_NETWORKED_EHANDLE_SERIAL_NUMBER_BITS) - 1);
   1.139 +	return (pEdict->m_NetworkSerialNumber == new_serial);
   1.140 +}
   1.141 +
   1.142 +CBaseEntity *DM_GetWeaponFromSlot(CBaseEntity *pEntity, int slot)
   1.143 +{
   1.144 +	unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)];
   1.145 +	*(CBaseEntity **)(&vstk[0]) = pEntity;
   1.146 +	*(int *)(&vstk[sizeof(CBaseEntity *)]) = slot;
   1.147 +
   1.148 +	CBaseEntity *pWeapon = NULL;
   1.149 +
   1.150 +	g_pWeaponGetSlot->Execute(vstk, &pWeapon);
   1.151 +
   1.152 +	return pWeapon;
   1.153 +}
   1.154 +
   1.155 +void DM_DropWeapon(CBaseEntity *pEntity, CBaseEntity *pWeapon)
   1.156 +{
   1.157 +	unsigned char vstk[sizeof(CBaseEntity *) * 2 + sizeof(bool) * 2];
   1.158 +	unsigned char *vptr = vstk;
   1.159 +
   1.160 +	*(CBaseEntity **)vptr = pEntity;
   1.161 +	vptr += sizeof(CBaseEntity *);
   1.162 +	*(CBaseEntity **)vptr = pWeapon;
   1.163 +	vptr += sizeof(CBaseEntity *);
   1.164 +	*(bool *)vptr = true;
   1.165 +	vptr += sizeof(bool);
   1.166 +	*(bool *)vptr = false;
   1.167 +	//vptr += sizeof(bool);
   1.168 +
   1.169 +	g_pDropWeapon->Execute(vstk, NULL);
   1.170 +}
   1.171 +
   1.172 +void DM_RemoveAllItems(CBaseEntity *pEntity, bool removeSuit)
   1.173 +{
   1.174 +	unsigned char vstk[sizeof(CBaseEntity *) + sizeof(bool)];
   1.175 +	unsigned char *vptr = vstk;
   1.176 +
   1.177 +	*(CBaseEntity **)vptr = pEntity;
   1.178 +	vptr += sizeof(CBaseEntity *);
   1.179 +	*(bool *)vptr = removeSuit;
   1.180 +	//vptr += sizeof(bool);
   1.181 +
   1.182 +	g_pRemoveAllItems->Execute(vstk, NULL);
   1.183 +}
   1.184 +
   1.185 +void DM_SetDefuseKit(CBaseEntity *pEntity, bool defuseKit)
   1.186 +{
   1.187 +	if (!g_DefuserOffset)
   1.188 +	{
   1.189 +		return;
   1.190 +	}
   1.191 +
   1.192 +	*(bool *)((unsigned char *)pEntity + g_DefuserOffset) = defuseKit;
   1.193 +}
   1.194 +
   1.195 +int DM_GiveAmmo(CBaseEntity *pEntity, int type, int count, bool noSound)
   1.196 +{
   1.197 +	unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)*2 + sizeof(bool)];
   1.198 +	unsigned char *vptr = vstk;
   1.199 +
   1.200 +	*(CBaseEntity **)vptr = pEntity;
   1.201 +	vptr += sizeof(CBaseEntity *);
   1.202 +	*(int *)vptr = count;
   1.203 +	vptr += sizeof(int);
   1.204 +	*(int *)vptr = type;
   1.205 +	vptr += sizeof(int);
   1.206 +	*(bool *)vptr = noSound;
   1.207 +	//vptr += sizeof(bool);
   1.208 +
   1.209 +	int ret;
   1.210 +	g_pGiveAmmo->Execute(vstk, &ret);
   1.211 +
   1.212 +	return ret;
   1.213 +}
   1.214 +
   1.215 +size_t DM_StringToBytes(const char *str, unsigned char buffer[], size_t maxlength)
   1.216 +{
   1.217 +	size_t real_bytes = 0;
   1.218 +	size_t length = strlen(str);
   1.219 +
   1.220 +	for (size_t i=0; i<length; i++)
   1.221 +	{
   1.222 +		if (real_bytes >= maxlength)
   1.223 +		{
   1.224 +			break;
   1.225 +		}
   1.226 +		buffer[real_bytes++] = (unsigned char)str[i];
   1.227 +		if (str[i] == '\\'
   1.228 +			&& str[i+1] == 'x')
   1.229 +		{
   1.230 +			if (i + 3 >= length)
   1.231 +			{
   1.232 +				continue;
   1.233 +			}
   1.234 +			/* Get the hex part */
   1.235 +			char s_byte[3];
   1.236 +			int r_byte;
   1.237 +			s_byte[0] = str[i+2];
   1.238 +			s_byte[1] = str[i+3];
   1.239 +			s_byte[2] = '\n';
   1.240 +			/* Read it as an integer */
   1.241 +			sscanf(s_byte, "%x", &r_byte);
   1.242 +			/* Save the value */
   1.243 +			buffer[real_bytes-1] = (unsigned char)r_byte;
   1.244 +			/* Adjust index */
   1.245 +			i += 3;
   1.246 +		}
   1.247 +	}
   1.248 +
   1.249 +	return real_bytes;
   1.250 +}
   1.251 +
   1.252 +void DM_ApplyPatch(void *address, int offset, const dmpatch_t *patch, dmpatch_t *restore)
   1.253 +{
   1.254 +	DM_ProtectMemory(address, 20, PAGE_EXECUTE_READWRITE);
   1.255 +
   1.256 +	unsigned char *addr = (unsigned char *)address + offset;
   1.257 +	if (restore)
   1.258 +	{
   1.259 +		for (size_t i=0; i<patch->bytes; i++)
   1.260 +		{
   1.261 +			restore->patch[i] = addr[i];
   1.262 +		}
   1.263 +		restore->bytes = patch->bytes;
   1.264 +	}
   1.265 +
   1.266 +	for (size_t i=0; i<patch->bytes; i++)
   1.267 +	{
   1.268 +		addr[i] = patch->patch[i];
   1.269 +	}
   1.270 +}
   1.271 +
   1.272 +void DM_SetMemPatchable(void *address, size_t size)
   1.273 +{
   1.274 +	DM_ProtectMemory(address, (int)size, PAGE_EXECUTE_READWRITE);
   1.275 +}
   1.276 +
   1.277 +void DM_ProtectMemory(void *addr, int length, int prot)
   1.278 +{
   1.279 +#if defined PLATFORM_LINUX
   1.280 +	void *addr2 = (void *)ALIGN(addr);
   1.281 +	mprotect(addr2, sysconf(_SC_PAGESIZE), prot);
   1.282 +#elif defined PLATFORM_WINDOWS
   1.283 +	DWORD old_prot;
   1.284 +	VirtualProtect(addr, length, prot, &old_prot);
   1.285 +#endif
   1.286 +}
   1.287 +
   1.288 +#define GET_PROPERTY(cls, name, var) \
   1.289 +	if ( (prop = gamehelpers->FindInSendTable(cls, name)) == NULL ) { \
   1.290 +		snprintf(error, maxlength, "Could not find property: %s", name); \
   1.291 +		return false; \
   1.292 +	} \
   1.293 +	var = prop->GetOffset();
   1.294 +
   1.295 +bool InitializeUtils(char *error, size_t maxlength)
   1.296 +{
   1.297 +	void *addr;
   1.298 +	int offset;
   1.299 +	PassInfo pass[4];
   1.300 +
   1.301 +	/** ROUNDRESPAWN */
   1.302 +	g_pDmConf->GetMemSig("RoundRespawn", &addr);
   1.303 +	g_pRoundRespawn = bintools->CreateCall(addr, CallConv_ThisCall, NULL, NULL, 0);
   1.304 +	g_CallWrappers.push_back(g_pRoundRespawn);
   1.305 +
   1.306 +	/** UTIL_REMOVE */
   1.307 +	g_pDmConf->GetMemSig("UTIL_Remove", &addr);
   1.308 +	pass[0].flags = PASSFLAG_BYVAL;
   1.309 +	pass[0].size = sizeof(CBaseEntity *);
   1.310 +	pass[0].type = PassType_Basic;
   1.311 +	g_pRemoveEntity = bintools->CreateCall(addr, CallConv_Cdecl, NULL, pass, 1);
   1.312 +	g_CallWrappers.push_back(g_pRemoveEntity);
   1.313 +	
   1.314 +	/** WEAPON_GETSLOT */
   1.315 +	g_pDmConf->GetOffset("Weapon_GetSlot", &offset);
   1.316 +	pass[0].flags = PASSFLAG_BYVAL;
   1.317 +	pass[0].size = sizeof(int);
   1.318 +	pass[0].type = PassType_Basic;
   1.319 +	pass[1].flags = PASSFLAG_BYVAL;
   1.320 +	pass[1].size = sizeof(CBaseEntity *);
   1.321 +	pass[1].type = PassType_Basic;
   1.322 +	g_pWeaponGetSlot = bintools->CreateVCall(offset, 0, 0, &pass[1], &pass[0], 1);
   1.323 +	g_CallWrappers.push_back(g_pWeaponGetSlot);
   1.324 +
   1.325 +	/** CSWEAPONDROP */
   1.326 +	g_pDmConf->GetMemSig("CSWeaponDrop", &addr);
   1.327 +	pass[0].flags = pass[1].flags = pass[2].flags  = PASSFLAG_BYVAL;
   1.328 +	pass[0].type = pass[1].type = pass[2].type = PassType_Basic;
   1.329 +	pass[0].size = sizeof(CBaseEntity *);
   1.330 +	pass[1].size = pass[2].size = sizeof(bool);
   1.331 +	g_pDropWeapon = bintools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 3);
   1.332 +	g_CallWrappers.push_back(g_pDropWeapon);
   1.333 +
   1.334 +	/** REMOVEALLITEMS */
   1.335 +	g_pDmConf->GetOffset("RemoveAllItems", &offset);
   1.336 +	pass[0].flags = PASSFLAG_BYVAL;
   1.337 +	pass[0].size = sizeof(bool);
   1.338 +	pass[0].type = PassType_Basic;
   1.339 +	g_pRemoveAllItems = bintools->CreateVCall(offset, 0, 0, NULL, pass, 1);
   1.340 +	g_CallWrappers.push_back(g_pRemoveAllItems);
   1.341 +
   1.342 +	/** GIVEAMMO */
   1.343 +	g_pDmConf->GetOffset("GiveAmmo", &offset);
   1.344 +	pass[0].flags = pass[1].flags = pass[2].flags = pass[3].flags = PASSFLAG_BYVAL;
   1.345 +	pass[0].size = pass[1].size = pass[2].size = sizeof(int);
   1.346 +	pass[3].size = sizeof(bool);
   1.347 +	pass[0].type = pass[1].type = pass[2].type = pass[3].type = PassType_Basic;
   1.348 +	g_pGiveAmmo = bintools->CreateVCall(offset, 0, 0, &pass[3], pass, 3);
   1.349 +	g_CallWrappers.push_back(g_pGiveAmmo);
   1.350 +
   1.351 +	/** PROPERTIES */
   1.352 +	SendProp *prop;
   1.353 +	GET_PROPERTY("CCSPlayer", "m_hRagdoll", g_RagdollOffset);
   1.354 +	GET_PROPERTY("CCSPlayer", "m_lifeState", g_LifeStateOffset);
   1.355 +	GET_PROPERTY("CCSPlayer", "m_bHasDefuser", g_DefuserOffset);
   1.356 +
   1.357 +	return true;
   1.358 +}
   1.359 +
   1.360 +void ShutdownUtils()
   1.361 +{
   1.362 +	List<ICallWrapper *>::iterator iter;
   1.363 +	for (iter = g_CallWrappers.begin();
   1.364 +		 iter != g_CallWrappers.end();
   1.365 +		 iter++)
   1.366 +	{
   1.367 +		(*iter)->Destroy();
   1.368 +	}
   1.369 +	g_CallWrappers.clear();
   1.370 +}