cssdm_detours.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Wed Oct 01 11:50:24 2014 -0400 (2014-10-01)
changeset 193 277c856152b0
parent 53 3c0401873aa6
permissions -rw-r--r--
Update linix CS:GO gamedata
[email protected]
     1
/**
[email protected]
     2
 * vim: set ts=4 :
[email protected]
     3
 * ===============================================================
[email protected]
     4
 * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC. 
[email protected]
     5
 * By David "BAILOPAN" Anderson
[email protected]
     6
 * All rights reserved.
[email protected]
     7
 * ===============================================================
[email protected]
     8
 *
[email protected]
     9
 * This program is free software; you can redistribute it and/or modify
[email protected]
    10
 * it under the terms of the GNU General Public License as published by
[email protected]
    11
 * the Free Software Foundation; either version 2 of the License, or (at
[email protected]
    12
 * your option) any later version.
[email protected]
    13
 * 
[email protected]
    14
 * This program is distributed in the hope that it will be useful, but
[email protected]
    15
 * WITHOUT ANY WARRANTY; without even the implied warranty of
[email protected]
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
[email protected]
    17
 * General Public License for more details.
[email protected]
    18
 * 
[email protected]
    19
 * You should have received a copy of the GNU General Public License
[email protected]
    20
 * along with this program; see the file COPYING; if not, write to the
[email protected]
    21
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
[email protected]
    22
 * MA 02110-1301 USA
[email protected]
    23
 * 
[email protected]
    24
 * Version: $Id$
[email protected]
    25
 */
[email protected]
    26
[email protected]
    27
#include "cssdm_headers.h"
[email protected]
    28
#include "cssdm_detours.h"
[email protected]
    29
#include "cssdm_utils.h"
[email protected]
    30
#include "cssdm_events.h"
[email protected]
    31
#include <sm_platform.h>
[email protected]
    32
#include <jit/jit_helpers.h>
[email protected]
    33
#include <jit/x86/x86_macros.h>
[email protected]
    34
#include "CDetour/detours.h"
[email protected]
    35
[email protected]
    36
dmpatch_t drpwpns_restore;
[email protected]
    37
void *drpwpns_address = NULL;
[email protected]
    38
void *drpwpns_callback = NULL;
[email protected]
    39
[email protected]
    40
CDetour *csdrop_callback = NULL;
[email protected]
    41
[email protected]
    42
#if SOURCE_ENGINE == SE_CSGO
[email protected]
    43
DETOUR_DECL_MEMBER3(DetourCSWeaponDrop, void, CBaseEntity *, weapon, Vector, vec, bool, unknown)
[email protected]
    44
#else
[email protected]
    45
DETOUR_DECL_MEMBER3(DetourCSWeaponDrop, void, CBaseEntity *, weapon, bool, bDropShield, bool, bThrowForward)
[email protected]
    46
#endif
[email protected]
    47
{
[email protected]
    48
#if SOURCE_ENGINE == SE_CSGO
[email protected]
    49
		DETOUR_MEMBER_CALL(DetourCSWeaponDrop)(weapon, vec, unknown);
[email protected]
    50
#else
[email protected]
    51
		DETOUR_MEMBER_CALL(DetourCSWeaponDrop)(weapon, bDropShield, bThrowForward);
[email protected]
    52
#endif
[email protected]
    53
		OnClientDroppedWeapon(reinterpret_cast<CBaseEntity *>(this), weapon);
[email protected]
    54
}
[email protected]
    55
[email protected]
    56
void DoGatePatch(unsigned char *target, void *callback, dmpatch_t *restore)
[email protected]
    57
{
[email protected]
    58
	DM_SetMemPatchable(target, 20);
[email protected]
    59
[email protected]
    60
	/* First, save restore bits */
[email protected]
    61
	for (size_t i=0; i<restore->bytes; i++)
[email protected]
    62
	{
[email protected]
    63
		restore->patch[i] = target[i];
[email protected]
    64
	}
[email protected]
    65
[email protected]
    66
	target[0] = 0xFF;	/* JMP */
[email protected]
    67
	target[1] = 0x25;	/* MEM32 */
[email protected]
    68
	*(void **)(&target[2]) = callback;
[email protected]
    69
}
[email protected]
    70
[email protected]
    71
void InitDropWeaponsDetour()
[email protected]
    72
{
[email protected]
    73
	int offset;
[email protected]
    74
	g_pDmConf->GetMemSig("DropWeapons", &drpwpns_address);
[email protected]
    75
	g_pDmConf->GetOffset("DropWeaponsPatch", &offset);
[email protected]
    76
	drpwpns_restore.bytes = offset;
[email protected]
    77
[email protected]
    78
	/* Patch the gate */
[email protected]
    79
	DoGatePatch((unsigned char *)drpwpns_address, &drpwpns_callback, &drpwpns_restore);
[email protected]
    80
[email protected]
    81
	/* Create the callback */
[email protected]
    82
	drpwpns_callback = spengine->ExecAlloc(100);
[email protected]
    83
	JitWriter wr;
[email protected]
    84
	JitWriter *jit = &wr;
[email protected]
    85
	wr.outbase = (jitcode_t)drpwpns_callback;
[email protected]
    86
	wr.outptr = wr.outbase;
[email protected]
    87
[email protected]
    88
	/* Luckily for this we have no parameters!
[email protected]
    89
	 * The prototype we are pushing is:
[email protected]
    90
	 *
[email protected]
    91
	 * void OnClientDropWeapons(CBaseEntity *pEntity);
[email protected]
    92
	 */
[email protected]
    93
#if defined PLATFORM_POSIX
[email protected]
    94
	IA32_Push_Rm_Disp8_ESP(jit, 4);					//push [esp+4]
[email protected]
    95
#elif defined PLATFORM_WINDOWS
[email protected]
    96
	IA32_Push_Reg(jit, REG_ECX);					//push ecx
[email protected]
    97
#endif
[email protected]
    98
	//call <function>
[email protected]
    99
	jitoffs_t call = IA32_Call_Imm32(jit, 0);		//call <function>
[email protected]
   100
	IA32_Write_Jump32_Abs(jit, call, (void *)OnClientDropWeapons);
[email protected]
   101
#if defined PLATFORM_POSIX
[email protected]
   102
	IA32_Add_Rm_Imm8(jit, REG_ESP, 4, MOD_REG);		//add esp, 4
[email protected]
   103
#elif defined PLATFORM_WINDOWS
[email protected]
   104
	IA32_Pop_Reg(jit, REG_ECX);
[email protected]
   105
#endif
[email protected]
   106
	/* Patch old bytes in */
[email protected]
   107
	for (size_t i=0; i<drpwpns_restore.bytes; i++)
[email protected]
   108
	{
[email protected]
   109
		jit->write_ubyte(drpwpns_restore.patch[i]);
[email protected]
   110
	}
[email protected]
   111
	/* Finally, do a jump back to the original */
[email protected]
   112
	call = IA32_Jump_Imm32(jit, 0);
[email protected]
   113
	IA32_Write_Jump32_Abs(jit, call, (unsigned char *)drpwpns_address + drpwpns_restore.bytes);
[email protected]
   114
}
[email protected]
   115
[email protected]
   116
void InitCSDropDetour()
[email protected]
   117
{
[email protected]
   118
	csdrop_callback = DETOUR_CREATE_MEMBER(DetourCSWeaponDrop, "CSWeaponDrop");
[email protected]
   119
	if(csdrop_callback)
[email protected]
   120
	{
[email protected]
   121
		csdrop_callback->EnableDetour();
[email protected]
   122
	}
[email protected]
   123
[email protected]
   124
}
[email protected]
   125
[email protected]
   126
void DM_InitDetours()
[email protected]
   127
{
[email protected]
   128
	CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pDmConf);
[email protected]
   129
	InitDropWeaponsDetour();
[email protected]
   130
	InitCSDropDetour();
[email protected]
   131
}
[email protected]
   132
[email protected]
   133
void DM_ShutdownDetours()
[email protected]
   134
{
[email protected]
   135
	if (drpwpns_callback)
[email protected]
   136
	{
[email protected]
   137
		/* Remove the patch */
[email protected]
   138
		DM_ApplyPatch(drpwpns_address, 0, &drpwpns_restore, NULL);
[email protected]
   139
		/* Free the gate */
[email protected]
   140
		spengine->ExecFree(drpwpns_callback);
[email protected]
   141
		drpwpns_callback = NULL;
[email protected]
   142
	}
[email protected]
   143
	if (csdrop_callback)
[email protected]
   144
	{
[email protected]
   145
		csdrop_callback->Destroy();
[email protected]
   146
		csdrop_callback = NULL;
[email protected]
   147
	}
[email protected]
   148
}