2005-04-14 14:54:13 +02:00
|
|
|
/* ======== SourceHook ========
|
|
|
|
* By PM
|
|
|
|
* No warranties of any kind
|
|
|
|
* ============================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file sourcehook.h
|
|
|
|
* @brief Contains the public SourceHook API
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __SOURCEHOOK_H__
|
|
|
|
#define __SOURCEHOOK_H__
|
|
|
|
|
2005-04-16 18:55:53 +02:00
|
|
|
#ifndef SH_GLOB_SHPTR
|
|
|
|
#define SH_GLOB_SHPTR g_SHPtr
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SH_GLOB_PLUGPTR
|
2005-04-16 22:26:33 +02:00
|
|
|
#define SH_GLOB_PLUGPTR g_PLID
|
2005-04-16 18:55:53 +02:00
|
|
|
#endif
|
|
|
|
|
2005-05-01 14:36:48 +02:00
|
|
|
#ifdef SH_DEBUG
|
|
|
|
# define SH_ASSERT__(x, info, file, line, func) \
|
|
|
|
((printf("SOURCEHOOK DEBUG ASSERTION FAILED:\n %s:%u(%s): %s\n", file, line, func, info), true) ? (abort(), 0) : 0)
|
|
|
|
# define SH_ASSERT(x, info) if (!(x)) SH_ASSERT__(x, info, __FILE__, __LINE__, __FUNCTION__)
|
|
|
|
#else
|
|
|
|
# define SH_ASSERT(x, info)
|
|
|
|
#endif
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
// System
|
|
|
|
#define SH_SYS_WIN32 1
|
|
|
|
#define SH_SYS_LINUX 2
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
# define SH_SYS SH_SYS_WIN32
|
|
|
|
#elif defined __linux__
|
|
|
|
# define SH_SYS SH_SYS_LINUX
|
|
|
|
#else
|
|
|
|
# error Unsupported system
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Compiler
|
|
|
|
#define SH_COMP_GCC 1
|
|
|
|
#define SH_COMP_MSVC 2
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
# define SH_COMP SH_COMP_MSVC
|
|
|
|
#elif defined __GNUC__
|
|
|
|
# define SH_COMP SH_COMP_GCC
|
|
|
|
#else
|
|
|
|
# error Unsupported compiler
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if SH_COMP==SH_COMP_MSVC
|
|
|
|
# define vsnprintf _vsnprintf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "FastDelegate.h"
|
|
|
|
#include "sh_memfuncinfo.h"
|
|
|
|
#include "sh_memory.h"
|
|
|
|
#include <list>
|
2005-04-29 22:29:46 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
2005-04-14 14:54:13 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
// Good old metamod!
|
|
|
|
|
|
|
|
// Flags returned by a plugin's api function.
|
|
|
|
// NOTE: order is crucial, as greater/less comparisons are made.
|
|
|
|
enum META_RES
|
|
|
|
{
|
|
|
|
MRES_IGNORED=0, // plugin didn't take any action
|
|
|
|
MRES_HANDLED, // plugin did something, but real function should still be called
|
|
|
|
MRES_OVERRIDE, // call real function, but use my return value
|
2005-05-01 14:36:48 +02:00
|
|
|
MRES_SUPERCEDE // skip real function; use my return value
|
2005-04-14 14:54:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
namespace SourceHook
|
|
|
|
{
|
|
|
|
const int STRBUF_LEN=8192; // In bytes, for "vafmt" functions
|
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
/**
|
|
|
|
* @brief An empty class. No inheritance used. Used for original-function-call hacks
|
|
|
|
*/
|
|
|
|
class EmptyClass
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2005-04-14 14:54:13 +02:00
|
|
|
/**
|
|
|
|
* @brief A plugin typedef
|
|
|
|
*
|
|
|
|
* SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it
|
|
|
|
* and every plugin has a unique identifier, everything is ok.
|
|
|
|
*/
|
2005-04-16 22:26:33 +02:00
|
|
|
typedef int Plugin;
|
2005-04-14 14:54:13 +02:00
|
|
|
|
2005-04-16 18:55:53 +02:00
|
|
|
enum HookManagerAction
|
2005-04-14 14:54:13 +02:00
|
|
|
{
|
|
|
|
HA_GetInfo = 0, // -> Only store info
|
|
|
|
HA_Register, // -> Save the pointer for future reference
|
2005-04-16 18:55:53 +02:00
|
|
|
HA_Unregister // -> Clear the saved pointer
|
2005-04-14 14:54:13 +02:00
|
|
|
};
|
|
|
|
|
2005-04-16 18:55:53 +02:00
|
|
|
struct HookManagerInfo;
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
/**
|
2005-04-16 18:55:53 +02:00
|
|
|
* @brief Pointer to hook manager type
|
2005-04-14 14:54:13 +02:00
|
|
|
*
|
2005-04-16 18:55:53 +02:00
|
|
|
* A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
|
2005-04-14 14:54:13 +02:00
|
|
|
*
|
2005-04-16 18:55:53 +02:00
|
|
|
* @param hi A pointer to a HookManagerInfo structure. The hook manager should fill it and store it for
|
2005-04-14 14:54:13 +02:00
|
|
|
* future reference (mainly if something should get hooked to its hookfunc)
|
|
|
|
*/
|
2005-04-16 18:55:53 +02:00
|
|
|
typedef int (*HookManagerPubFunc)(HookManagerAction ha, HookManagerInfo *hi);
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
class ISHDelegate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void DeleteThis() = 0; // Ugly, I know
|
|
|
|
virtual bool IsEqual(ISHDelegate *other) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T> class CSHDelegate : public ISHDelegate
|
|
|
|
{
|
|
|
|
T m_Deleg;
|
|
|
|
public:
|
|
|
|
CSHDelegate(T deleg) : m_Deleg(deleg)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-05-01 11:55:51 +02:00
|
|
|
CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-04-14 14:54:13 +02:00
|
|
|
void DeleteThis()
|
|
|
|
{
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEqual(ISHDelegate *other)
|
|
|
|
{
|
|
|
|
return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
|
|
|
|
}
|
|
|
|
|
|
|
|
T &GetDeleg()
|
|
|
|
{
|
|
|
|
return m_Deleg;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2005-04-16 18:55:53 +02:00
|
|
|
* @brief This structure contains information about a hook manager (hook manager)
|
2005-04-14 14:54:13 +02:00
|
|
|
*/
|
2005-04-16 18:55:53 +02:00
|
|
|
struct HookManagerInfo
|
2005-04-14 14:54:13 +02:00
|
|
|
{
|
2005-04-29 22:29:46 +02:00
|
|
|
struct VfnPtr
|
2005-04-14 14:54:13 +02:00
|
|
|
{
|
2005-04-29 22:29:46 +02:00
|
|
|
struct Iface
|
2005-04-14 14:54:13 +02:00
|
|
|
{
|
2005-04-29 22:29:46 +02:00
|
|
|
struct Hook
|
|
|
|
{
|
|
|
|
ISHDelegate *handler; //!< Pointer to the handler
|
|
|
|
bool paused; //!< If true, the hook should not be executed
|
|
|
|
Plugin plug; //!< The owner plugin
|
|
|
|
};
|
|
|
|
void *ptr; //!< Pointer to the interface instance
|
|
|
|
std::list<Hook> hooks_pre; //!< A list of pre-hooks
|
|
|
|
std::list<Hook> hooks_post; //!< A list of post-hooks
|
|
|
|
bool operator ==(void *other) const
|
|
|
|
{
|
|
|
|
return ptr == other;
|
|
|
|
}
|
2005-04-14 14:54:13 +02:00
|
|
|
};
|
2005-04-29 22:29:46 +02:00
|
|
|
|
|
|
|
void *vfnptr; //!< Pointer to the function
|
2005-05-01 11:55:51 +02:00
|
|
|
void *orig_entry; //!< The original vtable entry
|
2005-04-29 22:29:46 +02:00
|
|
|
|
|
|
|
typedef std::list<Iface> IfaceList;
|
|
|
|
typedef IfaceList::iterator IfaceListIter;
|
2005-05-01 11:55:51 +02:00
|
|
|
IfaceList ifaces; //!< List of interface pointers
|
2005-04-29 22:29:46 +02:00
|
|
|
|
|
|
|
bool operator ==(void *other)
|
2005-04-14 14:54:13 +02:00
|
|
|
{
|
2005-04-29 22:29:46 +02:00
|
|
|
return vfnptr == other;
|
2005-04-14 14:54:13 +02:00
|
|
|
}
|
|
|
|
};
|
2005-05-01 11:55:51 +02:00
|
|
|
|
2005-04-14 14:54:13 +02:00
|
|
|
Plugin plug; //!< The owner plugin
|
2005-04-16 18:55:53 +02:00
|
|
|
const char *proto; //!< The prototype of the function the hook manager is responsible for
|
2005-04-14 14:54:13 +02:00
|
|
|
int vtbl_idx; //!< The vtable index
|
|
|
|
int vtbl_offs; //!< The vtable offset
|
2005-04-16 18:55:53 +02:00
|
|
|
HookManagerPubFunc func; //!< The interface to the hook manager
|
2005-04-14 14:54:13 +02:00
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
void *hookfunc_vfnptr; //!< Pointer to the hookfunc impl
|
2005-04-14 14:54:13 +02:00
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
typedef std::list<VfnPtr> VfnPtrList;
|
|
|
|
typedef VfnPtrList::iterator VfnPtrListIter;
|
|
|
|
VfnPtrList vfnptrs; //!< List of hooked interfaces
|
2005-04-14 14:54:13 +02:00
|
|
|
};
|
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
typedef std::vector<void*> OrigFuncs;
|
|
|
|
typedef std::map<int, OrigFuncs> OrigVTables;
|
|
|
|
|
|
|
|
template<class B> struct CallClass
|
2005-04-14 14:54:13 +02:00
|
|
|
{
|
2005-04-29 22:29:46 +02:00
|
|
|
B *ptr;
|
|
|
|
OrigVTables vt;
|
2005-04-14 14:54:13 +02:00
|
|
|
};
|
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
typedef CallClass<void> GenericCallClass;
|
|
|
|
|
2005-04-14 14:54:13 +02:00
|
|
|
/**
|
|
|
|
* @brief The main SourceHook interface
|
|
|
|
*/
|
|
|
|
class ISourceHook
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @brief Add a hook.
|
|
|
|
*
|
|
|
|
* @return True if the function succeeded, false otherwise
|
|
|
|
*
|
|
|
|
* @param plug The unique identifier of the plugin that calls this function
|
|
|
|
* @param iface The interface pointer
|
|
|
|
* @param ifacesize The size of the class iface points to
|
2005-04-16 18:55:53 +02:00
|
|
|
* @param myHookMan A hook manager function that should be capable of handling the function
|
2005-04-14 14:54:13 +02:00
|
|
|
* @param handler A pointer to a FastDelegate containing the hook handler
|
|
|
|
* @param post Set to true if you want a post handler
|
|
|
|
*/
|
2005-04-29 22:29:46 +02:00
|
|
|
virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Removes a hook.
|
|
|
|
*
|
|
|
|
* @return True if the function succeeded, false otherwise
|
|
|
|
*
|
|
|
|
* @param plug The unique identifier of the plugin that calls this function
|
|
|
|
* @param iface The interface pointer
|
2005-04-16 18:55:53 +02:00
|
|
|
* @param myHookMan A hook manager function that should be capable of handling the function
|
2005-04-14 14:54:13 +02:00
|
|
|
* @param handler A pointer to a FastDelegate containing the hook handler
|
|
|
|
* @param post Set to true if you want a post handler
|
|
|
|
*/
|
2005-04-29 22:29:46 +02:00
|
|
|
virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
/**
|
2005-04-16 18:55:53 +02:00
|
|
|
* @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
|
2005-04-14 14:54:13 +02:00
|
|
|
*
|
|
|
|
* @param plug The unique identifier of the plugin in question
|
|
|
|
*/
|
|
|
|
virtual bool IsPluginInUse(Plugin plug) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return a pointer to a callclass. Generate a new one if required.
|
|
|
|
*
|
|
|
|
* @param iface The interface pointer
|
|
|
|
* @param size Size of the class
|
|
|
|
*/
|
2005-04-29 22:29:46 +02:00
|
|
|
virtual GenericCallClass *GetCallClass(void *iface) = 0;
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Release a callclass
|
|
|
|
*
|
|
|
|
* @param ptr Pointer to the callclass
|
|
|
|
*/
|
2005-04-29 22:29:46 +02:00
|
|
|
virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
|
|
|
|
virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the previously called handler
|
|
|
|
virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
|
|
|
|
virtual const void *GetOrigRet() = 0; //!< Gets the original result. If not in post function, undefined
|
|
|
|
virtual const void *GetOverrideRet() = 0; //!< Gets the override result. If none is specified, NULL
|
|
|
|
virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2005-04-16 18:55:53 +02:00
|
|
|
// For hook managers
|
2005-04-14 14:54:13 +02:00
|
|
|
virtual META_RES &GetCurResRef() = 0; //!< Gets the pointer to the current meta result
|
|
|
|
virtual META_RES &GetPrevResRef() = 0; //!< Gets the pointer to the previous meta result
|
|
|
|
virtual META_RES &GetStatusRef() = 0; //!< Gets the pointer to the status variable
|
|
|
|
virtual void SetOrigRet(const void *ptr) = 0; //!< Sets the original return pointer
|
|
|
|
virtual void SetOverrideRet(const void *ptr) = 0; //!< Sets the override result pointer
|
|
|
|
virtual void SetIfacePtr(void *ptr) = 0; //!< Sets the interface this pointer
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2005-05-01 11:55:51 +02:00
|
|
|
/************************************************************************/
|
|
|
|
/* High level interface */
|
|
|
|
/************************************************************************/
|
|
|
|
#define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
|
|
|
|
#define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
|
|
|
|
#define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
|
2005-04-14 14:54:13 +02:00
|
|
|
|
2005-04-16 18:55:53 +02:00
|
|
|
#define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
|
|
|
|
#define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
|
2005-05-01 11:55:51 +02:00
|
|
|
#define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
|
|
|
|
#define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
|
2005-04-16 18:55:53 +02:00
|
|
|
#define META_IFACEPTR SH_GLOB_SHPTR->GetIfacePtr()
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get/generate callclass for an interface pointer
|
|
|
|
*
|
|
|
|
* @param ifaceptr The interface pointer
|
|
|
|
*/
|
2005-04-29 22:29:46 +02:00
|
|
|
template<class ifacetype>
|
2005-05-01 11:55:51 +02:00
|
|
|
inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
|
2005-04-29 22:29:46 +02:00
|
|
|
{
|
|
|
|
return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
|
2005-05-01 11:55:51 +02:00
|
|
|
shptr->GetCallClass(reinterpret_cast<void*>(ptr)));
|
2005-04-29 22:29:46 +02:00
|
|
|
}
|
|
|
|
|
2005-05-01 11:55:51 +02:00
|
|
|
template<class ifacetype>
|
|
|
|
inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
|
|
|
|
{
|
|
|
|
shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
|
|
|
|
#define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
#define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
|
2005-04-30 21:09:51 +02:00
|
|
|
__SourceHook_FHAdd##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
|
2005-04-14 14:54:13 +02:00
|
|
|
#define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
|
2005-04-30 21:09:51 +02:00
|
|
|
SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
|
2005-04-14 14:54:13 +02:00
|
|
|
#define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
|
|
|
|
SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
|
|
|
|
|
|
|
|
#define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
|
2005-04-30 21:09:51 +02:00
|
|
|
__SourceHook_FHRemove##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
|
2005-04-14 14:54:13 +02:00
|
|
|
#define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
|
2005-04-30 21:09:51 +02:00
|
|
|
SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
|
2005-04-14 14:54:13 +02:00
|
|
|
#define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
|
|
|
|
SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
|
|
|
|
|
|
|
|
#define SH_NOATTRIB
|
|
|
|
|
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
|
2005-05-01 11:55:51 +02:00
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
#if SH_COMP == SH_COMP_MSVC
|
|
|
|
# define SH_SETUP_MFP(mfp) \
|
|
|
|
reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry;
|
|
|
|
#elif SH_COMP == SH_COMP_GCC
|
|
|
|
# define SH_SETUP_MFP(mfp) \
|
|
|
|
reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry; \
|
|
|
|
reinterpret_cast<void**>(&mfp)[1] = 0;
|
|
|
|
#else
|
|
|
|
# error Not supported yet.
|
|
|
|
#endif
|
|
|
|
|
2005-04-14 14:54:13 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2005-04-30 21:09:51 +02:00
|
|
|
#define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
|
2005-04-14 14:54:13 +02:00
|
|
|
|
2005-04-16 18:55:53 +02:00
|
|
|
#define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
|
2005-04-30 21:09:51 +02:00
|
|
|
static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::HookManagerInfo *param) \
|
2005-04-14 14:54:13 +02:00
|
|
|
{ \
|
2005-04-30 21:09:51 +02:00
|
|
|
using namespace ::SourceHook; \
|
|
|
|
if (action == ::SourceHook::HA_GetInfo) \
|
2005-04-14 14:54:13 +02:00
|
|
|
{ \
|
|
|
|
param->proto = ms_Proto; \
|
|
|
|
MemFuncInfo mfi; \
|
|
|
|
GetFuncInfo(funcptr, mfi); \
|
|
|
|
param->vtbl_idx = mfi.vtblindex; \
|
|
|
|
param->vtbl_offs = mfi.vtbloffs; \
|
2005-04-29 22:29:46 +02:00
|
|
|
\
|
2005-04-14 14:54:13 +02:00
|
|
|
GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
|
2005-04-29 22:29:46 +02:00
|
|
|
param->hookfunc_vfnptr = \
|
|
|
|
reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]; \
|
2005-04-14 14:54:13 +02:00
|
|
|
return 0; \
|
|
|
|
} \
|
2005-04-30 21:09:51 +02:00
|
|
|
else if (action == ::SourceHook::HA_Register) \
|
2005-04-14 14:54:13 +02:00
|
|
|
{ \
|
|
|
|
ms_HI = param; \
|
|
|
|
return 0; \
|
|
|
|
} \
|
2005-04-30 21:09:51 +02:00
|
|
|
else if (action == ::SourceHook::HA_Unregister) \
|
2005-04-14 14:54:13 +02:00
|
|
|
{ \
|
|
|
|
ms_HI = NULL; \
|
|
|
|
return 0; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
return 1; \
|
|
|
|
}
|
|
|
|
|
2005-04-30 21:09:51 +02:00
|
|
|
// It has to be possible to use the macros in namespaces
|
|
|
|
// -> So we need to access and extend the global SourceHook namespace
|
|
|
|
// We use a namespace alias for this
|
2005-04-14 14:54:13 +02:00
|
|
|
#define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
|
2005-04-30 21:09:51 +02:00
|
|
|
struct SH_FHCls(ifacetype,ifacefunc,overload) \
|
2005-04-14 14:54:13 +02:00
|
|
|
{ \
|
2005-04-30 21:09:51 +02:00
|
|
|
static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
|
|
|
|
static ::SourceHook::HookManagerInfo *ms_HI; \
|
|
|
|
static const char *ms_Proto; \
|
|
|
|
SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
|
2005-04-14 14:54:13 +02:00
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
#define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
|
2005-04-30 21:09:51 +02:00
|
|
|
}; \
|
|
|
|
const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
|
|
|
|
SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
|
|
|
|
::SourceHook::HookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
|
|
|
|
bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
|
|
|
|
SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
|
|
|
|
{ \
|
|
|
|
using namespace ::SourceHook; \
|
|
|
|
MemFuncInfo mfi; \
|
|
|
|
GetFuncInfo(funcptr, mfi); \
|
|
|
|
if (mfi.thisptroffs < 0) \
|
|
|
|
return false; /* No virtual inheritance supported */ \
|
|
|
|
\
|
|
|
|
return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
|
|
|
|
SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
|
|
|
|
new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
|
|
|
|
} \
|
|
|
|
bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
|
|
|
|
SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
|
|
|
|
{ \
|
|
|
|
using namespace ::SourceHook; \
|
|
|
|
MemFuncInfo mfi; \
|
|
|
|
GetFuncInfo(funcptr, mfi); \
|
|
|
|
if (mfi.thisptroffs < 0) \
|
|
|
|
return false; /* No virtual inheritance supported */ \
|
|
|
|
\
|
|
|
|
CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
|
|
|
|
return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
|
|
|
|
SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
|
|
|
|
} \
|
2005-04-14 14:54:13 +02:00
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
#define SH_SETUPCALLS(rettype, paramtypes, params) \
|
|
|
|
/* 1) Find the vfnptr */ \
|
2005-04-30 21:09:51 +02:00
|
|
|
using namespace ::SourceHook; \
|
2005-04-29 22:29:46 +02:00
|
|
|
void *ourvfnptr = reinterpret_cast<void*>( \
|
|
|
|
*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
|
|
|
|
\
|
|
|
|
HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
|
|
|
|
ms_HI->vfnptrs.end(), ourvfnptr); \
|
|
|
|
if (vfptriter == ms_HI->vfnptrs.end()) \
|
|
|
|
{ \
|
|
|
|
/* Bleh? Should be impossible! */ \
|
|
|
|
SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
|
|
|
|
} \
|
|
|
|
HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
|
|
|
|
/* 2) Find the iface */ \
|
|
|
|
HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
|
|
|
|
if (ifiter == vfnptr.ifaces.end()) \
|
|
|
|
{ \
|
|
|
|
/* The iface info was not found. Redirect the call to the original function. */ \
|
|
|
|
rettype (EmptyClass::*mfp)paramtypes; \
|
|
|
|
SH_SETUP_MFP(mfp); \
|
|
|
|
return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
|
|
|
|
} \
|
|
|
|
HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
|
2005-04-14 14:54:13 +02:00
|
|
|
/* 2) Declare some vars and set it up */ \
|
2005-04-29 22:29:46 +02:00
|
|
|
std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
|
|
|
|
std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
|
2005-04-30 21:09:51 +02:00
|
|
|
rettype orig_ret; \
|
|
|
|
rettype override_ret; \
|
|
|
|
rettype plugin_ret; \
|
2005-04-16 18:55:53 +02:00
|
|
|
META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
|
|
|
|
META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
|
|
|
|
META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
|
2005-04-14 14:54:13 +02:00
|
|
|
status = MRES_IGNORED; \
|
2005-04-29 22:29:46 +02:00
|
|
|
SH_GLOB_SHPTR->SetIfacePtr(this); \
|
2005-04-16 18:55:53 +02:00
|
|
|
SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
|
|
|
|
SH_GLOB_SHPTR->SetOverrideRet(NULL);
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
#define SH_CALL_HOOKS(post, params) \
|
|
|
|
prev_res = MRES_IGNORED; \
|
2005-04-29 22:29:46 +02:00
|
|
|
for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
|
2005-04-14 14:54:13 +02:00
|
|
|
{ \
|
2005-04-16 22:26:33 +02:00
|
|
|
if (hiter->paused) continue; \
|
2005-04-14 14:54:13 +02:00
|
|
|
cur_res = MRES_IGNORED; \
|
|
|
|
plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
|
|
|
|
prev_res = cur_res; \
|
|
|
|
if (cur_res > status) \
|
|
|
|
status = cur_res; \
|
|
|
|
if (cur_res >= MRES_OVERRIDE) \
|
|
|
|
{ \
|
|
|
|
override_ret = plugin_ret; \
|
2005-04-16 18:55:53 +02:00
|
|
|
SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
|
2005-04-14 14:54:13 +02:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
#define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
|
2005-04-14 14:54:13 +02:00
|
|
|
if (status != MRES_SUPERCEDE) \
|
2005-04-29 22:29:46 +02:00
|
|
|
{ \
|
|
|
|
rettype (EmptyClass::*mfp)paramtypes; \
|
|
|
|
SH_SETUP_MFP(mfp); \
|
|
|
|
orig_ret = (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
|
|
|
|
} \
|
2005-04-14 14:54:13 +02:00
|
|
|
else \
|
|
|
|
orig_ret = override_ret;
|
|
|
|
|
|
|
|
#define SH_RETURN() \
|
|
|
|
return status >= MRES_OVERRIDE ? override_ret : orig_ret;
|
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
#define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
|
|
|
|
SH_SETUPCALLS(rettype, paramtypes, params) \
|
2005-04-14 14:54:13 +02:00
|
|
|
SH_CALL_HOOKS(pre, params) \
|
2005-04-29 22:29:46 +02:00
|
|
|
SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
|
2005-04-14 14:54:13 +02:00
|
|
|
SH_CALL_HOOKS(post, params) \
|
|
|
|
SH_RETURN()
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2005-04-29 22:29:46 +02:00
|
|
|
#define SH_SETUPCALLS_void(paramtypes, params) \
|
2005-04-30 21:09:51 +02:00
|
|
|
using namespace ::SourceHook; \
|
2005-04-29 22:29:46 +02:00
|
|
|
/* 1) Find the vfnptr */ \
|
|
|
|
void *ourvfnptr = reinterpret_cast<void*>( \
|
|
|
|
*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
|
|
|
|
\
|
|
|
|
HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
|
|
|
|
ms_HI->vfnptrs.end(), ourvfnptr); \
|
|
|
|
if (vfptriter == ms_HI->vfnptrs.end()) \
|
|
|
|
{ \
|
|
|
|
/* Bleh? Should be impossible! */ \
|
|
|
|
SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
|
|
|
|
} \
|
|
|
|
HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
|
|
|
|
/* 2) Find the iface */ \
|
|
|
|
HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
|
|
|
|
if (ifiter == vfnptr.ifaces.end()) \
|
|
|
|
{ \
|
|
|
|
/* The iface info was not found. Redirect the call to the original function. */ \
|
|
|
|
void (EmptyClass::*mfp)paramtypes; \
|
|
|
|
SH_SETUP_MFP(mfp); \
|
|
|
|
(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
|
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
|
2005-04-14 14:54:13 +02:00
|
|
|
/* 2) Declare some vars and set it up */ \
|
2005-04-29 22:29:46 +02:00
|
|
|
std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
|
|
|
|
std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
|
2005-04-16 18:55:53 +02:00
|
|
|
META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
|
|
|
|
META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
|
|
|
|
META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
|
2005-04-14 14:54:13 +02:00
|
|
|
status = MRES_IGNORED; \
|
2005-04-29 22:29:46 +02:00
|
|
|
SH_GLOB_SHPTR->SetIfacePtr(this); \
|
2005-04-16 18:55:53 +02:00
|
|
|
SH_GLOB_SHPTR->SetOverrideRet(NULL);
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
#define SH_CALL_HOOKS_void(post, params) \
|
|
|
|
prev_res = MRES_IGNORED; \
|
2005-04-29 22:29:46 +02:00
|
|
|
for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
|
2005-04-14 14:54:13 +02:00
|
|
|
{ \
|
2005-04-16 22:26:33 +02:00
|
|
|
if (hiter->paused) continue; \
|
2005-04-14 14:54:13 +02:00
|
|
|
cur_res = MRES_IGNORED; \
|
|
|
|
reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
|
|
|
|
prev_res = cur_res; \
|
|
|
|
if (cur_res > status) \
|
|
|
|
status = cur_res; \
|
|
|
|
}
|
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
#define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
|
2005-04-14 14:54:13 +02:00
|
|
|
if (status != MRES_SUPERCEDE) \
|
2005-04-29 22:29:46 +02:00
|
|
|
{ \
|
|
|
|
void (EmptyClass::*mfp)paramtypes; \
|
|
|
|
SH_SETUP_MFP(mfp); \
|
|
|
|
(reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
|
|
|
|
}
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
#define SH_RETURN_void()
|
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
#define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
|
|
|
|
SH_SETUPCALLS_void(paramtypes, params) \
|
2005-04-14 14:54:13 +02:00
|
|
|
SH_CALL_HOOKS_void(pre, params) \
|
2005-04-29 22:29:46 +02:00
|
|
|
SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
|
2005-04-14 14:54:13 +02:00
|
|
|
SH_CALL_HOOKS_void(post, params) \
|
|
|
|
SH_RETURN_void()
|
|
|
|
|
|
|
|
|
|
|
|
// Special vafmt handlers
|
2005-04-29 22:29:46 +02:00
|
|
|
#define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
|
|
|
|
SH_SETUPCALLS(rettype, paramtypes, params_orig) \
|
2005-04-14 14:54:13 +02:00
|
|
|
SH_CALL_HOOKS(pre, params_plug) \
|
2005-04-29 22:29:46 +02:00
|
|
|
SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
|
2005-04-14 14:54:13 +02:00
|
|
|
SH_CALL_HOOKS(post, params_plug) \
|
|
|
|
SH_RETURN()
|
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
#define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
|
|
|
|
SH_SETUPCALLS_void(paramtypes, params_orig) \
|
2005-04-14 14:54:13 +02:00
|
|
|
SH_CALL_HOOKS_void(pre, params_plug) \
|
2005-04-29 22:29:46 +02:00
|
|
|
SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
|
2005-04-14 14:54:13 +02:00
|
|
|
SH_CALL_HOOKS_void(post, params_plug) \
|
|
|
|
SH_RETURN_void()
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
@VARARGS@
|
2005-04-29 22:29:46 +02:00
|
|
|
// ********* Support for @$@ arguments *********
|
2005-04-14 14:54:13 +02:00
|
|
|
#define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
|
2005-04-30 21:09:51 +02:00
|
|
|
SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
|
2005-04-14 14:54:13 +02:00
|
|
|
(&ifacetype::ifacefunc))) \
|
|
|
|
typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
|
2005-04-30 21:09:51 +02:00
|
|
|
virtual rettype Func(@param%% p%%|, @) \
|
2005-04-29 22:29:46 +02:00
|
|
|
{ SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
|
|
|
|
SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
|
2005-04-30 21:09:51 +02:00
|
|
|
(static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
#define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
|
2005-04-30 21:09:51 +02:00
|
|
|
SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
|
2005-04-14 14:54:13 +02:00
|
|
|
(&ifacetype::ifacefunc))) \
|
|
|
|
typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
|
2005-04-30 21:09:51 +02:00
|
|
|
virtual void Func(@param%% p%%|, @) \
|
2005-04-29 22:29:46 +02:00
|
|
|
{ SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
|
|
|
|
SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
|
2005-04-30 21:09:51 +02:00
|
|
|
(static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
#define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
|
2005-04-30 21:09:51 +02:00
|
|
|
SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
|
2005-04-14 14:54:13 +02:00
|
|
|
(&ifacetype::ifacefunc))) \
|
|
|
|
typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
|
2005-04-30 21:09:51 +02:00
|
|
|
virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
|
2005-04-14 14:54:13 +02:00
|
|
|
{ \
|
2005-05-01 16:30:52 +02:00
|
|
|
char buf[::SourceHook::STRBUF_LEN]; \
|
2005-04-14 14:54:13 +02:00
|
|
|
va_list ap; \
|
|
|
|
va_start(ap, fmt); \
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, ap); \
|
|
|
|
va_end(ap); \
|
2005-04-29 22:29:46 +02:00
|
|
|
SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
|
2005-04-14 14:54:13 +02:00
|
|
|
} \
|
2005-04-29 22:29:46 +02:00
|
|
|
SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
|
2005-04-30 21:09:51 +02:00
|
|
|
(static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
|
2005-04-14 14:54:13 +02:00
|
|
|
|
2005-05-01 16:30:52 +02:00
|
|
|
#define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload@, param%%@) \
|
2005-04-30 21:09:51 +02:00
|
|
|
SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
|
2005-04-14 14:54:13 +02:00
|
|
|
(&ifacetype::ifacefunc))) \
|
|
|
|
typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
|
2005-05-01 16:30:52 +02:00
|
|
|
virtual void Func(@param%% p%%|, @@, @const char *fmt, ...) \
|
2005-04-14 14:54:13 +02:00
|
|
|
{ \
|
2005-05-01 16:30:52 +02:00
|
|
|
char buf[::SourceHook::STRBUF_LEN]; \
|
2005-04-14 14:54:13 +02:00
|
|
|
va_list ap; \
|
|
|
|
va_start(ap, fmt); \
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, ap); \
|
|
|
|
va_end(ap); \
|
2005-04-29 22:29:46 +02:00
|
|
|
SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
|
2005-04-14 14:54:13 +02:00
|
|
|
} \
|
2005-04-29 22:29:46 +02:00
|
|
|
SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
|
2005-04-30 21:09:51 +02:00
|
|
|
(static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
@ENDARGS@
|
|
|
|
|
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// SH_CALL
|
|
|
|
|
|
|
|
|
|
|
|
#define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
|
|
|
|
{ \
|
2005-04-30 21:09:51 +02:00
|
|
|
using namespace ::SourceHook; \
|
2005-04-29 22:29:46 +02:00
|
|
|
MemFuncInfo mfi; \
|
|
|
|
MFI_Impl<sizeof(m_MFP)>::GetFuncInfo(m_MFP, mfi); \
|
|
|
|
OrigVTables::const_iterator iter = m_CC->vt.find(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; \
|
|
|
|
void *addr; \
|
|
|
|
} u; \
|
|
|
|
u.addr = iter->second[mfi.vtblindex]; \
|
|
|
|
\
|
|
|
|
return (reinterpret_cast<EmptyClass*>(m_CC->ptr)->*u.mfpnew)call; \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace SourceHook
|
|
|
|
{
|
|
|
|
template<class CCType, class RetType, class MFPType> class ExecutableClass
|
|
|
|
{
|
|
|
|
CCType *m_CC;
|
|
|
|
MFPType m_MFP;
|
|
|
|
public:
|
|
|
|
ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@VARARGS@
|
|
|
|
// Support for @$@ arguments
|
2005-04-30 21:09:51 +02:00
|
|
|
@template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
|
2005-05-01 14:36:48 +02:00
|
|
|
SH_MAKE_EXECUTABLECLASS_OB((@p%%|, @), (@Param%%|, @))
|
2005-04-29 22:29:46 +02:00
|
|
|
|
|
|
|
@ENDARGS@
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@VARARGS@
|
|
|
|
// Support for @$@ arguments
|
|
|
|
template <class X, class Y, class RetType@, @@class Param%%|, @>
|
|
|
|
SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>
|
|
|
|
SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @))
|
|
|
|
{
|
|
|
|
return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>(ptr, mfp);
|
|
|
|
}
|
|
|
|
|
2005-04-30 21:09:51 +02:00
|
|
|
template <class X, class Y, class RetType@, @@class Param%%|, @>
|
|
|
|
SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @) const>
|
|
|
|
SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @)const)
|
|
|
|
{
|
|
|
|
return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @) const>(ptr, mfp);
|
|
|
|
}
|
|
|
|
|
2005-05-01 20:04:18 +02:00
|
|
|
template <class X, class Y, class RetType@, @@class Param%%|, @>
|
|
|
|
SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @@, @...)>
|
|
|
|
SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @@, @...))
|
|
|
|
{
|
|
|
|
return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @@, @...)>(ptr, mfp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class X, class Y, class RetType@, @@class Param%%|, @>
|
|
|
|
SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @@, @...) const>
|
|
|
|
SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @@, @...)const)
|
|
|
|
{
|
|
|
|
return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @@, @...) const>(ptr, mfp);
|
|
|
|
}
|
|
|
|
|
2005-04-29 22:29:46 +02:00
|
|
|
@ENDARGS@
|
|
|
|
|
|
|
|
#undef SH_MAKE_EXECUTABLECLASS_BODY
|
2005-04-14 14:54:13 +02:00
|
|
|
|
|
|
|
#endif
|
2005-05-01 14:36:48 +02:00
|
|
|
// The pope is dead. -> :(
|