sourcehook/sourcehook.cpp
changeset 328 3a04ef79edf1
parent 324 257f0837a885
child 331 40467046ea05
     1.1 --- a/sourcehook/sourcehook.cpp	Fri May 11 19:21:14 2007 +0000
     1.2 +++ b/sourcehook/sourcehook.cpp	Sat May 12 08:08:01 2007 +0000
     1.3 @@ -39,7 +39,7 @@
     1.4  			return -1;
     1.5  		return 0;
     1.6  	}
     1.7 -	CSourceHookImpl::CSourceHookImpl()
     1.8 +	CSourceHookImpl::CSourceHookImpl() : m_OneIgnore(NULL), m_IgnoreActive(false)
     1.9  	{
    1.10  	}
    1.11  	CSourceHookImpl::~CSourceHookImpl()
    1.12 @@ -286,7 +286,12 @@
    1.13  	int CSourceHookImpl::AddHookNew(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
    1.14  		ISHDelegate *handler, bool post)
    1.15  	{
    1.16 -		void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
    1.17 +		void *adjustediface = NULL;
    1.18 +		void **cur_vtptr = NULL;
    1.19 +		void *cur_vfnptr = NULL;
    1.20 +
    1.21 +		if (mode != Hook_Normal && mode != Hook_VP && mode != Hook_DVP)
    1.22 +			return 0;
    1.23  
    1.24  		// 1) Get info about the hook manager
    1.25  		CHookManagerInfo tmp;
    1.26 @@ -298,10 +303,6 @@
    1.27  
    1.28  		CHookManagerContainer::HMCI hmci(tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx);
    1.29  
    1.30 -		void **cur_vtptr = *reinterpret_cast<void***>(
    1.31 -			reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs);
    1.32 -		void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.m_VtblIdx);
    1.33 -
    1.34  		// Add the container if not already added
    1.35  		HookManContList::iterator hmcl_iter = m_HookMans.find(hmci);
    1.36  		if (hmcl_iter == m_HookMans.end())
    1.37 @@ -336,12 +337,33 @@
    1.38  		if (hookman->m_VfnPtrs.empty())
    1.39  			hookman->m_Func(HA_Register, &(*hookman));
    1.40  
    1.41 +
    1.42 +		// find vfnptr
    1.43 +		if (mode == Hook_Normal || mode == Hook_VP)
    1.44 +		{
    1.45 +			adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
    1.46 +
    1.47 +			cur_vtptr = *reinterpret_cast<void***>(
    1.48 +				reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs);
    1.49 +			cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.m_VtblIdx);
    1.50 +
    1.51 +			// For Hook_VP, adjustediface will be set to NULL later
    1.52 +			// because we first have to patch callclasses (callclasses are deprecated but left in for backwards compat)
    1.53 +		}
    1.54 +		else if (mode == Hook_DVP)
    1.55 +		{
    1.56 +			adjustediface = NULL;
    1.57 +
    1.58 +			cur_vtptr = reinterpret_cast<void**>(iface);
    1.59 +			cur_vfnptr = cur_vtptr + tmp.m_VtblIdx;
    1.60 +		}
    1.61 +
    1.62  		CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->m_VfnPtrs.find(cur_vfnptr);
    1.63  
    1.64  		if (vfnptr_iter == hookman->m_VfnPtrs.end())
    1.65  		{
    1.66  			// Add a new one
    1.67 -			CVfnPtr vfp(cur_vfnptr);
    1.68 +			CVfnPtr vfp(cur_vfnptr, &m_OneIgnore);
    1.69  
    1.70  			// Alter vtable entry
    1.71  			if (!SetMemAccess(cur_vtptr, sizeof(void*) * (tmp.m_VtblIdx + 1), SH_MEM_READ | SH_MEM_WRITE))
    1.72 @@ -885,6 +907,16 @@
    1.73  		}
    1.74  	}
    1.75  
    1.76 +	void CSourceHookImpl::SetIgnoreHooks(Plugin plug, void *vfnptr)
    1.77 +	{
    1.78 +		m_OneIgnore = vfnptr;
    1.79 +	}
    1.80 +
    1.81 +	void CSourceHookImpl::ResetIgnoreHooks(Plugin plug, void *vfnptr)
    1.82 +	{
    1.83 +		m_OneIgnore = NULL;
    1.84 +	}
    1.85 +
    1.86  	////////////////////////////
    1.87  	// CCallClassImpl
    1.88  	////////////////////////////
    1.89 @@ -980,7 +1012,8 @@
    1.90  
    1.91  	// If you get a crash here, the ptr passed is invalid
    1.92  	// This usually means a SH_DECL_MANUALHOOK* with wrong thisptroffs/vtbloffs/vtblidx
    1.93 -	CSourceHookImpl::CVfnPtr::CVfnPtr(void *ptr) : m_Ptr(ptr), m_OrigEntry(*reinterpret_cast<void**>(ptr))
    1.94 +	CSourceHookImpl::CVfnPtr::CVfnPtr(void *ptr, void **pOneIgnore) : m_Ptr(ptr), 
    1.95 +		m_pOneIgnore(pOneIgnore), m_OrigEntry(*reinterpret_cast<void**>(ptr))
    1.96  	{
    1.97  	}
    1.98  	CSourceHookImpl::CVfnPtr::~CVfnPtr()
    1.99 @@ -999,6 +1032,12 @@
   1.100  
   1.101  	IIface *CSourceHookImpl::CVfnPtr::FindIface(void *ptr)
   1.102  	{
   1.103 +		if (m_Ptr == *m_pOneIgnore)
   1.104 +		{
   1.105 +			*m_pOneIgnore = NULL;		// Only once!
   1.106 +			return NULL;
   1.107 +		}
   1.108 +
   1.109  		IfaceListIter iter = m_Ifaces.find(ptr);
   1.110  		
   1.111  		// If nothing is found, check for a NULL-interface (VP hooks only)