3 * =============================================================================
5 * Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
29 * Version: $Id: detours.h 257 2008-09-23 03:12:13Z pred $
32 #ifndef _INCLUDE_SOURCEMOD_DETOURS_H_
33 #define _INCLUDE_SOURCEMOD_DETOURS_H_
35 #include "../cssdm_main.h"
36 #include <jit/jit_helpers.h>
37 #include <jit/x86/x86_macros.h>
38 #include "detourhelpers.h"
41 * CDetours class for SourceMod Extensions by pRED*
42 * detourhelpers.h entirely stolen from CSS:DM and were written by BAILOPAN (I assume).
43 * asm.h/c from devmaster.net (thanks cybermind) edited by pRED* to handle gcc -fPIC thunks correctly
44 * Concept by Nephyrin Zey (http://www.doublezen.net/) and Windows Detour Library (http://research.microsoft.com/sn/detours/)
45 * Member function pointer ideas by Don Clugston (http://www.codeproject.com/cpp/FastDelegate.asp)
48 #define DETOUR_MEMBER_CALL(name) (this->*name##_Actual)
49 #define DETOUR_STATIC_CALL(name) (name##_Actual)
51 #define DETOUR_DECL_STATIC0(name, ret) \
52 ret (*name##_Actual)(void) = NULL; \
55 #define DETOUR_DECL_STATIC1(name, ret, p1type, p1name) \
56 ret (*name##_Actual)(p1type) = NULL; \
57 ret name(p1type p1name)
59 #define DETOUR_DECL_STATIC4(name, ret, p1type, p1name, p2type, p2name, p3type, p3name, p4type, p4name) \
60 ret (*name##_Actual)(p1type, p2type, p3type, p4type) = NULL; \
61 ret name(p1type p1name, p2type p2name, p3type p3name, p4type p4name)
63 #define DETOUR_DECL_MEMBER0(name, ret) \
68 static ret (name##Class::* name##_Actual)(void); \
70 ret (name##Class::* name##Class::name##_Actual)(void) = NULL; \
71 ret name##Class::name()
73 #define DETOUR_DECL_MEMBER1(name, ret, p1type, p1name) \
77 ret name(p1type p1name); \
78 static ret (name##Class::* name##_Actual)(p1type); \
80 ret (name##Class::* name##Class::name##_Actual)(p1type) = NULL; \
81 ret name##Class::name(p1type p1name)
83 #define DETOUR_DECL_MEMBER2(name, ret, p1type, p1name, p2type, p2name) \
87 ret name(p1type p1name, p2type p2name); \
88 static ret (name##Class::* name##_Actual)(p1type, p2type); \
90 ret (name##Class::* name##Class::name##_Actual)(p1type, p2type) = NULL; \
91 ret name##Class::name(p1type p1name, p2type p2name)
93 #define DETOUR_DECL_MEMBER3(name, ret, p1type, p1name, p2type, p2name, p3type, p3name) \
97 ret name(p1type p1name, p2type p2name, p3type p3name); \
98 static ret (name##Class::* name##_Actual)(p1type, p2type, p3type); \
100 ret (name##Class::* name##Class::name##_Actual)(p1type, p2type, p3type) = NULL; \
101 ret name##Class::name(p1type p1name, p2type p2name, p3type p3name)
103 #define DETOUR_DECL_MEMBER4(name, ret, p1type, p1name, p2type, p2name, p3type, p3name, p4type, p4name) \
107 ret name(p1type p1name, p2type p2name, p3type p3name, p4type p4name); \
108 static ret (name##Class::* name##_Actual)(p1type, p2type, p3type, p4type); \
110 ret (name##Class::* name##Class::name##_Actual)(p1type, p2type, p3type, p4type) = NULL; \
111 ret name##Class::name(p1type p1name, p2type p2name, p3type p3name, p4type p4name)
113 #define DETOUR_DECL_MEMBER8(name, ret, p1type, p1name, p2type, p2name, p3type, p3name, p4type, p4name, p5type, p5name, p6type, p6name, p7type, p7name, p8type, p8name) \
117 ret name(p1type p1name, p2type p2name, p3type p3name, p4type p4name, p5type p5name, p6type p6name, p7type p7name, p8type p8name); \
118 static ret (name##Class::* name##_Actual)(p1type, p2type, p3type, p4type, p5type, p6type, p7type, p8type); \
120 ret (name##Class::* name##Class::name##_Actual)(p1type, p2type, p3type, p4type, p5type, p6type, p7type, p8type) = NULL; \
121 ret name##Class::name(p1type p1name, p2type p2name, p3type p3name, p4type p4name, p5type p5name, p6type p6name, p7type p7name, p8type p8name)
124 #define GET_MEMBER_CALLBACK(name) (void *)GetCodeAddress(&name##Class::name)
125 #define GET_MEMBER_TRAMPOLINE(name) (void **)(&name##Class::name##_Actual)
127 #define GET_STATIC_CALLBACK(name) (void *)&name
128 #define GET_STATIC_TRAMPOLINE(name) (void **)&name##_Actual
130 #define DETOUR_CREATE_MEMBER(name, gamedata) CDetourManager::CreateDetour(GET_MEMBER_CALLBACK(name), GET_MEMBER_TRAMPOLINE(name), gamedata);
131 #define DETOUR_CREATE_STATIC(name, gamedata) CDetourManager::CreateDetour(GET_STATIC_CALLBACK(name), GET_STATIC_TRAMPOLINE(name), gamedata);
134 class GenericClass {};
135 typedef void (GenericClass::*VoidFunc)();
137 inline void *GetCodeAddr(VoidFunc mfp)
139 return *(void **)&mfp;
143 * Converts a member function pointer to a void pointer.
144 * This relies on the assumption that the code address lies at mfp+0
145 * This is the case for both g++ and later MSVC versions on non virtual functions but may be different for other compilers
146 * Based on research by Don Clugston : http://www.codeproject.com/cpp/FastDelegate.asp
148 #define GetCodeAddress(mfp) GetCodeAddr(reinterpret_cast<VoidFunc>(mfp))
150 class CDetourManager;
159 * These would be somewhat self-explanatory I hope
162 void DisableDetour();
166 friend class CDetourManager;
169 CDetour(void *callbackfunction, void **trampoline, const char *signame);
171 bool Init(ISourcePawnEngine *spengine, IGameConfig *gameconf);
174 /* These create/delete the allocated memory */
181 patch_t detour_restore;
182 /* Address of the detoured function */
183 void *detour_address;
184 /* Address of the allocated trampoline function */
185 void *detour_trampoline;
186 /* Address of the callback handler */
187 void *detour_callback;
188 /* The function pointer used to call our trampoline */
192 ISourcePawnEngine *spengine;
193 IGameConfig *gameconf;
200 static void Init(ISourcePawnEngine *spengine, IGameConfig *gameconf);
203 * Creates a new detour
205 * @param callbackfunction Void pointer to your detour callback function.
206 * @param trampoline Address of the trampoline pointer
207 * @param signame Section name containing a signature to fetch from the gamedata file.
208 * @return A new CDetour pointer to control your detour.
212 * CBaseServer::ConnectClient(netadr_s &, int, int, int, char const*, char const*, char const*, int)
214 * Define a new class with the required function and a member function pointer to the same type:
216 * class CBaseServerDetour
219 * bool ConnectClient(void *netaddr_s, int, int, int, char const*, char const*, char const*, int);
220 * static bool (CBaseServerDetour::* ConnectClient_Actual)(void *netaddr_s, int, int, int, char const*, char const*, char const*, int);
223 * void *callbackfunc = GetCodeAddress(&CBaseServerDetour::ConnectClient);
224 * void **trampoline = (void **)(&CBaseServerDetour::ConnectClient_Actual);
227 * CDetourManager::CreateDetour(callbackfunc, trampoline, "ConnectClient");
231 * CBaseServerDetour::ConnectClient(void *netaddr_s, int, int, int, char const*, char const*, char const*, int)
234 * bool result = (this->*ConnectClient_Actual)(netaddr_s, rest of params);
239 * Note we changed the netadr_s reference into a void* to avoid needing to define the type
241 static CDetour *CreateDetour(void *callbackfunction, void **trampoline, const char *signame);
243 friend class CBlocker;
244 friend class CDetour;
247 static ISourcePawnEngine *spengine;
248 static IGameConfig *gameconf;
251 #endif // _INCLUDE_SOURCEMOD_DETOURS_H_