CDetour/detours.h
author Ruben Gonzalez <drifter01620@gmail.com>
Wed Nov 12 08:29:24 2014 -0500 (2014-11-12)
changeset 197 fd967f09ab68
parent 184 7ddf645a50d5
permissions -rw-r--r--
Update CSGO Gamedata
     1 /**
     2 * vim: set ts=4 :
     3 * =============================================================================
     4 * SourceMod
     5 * Copyright (C) 2004-2010 AlliedModders LLC.  All rights reserved.
     6 * =============================================================================
     7 *
     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.
    11 * 
    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
    15 * details.
    16 *
    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/>.
    19 *
    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>.
    28 *
    29 * Version: $Id: detours.h 257 2008-09-23 03:12:13Z pred $
    30 */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_DETOURS_H_
    33 #define _INCLUDE_SOURCEMOD_DETOURS_H_
    34 
    35 #include "../cssdm_main.h"
    36 #include <jit/jit_helpers.h>
    37 #include <jit/x86/x86_macros.h>
    38 #include "detourhelpers.h"
    39 
    40 /**
    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)
    46  */
    47 
    48 #define DETOUR_MEMBER_CALL(name) (this->*name##_Actual)
    49 #define DETOUR_STATIC_CALL(name) (name##_Actual)
    50 
    51 #define DETOUR_DECL_STATIC0(name, ret) \
    52 ret (*name##_Actual)(void) = NULL; \
    53 ret name(void)
    54 
    55 #define DETOUR_DECL_STATIC1(name, ret, p1type, p1name) \
    56 ret (*name##_Actual)(p1type) = NULL; \
    57 ret name(p1type p1name)
    58 
    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)
    62 
    63 #define DETOUR_DECL_MEMBER0(name, ret) \
    64 class name##Class \
    65 { \
    66 public: \
    67 	ret name(); \
    68 	static ret (name##Class::* name##_Actual)(void); \
    69 }; \
    70 ret (name##Class::* name##Class::name##_Actual)(void) = NULL; \
    71 ret name##Class::name()
    72 
    73 #define DETOUR_DECL_MEMBER1(name, ret, p1type, p1name) \
    74 class name##Class \
    75 { \
    76 public: \
    77 	ret name(p1type p1name); \
    78 	static ret (name##Class::* name##_Actual)(p1type); \
    79 }; \
    80 ret (name##Class::* name##Class::name##_Actual)(p1type) = NULL; \
    81 ret name##Class::name(p1type p1name)
    82 
    83 #define DETOUR_DECL_MEMBER2(name, ret, p1type, p1name, p2type, p2name) \
    84 class name##Class \
    85 { \
    86 public: \
    87 	ret name(p1type p1name, p2type p2name); \
    88 	static ret (name##Class::* name##_Actual)(p1type, p2type); \
    89 }; \
    90 ret (name##Class::* name##Class::name##_Actual)(p1type, p2type) = NULL; \
    91 ret name##Class::name(p1type p1name, p2type p2name)
    92 
    93 #define DETOUR_DECL_MEMBER3(name, ret, p1type, p1name, p2type, p2name, p3type, p3name) \
    94 class name##Class \
    95 { \
    96 public: \
    97 	ret name(p1type p1name, p2type p2name, p3type p3name); \
    98 	static ret (name##Class::* name##_Actual)(p1type, p2type, p3type); \
    99 }; \
   100 ret (name##Class::* name##Class::name##_Actual)(p1type, p2type, p3type) = NULL; \
   101 ret name##Class::name(p1type p1name, p2type p2name, p3type p3name)
   102 
   103 #define DETOUR_DECL_MEMBER4(name, ret, p1type, p1name, p2type, p2name, p3type, p3name, p4type, p4name) \
   104 class name##Class \
   105 { \
   106 public: \
   107         ret name(p1type p1name, p2type p2name, p3type p3name, p4type p4name); \
   108         static ret (name##Class::* name##_Actual)(p1type, p2type, p3type, p4type); \
   109 }; \
   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)
   112 
   113 #define DETOUR_DECL_MEMBER8(name, ret, p1type, p1name, p2type, p2name, p3type, p3name, p4type, p4name, p5type, p5name, p6type, p6name, p7type, p7name, p8type, p8name) \
   114 class name##Class \
   115 { \
   116 public: \
   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); \
   119 }; \
   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)
   122 
   123 
   124 #define GET_MEMBER_CALLBACK(name) (void *)GetCodeAddress(&name##Class::name)
   125 #define GET_MEMBER_TRAMPOLINE(name) (void **)(&name##Class::name##_Actual)
   126 
   127 #define GET_STATIC_CALLBACK(name) (void *)&name
   128 #define GET_STATIC_TRAMPOLINE(name) (void **)&name##_Actual
   129 
   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);
   132 
   133 
   134 class GenericClass {};
   135 typedef void (GenericClass::*VoidFunc)();
   136 
   137 inline void *GetCodeAddr(VoidFunc mfp)
   138 {
   139 	return *(void **)&mfp;
   140 }
   141 
   142 /**
   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
   147  */
   148 #define GetCodeAddress(mfp) GetCodeAddr(reinterpret_cast<VoidFunc>(mfp))
   149 
   150 class CDetourManager;
   151 
   152 class CDetour
   153 {
   154 public:
   155 
   156 	bool IsEnabled();
   157 
   158 	/**
   159 	 * These would be somewhat self-explanatory I hope
   160 	 */
   161 	void EnableDetour();
   162 	void DisableDetour();
   163 
   164 	void Destroy();
   165 
   166 	friend class CDetourManager;
   167 
   168 protected:
   169 	CDetour(void *callbackfunction, void **trampoline, const char *signame);
   170 
   171 	bool Init(ISourcePawnEngine *spengine, IGameConfig *gameconf);
   172 private:
   173 
   174 	/* These create/delete the allocated memory */
   175 	bool CreateDetour();
   176 	void DeleteDetour();
   177 
   178 	bool enabled;
   179 	bool detoured;
   180 
   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 */
   189 	void **trampoline;
   190 	
   191 	const char *signame;
   192 	ISourcePawnEngine *spengine;
   193 	IGameConfig *gameconf;
   194 };
   195 
   196 class CDetourManager
   197 {
   198 public:
   199 
   200 	static void Init(ISourcePawnEngine *spengine, IGameConfig *gameconf);
   201 
   202 	/**
   203 	 * Creates a new detour
   204 	 *
   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.
   209 	 *
   210 	 * Example:
   211 	 *
   212 	 * CBaseServer::ConnectClient(netadr_s &, int, int, int, char  const*, char  const*, char  const*, int)
   213 	 *
   214 	 * Define a new class with the required function and a member function pointer to the same type:
   215 	 *
   216 	 * class CBaseServerDetour
   217 	 * {
   218 	 * public:
   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);
   221 	 * }
   222 	 *
   223 	 *	void *callbackfunc = GetCodeAddress(&CBaseServerDetour::ConnectClient);
   224 	 *	void **trampoline = (void **)(&CBaseServerDetour::ConnectClient_Actual);
   225 	 *
   226 	 * Creation:
   227 	 * CDetourManager::CreateDetour(callbackfunc,  trampoline, "ConnectClient");
   228 	 *
   229 	 * Usage:
   230 	 *
   231 	 * CBaseServerDetour::ConnectClient(void *netaddr_s, int, int, int, char  const*, char  const*, char  const*, int)
   232 	 * {
   233 	 *			//pre hook code
   234 	 *			bool result = (this->*ConnectClient_Actual)(netaddr_s, rest of params);
   235 	 *			//post hook code
   236 	 *			return result;
   237 	 * }
   238 	 *
   239 	 * Note we changed the netadr_s reference into a void* to avoid needing to define the type
   240 	 */
   241 	static CDetour *CreateDetour(void *callbackfunction, void **trampoline, const char *signame);
   242 
   243 	friend class CBlocker;
   244 	friend class CDetour;
   245 
   246 private:
   247 	static ISourcePawnEngine *spengine;
   248 	static IGameConfig *gameconf;
   249 };
   250 
   251 #endif // _INCLUDE_SOURCEMOD_DETOURS_H_