1
0
mirror of https://github.com/alliedmodders/metamod-source.git synced 2024-11-28 10:24:20 +01:00

More verbosity, gcc/linux/amd64 compatibility

--HG--
extra : convert_revision : svn%3Ac2935e3e-5518-0410-8daf-afa5dab7d4e3/trunk%4062
This commit is contained in:
Pavol Marko 2005-05-05 14:43:40 +00:00
parent 3c82560621
commit 9a5437c0f1
6 changed files with 87 additions and 26 deletions

View File

@ -54,16 +54,16 @@ namespace SourceHook
# if SH_COMP == SH_COMP_GCC # if SH_COMP == SH_COMP_GCC
template<> struct MFI_Impl<8> // All of these have size==8 template<> struct MFI_Impl<2*SH_PTRSIZE> // All of these have size==8/16
{ {
struct GCC_MemFunPtr struct GCC_MemFunPtr
{ {
union union
{ {
void *funcadr; // always even void *funcadr; // always even
int vtable_index_plus1; // = vindex+1, always odd intptr_t vtable_index_plus1; // = vindex+1, always odd
}; };
int delta; intptr_t delta;
}; };
template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out) template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
{ {
@ -71,7 +71,7 @@ namespace SourceHook
out.thisptroffs = mfp_detail->delta; out.thisptroffs = mfp_detail->delta;
if (mfp_detail->vtable_index_plus1 & 1) if (mfp_detail->vtable_index_plus1 & 1)
{ {
out.vtblindex = (mfp_detail->vtable_index_plus1 - 1) / 4; out.vtblindex = (mfp_detail->vtable_index_plus1 - 1) / SH_PTRSIZE;
out.vtbloffs = 0; out.vtbloffs = 0;
out.isVirtual = true; out.isVirtual = true;
} }
@ -147,7 +147,7 @@ namespace SourceHook
} }
} }
template<> struct MFI_Impl<4> // simple ones template<> struct MFI_Impl<1*SH_PTRSIZE> // simple ones
{ {
template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out) template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
{ {
@ -158,7 +158,7 @@ namespace SourceHook
} }
}; };
template<> struct MFI_Impl<8> // more complicated ones! template<> struct MFI_Impl<2*SH_PTRSIZE> // more complicated ones!
{ {
struct MSVC_MemFunPtr2 struct MSVC_MemFunPtr2
{ {
@ -175,7 +175,7 @@ namespace SourceHook
}; };
// By Don Clugston, adapted // By Don Clugston, adapted
template<> struct MFI_Impl<12> // WOW IT"S GETTING BIGGER OMGOMOGMG template<> struct MFI_Impl<3*SH_PTRSIZE> // WOW IT"S GETTING BIGGER OMGOMOGMG
{ {
class __single_inheritance GenericClass; class __single_inheritance GenericClass;
class GenericClass {}; class GenericClass {};
@ -228,7 +228,7 @@ namespace SourceHook
// unknown_inheritance classes go here // unknown_inheritance classes go here
// This is probably the ugliest bit of code I've ever written. Look at the casts! // This is probably the ugliest bit of code I've ever written. Look at the casts!
// There is a compiler bug in MSVC6 which prevents it from using this code. // There is a compiler bug in MSVC6 which prevents it from using this code.
template<> struct MFI_Impl<16> // THE BIGGEST ONE!!!1GABEN template<> struct MFI_Impl<4*SH_PTRSIZE> // THE BIGGEST ONE!!!1GABEN
{ {
template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out) template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
{ {

View File

@ -63,6 +63,8 @@
# define vsnprintf _vsnprintf # define vsnprintf _vsnprintf
#endif #endif
#define SH_PTRSIZE sizeof(void*)
#include "FastDelegate.h" #include "FastDelegate.h"
#include "sh_memfuncinfo.h" #include "sh_memfuncinfo.h"
#include "sh_memory.h" #include "sh_memory.h"
@ -86,8 +88,8 @@ enum META_RES
namespace SourceHook namespace SourceHook
{ {
const int STRBUF_LEN=8192; // In bytes, for "vafmt" functions const int STRBUF_LEN=4096; // In bytes, for "vafmt" functions
/** /**
* @brief An empty class. No inheritance used. Used for original-function-call hacks * @brief An empty class. No inheritance used. Used for original-function-call hacks
*/ */
@ -677,8 +679,9 @@ inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::C
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// SH_CALL // SH_CALL
#if SH_COMP == SH_COMP_MSVC
#define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \ # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
{ \ { \
using namespace ::SourceHook; \ using namespace ::SourceHook; \
MemFuncInfo mfi; \ MemFuncInfo mfi; \
@ -699,6 +702,34 @@ inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::C
return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \ return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
} }
#elif SH_COMP == SH_COMP_GCC
# define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
{ \
using namespace ::SourceHook; \
MemFuncInfo mfi; \
GetFuncInfo(m_CC->ptr, m_MFP, mfi); \
OrigVTables::const_iterator iter = m_CC->vt.find(mfi.thisptroffs + mfi.vtbloffs); \
if (iter == m_CC->vt.end() || mfi.vtblindex >= (int)iter->second.size() || iter->second[mfi.vtblindex] == NULL) \
return (m_CC->ptr->*m_MFP)call; \
\
/* It's hooked. Call the original function. */ \
union \
{ \
RetType(EmptyClass::*mfpnew)prms; \
struct \
{ \
void *addr; \
intptr_t adjustor; \
} s; \
} u; \
u.s.addr = iter->second[mfi.vtblindex]; \
u.s.adjustor = mfi.thisptroffs; \
\
return (reinterpret_cast<EmptyClass*>(m_CC->ptr)->*u.mfpnew)call; \
}
#endif
namespace SourceHook namespace SourceHook
{ {

View File

@ -54,16 +54,16 @@ namespace SourceHook
# if SH_COMP == SH_COMP_GCC # if SH_COMP == SH_COMP_GCC
template<> struct MFI_Impl<8> // All of these have size==8 template<> struct MFI_Impl<2*SH_PTRSIZE> // All of these have size==8/16
{ {
struct GCC_MemFunPtr struct GCC_MemFunPtr
{ {
union union
{ {
void *funcadr; // always even void *funcadr; // always even
int vtable_index_plus1; // = vindex+1, always odd intptr_t vtable_index_plus1; // = vindex+1, always odd
}; };
int delta; intptr_t delta;
}; };
template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out) template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
{ {
@ -71,7 +71,7 @@ namespace SourceHook
out.thisptroffs = mfp_detail->delta; out.thisptroffs = mfp_detail->delta;
if (mfp_detail->vtable_index_plus1 & 1) if (mfp_detail->vtable_index_plus1 & 1)
{ {
out.vtblindex = (mfp_detail->vtable_index_plus1 - 1) / 4; out.vtblindex = (mfp_detail->vtable_index_plus1 - 1) / SH_PTRSIZE;
out.vtbloffs = 0; out.vtbloffs = 0;
out.isVirtual = true; out.isVirtual = true;
} }
@ -147,7 +147,7 @@ namespace SourceHook
} }
} }
template<> struct MFI_Impl<4> // simple ones template<> struct MFI_Impl<1*SH_PTRSIZE> // simple ones
{ {
template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out) template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
{ {
@ -158,7 +158,7 @@ namespace SourceHook
} }
}; };
template<> struct MFI_Impl<8> // more complicated ones! template<> struct MFI_Impl<2*SH_PTRSIZE> // more complicated ones!
{ {
struct MSVC_MemFunPtr2 struct MSVC_MemFunPtr2
{ {
@ -175,7 +175,7 @@ namespace SourceHook
}; };
// By Don Clugston, adapted // By Don Clugston, adapted
template<> struct MFI_Impl<12> // WOW IT"S GETTING BIGGER OMGOMOGMG template<> struct MFI_Impl<3*SH_PTRSIZE> // WOW IT"S GETTING BIGGER OMGOMOGMG
{ {
class __single_inheritance GenericClass; class __single_inheritance GenericClass;
class GenericClass {}; class GenericClass {};
@ -228,7 +228,7 @@ namespace SourceHook
// unknown_inheritance classes go here // unknown_inheritance classes go here
// This is probably the ugliest bit of code I've ever written. Look at the casts! // This is probably the ugliest bit of code I've ever written. Look at the casts!
// There is a compiler bug in MSVC6 which prevents it from using this code. // There is a compiler bug in MSVC6 which prevents it from using this code.
template<> struct MFI_Impl<16> // THE BIGGEST ONE!!!1GABEN template<> struct MFI_Impl<4*SH_PTRSIZE> // THE BIGGEST ONE!!!1GABEN
{ {
template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out) template<class MFP> static inline void GetFuncInfo(MFP mfp, MemFuncInfo &out)
{ {

View File

@ -34,8 +34,8 @@
// We need to align addr down to pagesize on linux // We need to align addr down to pagesize on linux
// We assume PAGESIZE is a power of two // We assume PAGESIZE is a power of two
# define SH_LALIGN(x) (void*)((int)(x) & ~(PAGESIZE-1)) # define SH_LALIGN(x) (void*)((intptr_t)(x) & ~(PAGESIZE-1))
# define SH_LALDIF(x) ((int)(x) & (PAGESIZE-1)) # define SH_LALDIF(x) ((intptr_t)(x) & (PAGESIZE-1))
# else # else
# error Unsupported OS/Compiler # error Unsupported OS/Compiler
# endif # endif

View File

@ -63,6 +63,8 @@
# define vsnprintf _vsnprintf # define vsnprintf _vsnprintf
#endif #endif
#define SH_PTRSIZE sizeof(void*)
#include "FastDelegate.h" #include "FastDelegate.h"
#include "sh_memfuncinfo.h" #include "sh_memfuncinfo.h"
#include "sh_memory.h" #include "sh_memory.h"
@ -86,8 +88,8 @@ enum META_RES
namespace SourceHook namespace SourceHook
{ {
const int STRBUF_LEN=8192; // In bytes, for "vafmt" functions const int STRBUF_LEN=4096; // In bytes, for "vafmt" functions
/** /**
* @brief An empty class. No inheritance used. Used for original-function-call hacks * @brief An empty class. No inheritance used. Used for original-function-call hacks
*/ */
@ -1491,8 +1493,9 @@ inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::C
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// SH_CALL // SH_CALL
#if SH_COMP == SH_COMP_MSVC
#define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \ # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
{ \ { \
using namespace ::SourceHook; \ using namespace ::SourceHook; \
MemFuncInfo mfi; \ MemFuncInfo mfi; \
@ -1513,6 +1516,34 @@ inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::C
return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \ return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
} }
#elif SH_COMP == SH_COMP_GCC
# define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
{ \
using namespace ::SourceHook; \
MemFuncInfo mfi; \
GetFuncInfo(m_CC->ptr, m_MFP, mfi); \
OrigVTables::const_iterator iter = m_CC->vt.find(mfi.thisptroffs + mfi.vtbloffs); \
if (iter == m_CC->vt.end() || mfi.vtblindex >= (int)iter->second.size() || iter->second[mfi.vtblindex] == NULL) \
return (m_CC->ptr->*m_MFP)call; \
\
/* It's hooked. Call the original function. */ \
union \
{ \
RetType(EmptyClass::*mfpnew)prms; \
struct \
{ \
void *addr; \
intptr_t adjustor; \
} s; \
} u; \
u.s.addr = iter->second[mfi.vtblindex]; \
u.s.adjustor = mfi.thisptroffs; \
\
return (reinterpret_cast<EmptyClass*>(m_CC->ptr)->*u.mfpnew)call; \
}
#endif
namespace SourceHook namespace SourceHook
{ {

View File

@ -100,8 +100,7 @@ namespace
} }
} }
//#define CHECK_STATES(mwah, myerr) if (!StatesOk mwah) { error=myerr; return false; } else if (g_Verbose) { std::cout << "No error: " << myerr << std::endl; } #define CHECK_STATES(mwah, myerr) if (!StatesOk mwah) { error=myerr; return false; } else if (g_Verbose) { std::cout << "No error: " << myerr << std::endl; }
#define CHECK_STATES(mwah, myerr) if (!StatesOk mwah) { error=myerr; return false; }
#define MAKE_STATE(name) struct name : State { \ #define MAKE_STATE(name) struct name : State { \
virtual void Dump() { \ virtual void Dump() { \