Added SH_GET_ORIG_VFNPTR_ENTRY
authorPavol Marko <pavolmarko@gmail.com>
Sun Oct 07 23:00:38 2007 +0000 (2007-10-07)
changeset 38608583103ba6f
parent 385 e3d55ad50ba6
child 387 093c72464b35
Added SH_GET_ORIG_VFNPTR_ENTRY
sourcehook/generate/sourcehook.h
sourcehook/generate/sourcehook.hxx
sourcehook/sourcehook.cpp
sourcehook/sourcehook.h
sourcehook/sourcehook_impl.h
sourcehook/test/test1.cpp
     1.1 --- a/sourcehook/generate/sourcehook.h	Sun Oct 07 22:45:51 2007 +0000
     1.2 +++ b/sourcehook/generate/sourcehook.h	Sun Oct 07 23:00:38 2007 +0000
     1.3 @@ -391,6 +391,14 @@
     1.4  		*/
     1.5  		virtual void ResetIgnoreHooks(void *vfnptr) = 0;
     1.6  
     1.7 +		/**
     1.8 +		*	@brief Finds the original entry of a virtual function pointer
     1.9 +		*
    1.10 +		*	@param vfnptr The virtual function pointer
    1.11 +		*	@return The original entry if the virtual function pointer has been patched; NULL otherwise.
    1.12 +		*/
    1.13 +		virtual void *GetOrigVfnPtrEntry(void *vfnptr) = 0;
    1.14 +
    1.15  		//////////////////////////////////////////////////////////////////////////
    1.16  
    1.17  		// For hook managers	
    1.18 @@ -466,6 +474,20 @@
    1.19  			return &ref;
    1.20  		}
    1.21  	};
    1.22 +
    1.23 +	template <class X, class MFP>
    1.24 +		void *GetOrigVfnPtrEntry(X *pInstance, MFP mfp, ISourceHook *pSH)
    1.25 +	{
    1.26 +		SourceHook::MemFuncInfo info = {true, -1, 0, 0};
    1.27 +		SourceHook::GetFuncInfo(pInstance, mfp, info);
    1.28 +
    1.29 +		void *vfnptr = reinterpret_cast<void*>(
    1.30 +			*reinterpret_cast<void***>(reinterpret_cast<char*>(pInstance) + info.thisptroffs + info.vtbloffs) + info.vtblindex);
    1.31 +
    1.32 +		void *origentry = pSH->GetOrigVfnPtrEntry(vfnptr);
    1.33 +
    1.34 +		return origentry ? origentry : *reinterpret_cast<void**>(vfnptr);
    1.35 +	}
    1.36  }
    1.37  
    1.38  /************************************************************************/
    1.39 @@ -588,6 +610,8 @@
    1.40  		SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
    1.41  	} while (0)
    1.42  
    1.43 +#define SH_GET_ORIG_VFNPTR_ENTRY(inst, mfp) (SourceHook::GetOrigVfnPtrEntry(inst, mfp, SH_GLOB_SHPTR))
    1.44 +
    1.45  // New ADD / REMOVE macros.
    1.46  #define SH_STATIC(func) fastdelegate::MakeDelegate(func)
    1.47  #define SH_MEMBER(inst, func) fastdelegate::MakeDelegate(inst, func)
     2.1 --- a/sourcehook/generate/sourcehook.hxx	Sun Oct 07 22:45:51 2007 +0000
     2.2 +++ b/sourcehook/generate/sourcehook.hxx	Sun Oct 07 23:00:38 2007 +0000
     2.3 @@ -391,6 +391,14 @@
     2.4  		*/
     2.5  		virtual void ResetIgnoreHooks(void *vfnptr) = 0;
     2.6  
     2.7 +		/**
     2.8 +		*	@brief Finds the original entry of a virtual function pointer
     2.9 +		*
    2.10 +		*	@param vfnptr The virtual function pointer
    2.11 +		*	@return The original entry if the virtual function pointer has been patched; NULL otherwise.
    2.12 +		*/
    2.13 +		virtual void *GetOrigVfnPtrEntry(void *vfnptr) = 0;
    2.14 +
    2.15  		//////////////////////////////////////////////////////////////////////////
    2.16  
    2.17  		// For hook managers	
    2.18 @@ -466,6 +474,20 @@
    2.19  			return &ref;
    2.20  		}
    2.21  	};
    2.22 +
    2.23 +	template <class X, class MFP>
    2.24 +		void *GetOrigVfnPtrEntry(X *pInstance, MFP mfp, ISourceHook *pSH)
    2.25 +	{
    2.26 +		SourceHook::MemFuncInfo info = {true, -1, 0, 0};
    2.27 +		SourceHook::GetFuncInfo(pInstance, mfp, info);
    2.28 +
    2.29 +		void *vfnptr = reinterpret_cast<void*>(
    2.30 +			*reinterpret_cast<void***>(reinterpret_cast<char*>(pInstance) + info.thisptroffs + info.vtbloffs) + info.vtblindex);
    2.31 +
    2.32 +		void *origentry = pSH->GetOrigVfnPtrEntry(vfnptr);
    2.33 +
    2.34 +		return origentry ? origentry : *reinterpret_cast<void**>(vfnptr);
    2.35 +	}
    2.36  }
    2.37  
    2.38  /************************************************************************/
    2.39 @@ -588,6 +610,8 @@
    2.40  		SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
    2.41  	} while (0)
    2.42  
    2.43 +#define SH_GET_ORIG_VFNPTR_ENTRY(inst, mfp) (SourceHook::GetOrigVfnPtrEntry(inst, mfp, SH_GLOB_SHPTR))
    2.44 +
    2.45  // New ADD / REMOVE macros.
    2.46  #define SH_STATIC(func) fastdelegate::MakeDelegate(func)
    2.47  #define SH_MEMBER(inst, func) fastdelegate::MakeDelegate(inst, func)
     3.1 --- a/sourcehook/sourcehook.cpp	Sun Oct 07 22:45:51 2007 +0000
     3.2 +++ b/sourcehook/sourcehook.cpp	Sun Oct 07 23:00:38 2007 +0000
     3.3 @@ -733,6 +733,25 @@
     3.4  				m_ContextStack.pop();
     3.5  		}
     3.6  
     3.7 +		void *CSourceHookImpl::GetOrigVfnPtrEntry(void *vfnptr)
     3.8 +		{
     3.9 +			for (CHookManContainerList::iterator hmcl_iter = m_HookManContainers.begin();
    3.10 +				hmcl_iter != m_HookManContainers.end(); ++hmcl_iter)
    3.11 +			{
    3.12 +				for (CHookManagerContainer::iterator hookmaniter = hmcl_iter->begin();
    3.13 +					hookmaniter != hmcl_iter->end(); ++hookmaniter)
    3.14 +				{
    3.15 +					for (List<CVfnPtr>::iterator vfnptr_iter = hookmaniter->GetVfnPtrList().begin();
    3.16 +						vfnptr_iter != hookmaniter->GetVfnPtrList().end(); ++vfnptr_iter)
    3.17 +					{
    3.18 +						if (vfnptr_iter->GetPtr() == vfnptr)
    3.19 +							return vfnptr_iter->GetOrigEntry();
    3.20 +					}
    3.21 +				}
    3.22 +			}
    3.23 +			return NULL;
    3.24 +		}
    3.25 +
    3.26  		void CSourceHookImpl::DoRecall()
    3.27  		{
    3.28  			CHookContext newCtx;
     4.1 --- a/sourcehook/sourcehook.h	Sun Oct 07 22:45:51 2007 +0000
     4.2 +++ b/sourcehook/sourcehook.h	Sun Oct 07 23:00:38 2007 +0000
     4.3 @@ -391,6 +391,14 @@
     4.4  		*/
     4.5  		virtual void ResetIgnoreHooks(void *vfnptr) = 0;
     4.6  
     4.7 +		/**
     4.8 +		*	@brief Finds the original entry of a virtual function pointer
     4.9 +		*
    4.10 +		*	@param vfnptr The virtual function pointer
    4.11 +		*	@return The original entry if the virtual function pointer has been patched; NULL otherwise.
    4.12 +		*/
    4.13 +		virtual void *GetOrigVfnPtrEntry(void *vfnptr) = 0;
    4.14 +
    4.15  		//////////////////////////////////////////////////////////////////////////
    4.16  
    4.17  		// For hook managers	
    4.18 @@ -466,6 +474,20 @@
    4.19  			return &ref;
    4.20  		}
    4.21  	};
    4.22 +
    4.23 +	template <class X, class MFP>
    4.24 +		void *GetOrigVfnPtrEntry(X *pInstance, MFP mfp, ISourceHook *pSH)
    4.25 +	{
    4.26 +		SourceHook::MemFuncInfo info = {true, -1, 0, 0};
    4.27 +		SourceHook::GetFuncInfo(pInstance, mfp, info);
    4.28 +
    4.29 +		void *vfnptr = reinterpret_cast<void*>(
    4.30 +			*reinterpret_cast<void***>(reinterpret_cast<char*>(pInstance) + info.thisptroffs + info.vtbloffs) + info.vtblindex);
    4.31 +
    4.32 +		void *origentry = pSH->GetOrigVfnPtrEntry(vfnptr);
    4.33 +
    4.34 +		return origentry ? origentry : *reinterpret_cast<void**>(vfnptr);
    4.35 +	}
    4.36  }
    4.37  
    4.38  /************************************************************************/
    4.39 @@ -588,6 +610,8 @@
    4.40  		SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
    4.41  	} while (0)
    4.42  
    4.43 +#define SH_GET_ORIG_VFNPTR_ENTRY(inst, mfp) (SourceHook::GetOrigVfnPtrEntry(inst, mfp, SH_GLOB_SHPTR))
    4.44 +
    4.45  // New ADD / REMOVE macros.
    4.46  #define SH_STATIC(func) fastdelegate::MakeDelegate(func)
    4.47  #define SH_MEMBER(inst, func) fastdelegate::MakeDelegate(inst, func)
     5.1 --- a/sourcehook/sourcehook_impl.h	Sun Oct 07 22:45:51 2007 +0000
     5.2 +++ b/sourcehook/sourcehook_impl.h	Sun Oct 07 23:00:38 2007 +0000
     5.3 @@ -314,6 +314,8 @@
     5.4  
     5.5  			void EndContext(IHookContext *pCtx);
     5.6  
     5.7 +			void *GetOrigVfnPtrEntry(void *vfnptr);
     5.8 +
     5.9  			/**
    5.10  			*	@brief Shut down the whole system, unregister all hook managers
    5.11  			*/
     6.1 --- a/sourcehook/test/test1.cpp	Sun Oct 07 22:45:51 2007 +0000
     6.2 +++ b/sourcehook/test/test1.cpp	Sun Oct 07 23:00:38 2007 +0000
     6.3 @@ -419,6 +419,8 @@
     6.4  	Test test;
     6.5  	Test *pTest = &test;
     6.6  
     6.7 +	void *pOrigVfnPtrF1 = (*reinterpret_cast<void***>(pTest))[0];
     6.8 +
     6.9  	// 1) Get a call class and call the member through it and normally
    6.10  	SourceHook::CallClass<Test> *cc = SH_GET_CALLCLASS(pTest);
    6.11  
    6.12 @@ -433,6 +435,8 @@
    6.13  		new State_F1_Called,
    6.14  		NULL), "Part 1");
    6.15  
    6.16 +	CHECK_COND(SH_GET_ORIG_VFNPTR_ENTRY(pTest, &Test::F1) == pOrigVfnPtrF1, "Part S1");
    6.17 +
    6.18  	// 2) Request a call class again
    6.19  	SourceHook::CallClass<Test> *cc2 = SH_GET_CALLCLASS(pTest);
    6.20  	ADD_STATE(State_F1_CallClassGenerated);
    6.21 @@ -471,6 +475,8 @@
    6.22  		new State_F1_PreHandler_Called(&f1_handlers),
    6.23  		NULL), "Part 3");
    6.24  
    6.25 +	CHECK_COND(SH_GET_ORIG_VFNPTR_ENTRY(pTest, &Test::F1) == pOrigVfnPtrF1, "Part S3");
    6.26 +
    6.27  	// 4) Rerequest the callclass
    6.28  	SH_RELEASE_CALLCLASS(cc);
    6.29