cssdm_utils.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Fri Mar 22 14:42:08 2013 -0400 (2013-03-22)
changeset 137 d83c89c65e91
parent 105 09109e313871
child 138 e32cd100b7c2
permissions -rw-r--r--
Fixed possible "page boundary" crashes
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC. 
     5  * By David "BAILOPAN" Anderson
     6  * All rights reserved.
     7  * ===============================================================
     8  *
     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.
    13  * 
    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.
    18  * 
    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,
    22  * MA 02110-1301 USA
    23  * 
    24  * Version: $Id$
    25  */
    26 
    27 #include "cssdm_headers.h"
    28 #include "cssdm_players.h"
    29 #include "cssdm_utils.h"
    30 #include "cssdm_includesdk.h"
    31 #include <sh_list.h>
    32 #include <sh_memory.h>
    33 
    34 #if defined PLATFORM_POSIX
    35 #include <sys/mman.h>
    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
    39 #endif
    40 
    41 using namespace SourceHook;
    42 
    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;
    53 
    54 void DM_ProtectMemory(void *addr, int length, int prot);
    55 
    56 CBaseEntity *DM_GetBaseEntity(int index)
    57 {
    58 	edict_t *pEdict = gamehelpers->EdictOfIndex(index);
    59 	if (!pEdict || pEdict->IsFree())
    60 	{
    61 		return NULL;
    62 	}
    63 	IServerUnknown *pUnknown = pEdict->GetUnknown();
    64 	if (!pUnknown)
    65 	{
    66 		return NULL;
    67 	}
    68 	return pUnknown->GetBaseEntity();
    69 }
    70 
    71 CBaseEntity *DM_GetAndClearRagdoll(CBaseEntity *pEntity, int &serial)
    72 {
    73 	if (!g_RagdollOffset)
    74 	{
    75 		return NULL;
    76 	}
    77 
    78 	unsigned char *ptr = (unsigned char *)pEntity + g_RagdollOffset;
    79 	CBaseHandle &ph = *(CBaseHandle *)ptr;
    80 
    81 	if (!ph.IsValid())
    82 	{
    83 		return NULL;
    84 	}
    85 
    86 	int index = ph.GetEntryIndex();
    87 	serial = ph.GetSerialNumber();
    88 
    89 	if (!index)
    90 	{
    91 		return NULL;
    92 	}
    93 
    94 	CBaseEntity *pRagdoll = DM_GetBaseEntity(index);
    95 
    96 	ph.Set(NULL);
    97 
    98 	return pRagdoll;
    99 }
   100 
   101 void DM_RespawnPlayer(int client)
   102 {
   103 	dm_player_t *player = DM_GetPlayer(client);
   104 	if (!player || !player->pEntity)
   105 	{
   106 		return;
   107 	}
   108 
   109 	if (player->respawn_timer)
   110 	{
   111 		timersys->KillTimer(player->respawn_timer);
   112 		player->respawn_timer = NULL;
   113 	}
   114 
   115 	CBaseEntity *pEntity = player->pEntity;
   116 	g_pRoundRespawn->Execute(&pEntity, NULL);
   117 }
   118 
   119 void DM_RemoveEntity(CBaseEntity *pEntity)
   120 {
   121 	g_pRemoveEntity->Execute(&pEntity, NULL);
   122 }
   123 
   124 bool DM_IsPlayerAlive(int client)
   125 {
   126 	dm_player_t *player = DM_GetPlayer(client);
   127 	if (!player || !player->pEntity || !g_LifeStateOffset)
   128 	{
   129 		return false;
   130 	}
   131 
   132 	if (!g_LifeStateOffset)
   133 	{
   134 		return player->pInfo->IsDead() ? false : true;
   135 	}
   136 
   137 	unsigned char *ptr = (unsigned char *)(player->pEntity) + g_LifeStateOffset;
   138 	return (*(char *)ptr == LIFE_ALIVE);
   139 }
   140 
   141 bool DM_CheckSerial(edict_t *pEdict, int serial)
   142 {
   143 	int new_serial = (serial & ((1 << NUM_NETWORKED_EHANDLE_SERIAL_NUMBER_BITS) - 1));
   144 	return (pEdict->m_NetworkSerialNumber == new_serial);
   145 }
   146 
   147 CBaseEntity *DM_GetWeaponFromSlot(CBaseEntity *pEntity, int slot)
   148 {
   149 	unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)];
   150 	*(CBaseEntity **)(&vstk[0]) = pEntity;
   151 	*(int *)(&vstk[sizeof(CBaseEntity *)]) = slot;
   152 
   153 	CBaseEntity *pWeapon = NULL;
   154 
   155 	g_pWeaponGetSlot->Execute(vstk, &pWeapon);
   156 
   157 	return pWeapon;
   158 }
   159 
   160 void DM_DropWeapon(CBaseEntity *pEntity, CBaseEntity *pWeapon)
   161 {
   162 	unsigned char vstk[sizeof(CBaseEntity *) * 2 + sizeof(bool) * 2];
   163 	unsigned char *vptr = vstk;
   164 
   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);
   173 
   174 	g_pDropWeapon->Execute(vstk, NULL);
   175 }
   176 
   177 void DM_RemoveAllItems(CBaseEntity *pEntity, bool removeSuit)
   178 {
   179 	unsigned char vstk[sizeof(CBaseEntity *) + sizeof(bool)];
   180 	unsigned char *vptr = vstk;
   181 
   182 	*(CBaseEntity **)vptr = pEntity;
   183 	vptr += sizeof(CBaseEntity *);
   184 	*(bool *)vptr = removeSuit;
   185 	//vptr += sizeof(bool);
   186 
   187 	g_pRemoveAllItems->Execute(vstk, NULL);
   188 }
   189 
   190 void DM_SetDefuseKit(CBaseEntity *pEntity, bool defuseKit)
   191 {
   192 	if (!g_DefuserOffset)
   193 	{
   194 		return;
   195 	}
   196 
   197 	*(bool *)((unsigned char *)pEntity + g_DefuserOffset) = defuseKit;
   198 }
   199 
   200 int DM_GiveAmmo(CBaseEntity *pEntity, int type, int count, bool noSound)
   201 {
   202 	unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)*2 + sizeof(bool)];
   203 	unsigned char *vptr = vstk;
   204 
   205 	*(CBaseEntity **)vptr = pEntity;
   206 	vptr += sizeof(CBaseEntity *);
   207 	*(int *)vptr = count;
   208 	vptr += sizeof(int);
   209 	*(int *)vptr = type;
   210 	vptr += sizeof(int);
   211 	*(bool *)vptr = noSound;
   212 	//vptr += sizeof(bool);
   213 
   214 	int ret;
   215 	g_pGiveAmmo->Execute(vstk, &ret);
   216 
   217 	return ret;
   218 }
   219 
   220 size_t DM_StringToBytes(const char *str, unsigned char buffer[], size_t maxlength)
   221 {
   222 	size_t real_bytes = 0;
   223 	size_t length = strlen(str);
   224 
   225 	for (size_t i=0; i<length; i++)
   226 	{
   227 		if (real_bytes >= maxlength)
   228 		{
   229 			break;
   230 		}
   231 		buffer[real_bytes++] = (unsigned char)str[i];
   232 		if (str[i] == '\\'
   233 			&& str[i+1] == 'x')
   234 		{
   235 			if (i + 3 >= length)
   236 			{
   237 				continue;
   238 			}
   239 			/* Get the hex part */
   240 			char s_byte[3];
   241 			int r_byte;
   242 			s_byte[0] = str[i+2];
   243 			s_byte[1] = str[i+3];
   244 			s_byte[2] = '\n';
   245 			/* Read it as an integer */
   246 			sscanf(s_byte, "%x", &r_byte);
   247 			/* Save the value */
   248 			buffer[real_bytes-1] = (unsigned char)r_byte;
   249 			/* Adjust index */
   250 			i += 3;
   251 		}
   252 	}
   253 
   254 	return real_bytes;
   255 }
   256 
   257 void DM_ApplyPatch(void *address, int offset, const dmpatch_t *patch, dmpatch_t *restore)
   258 {
   259 	unsigned char *addr = (unsigned char *)address + offset;
   260 
   261 	DM_ProtectMemory(addr, 20, PAGE_EXECUTE_READWRITE);
   262 
   263 	if (restore)
   264 	{
   265 		for (size_t i=0; i<patch->bytes; i++)
   266 		{
   267 			restore->patch[i] = addr[i];
   268 		}
   269 		restore->bytes = patch->bytes;
   270 	}
   271 
   272 	for (size_t i=0; i<patch->bytes; i++)
   273 	{
   274 		addr[i] = patch->patch[i];
   275 	}
   276 }
   277 
   278 void DM_SetMemPatchable(void *address, size_t size)
   279 {
   280 	//DM_ProtectMemory(address, (int)size, PAGE_EXECUTE_READWRITE);
   281 	SourceHook::SetMemAccess(address, (int)size, SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
   282 }
   283 
   284 void DM_ProtectMemory(void *addr, int length, int prot)
   285 {
   286 #if defined PLATFORM_POSIX
   287 	void *addr2 = (void *)ALIGN(addr);
   288 	mprotect(addr2, sysconf(_SC_PAGESIZE), prot);
   289 #elif defined PLATFORM_WINDOWS
   290 	DWORD old_prot;
   291 	VirtualProtect(addr, length, prot, &old_prot);
   292 #endif
   293 }
   294 
   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); \
   298 		return false; \
   299 	} \
   300 	var = prop->GetOffset();
   301 
   302 bool InitializeUtils(char *error, size_t maxlength)
   303 {
   304 	void *addr;
   305 	int offset;
   306 	SourceMod::PassInfo pass[4];
   307 
   308 	/** ROUNDRESPAWN */
   309 	g_pDmConf->GetMemSig("RoundRespawn", &addr);
   310 	g_pRoundRespawn = bintools->CreateCall(addr, CallConv_ThisCall, NULL, NULL, 0);
   311 	g_CallWrappers.push_back(g_pRoundRespawn);
   312 
   313 	/** UTIL_REMOVE */
   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);
   320 	
   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);
   331 
   332 	/** CSWEAPONDROP */
   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);
   340 
   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);
   348 
   349 	/** GIVEAMMO */
   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);
   357 
   358 	/** PROPERTIES */
   359 	sm_sendprop_info_t prop;
   360 	if(!gamehelpers->FindSendPropInfo("CCSPlayer", "m_hRagdoll", &prop))
   361 	{
   362 		snprintf(error, maxlength, "Failed to get prop info for m_hRagdoll");
   363 		return false;
   364 	}
   365 	g_RagdollOffset = prop.actual_offset;
   366 	if(!gamehelpers->FindSendPropInfo("CCSPlayer", "m_lifeState", &prop))
   367 	{
   368 		snprintf(error, maxlength, "Failed to get prop info for m_lifeState");
   369 		return false;
   370 	}
   371 	g_LifeStateOffset = prop.actual_offset;
   372 	if(!gamehelpers->FindSendPropInfo("CCSPlayer", "m_bHasDefuser", &prop))
   373 	{
   374 		snprintf(error, maxlength, "Failed to get prop info for m_bHasDefuser");
   375 		return false;
   376 	}
   377 	g_DefuserOffset = prop.actual_offset;
   378 
   379 	return true;
   380 }
   381 
   382 void ShutdownUtils()
   383 {
   384 	List<ICallWrapper *>::iterator iter;
   385 	for (iter = g_CallWrappers.begin();
   386 		 iter != g_CallWrappers.end();
   387 		 iter++)
   388 	{
   389 		(*iter)->Destroy();
   390 	}
   391 	g_CallWrappers.clear();
   392 }