cssdm_detours.cpp
changeset 0 67dcd379a2d5
child 7 1107793f4b1c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/cssdm_detours.cpp	Sun Jun 24 21:58:09 2007 +0000
     1.3 @@ -0,0 +1,215 @@
     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_detours.h"
    1.32 +#include "cssdm_utils.h"
    1.33 +#include "cssdm_events.h"
    1.34 +#include <sm_platform.h>
    1.35 +#include <jit/jit_helpers.h>
    1.36 +#include <jit/x86/x86_macros.h>
    1.37 +
    1.38 +dmpatch_t drpwpns_restore = {0, ""};
    1.39 +void *drpwpns_address = NULL;
    1.40 +void *drpwpns_callback = NULL;
    1.41 +
    1.42 +dmpatch_t csdrop_restore = {0, ""};
    1.43 +void *csdrop_address = NULL;
    1.44 +void *csdrop_callback = NULL;
    1.45 +
    1.46 +void DoGatePatch(unsigned char *target, void *callback, dmpatch_t *restore)
    1.47 +{
    1.48 +	DM_SetMemPatchable(target, 20);
    1.49 +
    1.50 +	/* First, save restore bits */
    1.51 +	for (size_t i=0; i<restore->bytes; i++)
    1.52 +	{
    1.53 +		restore->patch[i] = target[i];
    1.54 +	}
    1.55 +
    1.56 +	target[0] = 0xFF;	/* JMP */
    1.57 +	target[1] = 0x25;	/* MEM32 */
    1.58 +	*(void **)(&target[2]) = callback;
    1.59 +}
    1.60 +
    1.61 +void InitDropWeaponsDetour()
    1.62 +{
    1.63 +	int offset;
    1.64 +	g_pDmConf->GetMemSig("DropWeapons", &drpwpns_address);
    1.65 +	g_pDmConf->GetOffset("DropWeaponsPatch", &offset);
    1.66 +	drpwpns_restore.bytes = offset;
    1.67 +
    1.68 +	/* Patch the gate */
    1.69 +	DoGatePatch((unsigned char *)drpwpns_address, &drpwpns_callback, &drpwpns_restore);
    1.70 +
    1.71 +	/* Create the callback */
    1.72 +	drpwpns_callback = spengine->ExecAlloc(100);
    1.73 +	JitWriter wr;
    1.74 +	JitWriter *jit = &wr;
    1.75 +	wr.outbase = (jitcode_t)drpwpns_callback;
    1.76 +	wr.outptr = wr.outbase;
    1.77 +
    1.78 +	/* Luckily for this we have no parameters!
    1.79 +	 * The prototype we are pushing is:
    1.80 +	 *
    1.81 +	 * void OnClientDropWeapons(CBaseEntity *pEntity);
    1.82 +	 */
    1.83 +#if defined PLATFORM_LINUX
    1.84 +	IA32_Push_Rm_Disp8_ESP(jit, 4);					//push [esp+4]
    1.85 +#elif defined PLATFORM_WINDOWS
    1.86 +	IA32_Push_Reg(jit, REG_ECX);					//push ecx
    1.87 +#endif
    1.88 +	//call <function>
    1.89 +	jitoffs_t call = IA32_Call_Imm32(jit, 0);		//call <function>
    1.90 +	IA32_Write_Jump32_Abs(jit, call, (void *)OnClientDropWeapons);
    1.91 +#if defined PLATFORM_LINUX
    1.92 +	IA32_Add_Rm_Imm8(jit, REG_ESP, 4, MOD_REG);		//add esp, 4
    1.93 +#elif defined PLATFORM_WINDOWS
    1.94 +	IA32_Pop_Reg(jit, REG_ECX);
    1.95 +#endif
    1.96 +	/* Patch old bytes in */
    1.97 +	for (size_t i=0; i<drpwpns_restore.bytes; i++)
    1.98 +	{
    1.99 +		jit->write_ubyte(drpwpns_restore.patch[i]);
   1.100 +	}
   1.101 +	/* Finally, do a jump back to the original */
   1.102 +	call = IA32_Jump_Imm32(jit, 0);
   1.103 +	IA32_Write_Jump32_Abs(jit, call, (unsigned char *)drpwpns_address + drpwpns_restore.bytes);
   1.104 +}
   1.105 +
   1.106 +void InitCSDropDetour()
   1.107 +{
   1.108 +	int offset;
   1.109 +	g_pDmConf->GetMemSig("CSWeaponDrop", &csdrop_address);
   1.110 +	g_pDmConf->GetOffset("CSWeaponDropPatch", &offset);
   1.111 +	csdrop_restore.bytes = offset;
   1.112 +
   1.113 +	/* Patch the gate */
   1.114 +	DoGatePatch((unsigned char *)csdrop_address, &csdrop_callback, &csdrop_restore);
   1.115 +
   1.116 +	/* Create the callback */
   1.117 +	csdrop_callback = spengine->ExecAlloc(100);
   1.118 +	JitWriter wr;
   1.119 +	JitWriter *jit = &wr;
   1.120 +	wr.outbase = (jitcode_t)csdrop_callback;
   1.121 +	wr.outptr = wr.outbase;
   1.122 +
   1.123 +	/* This one is a POST detour instead of a pre-detour!
   1.124 +	 * This means we call first and ask questions later.
   1.125 +	 */
   1.126 +	//push ebp
   1.127 +	//mov ebp, esp
   1.128 +	IA32_Push_Reg(jit, REG_EBP);
   1.129 +	IA32_Mov_Reg_Rm(jit, REG_EBP, REG_ESP, MOD_REG);
   1.130 +
   1.131 +	/* Re-push everything, but setup our next call beforehand. */
   1.132 +#if defined PLATFORM_LINUX
   1.133 +	IA32_Push_Rm_Disp8(jit, REG_EBP, 12);
   1.134 +	IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
   1.135 +	/* Linux pushes four things, it seems */
   1.136 +	IA32_Push_Rm_Disp8(jit, REG_EBP, 20);
   1.137 +	IA32_Push_Rm_Disp8(jit, REG_EBP, 16);
   1.138 +	IA32_Push_Rm_Disp8(jit, REG_EBP, 12);
   1.139 +	IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
   1.140 +#elif defined PLATFORM_WINDOWS
   1.141 +	IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
   1.142 +	IA32_Push_Reg(jit, REG_ECX);
   1.143 +	/* Windows only pushes three because of ECX */
   1.144 +	IA32_Push_Rm_Disp8(jit, REG_EBP, 16);
   1.145 +	IA32_Push_Rm_Disp8(jit, REG_EBP, 12);
   1.146 +	IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
   1.147 +#endif
   1.148 +
   1.149 +	/* Make the ORIGINAL call */
   1.150 +	jitoffs_t orig_call = IA32_Call_Imm32(jit, 0);
   1.151 +
   1.152 +#if defined PLATFORM_LINUX
   1.153 +	IA32_Add_Rm_Imm8(jit, REG_ESP, 20, MOD_REG);
   1.154 +#endif
   1.155 +
   1.156 +	/* Make the POST call */
   1.157 +	//call <function>
   1.158 +	//add esp, 8
   1.159 +	jitoffs_t call = IA32_Call_Imm32(jit, 0);
   1.160 +	IA32_Write_Jump32_Abs(jit, call, (void *)OnClientDroppedWeapon);
   1.161 +
   1.162 +	/* Do cleanup */
   1.163 +#if defined PLATFORM_LINUX
   1.164 +	IA32_Add_Rm_Imm8(jit, REG_ESP, 8, MOD_REG);
   1.165 +#elif defined PLATFORM_WINDOWS
   1.166 +	//this probably isn't needed at all!
   1.167 +	IA32_Pop_Reg(jit, REG_ECX);
   1.168 +	IA32_Add_Rm_Imm8(jit, REG_ESP, 4, MOD_REG);
   1.169 +#endif
   1.170 +
   1.171 +	/* Return */
   1.172 +	//pop ebp
   1.173 +	//ret / retn 12
   1.174 +	IA32_Pop_Reg(jit, REG_EBP);
   1.175 +#if defined PLATFORM_LINUX
   1.176 +	IA32_Return(jit);
   1.177 +#elif defined PLATFORM_WINDOWS
   1.178 +	IA32_Return_Popstack(jit, 0xC);
   1.179 +#endif
   1.180 +
   1.181 +	/* Jump back to the original */
   1.182 +	IA32_Send_Jump32_Here(jit, orig_call);
   1.183 +
   1.184 +	/* Copy original bytes */
   1.185 +	for (size_t i=0; i<csdrop_restore.bytes; i++)
   1.186 +	{
   1.187 +		jit->write_ubyte(csdrop_restore.patch[i]);
   1.188 +	}
   1.189 +
   1.190 +	call = IA32_Jump_Imm32(jit, 0);
   1.191 +	IA32_Write_Jump32_Abs(jit, call, (unsigned char *)csdrop_address + csdrop_restore.bytes);
   1.192 +}
   1.193 +
   1.194 +void DM_InitDetours()
   1.195 +{
   1.196 +	InitDropWeaponsDetour();
   1.197 +	InitCSDropDetour();
   1.198 +}
   1.199 +
   1.200 +void DM_ShutdownDetours()
   1.201 +{
   1.202 +	if (drpwpns_callback)
   1.203 +	{
   1.204 +		/* Remove the patch */
   1.205 +		DM_ApplyPatch(drpwpns_address, 0, &drpwpns_restore, NULL);
   1.206 +		/* Free the gate */
   1.207 +		spengine->ExecFree(drpwpns_callback);
   1.208 +		drpwpns_callback = NULL;
   1.209 +	}
   1.210 +	if (csdrop_callback)
   1.211 +	{
   1.212 +		/* Remove the patch */
   1.213 +		DM_ApplyPatch(csdrop_address, 0, &csdrop_restore, NULL);
   1.214 +		/* Free the gate */
   1.215 +		spengine->ExecFree(csdrop_callback);
   1.216 +		csdrop_callback = NULL;
   1.217 +	}
   1.218 +}