Fix for CS:GO update.
authorRuben Gonzalez <drifter01620@gmail.com>
Wed Jul 02 13:53:40 2014 -0400 (2014-07-02)
changeset 1847ddf645a50d5
parent 183 d6abe75f1896
child 185 813f027b06cb
Fix for CS:GO update.
CDetour/detourhelpers.h
CDetour/detours.cpp
CDetour/detours.h
ExtensionBuilder
asm/asm.c
asm/asm.h
cssdm_detours.cpp
gamedata/cssdm.games.txt
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/CDetour/detourhelpers.h	Wed Jul 02 13:53:40 2014 -0400
     1.3 @@ -0,0 +1,100 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2010 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id: detourhelpers.h 248 2008-08-27 00:56:22Z pred $
    1.33 + */
    1.34 +
    1.35 +#ifndef _INCLUDE_SOURCEMOD_DETOURHELPERS_H_
    1.36 +#define _INCLUDE_SOURCEMOD_DETOURHELPERS_H_
    1.37 +
    1.38 +#if defined PLATFORM_POSIX
    1.39 +#include <sys/mman.h>
    1.40 +#ifndef PAGE_SIZE
    1.41 +#define	PAGE_SIZE	4096
    1.42 +#endif
    1.43 +#define ALIGN(ar) ((long)ar & ~(PAGE_SIZE-1))
    1.44 +#define	PAGE_EXECUTE_READWRITE	PROT_READ|PROT_WRITE|PROT_EXEC
    1.45 +#endif
    1.46 +
    1.47 +struct patch_t
    1.48 +{
    1.49 +	patch_t()
    1.50 +	{
    1.51 +		patch[0] = 0;
    1.52 +		bytes = 0;
    1.53 +	}
    1.54 +	unsigned char patch[20];
    1.55 +	size_t bytes;
    1.56 +};
    1.57 +
    1.58 +inline void ProtectMemory(void *addr, int length, int prot)
    1.59 +{
    1.60 +#if defined PLATFORM_POSIX
    1.61 +	void *addr2 = (void *)ALIGN(addr);
    1.62 +	mprotect(addr2, sysconf(_SC_PAGESIZE), prot);
    1.63 +#elif defined PLATFORM_WINDOWS
    1.64 +	DWORD old_prot;
    1.65 +	VirtualProtect(addr, length, prot, &old_prot);
    1.66 +#endif
    1.67 +}
    1.68 +
    1.69 +inline void SetMemPatchable(void *address, size_t size)
    1.70 +{
    1.71 +	ProtectMemory(address, (int)size, PAGE_EXECUTE_READWRITE);
    1.72 +}
    1.73 +
    1.74 +inline void DoGatePatch(unsigned char *target, void *callback)
    1.75 +{
    1.76 +	SetMemPatchable(target, 20);
    1.77 +
    1.78 +	target[0] = 0xFF;	/* JMP */
    1.79 +	target[1] = 0x25;	/* MEM32 */
    1.80 +	*(void **)(&target[2]) = callback;
    1.81 +}
    1.82 +
    1.83 +inline void ApplyPatch(void *address, int offset, const patch_t *patch, patch_t *restore)
    1.84 +{
    1.85 +	ProtectMemory(address, 20, PAGE_EXECUTE_READWRITE);
    1.86 +
    1.87 +	unsigned char *addr = (unsigned char *)address + offset;
    1.88 +	if (restore)
    1.89 +	{
    1.90 +		for (size_t i=0; i<patch->bytes; i++)
    1.91 +		{
    1.92 +			restore->patch[i] = addr[i];
    1.93 +		}
    1.94 +		restore->bytes = patch->bytes;
    1.95 +	}
    1.96 +
    1.97 +	for (size_t i=0; i<patch->bytes; i++)
    1.98 +	{
    1.99 +		addr[i] = patch->patch[i];
   1.100 +	}
   1.101 +}
   1.102 +
   1.103 +#endif //_INCLUDE_SOURCEMOD_DETOURHELPERS_H_
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/CDetour/detours.cpp	Wed Jul 02 13:53:40 2014 -0400
     2.3 @@ -0,0 +1,192 @@
     2.4 +/**
     2.5 +* vim: set ts=4 :
     2.6 +* =============================================================================
     2.7 +* SourceMod
     2.8 +* Copyright (C) 2004-2010 AlliedModders LLC.  All rights reserved.
     2.9 +* =============================================================================
    2.10 +*
    2.11 +* This program is free software; you can redistribute it and/or modify it under
    2.12 +* the terms of the GNU General Public License, version 3.0, as published by the
    2.13 +* Free Software Foundation.
    2.14 +* 
    2.15 +* This program is distributed in the hope that it will be useful, but WITHOUT
    2.16 +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    2.17 +* FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    2.18 +* details.
    2.19 +*
    2.20 +* You should have received a copy of the GNU General Public License along with
    2.21 +* this program.  If not, see <http://www.gnu.org/licenses/>.
    2.22 +*
    2.23 +* As a special exception, AlliedModders LLC gives you permission to link the
    2.24 +* code of this program (as well as its derivative works) to "Half-Life 2," the
    2.25 +* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    2.26 +* by the Valve Corporation.  You must obey the GNU General Public License in
    2.27 +* all respects for all other code used.  Additionally, AlliedModders LLC grants
    2.28 +* this exception to all derivative works.  AlliedModders LLC defines further
    2.29 +* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    2.30 +* or <http://www.sourcemod.net/license.php>.
    2.31 +*
    2.32 +* Version: $Id: detours.cpp 248 2008-08-27 00:56:22Z pred $
    2.33 +*/
    2.34 +
    2.35 +#include "detours.h"
    2.36 +#include "../asm/asm.h"
    2.37 +
    2.38 +ISourcePawnEngine *CDetourManager::spengine = NULL;
    2.39 +IGameConfig *CDetourManager::gameconf = NULL;
    2.40 +
    2.41 +void CDetourManager::Init(ISourcePawnEngine *spengine, IGameConfig *gameconf)
    2.42 +{
    2.43 +	CDetourManager::spengine = spengine;
    2.44 +	CDetourManager::gameconf = gameconf;
    2.45 +}
    2.46 +
    2.47 +CDetour *CDetourManager::CreateDetour(void *callbackfunction, void **trampoline, const char *signame)
    2.48 +{
    2.49 +	CDetour *detour = new CDetour(callbackfunction, trampoline, signame);
    2.50 +	if (detour)
    2.51 +	{
    2.52 +		if (!detour->Init(spengine, gameconf))
    2.53 +		{
    2.54 +			delete detour;
    2.55 +			return NULL;
    2.56 +		}
    2.57 +
    2.58 +		return detour;
    2.59 +	}
    2.60 +
    2.61 +	return NULL;
    2.62 +}
    2.63 +
    2.64 +CDetour::CDetour(void *callbackfunction, void **trampoline, const char *signame)
    2.65 +{
    2.66 +	enabled = false;
    2.67 +	detoured = false;
    2.68 +	detour_address = NULL;
    2.69 +	detour_trampoline = NULL;
    2.70 +	this->signame = signame;
    2.71 +	this->detour_callback = callbackfunction;
    2.72 +	spengine = NULL;
    2.73 +	gameconf = NULL;
    2.74 +	this->trampoline = trampoline;
    2.75 +}
    2.76 +
    2.77 +bool CDetour::Init(ISourcePawnEngine *spengine, IGameConfig *gameconf)
    2.78 +{
    2.79 +	this->spengine = spengine;
    2.80 +	this->gameconf = gameconf;
    2.81 +
    2.82 +	if (!CreateDetour())
    2.83 +	{
    2.84 +		enabled = false;
    2.85 +		return enabled;
    2.86 +	}
    2.87 +
    2.88 +	enabled = true;
    2.89 +
    2.90 +	return enabled;
    2.91 +}
    2.92 +
    2.93 +void CDetour::Destroy()
    2.94 +{
    2.95 +	DeleteDetour();
    2.96 +	delete this;
    2.97 +}
    2.98 +
    2.99 +bool CDetour::IsEnabled()
   2.100 +{
   2.101 +	return enabled;
   2.102 +}
   2.103 +
   2.104 +bool CDetour::CreateDetour()
   2.105 +{
   2.106 +	if (!gameconf->GetMemSig(signame, &detour_address))
   2.107 +	{
   2.108 +		g_pSM->LogError(myself, "Could not locate %s - Disabling detour", signame);
   2.109 +		return false;
   2.110 +	}
   2.111 +
   2.112 +	if (!detour_address)
   2.113 +	{
   2.114 +		g_pSM->LogError(myself, "Sigscan for %s failed - Disabling detour to prevent crashes", signame);
   2.115 +		return false;
   2.116 +	}
   2.117 +
   2.118 +	detour_restore.bytes = copy_bytes((unsigned char *)detour_address, NULL, OP_JMP_SIZE+1);
   2.119 +
   2.120 +	/* First, save restore bits */
   2.121 +	for (size_t i=0; i<detour_restore.bytes; i++)
   2.122 +	{
   2.123 +		detour_restore.patch[i] = ((unsigned char *)detour_address)[i];
   2.124 +	}
   2.125 +
   2.126 +	JitWriter wr;
   2.127 +	JitWriter *jit = &wr;
   2.128 +	jit_uint32_t CodeSize = 0;
   2.129 +	
   2.130 +	wr.outbase = NULL;
   2.131 +	wr.outptr = NULL;
   2.132 +
   2.133 +jit_rewind:
   2.134 +
   2.135 +	/* Patch old bytes in */
   2.136 +	if (wr.outbase != NULL)
   2.137 +	{
   2.138 +		copy_bytes((unsigned char *)detour_address, (unsigned char*)wr.outptr, detour_restore.bytes);
   2.139 +	}
   2.140 +	wr.outptr += detour_restore.bytes;
   2.141 +
   2.142 +	/* Return to the original function */
   2.143 +	jitoffs_t call = IA32_Jump_Imm32(jit, 0);
   2.144 +	IA32_Write_Jump32_Abs(jit, call, (unsigned char *)detour_address + detour_restore.bytes);
   2.145 +
   2.146 +	if (wr.outbase == NULL)
   2.147 +	{
   2.148 +		CodeSize = wr.get_outputpos();
   2.149 +		wr.outbase = (jitcode_t)spengine->AllocatePageMemory(CodeSize);
   2.150 +		spengine->SetReadWrite(wr.outbase);
   2.151 +		wr.outptr = wr.outbase;
   2.152 +		detour_trampoline = wr.outbase;
   2.153 +		goto jit_rewind;
   2.154 +	}
   2.155 +
   2.156 +	spengine->SetReadExecute(wr.outbase);
   2.157 +
   2.158 +	*trampoline = detour_trampoline;
   2.159 +
   2.160 +	return true;
   2.161 +}
   2.162 +
   2.163 +void CDetour::DeleteDetour()
   2.164 +{
   2.165 +	if (detoured)
   2.166 +	{
   2.167 +		DisableDetour();
   2.168 +	}
   2.169 +
   2.170 +	if (detour_trampoline)
   2.171 +	{
   2.172 +		/* Free the allocated trampoline memory */
   2.173 +		spengine->FreePageMemory(detour_trampoline);
   2.174 +		detour_trampoline = NULL;
   2.175 +	}
   2.176 +}
   2.177 +
   2.178 +void CDetour::EnableDetour()
   2.179 +{
   2.180 +	if (!detoured)
   2.181 +	{
   2.182 +		DoGatePatch((unsigned char *)detour_address, &detour_callback);
   2.183 +		detoured = true;
   2.184 +	}
   2.185 +}
   2.186 +
   2.187 +void CDetour::DisableDetour()
   2.188 +{
   2.189 +	if (detoured)
   2.190 +	{
   2.191 +		/* Remove the patch */
   2.192 +		ApplyPatch(detour_address, 0, &detour_restore, NULL);
   2.193 +		detoured = false;
   2.194 +	}
   2.195 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/CDetour/detours.h	Wed Jul 02 13:53:40 2014 -0400
     3.3 @@ -0,0 +1,251 @@
     3.4 +/**
     3.5 +* vim: set ts=4 :
     3.6 +* =============================================================================
     3.7 +* SourceMod
     3.8 +* Copyright (C) 2004-2010 AlliedModders LLC.  All rights reserved.
     3.9 +* =============================================================================
    3.10 +*
    3.11 +* This program is free software; you can redistribute it and/or modify it under
    3.12 +* the terms of the GNU General Public License, version 3.0, as published by the
    3.13 +* Free Software Foundation.
    3.14 +* 
    3.15 +* This program is distributed in the hope that it will be useful, but WITHOUT
    3.16 +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    3.17 +* FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    3.18 +* details.
    3.19 +*
    3.20 +* You should have received a copy of the GNU General Public License along with
    3.21 +* this program.  If not, see <http://www.gnu.org/licenses/>.
    3.22 +*
    3.23 +* As a special exception, AlliedModders LLC gives you permission to link the
    3.24 +* code of this program (as well as its derivative works) to "Half-Life 2," the
    3.25 +* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    3.26 +* by the Valve Corporation.  You must obey the GNU General Public License in
    3.27 +* all respects for all other code used.  Additionally, AlliedModders LLC grants
    3.28 +* this exception to all derivative works.  AlliedModders LLC defines further
    3.29 +* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    3.30 +* or <http://www.sourcemod.net/license.php>.
    3.31 +*
    3.32 +* Version: $Id: detours.h 257 2008-09-23 03:12:13Z pred $
    3.33 +*/
    3.34 +
    3.35 +#ifndef _INCLUDE_SOURCEMOD_DETOURS_H_
    3.36 +#define _INCLUDE_SOURCEMOD_DETOURS_H_
    3.37 +
    3.38 +#include "../cssdm_main.h"
    3.39 +#include <jit/jit_helpers.h>
    3.40 +#include <jit/x86/x86_macros.h>
    3.41 +#include "detourhelpers.h"
    3.42 +
    3.43 +/**
    3.44 + * CDetours class for SourceMod Extensions by pRED*
    3.45 + * detourhelpers.h entirely stolen from CSS:DM and were written by BAILOPAN (I assume).
    3.46 + * asm.h/c from devmaster.net (thanks cybermind) edited by pRED* to handle gcc -fPIC thunks correctly
    3.47 + * Concept by Nephyrin Zey (http://www.doublezen.net/) and Windows Detour Library (http://research.microsoft.com/sn/detours/)
    3.48 + * Member function pointer ideas by Don Clugston (http://www.codeproject.com/cpp/FastDelegate.asp)
    3.49 + */
    3.50 +
    3.51 +#define DETOUR_MEMBER_CALL(name) (this->*name##_Actual)
    3.52 +#define DETOUR_STATIC_CALL(name) (name##_Actual)
    3.53 +
    3.54 +#define DETOUR_DECL_STATIC0(name, ret) \
    3.55 +ret (*name##_Actual)(void) = NULL; \
    3.56 +ret name(void)
    3.57 +
    3.58 +#define DETOUR_DECL_STATIC1(name, ret, p1type, p1name) \
    3.59 +ret (*name##_Actual)(p1type) = NULL; \
    3.60 +ret name(p1type p1name)
    3.61 +
    3.62 +#define DETOUR_DECL_STATIC4(name, ret, p1type, p1name, p2type, p2name, p3type, p3name, p4type, p4name) \
    3.63 +ret (*name##_Actual)(p1type, p2type, p3type, p4type) = NULL; \
    3.64 +ret name(p1type p1name, p2type p2name, p3type p3name, p4type p4name)
    3.65 +
    3.66 +#define DETOUR_DECL_MEMBER0(name, ret) \
    3.67 +class name##Class \
    3.68 +{ \
    3.69 +public: \
    3.70 +	ret name(); \
    3.71 +	static ret (name##Class::* name##_Actual)(void); \
    3.72 +}; \
    3.73 +ret (name##Class::* name##Class::name##_Actual)(void) = NULL; \
    3.74 +ret name##Class::name()
    3.75 +
    3.76 +#define DETOUR_DECL_MEMBER1(name, ret, p1type, p1name) \
    3.77 +class name##Class \
    3.78 +{ \
    3.79 +public: \
    3.80 +	ret name(p1type p1name); \
    3.81 +	static ret (name##Class::* name##_Actual)(p1type); \
    3.82 +}; \
    3.83 +ret (name##Class::* name##Class::name##_Actual)(p1type) = NULL; \
    3.84 +ret name##Class::name(p1type p1name)
    3.85 +
    3.86 +#define DETOUR_DECL_MEMBER2(name, ret, p1type, p1name, p2type, p2name) \
    3.87 +class name##Class \
    3.88 +{ \
    3.89 +public: \
    3.90 +	ret name(p1type p1name, p2type p2name); \
    3.91 +	static ret (name##Class::* name##_Actual)(p1type, p2type); \
    3.92 +}; \
    3.93 +ret (name##Class::* name##Class::name##_Actual)(p1type, p2type) = NULL; \
    3.94 +ret name##Class::name(p1type p1name, p2type p2name)
    3.95 +
    3.96 +#define DETOUR_DECL_MEMBER3(name, ret, p1type, p1name, p2type, p2name, p3type, p3name) \
    3.97 +class name##Class \
    3.98 +{ \
    3.99 +public: \
   3.100 +	ret name(p1type p1name, p2type p2name, p3type p3name); \
   3.101 +	static ret (name##Class::* name##_Actual)(p1type, p2type, p3type); \
   3.102 +}; \
   3.103 +ret (name##Class::* name##Class::name##_Actual)(p1type, p2type, p3type) = NULL; \
   3.104 +ret name##Class::name(p1type p1name, p2type p2name, p3type p3name)
   3.105 +
   3.106 +#define DETOUR_DECL_MEMBER4(name, ret, p1type, p1name, p2type, p2name, p3type, p3name, p4type, p4name) \
   3.107 +class name##Class \
   3.108 +{ \
   3.109 +public: \
   3.110 +        ret name(p1type p1name, p2type p2name, p3type p3name, p4type p4name); \
   3.111 +        static ret (name##Class::* name##_Actual)(p1type, p2type, p3type, p4type); \
   3.112 +}; \
   3.113 +ret (name##Class::* name##Class::name##_Actual)(p1type, p2type, p3type, p4type) = NULL; \
   3.114 +ret name##Class::name(p1type p1name, p2type p2name, p3type p3name, p4type p4name)
   3.115 +
   3.116 +#define DETOUR_DECL_MEMBER8(name, ret, p1type, p1name, p2type, p2name, p3type, p3name, p4type, p4name, p5type, p5name, p6type, p6name, p7type, p7name, p8type, p8name) \
   3.117 +class name##Class \
   3.118 +{ \
   3.119 +public: \
   3.120 +        ret name(p1type p1name, p2type p2name, p3type p3name, p4type p4name, p5type p5name, p6type p6name, p7type p7name, p8type p8name); \
   3.121 +        static ret (name##Class::* name##_Actual)(p1type, p2type, p3type, p4type, p5type, p6type, p7type, p8type); \
   3.122 +}; \
   3.123 +ret (name##Class::* name##Class::name##_Actual)(p1type, p2type, p3type, p4type, p5type, p6type, p7type, p8type) = NULL; \
   3.124 +ret name##Class::name(p1type p1name, p2type p2name, p3type p3name, p4type p4name, p5type p5name, p6type p6name, p7type p7name, p8type p8name)
   3.125 +
   3.126 +
   3.127 +#define GET_MEMBER_CALLBACK(name) (void *)GetCodeAddress(&name##Class::name)
   3.128 +#define GET_MEMBER_TRAMPOLINE(name) (void **)(&name##Class::name##_Actual)
   3.129 +
   3.130 +#define GET_STATIC_CALLBACK(name) (void *)&name
   3.131 +#define GET_STATIC_TRAMPOLINE(name) (void **)&name##_Actual
   3.132 +
   3.133 +#define DETOUR_CREATE_MEMBER(name, gamedata) CDetourManager::CreateDetour(GET_MEMBER_CALLBACK(name), GET_MEMBER_TRAMPOLINE(name), gamedata);
   3.134 +#define DETOUR_CREATE_STATIC(name, gamedata) CDetourManager::CreateDetour(GET_STATIC_CALLBACK(name), GET_STATIC_TRAMPOLINE(name), gamedata);
   3.135 +
   3.136 +
   3.137 +class GenericClass {};
   3.138 +typedef void (GenericClass::*VoidFunc)();
   3.139 +
   3.140 +inline void *GetCodeAddr(VoidFunc mfp)
   3.141 +{
   3.142 +	return *(void **)&mfp;
   3.143 +}
   3.144 +
   3.145 +/**
   3.146 + * Converts a member function pointer to a void pointer.
   3.147 + * This relies on the assumption that the code address lies at mfp+0
   3.148 + * This is the case for both g++ and later MSVC versions on non virtual functions but may be different for other compilers
   3.149 + * Based on research by Don Clugston : http://www.codeproject.com/cpp/FastDelegate.asp
   3.150 + */
   3.151 +#define GetCodeAddress(mfp) GetCodeAddr(reinterpret_cast<VoidFunc>(mfp))
   3.152 +
   3.153 +class CDetourManager;
   3.154 +
   3.155 +class CDetour
   3.156 +{
   3.157 +public:
   3.158 +
   3.159 +	bool IsEnabled();
   3.160 +
   3.161 +	/**
   3.162 +	 * These would be somewhat self-explanatory I hope
   3.163 +	 */
   3.164 +	void EnableDetour();
   3.165 +	void DisableDetour();
   3.166 +
   3.167 +	void Destroy();
   3.168 +
   3.169 +	friend class CDetourManager;
   3.170 +
   3.171 +protected:
   3.172 +	CDetour(void *callbackfunction, void **trampoline, const char *signame);
   3.173 +
   3.174 +	bool Init(ISourcePawnEngine *spengine, IGameConfig *gameconf);
   3.175 +private:
   3.176 +
   3.177 +	/* These create/delete the allocated memory */
   3.178 +	bool CreateDetour();
   3.179 +	void DeleteDetour();
   3.180 +
   3.181 +	bool enabled;
   3.182 +	bool detoured;
   3.183 +
   3.184 +	patch_t detour_restore;
   3.185 +	/* Address of the detoured function */
   3.186 +	void *detour_address;
   3.187 +	/* Address of the allocated trampoline function */
   3.188 +	void *detour_trampoline;
   3.189 +	/* Address of the callback handler */
   3.190 +	void *detour_callback;
   3.191 +	/* The function pointer used to call our trampoline */
   3.192 +	void **trampoline;
   3.193 +	
   3.194 +	const char *signame;
   3.195 +	ISourcePawnEngine *spengine;
   3.196 +	IGameConfig *gameconf;
   3.197 +};
   3.198 +
   3.199 +class CDetourManager
   3.200 +{
   3.201 +public:
   3.202 +
   3.203 +	static void Init(ISourcePawnEngine *spengine, IGameConfig *gameconf);
   3.204 +
   3.205 +	/**
   3.206 +	 * Creates a new detour
   3.207 +	 *
   3.208 +	 * @param callbackfunction			Void pointer to your detour callback function.
   3.209 +	 * @param trampoline				Address of the trampoline pointer
   3.210 +	 * @param signame					Section name containing a signature to fetch from the gamedata file.
   3.211 +	 * @return							A new CDetour pointer to control your detour.
   3.212 +	 *
   3.213 +	 * Example:
   3.214 +	 *
   3.215 +	 * CBaseServer::ConnectClient(netadr_s &, int, int, int, char  const*, char  const*, char  const*, int)
   3.216 +	 *
   3.217 +	 * Define a new class with the required function and a member function pointer to the same type:
   3.218 +	 *
   3.219 +	 * class CBaseServerDetour
   3.220 +	 * {
   3.221 +	 * public:
   3.222 +	 *		 bool ConnectClient(void *netaddr_s, int, int, int, char  const*, char  const*, char  const*, int);
   3.223 +	 *		 static bool (CBaseServerDetour::* ConnectClient_Actual)(void *netaddr_s, int, int, int, char  const*, char  const*, char  const*, int);
   3.224 +	 * }
   3.225 +	 *
   3.226 +	 *	void *callbackfunc = GetCodeAddress(&CBaseServerDetour::ConnectClient);
   3.227 +	 *	void **trampoline = (void **)(&CBaseServerDetour::ConnectClient_Actual);
   3.228 +	 *
   3.229 +	 * Creation:
   3.230 +	 * CDetourManager::CreateDetour(callbackfunc,  trampoline, "ConnectClient");
   3.231 +	 *
   3.232 +	 * Usage:
   3.233 +	 *
   3.234 +	 * CBaseServerDetour::ConnectClient(void *netaddr_s, int, int, int, char  const*, char  const*, char  const*, int)
   3.235 +	 * {
   3.236 +	 *			//pre hook code
   3.237 +	 *			bool result = (this->*ConnectClient_Actual)(netaddr_s, rest of params);
   3.238 +	 *			//post hook code
   3.239 +	 *			return result;
   3.240 +	 * }
   3.241 +	 *
   3.242 +	 * Note we changed the netadr_s reference into a void* to avoid needing to define the type
   3.243 +	 */
   3.244 +	static CDetour *CreateDetour(void *callbackfunction, void **trampoline, const char *signame);
   3.245 +
   3.246 +	friend class CBlocker;
   3.247 +	friend class CDetour;
   3.248 +
   3.249 +private:
   3.250 +	static ISourcePawnEngine *spengine;
   3.251 +	static IGameConfig *gameconf;
   3.252 +};
   3.253 +
   3.254 +#endif // _INCLUDE_SOURCEMOD_DETOURS_H_
     4.1 --- a/ExtensionBuilder	Thu Jun 12 10:18:02 2014 -0400
     4.2 +++ b/ExtensionBuilder	Wed Jul 02 13:53:40 2014 -0400
     4.3 @@ -29,6 +29,8 @@
     4.4  		'cssdm_utils.cpp',
     4.5  		'cssdm_weapons.cpp',
     4.6  		'sdk/smsdk_ext.cpp'
     4.7 +		'CDetour/detours.cpp'
     4.8 +		'asm/asm.c'
     4.9  		])
    4.10  	CSSDM.PostSetupHL2Job(extension, binary, i)
    4.11  	CSSDM.AutoVersion('.', binary)
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/asm/asm.c	Wed Jul 02 13:53:40 2014 -0400
     5.3 @@ -0,0 +1,421 @@
     5.4 +#include "asm.h"
     5.5 +
     5.6 +#ifndef WIN32
     5.7 +#define _GNU_SOURCE
     5.8 +#include <dlfcn.h>
     5.9 +#include <string.h>
    5.10 +
    5.11 +#define REG_EAX			0
    5.12 +#define REG_ECX			1
    5.13 +#define REG_EDX			2
    5.14 +#define REG_EBX			3
    5.15 +
    5.16 +#define IA32_MOV_REG_IMM		0xB8	// encoding is +r <imm32>
    5.17 +#endif
    5.18 +
    5.19 +extern void Msg( const char *, ... );
    5.20 +
    5.21 +/**
    5.22 +* Checks if a call to a fpic thunk has just been written into dest.
    5.23 +* If found replaces it with a direct mov that sets the required register to the value of pc.
    5.24 +*
    5.25 +* @param dest		Destination buffer where a call opcode + addr (5 bytes) has just been written.
    5.26 +* @param pc		The program counter value that needs to be set (usually the next address from the source).
    5.27 +* @noreturn
    5.28 +*/
    5.29 +void check_thunks(unsigned char *dest, unsigned char *pc)
    5.30 +{
    5.31 +#if defined WIN32
    5.32 +	return;
    5.33 +#else
    5.34 +	/* Step write address back 4 to the start of the function address */
    5.35 +	unsigned char *writeaddr = dest - 4;
    5.36 +	unsigned char *calloffset = *(unsigned char **)writeaddr;
    5.37 +	unsigned char *calladdr = (unsigned char *)(dest + (unsigned int)calloffset);
    5.38 +
    5.39 +	/* Lookup name of function being called */
    5.40 +	if ((*calladdr == 0x8B) && (*(calladdr+2) == 0x24) && (*(calladdr+3) == 0xC3))
    5.41 +	{
    5.42 +		//a thunk maybe?
    5.43 +		char movByte = IA32_MOV_REG_IMM;
    5.44 +
    5.45 +		/* Calculate the correct mov opcode */
    5.46 +		switch (*(calladdr+1))
    5.47 +		{
    5.48 +		case 0x04:
    5.49 +			{
    5.50 +				movByte += REG_EAX;
    5.51 +				break;
    5.52 +			}
    5.53 +		case 0x1C:
    5.54 +			{
    5.55 +				movByte += REG_EBX;
    5.56 +				break;
    5.57 +			}
    5.58 +		case 0x0C:
    5.59 +			{
    5.60 +				movByte += REG_ECX;
    5.61 +				break;
    5.62 +			}
    5.63 +		case 0x14:
    5.64 +			{
    5.65 +				movByte += REG_EDX;
    5.66 +				break;
    5.67 +			}
    5.68 +		default:
    5.69 +			{
    5.70 +				Msg("Unknown thunk: %c\n", *(calladdr+1));
    5.71 +				break;
    5.72 +			}
    5.73 +		}
    5.74 +
    5.75 +		/* Move our write address back one to where the call opcode was */
    5.76 +		writeaddr--;
    5.77 +
    5.78 +
    5.79 +		/* Write our mov */
    5.80 +		*writeaddr = movByte;
    5.81 +		writeaddr++;
    5.82 +
    5.83 +		/* Write the value - The provided program counter value */
    5.84 +		*(void **)writeaddr = (void *)pc;
    5.85 +		writeaddr += 4;
    5.86 +	}
    5.87 +
    5.88 +	return;
    5.89 +#endif
    5.90 +}
    5.91 +
    5.92 +//if dest is NULL, returns minimum number of bytes needed to be copied
    5.93 +//if dest is not NULL, it will copy the bytes to dest as well as fix CALLs and JMPs
    5.94 +//http://www.devmaster.net/forums/showthread.php?t=2311
    5.95 +int copy_bytes(unsigned char *func, unsigned char* dest, int required_len) {
    5.96 +	int bytecount = 0;
    5.97 +
    5.98 +	while(bytecount < required_len && *func != 0xCC)
    5.99 +	{
   5.100 +		// prefixes F0h, F2h, F3h, 66h, 67h, D8h-DFh, 2Eh, 36h, 3Eh, 26h, 64h and 65h
   5.101 +		int operandSize = 4;
   5.102 +		int FPU = 0;
   5.103 +		int twoByte = 0;
   5.104 +		unsigned char opcode = 0x90;
   5.105 +		unsigned char modRM = 0xFF;
   5.106 +		while(*func == 0xF0 ||
   5.107 +			  *func == 0xF2 ||
   5.108 +			  *func == 0xF3 ||
   5.109 +			 (*func & 0xFC) == 0x64 ||
   5.110 +			 (*func & 0xF8) == 0xD8 ||
   5.111 +			 (*func & 0x7E) == 0x62)
   5.112 +		{
   5.113 +			if(*func == 0x66)
   5.114 +			{
   5.115 +				operandSize = 2;
   5.116 +			}
   5.117 +			else if((*func & 0xF8) == 0xD8)
   5.118 +			{
   5.119 +				FPU = *func;
   5.120 +				if (dest)
   5.121 +						*dest++ = *func++;
   5.122 +				else
   5.123 +						func++;
   5.124 +				bytecount++;
   5.125 +				break;
   5.126 +			}
   5.127 +
   5.128 +			if (dest)
   5.129 +				*dest++ = *func++;
   5.130 +			else
   5.131 +				func++;
   5.132 +			bytecount++;
   5.133 +		}
   5.134 +
   5.135 +		// two-byte opcode byte
   5.136 +		if(*func == 0x0F)
   5.137 +		{
   5.138 +			twoByte = 1;
   5.139 +			if (dest)
   5.140 +				*dest++ = *func++;
   5.141 +			else
   5.142 +				func++;
   5.143 +			bytecount++;
   5.144 +		}
   5.145 +
   5.146 +		// opcode byte
   5.147 +		opcode = *func++;
   5.148 +		if (dest) *dest++ = opcode;
   5.149 +		bytecount++;
   5.150 +
   5.151 +		// mod R/M byte
   5.152 +		modRM = 0xFF;
   5.153 +		if(FPU)
   5.154 +		{
   5.155 +			if((opcode & 0xC0) != 0xC0)
   5.156 +			{
   5.157 +				modRM = opcode;
   5.158 +			}
   5.159 +		}
   5.160 +		else if(!twoByte)
   5.161 +		{
   5.162 +			if((opcode & 0xC4) == 0x00 ||
   5.163 +			   ((opcode & 0xF4) == 0x60 && ((opcode & 0x0A) == 0x02 || (opcode & 0x09) == 0x09)) ||
   5.164 +			   (opcode & 0xF0) == 0x80 ||
   5.165 +			   ((opcode & 0xF8) == 0xC0 && (opcode & 0x0E) != 0x02) ||
   5.166 +			   (opcode & 0xFC) == 0xD0 ||
   5.167 +			   (opcode & 0xF6) == 0xF6)
   5.168 +			{
   5.169 +				modRM = *func++;
   5.170 +				if (dest) *dest++ = modRM;
   5.171 +				bytecount++;
   5.172 +			}
   5.173 +		}
   5.174 +		else
   5.175 +		{
   5.176 +			if(((opcode & 0xF0) == 0x00 && (opcode & 0x0F) >= 0x04 && (opcode & 0x0D) != 0x0D) ||
   5.177 +			   (opcode & 0xF0) == 0x30 ||
   5.178 +			   opcode == 0x77 ||
   5.179 +			   (opcode & 0xF0) == 0x80 ||
   5.180 +			   ((opcode & 0xF0) == 0xA0 && (opcode & 0x07) <= 0x02) ||
   5.181 +			   (opcode & 0xF8) == 0xC8)
   5.182 +			{
   5.183 +				// No mod R/M byte
   5.184 +			}
   5.185 +			else
   5.186 +			{
   5.187 +				modRM = *func++;
   5.188 +				if (dest) *dest++ = modRM;
   5.189 +				bytecount++;
   5.190 +			}
   5.191 +		}
   5.192 +
   5.193 +		// SIB
   5.194 +		if((modRM & 0x07) == 0x04 &&
   5.195 +		   (modRM & 0xC0) != 0xC0)
   5.196 +		{
   5.197 +			if (dest)
   5.198 +				*dest++ = *func++;   //SIB
   5.199 +			else
   5.200 +				func++;
   5.201 +			bytecount++;
   5.202 +		}
   5.203 +
   5.204 +		// mod R/M displacement
   5.205 +
   5.206 +		// Dword displacement, no base
   5.207 +	if((modRM & 0xC5) == 0x05) {
   5.208 +		if (dest) {
   5.209 +			*(unsigned int*)dest = *(unsigned int*)func;
   5.210 +			dest += 4;
   5.211 +		}
   5.212 +		func += 4;
   5.213 +		bytecount += 4;
   5.214 +	}
   5.215 +
   5.216 +		// Byte displacement
   5.217 +	if((modRM & 0xC0) == 0x40) {
   5.218 +		if (dest)
   5.219 +			*dest++ = *func++;
   5.220 +		else
   5.221 +			func++;
   5.222 +		bytecount++;
   5.223 +	}
   5.224 +
   5.225 +		// Dword displacement
   5.226 +	if((modRM & 0xC0) == 0x80) {
   5.227 +		if (dest) {
   5.228 +			*(unsigned int*)dest = *(unsigned int*)func;
   5.229 +			dest += 4;
   5.230 +		}
   5.231 +		func += 4;
   5.232 +		bytecount += 4;
   5.233 +	}
   5.234 +
   5.235 +		// immediate
   5.236 +		if(FPU)
   5.237 +		{
   5.238 +			// Can't have immediate operand
   5.239 +		}
   5.240 +		else if(!twoByte)
   5.241 +		{
   5.242 +			if((opcode & 0xC7) == 0x04 ||
   5.243 +			   (opcode & 0xFE) == 0x6A ||   // PUSH/POP/IMUL
   5.244 +			   (opcode & 0xF0) == 0x70 ||   // Jcc
   5.245 +			   opcode == 0x80 ||
   5.246 +			   opcode == 0x83 ||
   5.247 +			   (opcode & 0xFD) == 0xA0 ||   // MOV
   5.248 +			   opcode == 0xA8 ||			// TEST
   5.249 +			   (opcode & 0xF8) == 0xB0 ||   // MOV
   5.250 +			   (opcode & 0xFE) == 0xC0 ||   // RCL
   5.251 +			   opcode == 0xC6 ||			// MOV
   5.252 +			   opcode == 0xCD ||			// INT
   5.253 +			   (opcode & 0xFE) == 0xD4 ||   // AAD/AAM
   5.254 +			   (opcode & 0xF8) == 0xE0 ||   // LOOP/JCXZ
   5.255 +			   opcode == 0xEB ||
   5.256 +			   (opcode == 0xF6 && (modRM & 0x30) == 0x00))   // TEST
   5.257 +			{
   5.258 +				if (dest)
   5.259 +					*dest++ = *func++;
   5.260 +				else
   5.261 +					func++;
   5.262 +				bytecount++;
   5.263 +			}
   5.264 +		else if((opcode & 0xF7) == 0xC2) // RET
   5.265 +			{
   5.266 +				if (dest) {
   5.267 +					*(unsigned short*)dest = *(unsigned short*)func;
   5.268 +					dest += 2;
   5.269 +				}
   5.270 +				func += 2;
   5.271 +				bytecount += 2;
   5.272 +			}
   5.273 +			else if((opcode & 0xFC) == 0x80 ||
   5.274 +					(opcode & 0xC7) == 0x05 ||
   5.275 +					(opcode & 0xF8) == 0xB8 ||
   5.276 +					(opcode & 0xFE) == 0xE8 ||	  // CALL/Jcc
   5.277 +					(opcode & 0xFE) == 0x68 ||
   5.278 +					(opcode & 0xFC) == 0xA0 ||
   5.279 +					(opcode & 0xEE) == 0xA8 ||
   5.280 +					opcode == 0xC7 ||
   5.281 +					(opcode == 0xF7 && (modRM & 0x30) == 0x00))
   5.282 +			{
   5.283 +				if (dest) {
   5.284 +					//Fix CALL/JMP offset
   5.285 +					if ((opcode & 0xFE) == 0xE8) {
   5.286 +						if (operandSize == 4)
   5.287 +						{
   5.288 +							*(long*)dest = ((func + *(long*)func) - dest);
   5.289 +
   5.290 +							//pRED* edit. func is the current address of the call address, +4 is the next instruction, so the value of $pc
   5.291 +							check_thunks(dest+4, func+4);
   5.292 +						}
   5.293 +						else
   5.294 +							*(short*)dest = ((func + *(short*)func) - dest);
   5.295 +
   5.296 +					} else {
   5.297 +						if (operandSize == 4)
   5.298 +							*(unsigned long*)dest = *(unsigned long*)func;
   5.299 +						else
   5.300 +							*(unsigned short*)dest = *(unsigned short*)func;
   5.301 +					}
   5.302 +					dest += operandSize;
   5.303 +				}
   5.304 +				func += operandSize;
   5.305 +				bytecount += operandSize;
   5.306 +
   5.307 +			}
   5.308 +		}
   5.309 +		else
   5.310 +		{
   5.311 +			if(opcode == 0xBA ||			// BT
   5.312 +			   opcode == 0x0F ||			// 3DNow!
   5.313 +			   (opcode & 0xFC) == 0x70 ||   // PSLLW
   5.314 +			   (opcode & 0xF7) == 0xA4 ||   // SHLD
   5.315 +			   opcode == 0xC2 ||
   5.316 +			   opcode == 0xC4 ||
   5.317 +			   opcode == 0xC5 ||
   5.318 +			   opcode == 0xC6)
   5.319 +			{
   5.320 +				if (dest)
   5.321 +					*dest++ = *func++;
   5.322 +				else
   5.323 +					func++;
   5.324 +			}
   5.325 +			else if((opcode & 0xF0) == 0x80) // Jcc -i
   5.326 +			{
   5.327 +				if (dest) {
   5.328 +					if (operandSize == 4)
   5.329 +						*(unsigned long*)dest = *(unsigned long*)func;
   5.330 +					else
   5.331 +						*(unsigned short*)dest = *(unsigned short*)func;
   5.332 +
   5.333 +					dest += operandSize;
   5.334 +				}
   5.335 +				func += operandSize;
   5.336 +				bytecount += operandSize;
   5.337 +			}
   5.338 +		}
   5.339 +	}
   5.340 +
   5.341 +	return bytecount;
   5.342 +}
   5.343 +
   5.344 +//insert a specific JMP instruction at the given location
   5.345 +void inject_jmp(void* src, void* dest) {
   5.346 +	*(unsigned char*)src = OP_JMP;
   5.347 +	*(long*)((unsigned char*)src+1) = (long)((unsigned char*)dest - ((unsigned char*)src + OP_JMP_SIZE));
   5.348 +}
   5.349 +
   5.350 +//fill a given block with NOPs
   5.351 +void fill_nop(void* src, unsigned int len) {
   5.352 +	unsigned char* src2 = (unsigned char*)src;
   5.353 +	while (len) {
   5.354 +		*src2++ = OP_NOP;
   5.355 +		--len;
   5.356 +	}
   5.357 +}
   5.358 +
   5.359 +void* eval_jump(void* src) {
   5.360 +	unsigned char* addr = (unsigned char*)src;
   5.361 +
   5.362 +	if (!addr) return 0;
   5.363 +
   5.364 +	//import table jump
   5.365 +	if (addr[0] == OP_PREFIX && addr[1] == OP_JMP_SEG) {
   5.366 +		addr += 2;
   5.367 +		addr = *(unsigned char**)addr;
   5.368 +		//TODO: if addr points into the IAT
   5.369 +		return *(void**)addr;
   5.370 +	}
   5.371 +
   5.372 +	//8bit offset
   5.373 +	else if (addr[0] == OP_JMP_BYTE) {
   5.374 +		addr = &addr[OP_JMP_BYTE_SIZE] + *(char*)&addr[1];
   5.375 +		//mangled 32bit jump?
   5.376 +		if (addr[0] == OP_JMP) {
   5.377 +			addr = addr + *(int*)&addr[1];
   5.378 +		}
   5.379 +		return addr;
   5.380 +	}
   5.381 +	/*
   5.382 +	//32bit offset
   5.383 +	else if (addr[0] == OP_JMP) {
   5.384 +		addr = &addr[OP_JMP_SIZE] + *(int*)&addr[1];
   5.385 +	}
   5.386 +	*/
   5.387 +
   5.388 +	return addr;
   5.389 +}
   5.390 +/*
   5.391 +from ms detours package
   5.392 +static bool detour_is_imported(PBYTE pbCode, PBYTE pbAddress)
   5.393 +{
   5.394 +	MEMORY_BASIC_INFORMATION mbi;
   5.395 +	VirtualQuery((PVOID)pbCode, &mbi, sizeof(mbi));
   5.396 +	__try {
   5.397 +		PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)mbi.AllocationBase;
   5.398 +		if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE) {
   5.399 +			return false;
   5.400 +		}
   5.401 +
   5.402 +		PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((PBYTE)pDosHeader +
   5.403 +														  pDosHeader->e_lfanew);
   5.404 +		if (pNtHeader->Signature != IMAGE_NT_SIGNATURE) {
   5.405 +			return false;
   5.406 +		}
   5.407 +
   5.408 +		if (pbAddress >= ((PBYTE)pDosHeader +
   5.409 +						  pNtHeader->OptionalHeader
   5.410 +						  .DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress) &&
   5.411 +			pbAddress < ((PBYTE)pDosHeader +
   5.412 +						 pNtHeader->OptionalHeader
   5.413 +						 .DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress +
   5.414 +						 pNtHeader->OptionalHeader
   5.415 +						 .DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].Size)) {
   5.416 +			return true;
   5.417 +		}
   5.418 +		return false;
   5.419 +	}
   5.420 +	__except(EXCEPTION_EXECUTE_HANDLER) {
   5.421 +		return false;
   5.422 +	}
   5.423 +}
   5.424 +*/
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/asm/asm.h	Wed Jul 02 13:53:40 2014 -0400
     6.3 @@ -0,0 +1,40 @@
     6.4 +#ifndef __ASM_H__
     6.5 +#define __ASM_H__
     6.6 +
     6.7 +#define OP_JMP				0xE9
     6.8 +#define OP_JMP_SIZE			5
     6.9 +
    6.10 +#define OP_NOP				0x90
    6.11 +#define OP_NOP_SIZE			1
    6.12 +
    6.13 +#define OP_PREFIX			0xFF
    6.14 +#define OP_JMP_SEG			0x25
    6.15 +
    6.16 +#define OP_JMP_BYTE			0xEB
    6.17 +#define OP_JMP_BYTE_SIZE	2
    6.18 +
    6.19 +#ifdef __cplusplus
    6.20 +extern "C" {
    6.21 +#endif
    6.22 +
    6.23 +void check_thunks(unsigned char *dest, unsigned char *pc);
    6.24 +
    6.25 +//if dest is NULL, returns minimum number of bytes needed to be copied
    6.26 +//if dest is not NULL, it will copy the bytes to dest as well as fix CALLs and JMPs
    6.27 +//http://www.devmaster.net/forums/showthread.php?t=2311
    6.28 +int copy_bytes(unsigned char *func, unsigned char* dest, int required_len);
    6.29 +
    6.30 +//insert a specific JMP instruction at the given location
    6.31 +void inject_jmp(void* src, void* dest);
    6.32 +
    6.33 +//fill a given block with NOPs
    6.34 +void fill_nop(void* src, unsigned int len);
    6.35 +
    6.36 +//evaluate a JMP at the target
    6.37 +void* eval_jump(void* src);
    6.38 +
    6.39 +#ifdef __cplusplus
    6.40 +}
    6.41 +#endif
    6.42 +
    6.43 +#endif //__ASM_H__
     7.1 --- a/cssdm_detours.cpp	Thu Jun 12 10:18:02 2014 -0400
     7.2 +++ b/cssdm_detours.cpp	Wed Jul 02 13:53:40 2014 -0400
     7.3 @@ -31,14 +31,27 @@
     7.4  #include <sm_platform.h>
     7.5  #include <jit/jit_helpers.h>
     7.6  #include <jit/x86/x86_macros.h>
     7.7 +#include "CDetour/detours.h"
     7.8  
     7.9  dmpatch_t drpwpns_restore;
    7.10  void *drpwpns_address = NULL;
    7.11  void *drpwpns_callback = NULL;
    7.12  
    7.13 -dmpatch_t csdrop_restore;
    7.14 -void *csdrop_address = NULL;
    7.15 -void *csdrop_callback = NULL;
    7.16 +CDetour *csdrop_callback = NULL;
    7.17 +
    7.18 +#if SOURCE_ENGINE == SE_CSGO
    7.19 +DETOUR_DECL_MEMBER3(DetourCSWeaponDrop, void, CBaseEntity *, weapon, Vector, vec, bool, unknown)
    7.20 +#else
    7.21 +DETOUR_DECL_MEMBER3(DetourCSWeaponDrop, void, CBaseEntity *, weapon, bool, bDropShield, bool, bThrowForward)
    7.22 +#endif
    7.23 +{
    7.24 +#if SOURCE_ENGINE == SE_CSGO
    7.25 +		DETOUR_MEMBER_CALL(DetourCSWeaponDrop)(weapon, vec, unknown);
    7.26 +#else
    7.27 +		DETOUR_MEMBER_CALL(DetourCSWeaponDrop)(weapon, bDropShield, bThrowForward);
    7.28 +#endif
    7.29 +		OnClientDroppedWeapon(reinterpret_cast<CBaseEntity *>(this), weapon);
    7.30 +}
    7.31  
    7.32  void DoGatePatch(unsigned char *target, void *callback, dmpatch_t *restore)
    7.33  {
    7.34 @@ -102,94 +115,17 @@
    7.35  
    7.36  void InitCSDropDetour()
    7.37  {
    7.38 -	int offset;
    7.39 -	g_pDmConf->GetMemSig("CSWeaponDrop", &csdrop_address);
    7.40 -	g_pDmConf->GetOffset("CSWeaponDropPatch", &offset);
    7.41 -	csdrop_restore.bytes = offset;
    7.42 -
    7.43 -	/* Patch the gate */
    7.44 -	DoGatePatch((unsigned char *)csdrop_address, &csdrop_callback, &csdrop_restore);
    7.45 -
    7.46 -	/* Create the callback */
    7.47 -	csdrop_callback = spengine->ExecAlloc(100);
    7.48 -	JitWriter wr;
    7.49 -	JitWriter *jit = &wr;
    7.50 -	wr.outbase = (jitcode_t)csdrop_callback;
    7.51 -	wr.outptr = wr.outbase;
    7.52 -
    7.53 -	/* This one is a POST detour instead of a pre-detour!
    7.54 -	 * This means we call first and ask questions later.
    7.55 -	 */
    7.56 -	//push ebp
    7.57 -	//mov ebp, esp
    7.58 -	IA32_Push_Reg(jit, REG_EBP);
    7.59 -	IA32_Mov_Reg_Rm(jit, REG_EBP, REG_ESP, MOD_REG);
    7.60 -
    7.61 -	/* Re-push everything, but setup our next call beforehand. */
    7.62 -#if defined PLATFORM_POSIX
    7.63 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 12);
    7.64 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
    7.65 -	/* Linux pushes four things, it seems */
    7.66 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 20);
    7.67 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 16);
    7.68 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 12);
    7.69 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
    7.70 -#elif defined PLATFORM_WINDOWS
    7.71 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
    7.72 -	IA32_Push_Reg(jit, REG_ECX);
    7.73 -	/* Windows only pushes three because of ECX */
    7.74 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 16);
    7.75 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 12);
    7.76 -	IA32_Push_Rm_Disp8(jit, REG_EBP, 8);
    7.77 -#endif
    7.78 -
    7.79 -	/* Make the ORIGINAL call */
    7.80 -	jitoffs_t orig_call = IA32_Call_Imm32(jit, 0);
    7.81 -
    7.82 -#if defined PLATFORM_POSIX
    7.83 -	IA32_Add_Rm_Imm8(jit, REG_ESP, 16, MOD_REG);
    7.84 -#endif
    7.85 -
    7.86 -	/* Make the POST call */
    7.87 -	//call <function>
    7.88 -	//add esp, 8
    7.89 -	jitoffs_t call = IA32_Call_Imm32(jit, 0);
    7.90 -	IA32_Write_Jump32_Abs(jit, call, (void *)OnClientDroppedWeapon);
    7.91 -
    7.92 -	/* Do cleanup */
    7.93 -#if defined PLATFORM_POSIX
    7.94 -	IA32_Add_Rm_Imm8(jit, REG_ESP, 8, MOD_REG);
    7.95 -#elif defined PLATFORM_WINDOWS
    7.96 -	//this probably isn't needed at all!
    7.97 -	IA32_Pop_Reg(jit, REG_ECX);
    7.98 -	IA32_Add_Rm_Imm8(jit, REG_ESP, 4, MOD_REG);
    7.99 -#endif
   7.100 -
   7.101 -	/* Return */
   7.102 -	//pop ebp
   7.103 -	//ret / retn 12
   7.104 -	IA32_Pop_Reg(jit, REG_EBP);
   7.105 -#if defined PLATFORM_POSIX
   7.106 -	IA32_Return(jit);
   7.107 -#elif defined PLATFORM_WINDOWS
   7.108 -	IA32_Return_Popstack(jit, 0xC);
   7.109 -#endif
   7.110 -
   7.111 -	/* Jump back to the original */
   7.112 -	IA32_Send_Jump32_Here(jit, orig_call);
   7.113 -
   7.114 -	/* Copy original bytes */
   7.115 -	for (size_t i=0; i<csdrop_restore.bytes; i++)
   7.116 +	csdrop_callback = DETOUR_CREATE_MEMBER(DetourCSWeaponDrop, "CSWeaponDrop");
   7.117 +	if(csdrop_callback)
   7.118  	{
   7.119 -		jit->write_ubyte(csdrop_restore.patch[i]);
   7.120 +		csdrop_callback->EnableDetour();
   7.121  	}
   7.122  
   7.123 -	call = IA32_Jump_Imm32(jit, 0);
   7.124 -	IA32_Write_Jump32_Abs(jit, call, (unsigned char *)csdrop_address + csdrop_restore.bytes);
   7.125  }
   7.126  
   7.127  void DM_InitDetours()
   7.128  {
   7.129 +	CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pDmConf);
   7.130  	InitDropWeaponsDetour();
   7.131  	InitCSDropDetour();
   7.132  }
   7.133 @@ -206,10 +142,7 @@
   7.134  	}
   7.135  	if (csdrop_callback)
   7.136  	{
   7.137 -		/* Remove the patch */
   7.138 -		DM_ApplyPatch(csdrop_address, 0, &csdrop_restore, NULL);
   7.139 -		/* Free the gate */
   7.140 -		spengine->ExecFree(csdrop_callback);
   7.141 +		csdrop_callback->Destroy();
   7.142  		csdrop_callback = NULL;
   7.143  	}
   7.144  }
     8.1 --- a/gamedata/cssdm.games.txt	Thu Jun 12 10:18:02 2014 -0400
     8.2 +++ b/gamedata/cssdm.games.txt	Wed Jul 02 13:53:40 2014 -0400
     8.3 @@ -33,8 +33,8 @@
     8.4  			}
     8.5  			"TakeDmgPatch2"
     8.6  			{
     8.7 -				"windows"		"3309"
     8.8 -				"linux"			"5829"
     8.9 +				"windows"		"3272"
    8.10 +				"linux"			"5778"
    8.11  			}
    8.12  			/* Offset into gamerules constructor - bogo value linux/mac */
    8.13  			"g_pGameRules"
    8.14 @@ -112,16 +112,17 @@
    8.15  				"windows"		"\x55\x8B\xEC\x83\xE4\xF8\x81\xEC\x2A\x2A\x2A\x2A\x53\x56\x57\x8B\xD9\x68\x2A\x2A\x2A\x2A\x89\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\xA1"
    8.16  				"linux"			"@g_pGameRules"
    8.17  			}
    8.18 +			//ValveBiped.Bip01_R_Hand
    8.19  			"CSWeaponDrop"
    8.20  			{
    8.21  				"library"		"server"
    8.22 -				"windows"		"\x2A\x2A\x2A\x2A\x2A\x2A\x81\xEC\x2A\x2A\x2A\x2A\x56\x8B\x75\x2A\x57\x6A\x00\x68"
    8.23 -				"linux"			"@_ZN9CCSPlayer12CSWeaponDropEP17CBaseCombatWeaponbb"
    8.24 +				"windows"		"\x2A\x2A\x2A\x2A\x2A\x2A\x81\xEC\x2A\x2A\x2A\x2A\x53\x8B\x5D\x08\x56\x57\x6A\x00\x68"
    8.25 +				"linux"			"@_ZN9CCSPlayer12CSWeaponDropEP17CBaseCombatWeapon6Vectorb"
    8.26  			}
    8.27  			"DropWeapons"
    8.28  			{
    8.29  				"library"		"server"
    8.30 -				"windows"		"\x55\x8B\xEC\x83\xE4\xF8\x83\xEC\x2A\x56\x8B\xF1\x57\x6A\x00\x68\x2A\x2A\x2A\x2A\x8B\x06\x8B\x80"
    8.31 +				"windows"		"\x55\x8B\xEC\x83\xE4\xF8\x83\xEC\x2A\x53\x56\x8B\xF1\x57\x6A\x00\x68\x2A\x2A\x2A\x2A\x8B\x06"		
    8.32  				"linux"			"@_ZN9CCSPlayer11DropWeaponsEbb"
    8.33  			}
    8.34  			"CalcDominationAndRevenge"