cssdm_utils.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Sun Oct 20 14:55:58 2013 -0400 (2013-10-20)
changeset 158 f388630a66df
parent 139 df5cbb604269
child 167 6e95cf358424
permissions -rw-r--r--
Fix mac build
     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 using namespace SourceHook;
    35 
    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;
    46 
    47 void DM_ProtectMemory(void *addr, int length, int prot);
    48 
    49 CBaseEntity *DM_GetBaseEntity(int index)
    50 {
    51 	edict_t *pEdict = gamehelpers->EdictOfIndex(index);
    52 	if (!pEdict || pEdict->IsFree())
    53 	{
    54 		return NULL;
    55 	}
    56 	IServerUnknown *pUnknown = pEdict->GetUnknown();
    57 	if (!pUnknown)
    58 	{
    59 		return NULL;
    60 	}
    61 	return pUnknown->GetBaseEntity();
    62 }
    63 
    64 CBaseEntity *DM_GetAndClearRagdoll(CBaseEntity *pEntity, int &serial)
    65 {
    66 	if (!g_RagdollOffset)
    67 	{
    68 		return NULL;
    69 	}
    70 
    71 	unsigned char *ptr = (unsigned char *)pEntity + g_RagdollOffset;
    72 	CBaseHandle &ph = *(CBaseHandle *)ptr;
    73 
    74 	if (!ph.IsValid())
    75 	{
    76 		return NULL;
    77 	}
    78 
    79 	int index = ph.GetEntryIndex();
    80 	serial = ph.GetSerialNumber();
    81 
    82 	if (!index)
    83 	{
    84 		return NULL;
    85 	}
    86 
    87 	CBaseEntity *pRagdoll = DM_GetBaseEntity(index);
    88 
    89 	ph.Set(NULL);
    90 
    91 	return pRagdoll;
    92 }
    93 
    94 void DM_RespawnPlayer(int client)
    95 {
    96 	dm_player_t *player = DM_GetPlayer(client);
    97 	if (!player || !player->pEntity)
    98 	{
    99 		return;
   100 	}
   101 
   102 	if (player->respawn_timer)
   103 	{
   104 		timersys->KillTimer(player->respawn_timer);
   105 		player->respawn_timer = NULL;
   106 	}
   107 
   108 	CBaseEntity *pEntity = player->pEntity;
   109 	g_pRoundRespawn->Execute(&pEntity, NULL);
   110 }
   111 
   112 void DM_RemoveEntity(CBaseEntity *pEntity)
   113 {
   114 	g_pRemoveEntity->Execute(&pEntity, NULL);
   115 }
   116 
   117 bool DM_IsPlayerAlive(int client)
   118 {
   119 	dm_player_t *player = DM_GetPlayer(client);
   120 	if (!player || !player->pEntity || !g_LifeStateOffset)
   121 	{
   122 		return false;
   123 	}
   124 
   125 	if (!g_LifeStateOffset)
   126 	{
   127 		return player->pInfo->IsDead() ? false : true;
   128 	}
   129 
   130 	unsigned char *ptr = (unsigned char *)(player->pEntity) + g_LifeStateOffset;
   131 	return (*(char *)ptr == LIFE_ALIVE);
   132 }
   133 
   134 bool DM_CheckSerial(edict_t *pEdict, int serial)
   135 {
   136 	int new_serial = (serial & ((1 << NUM_NETWORKED_EHANDLE_SERIAL_NUMBER_BITS) - 1));
   137 	return (pEdict->m_NetworkSerialNumber == new_serial);
   138 }
   139 
   140 CBaseEntity *DM_GetWeaponFromSlot(CBaseEntity *pEntity, int slot)
   141 {
   142 	unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)];
   143 	*(CBaseEntity **)(&vstk[0]) = pEntity;
   144 	*(int *)(&vstk[sizeof(CBaseEntity *)]) = slot;
   145 
   146 	CBaseEntity *pWeapon = NULL;
   147 
   148 	g_pWeaponGetSlot->Execute(vstk, &pWeapon);
   149 
   150 	return pWeapon;
   151 }
   152 
   153 void DM_DropWeapon(CBaseEntity *pEntity, CBaseEntity *pWeapon)
   154 {
   155 	unsigned char vstk[sizeof(CBaseEntity *) * 2 + sizeof(bool) * 2];
   156 	unsigned char *vptr = vstk;
   157 
   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);
   166 
   167 	g_pDropWeapon->Execute(vstk, NULL);
   168 }
   169 
   170 void DM_RemoveAllItems(CBaseEntity *pEntity, bool removeSuit)
   171 {
   172 	unsigned char vstk[sizeof(CBaseEntity *) + sizeof(bool)];
   173 	unsigned char *vptr = vstk;
   174 
   175 	*(CBaseEntity **)vptr = pEntity;
   176 	vptr += sizeof(CBaseEntity *);
   177 	*(bool *)vptr = removeSuit;
   178 	//vptr += sizeof(bool);
   179 
   180 	g_pRemoveAllItems->Execute(vstk, NULL);
   181 }
   182 
   183 void DM_SetDefuseKit(CBaseEntity *pEntity, bool defuseKit)
   184 {
   185 	if (!g_DefuserOffset)
   186 	{
   187 		return;
   188 	}
   189 
   190 	*(bool *)((unsigned char *)pEntity + g_DefuserOffset) = defuseKit;
   191 }
   192 
   193 int DM_GiveAmmo(CBaseEntity *pEntity, int type, int count, bool noSound)
   194 {
   195 	unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)*2 + sizeof(bool)];
   196 	unsigned char *vptr = vstk;
   197 
   198 	*(CBaseEntity **)vptr = pEntity;
   199 	vptr += sizeof(CBaseEntity *);
   200 	*(int *)vptr = count;
   201 	vptr += sizeof(int);
   202 	*(int *)vptr = type;
   203 	vptr += sizeof(int);
   204 	*(bool *)vptr = noSound;
   205 	//vptr += sizeof(bool);
   206 
   207 	int ret;
   208 	g_pGiveAmmo->Execute(vstk, &ret);
   209 
   210 	return ret;
   211 }
   212 
   213 size_t DM_StringToBytes(const char *str, unsigned char buffer[], size_t maxlength)
   214 {
   215 	size_t real_bytes = 0;
   216 	size_t length = strlen(str);
   217 
   218 	for (size_t i=0; i<length; i++)
   219 	{
   220 		if (real_bytes >= maxlength)
   221 		{
   222 			break;
   223 		}
   224 		buffer[real_bytes++] = (unsigned char)str[i];
   225 		if (str[i] == '\\'
   226 			&& str[i+1] == 'x')
   227 		{
   228 			if (i + 3 >= length)
   229 			{
   230 				continue;
   231 			}
   232 			/* Get the hex part */
   233 			char s_byte[3];
   234 			int r_byte;
   235 			s_byte[0] = str[i+2];
   236 			s_byte[1] = str[i+3];
   237 			s_byte[2] = '\n';
   238 			/* Read it as an integer */
   239 			sscanf(s_byte, "%x", &r_byte);
   240 			/* Save the value */
   241 			buffer[real_bytes-1] = (unsigned char)r_byte;
   242 			/* Adjust index */
   243 			i += 3;
   244 		}
   245 	}
   246 
   247 	return real_bytes;
   248 }
   249 
   250 void DM_ApplyPatch(void *address, int offset, const dmpatch_t *patch, dmpatch_t *restore)
   251 {
   252 	unsigned char *addr = (unsigned char *)address + offset;
   253 
   254 	SourceHook::SetMemAccess(addr, 20, SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
   255 
   256 	if (restore)
   257 	{
   258 		for (size_t i=0; i<patch->bytes; i++)
   259 		{
   260 			restore->patch[i] = addr[i];
   261 		}
   262 		restore->bytes = patch->bytes;
   263 	}
   264 
   265 	for (size_t i=0; i<patch->bytes; i++)
   266 	{
   267 		addr[i] = patch->patch[i];
   268 	}
   269 }
   270 
   271 void DM_SetMemPatchable(void *address, size_t size)
   272 {
   273 	SourceHook::SetMemAccess(address, (int)size, SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
   274 }
   275 
   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); \
   279 		return false; \
   280 	} \
   281 	var = prop->GetOffset();
   282 
   283 bool InitializeUtils(char *error, size_t maxlength)
   284 {
   285 	void *addr;
   286 	int offset;
   287 	SourceMod::PassInfo pass[4];
   288 
   289 	/** ROUNDRESPAWN */
   290 	g_pDmConf->GetMemSig("RoundRespawn", &addr);
   291 	g_pRoundRespawn = bintools->CreateCall(addr, CallConv_ThisCall, NULL, NULL, 0);
   292 	g_CallWrappers.push_back(g_pRoundRespawn);
   293 
   294 	/** UTIL_REMOVE */
   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);
   301 	
   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);
   312 
   313 	/** CSWEAPONDROP */
   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);
   321 
   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);
   329 
   330 	/** GIVEAMMO */
   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);
   338 
   339 	/** PROPERTIES */
   340 	sm_sendprop_info_t prop;
   341 	if(!gamehelpers->FindSendPropInfo("CCSPlayer", "m_hRagdoll", &prop))
   342 	{
   343 		snprintf(error, maxlength, "Failed to get prop info for m_hRagdoll");
   344 		return false;
   345 	}
   346 	g_RagdollOffset = prop.actual_offset;
   347 	if(!gamehelpers->FindSendPropInfo("CCSPlayer", "m_lifeState", &prop))
   348 	{
   349 		snprintf(error, maxlength, "Failed to get prop info for m_lifeState");
   350 		return false;
   351 	}
   352 	g_LifeStateOffset = prop.actual_offset;
   353 	if(!gamehelpers->FindSendPropInfo("CCSPlayer", "m_bHasDefuser", &prop))
   354 	{
   355 		snprintf(error, maxlength, "Failed to get prop info for m_bHasDefuser");
   356 		return false;
   357 	}
   358 	g_DefuserOffset = prop.actual_offset;
   359 
   360 	return true;
   361 }
   362 
   363 void ShutdownUtils()
   364 {
   365 	List<ICallWrapper *>::iterator iter;
   366 	for (iter = g_CallWrappers.begin();
   367 		 iter != g_CallWrappers.end();
   368 		 iter++)
   369 	{
   370 		(*iter)->Destroy();
   371 	}
   372 	g_CallWrappers.clear();
   373 }