cssdm_detours.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Wed Aug 14 17:18:49 2013 -0400 (2013-08-14)
changeset 149 a0257cfc286d
parent 53 3c0401873aa6
child 184 7ddf645a50d5
permissions -rw-r--r--
Fix TakeDmgPatch1 for linux on CS:GO
     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_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>
    34 
    35 dmpatch_t drpwpns_restore;
    36 void *drpwpns_address = NULL;
    37 void *drpwpns_callback = NULL;
    38 
    39 dmpatch_t csdrop_restore;
    40 void *csdrop_address = NULL;
    41 void *csdrop_callback = NULL;
    42 
    43 void DoGatePatch(unsigned char *target, void *callback, dmpatch_t *restore)
    44 {
    45 	DM_SetMemPatchable(target, 20);
    46 
    47 	/* First, save restore bits */
    48 	for (size_t i=0; i<restore->bytes; i++)
    49 	{
    50 		restore->patch[i] = target[i];
    51 	}
    52 
    53 	target[0] = 0xFF;	/* JMP */
    54 	target[1] = 0x25;	/* MEM32 */
    55 	*(void **)(&target[2]) = callback;
    56 }
    57 
    58 void InitDropWeaponsDetour()
    59 {
    60 	int offset;
    61 	g_pDmConf->GetMemSig("DropWeapons", &drpwpns_address);
    62 	g_pDmConf->GetOffset("DropWeaponsPatch", &offset);
    63 	drpwpns_restore.bytes = offset;
    64 
    65 	/* Patch the gate */
    66 	DoGatePatch((unsigned char *)drpwpns_address, &drpwpns_callback, &drpwpns_restore);
    67 
    68 	/* Create the callback */
    69 	drpwpns_callback = spengine->ExecAlloc(100);
    70 	JitWriter wr;
    71 	JitWriter *jit = &wr;
    72 	wr.outbase = (jitcode_t)drpwpns_callback;
    73 	wr.outptr = wr.outbase;
    74 
    75 	/* Luckily for this we have no parameters!
    76 	 * The prototype we are pushing is:
    77 	 *
    78 	 * void OnClientDropWeapons(CBaseEntity *pEntity);
    79 	 */
    80 #if defined PLATFORM_POSIX
    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
    84 #endif
    85 	//call <function>
    86 	jitoffs_t call = IA32_Call_Imm32(jit, 0);		//call <function>
    87 	IA32_Write_Jump32_Abs(jit, call, (void *)OnClientDropWeapons);
    88 #if defined PLATFORM_POSIX
    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);
    92 #endif
    93 	/* Patch old bytes in */
    94 	for (size_t i=0; i<drpwpns_restore.bytes; i++)
    95 	{
    96 		jit->write_ubyte(drpwpns_restore.patch[i]);
    97 	}
    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);
   101 }
   102 
   103 void InitCSDropDetour()
   104 {
   105 	int offset;
   106 	g_pDmConf->GetMemSig("CSWeaponDrop", &csdrop_address);
   107 	g_pDmConf->GetOffset("CSWeaponDropPatch", &offset);
   108 	csdrop_restore.bytes = offset;
   109 
   110 	/* Patch the gate */
   111 	DoGatePatch((unsigned char *)csdrop_address, &csdrop_callback, &csdrop_restore);
   112 
   113 	/* Create the callback */
   114 	csdrop_callback = spengine->ExecAlloc(100);
   115 	JitWriter wr;
   116 	JitWriter *jit = &wr;
   117 	wr.outbase = (jitcode_t)csdrop_callback;
   118 	wr.outptr = wr.outbase;
   119 
   120 	/* This one is a POST detour instead of a pre-detour!
   121 	 * This means we call first and ask questions later.
   122 	 */
   123 	//push ebp
   124 	//mov ebp, esp
   125 	IA32_Push_Reg(jit, REG_EBP);
   126 	IA32_Mov_Reg_Rm(jit, REG_EBP, REG_ESP, MOD_REG);
   127 
   128 	/* Re-push everything, but setup our next call beforehand. */
   129 #if defined PLATFORM_POSIX
   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);
   144 #endif
   145 
   146 	/* Make the ORIGINAL call */
   147 	jitoffs_t orig_call = IA32_Call_Imm32(jit, 0);
   148 
   149 #if defined PLATFORM_POSIX
   150 	IA32_Add_Rm_Imm8(jit, REG_ESP, 16, MOD_REG);
   151 #endif
   152 
   153 	/* Make the POST call */
   154 	//call <function>
   155 	//add esp, 8
   156 	jitoffs_t call = IA32_Call_Imm32(jit, 0);
   157 	IA32_Write_Jump32_Abs(jit, call, (void *)OnClientDroppedWeapon);
   158 
   159 	/* Do cleanup */
   160 #if defined PLATFORM_POSIX
   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);
   166 #endif
   167 
   168 	/* Return */
   169 	//pop ebp
   170 	//ret / retn 12
   171 	IA32_Pop_Reg(jit, REG_EBP);
   172 #if defined PLATFORM_POSIX
   173 	IA32_Return(jit);
   174 #elif defined PLATFORM_WINDOWS
   175 	IA32_Return_Popstack(jit, 0xC);
   176 #endif
   177 
   178 	/* Jump back to the original */
   179 	IA32_Send_Jump32_Here(jit, orig_call);
   180 
   181 	/* Copy original bytes */
   182 	for (size_t i=0; i<csdrop_restore.bytes; i++)
   183 	{
   184 		jit->write_ubyte(csdrop_restore.patch[i]);
   185 	}
   186 
   187 	call = IA32_Jump_Imm32(jit, 0);
   188 	IA32_Write_Jump32_Abs(jit, call, (unsigned char *)csdrop_address + csdrop_restore.bytes);
   189 }
   190 
   191 void DM_InitDetours()
   192 {
   193 	InitDropWeaponsDetour();
   194 	InitCSDropDetour();
   195 }
   196 
   197 void DM_ShutdownDetours()
   198 {
   199 	if (drpwpns_callback)
   200 	{
   201 		/* Remove the patch */
   202 		DM_ApplyPatch(drpwpns_address, 0, &drpwpns_restore, NULL);
   203 		/* Free the gate */
   204 		spengine->ExecFree(drpwpns_callback);
   205 		drpwpns_callback = NULL;
   206 	}
   207 	if (csdrop_callback)
   208 	{
   209 		/* Remove the patch */
   210 		DM_ApplyPatch(csdrop_address, 0, &csdrop_restore, NULL);
   211 		/* Free the gate */
   212 		spengine->ExecFree(csdrop_callback);
   213 		csdrop_callback = NULL;
   214 	}
   215 }