3 * ===============================================================
4 * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC.
5 * By David "BAILOPAN" Anderson
7 * ===============================================================
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.
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.
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,
27 #include "cssdm_headers.h"
28 #include "cssdm_detours.h"
29 #include "cssdm_utils.h"
30 #include "cssdm_events.h"
31 #include <sm_platform.h>
32 #include <jit/jit_helpers.h>
33 #include <jit/x86/x86_macros.h>
35 dmpatch_t drpwpns_restore = {0, ""};
36 void *drpwpns_address = NULL;
37 void *drpwpns_callback = NULL;
39 dmpatch_t csdrop_restore = {0, ""};
40 void *csdrop_address = NULL;
41 void *csdrop_callback = NULL;
43 void DoGatePatch(unsigned char *target, void *callback, dmpatch_t *restore)
45 DM_SetMemPatchable(target, 20);
47 /* First, save restore bits */
48 for (size_t i=0; i<restore->bytes; i++)
50 restore->patch[i] = target[i];
53 target[0] = 0xFF; /* JMP */
54 target[1] = 0x25; /* MEM32 */
55 *(void **)(&target[2]) = callback;
58 void InitDropWeaponsDetour()
61 g_pDmConf->GetMemSig("DropWeapons", &drpwpns_address);
62 g_pDmConf->GetOffset("DropWeaponsPatch", &offset);
63 drpwpns_restore.bytes = offset;
66 DoGatePatch((unsigned char *)drpwpns_address, &drpwpns_callback, &drpwpns_restore);
68 /* Create the callback */
69 drpwpns_callback = spengine->ExecAlloc(100);
72 wr.outbase = (jitcode_t)drpwpns_callback;
73 wr.outptr = wr.outbase;
75 /* Luckily for this we have no parameters!
76 * The prototype we are pushing is:
78 * void OnClientDropWeapons(CBaseEntity *pEntity);
80 #if defined PLATFORM_LINUX
81 IA32_Push_Rm_Disp8_ESP(jit, 4); //push [esp+4]
82 #elif defined PLATFORM_WINDOWS
83 IA32_Push_Reg(jit, REG_ECX); //push ecx
86 jitoffs_t call = IA32_Call_Imm32(jit, 0); //call <function>
87 IA32_Write_Jump32_Abs(jit, call, (void *)OnClientDropWeapons);
88 #if defined PLATFORM_LINUX
89 IA32_Add_Rm_Imm8(jit, REG_ESP, 4, MOD_REG); //add esp, 4
90 #elif defined PLATFORM_WINDOWS
91 IA32_Pop_Reg(jit, REG_ECX);
93 /* Patch old bytes in */
94 for (size_t i=0; i<drpwpns_restore.bytes; i++)
96 jit->write_ubyte(drpwpns_restore.patch[i]);
98 /* Finally, do a jump back to the original */
99 call = IA32_Jump_Imm32(jit, 0);
100 IA32_Write_Jump32_Abs(jit, call, (unsigned char *)drpwpns_address + drpwpns_restore.bytes);
103 void InitCSDropDetour()
106 g_pDmConf->GetMemSig("CSWeaponDrop", &csdrop_address);
107 g_pDmConf->GetOffset("CSWeaponDropPatch", &offset);
108 csdrop_restore.bytes = offset;
111 DoGatePatch((unsigned char *)csdrop_address, &csdrop_callback, &csdrop_restore);
113 /* Create the callback */
114 csdrop_callback = spengine->ExecAlloc(100);
116 JitWriter *jit = ≀
117 wr.outbase = (jitcode_t)csdrop_callback;
118 wr.outptr = wr.outbase;
120 /* This one is a POST detour instead of a pre-detour!
121 * This means we call first and ask questions later.
125 IA32_Push_Reg(jit, REG_EBP);
126 IA32_Mov_Reg_Rm(jit, REG_EBP, REG_ESP, MOD_REG);
128 /* Re-push everything, but setup our next call beforehand. */
129 #if defined PLATFORM_LINUX
130 IA32_Push_Rm_Disp8(jit, REG_EBP, 12);
131 IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
132 /* Linux pushes four things, it seems */
133 IA32_Push_Rm_Disp8(jit, REG_EBP, 20);
134 IA32_Push_Rm_Disp8(jit, REG_EBP, 16);
135 IA32_Push_Rm_Disp8(jit, REG_EBP, 12);
136 IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
137 #elif defined PLATFORM_WINDOWS
138 IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
139 IA32_Push_Reg(jit, REG_ECX);
140 /* Windows only pushes three because of ECX */
141 IA32_Push_Rm_Disp8(jit, REG_EBP, 16);
142 IA32_Push_Rm_Disp8(jit, REG_EBP, 12);
143 IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
146 /* Make the ORIGINAL call */
147 jitoffs_t orig_call = IA32_Call_Imm32(jit, 0);
149 #if defined PLATFORM_LINUX
150 IA32_Add_Rm_Imm8(jit, REG_ESP, 20, MOD_REG);
153 /* Make the POST call */
156 jitoffs_t call = IA32_Call_Imm32(jit, 0);
157 IA32_Write_Jump32_Abs(jit, call, (void *)OnClientDroppedWeapon);
160 #if defined PLATFORM_LINUX
161 IA32_Add_Rm_Imm8(jit, REG_ESP, 8, MOD_REG);
162 #elif defined PLATFORM_WINDOWS
163 //this probably isn't needed at all!
164 IA32_Pop_Reg(jit, REG_ECX);
165 IA32_Add_Rm_Imm8(jit, REG_ESP, 4, MOD_REG);
171 IA32_Pop_Reg(jit, REG_EBP);
172 #if defined PLATFORM_LINUX
174 #elif defined PLATFORM_WINDOWS
175 IA32_Return_Popstack(jit, 0xC);
178 /* Jump back to the original */
179 IA32_Send_Jump32_Here(jit, orig_call);
181 /* Copy original bytes */
182 for (size_t i=0; i<csdrop_restore.bytes; i++)
184 jit->write_ubyte(csdrop_restore.patch[i]);
187 call = IA32_Jump_Imm32(jit, 0);
188 IA32_Write_Jump32_Abs(jit, call, (unsigned char *)csdrop_address + csdrop_restore.bytes);
191 void DM_InitDetours()
193 InitDropWeaponsDetour();
197 void DM_ShutdownDetours()
199 if (drpwpns_callback)
201 /* Remove the patch */
202 DM_ApplyPatch(drpwpns_address, 0, &drpwpns_restore, NULL);
204 spengine->ExecFree(drpwpns_callback);
205 drpwpns_callback = NULL;
209 /* Remove the patch */
210 DM_ApplyPatch(csdrop_address, 0, &csdrop_restore, NULL);
212 spengine->ExecFree(csdrop_callback);
213 csdrop_callback = NULL;