diff -r d6abe75f1896 -r 7ddf645a50d5 cssdm_detours.cpp --- a/cssdm_detours.cpp Thu Jun 12 10:18:02 2014 -0400 +++ b/cssdm_detours.cpp Wed Jul 02 13:53:40 2014 -0400 @@ -31,14 +31,27 @@ #include #include #include +#include "CDetour/detours.h" dmpatch_t drpwpns_restore; void *drpwpns_address = NULL; void *drpwpns_callback = NULL; -dmpatch_t csdrop_restore; -void *csdrop_address = NULL; -void *csdrop_callback = NULL; +CDetour *csdrop_callback = NULL; + +#if SOURCE_ENGINE == SE_CSGO +DETOUR_DECL_MEMBER3(DetourCSWeaponDrop, void, CBaseEntity *, weapon, Vector, vec, bool, unknown) +#else +DETOUR_DECL_MEMBER3(DetourCSWeaponDrop, void, CBaseEntity *, weapon, bool, bDropShield, bool, bThrowForward) +#endif +{ +#if SOURCE_ENGINE == SE_CSGO + DETOUR_MEMBER_CALL(DetourCSWeaponDrop)(weapon, vec, unknown); +#else + DETOUR_MEMBER_CALL(DetourCSWeaponDrop)(weapon, bDropShield, bThrowForward); +#endif + OnClientDroppedWeapon(reinterpret_cast(this), weapon); +} void DoGatePatch(unsigned char *target, void *callback, dmpatch_t *restore) { @@ -102,94 +115,17 @@ void InitCSDropDetour() { - int offset; - g_pDmConf->GetMemSig("CSWeaponDrop", &csdrop_address); - g_pDmConf->GetOffset("CSWeaponDropPatch", &offset); - csdrop_restore.bytes = offset; - - /* Patch the gate */ - DoGatePatch((unsigned char *)csdrop_address, &csdrop_callback, &csdrop_restore); - - /* Create the callback */ - csdrop_callback = spengine->ExecAlloc(100); - JitWriter wr; - JitWriter *jit = ≀ - wr.outbase = (jitcode_t)csdrop_callback; - wr.outptr = wr.outbase; - - /* This one is a POST detour instead of a pre-detour! - * This means we call first and ask questions later. - */ - //push ebp - //mov ebp, esp - IA32_Push_Reg(jit, REG_EBP); - IA32_Mov_Reg_Rm(jit, REG_EBP, REG_ESP, MOD_REG); - - /* Re-push everything, but setup our next call beforehand. */ -#if defined PLATFORM_POSIX - IA32_Push_Rm_Disp8(jit, REG_EBP, 12); - IA32_Push_Rm_Disp8(jit, REG_EBP, 8); - /* Linux pushes four things, it seems */ - IA32_Push_Rm_Disp8(jit, REG_EBP, 20); - IA32_Push_Rm_Disp8(jit, REG_EBP, 16); - IA32_Push_Rm_Disp8(jit, REG_EBP, 12); - IA32_Push_Rm_Disp8(jit, REG_EBP, 8); -#elif defined PLATFORM_WINDOWS - IA32_Push_Rm_Disp8(jit, REG_EBP, 8); - IA32_Push_Reg(jit, REG_ECX); - /* Windows only pushes three because of ECX */ - IA32_Push_Rm_Disp8(jit, REG_EBP, 16); - IA32_Push_Rm_Disp8(jit, REG_EBP, 12); - IA32_Push_Rm_Disp8(jit, REG_EBP, 8); -#endif - - /* Make the ORIGINAL call */ - jitoffs_t orig_call = IA32_Call_Imm32(jit, 0); - -#if defined PLATFORM_POSIX - IA32_Add_Rm_Imm8(jit, REG_ESP, 16, MOD_REG); -#endif - - /* Make the POST call */ - //call - //add esp, 8 - jitoffs_t call = IA32_Call_Imm32(jit, 0); - IA32_Write_Jump32_Abs(jit, call, (void *)OnClientDroppedWeapon); - - /* Do cleanup */ -#if defined PLATFORM_POSIX - IA32_Add_Rm_Imm8(jit, REG_ESP, 8, MOD_REG); -#elif defined PLATFORM_WINDOWS - //this probably isn't needed at all! - IA32_Pop_Reg(jit, REG_ECX); - IA32_Add_Rm_Imm8(jit, REG_ESP, 4, MOD_REG); -#endif - - /* Return */ - //pop ebp - //ret / retn 12 - IA32_Pop_Reg(jit, REG_EBP); -#if defined PLATFORM_POSIX - IA32_Return(jit); -#elif defined PLATFORM_WINDOWS - IA32_Return_Popstack(jit, 0xC); -#endif - - /* Jump back to the original */ - IA32_Send_Jump32_Here(jit, orig_call); - - /* Copy original bytes */ - for (size_t i=0; iwrite_ubyte(csdrop_restore.patch[i]); + csdrop_callback->EnableDetour(); } - call = IA32_Jump_Imm32(jit, 0); - IA32_Write_Jump32_Abs(jit, call, (unsigned char *)csdrop_address + csdrop_restore.bytes); } void DM_InitDetours() { + CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pDmConf); InitDropWeaponsDetour(); InitCSDropDetour(); } @@ -206,10 +142,7 @@ } if (csdrop_callback) { - /* Remove the patch */ - DM_ApplyPatch(csdrop_address, 0, &csdrop_restore, NULL); - /* Free the gate */ - spengine->ExecFree(csdrop_callback); + csdrop_callback->Destroy(); csdrop_callback = NULL; } }