cssdm_detours.cpp
changeset 184 7ddf645a50d5
parent 53 3c0401873aa6
     1.1 --- a/cssdm_detours.cpp	Thu Jun 12 10:18:02 2014 -0400
     1.2 +++ b/cssdm_detours.cpp	Wed Jul 02 13:53:40 2014 -0400
     1.3 @@ -31,14 +31,27 @@
     1.4  #include <sm_platform.h>
     1.5  #include <jit/jit_helpers.h>
     1.6  #include <jit/x86/x86_macros.h>
     1.7 +#include "CDetour/detours.h"
     1.8  
     1.9  dmpatch_t drpwpns_restore;
    1.10  void *drpwpns_address = NULL;
    1.11  void *drpwpns_callback = NULL;
    1.12  
    1.13 -dmpatch_t csdrop_restore;
    1.14 -void *csdrop_address = NULL;
    1.15 -void *csdrop_callback = NULL;
    1.16 +CDetour *csdrop_callback = NULL;
    1.17 +
    1.18 +#if SOURCE_ENGINE == SE_CSGO
    1.19 +DETOUR_DECL_MEMBER3(DetourCSWeaponDrop, void, CBaseEntity *, weapon, Vector, vec, bool, unknown)
    1.20 +#else
    1.21 +DETOUR_DECL_MEMBER3(DetourCSWeaponDrop, void, CBaseEntity *, weapon, bool, bDropShield, bool, bThrowForward)
    1.22 +#endif
    1.23 +{
    1.24 +#if SOURCE_ENGINE == SE_CSGO
    1.25 +		DETOUR_MEMBER_CALL(DetourCSWeaponDrop)(weapon, vec, unknown);
    1.26 +#else
    1.27 +		DETOUR_MEMBER_CALL(DetourCSWeaponDrop)(weapon, bDropShield, bThrowForward);
    1.28 +#endif
    1.29 +		OnClientDroppedWeapon(reinterpret_cast<CBaseEntity *>(this), weapon);
    1.30 +}
    1.31  
    1.32  void DoGatePatch(unsigned char *target, void *callback, dmpatch_t *restore)
    1.33  {
    1.34 @@ -102,94 +115,17 @@
    1.35  
    1.36  void InitCSDropDetour()
    1.37  {
    1.38 -	int offset;
    1.39 -	g_pDmConf->GetMemSig("CSWeaponDrop", &csdrop_address);
    1.40 -	g_pDmConf->GetOffset("CSWeaponDropPatch", &offset);
    1.41 -	csdrop_restore.bytes = offset;
    1.42 -
    1.43 -	/* Patch the gate */
    1.44 -	DoGatePatch((unsigned char *)csdrop_address, &csdrop_callback, &csdrop_restore);
    1.45 -
    1.46 -	/* Create the callback */
    1.47 -	csdrop_callback = spengine->ExecAlloc(100);
    1.48 -	JitWriter wr;
    1.49 -	JitWriter *jit = &wr;
    1.50 -	wr.outbase = (jitcode_t)csdrop_callback;
    1.51 -	wr.outptr = wr.outbase;
    1.52 -
    1.53 -	/* This one is a POST detour instead of a pre-detour!
    1.54 -	 * This means we call first and ask questions later.
    1.55 -	 */
    1.56 -	//push ebp
    1.57 -	//mov ebp, esp
    1.58 -	IA32_Push_Reg(jit, REG_EBP);
    1.59 -	IA32_Mov_Reg_Rm(jit, REG_EBP, REG_ESP, MOD_REG);
    1.60 -
    1.61 -	/* Re-push everything, but setup our next call beforehand. */
    1.62 -#if defined PLATFORM_POSIX
    1.63 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 12);
    1.64 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
    1.65 -	/* Linux pushes four things, it seems */
    1.66 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 20);
    1.67 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 16);
    1.68 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 12);
    1.69 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
    1.70 -#elif defined PLATFORM_WINDOWS
    1.71 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
    1.72 -	IA32_Push_Reg(jit, REG_ECX);
    1.73 -	/* Windows only pushes three because of ECX */
    1.74 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 16);
    1.75 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 12);
    1.76 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
    1.77 -#endif
    1.78 -
    1.79 -	/* Make the ORIGINAL call */
    1.80 -	jitoffs_t orig_call = IA32_Call_Imm32(jit, 0);
    1.81 -
    1.82 -#if defined PLATFORM_POSIX
    1.83 -	IA32_Add_Rm_Imm8(jit, REG_ESP, 16, MOD_REG);
    1.84 -#endif
    1.85 -
    1.86 -	/* Make the POST call */
    1.87 -	//call <function>
    1.88 -	//add esp, 8
    1.89 -	jitoffs_t call = IA32_Call_Imm32(jit, 0);
    1.90 -	IA32_Write_Jump32_Abs(jit, call, (void *)OnClientDroppedWeapon);
    1.91 -
    1.92 -	/* Do cleanup */
    1.93 -#if defined PLATFORM_POSIX
    1.94 -	IA32_Add_Rm_Imm8(jit, REG_ESP, 8, MOD_REG);
    1.95 -#elif defined PLATFORM_WINDOWS
    1.96 -	//this probably isn't needed at all!
    1.97 -	IA32_Pop_Reg(jit, REG_ECX);
    1.98 -	IA32_Add_Rm_Imm8(jit, REG_ESP, 4, MOD_REG);
    1.99 -#endif
   1.100 -
   1.101 -	/* Return */
   1.102 -	//pop ebp
   1.103 -	//ret / retn 12
   1.104 -	IA32_Pop_Reg(jit, REG_EBP);
   1.105 -#if defined PLATFORM_POSIX
   1.106 -	IA32_Return(jit);
   1.107 -#elif defined PLATFORM_WINDOWS
   1.108 -	IA32_Return_Popstack(jit, 0xC);
   1.109 -#endif
   1.110 -
   1.111 -	/* Jump back to the original */
   1.112 -	IA32_Send_Jump32_Here(jit, orig_call);
   1.113 -
   1.114 -	/* Copy original bytes */
   1.115 -	for (size_t i=0; i<csdrop_restore.bytes; i++)
   1.116 +	csdrop_callback = DETOUR_CREATE_MEMBER(DetourCSWeaponDrop, "CSWeaponDrop");
   1.117 +	if(csdrop_callback)
   1.118  	{
   1.119 -		jit->write_ubyte(csdrop_restore.patch[i]);
   1.120 +		csdrop_callback->EnableDetour();
   1.121  	}
   1.122  
   1.123 -	call = IA32_Jump_Imm32(jit, 0);
   1.124 -	IA32_Write_Jump32_Abs(jit, call, (unsigned char *)csdrop_address + csdrop_restore.bytes);
   1.125  }
   1.126  
   1.127  void DM_InitDetours()
   1.128  {
   1.129 +	CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pDmConf);
   1.130  	InitDropWeaponsDetour();
   1.131  	InitCSDropDetour();
   1.132  }
   1.133 @@ -206,10 +142,7 @@
   1.134  	}
   1.135  	if (csdrop_callback)
   1.136  	{
   1.137 -		/* Remove the patch */
   1.138 -		DM_ApplyPatch(csdrop_address, 0, &csdrop_restore, NULL);
   1.139 -		/* Free the gate */
   1.140 -		spengine->ExecFree(csdrop_callback);
   1.141 +		csdrop_callback->Destroy();
   1.142  		csdrop_callback = NULL;
   1.143  	}
   1.144  }