core/sourcehook/sourcehook.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Fri Jun 17 23:06:56 2011 -0400 (2011-06-17)
changeset 755 87f64d655956
parent 733 2365240861a2
child 768 e6394d70ced0
permissions -rw-r--r--
Merge.
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2010 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): Pavol "PM OnoTo" Marko
     8 * Contributors: Scott "Damaged Soul" Ehlert
     9 * ============================
    10 */
    11 
    12 /**
    13  * @file sourcehook.cpp
    14  * @brief Contains the implementation of the SourceHook API
    15 */
    16 
    17 #if defined __GNUC__
    18 #include <stdint.h>
    19 #endif
    20 
    21 #include "sourcehook_impl.h"
    22 
    23 namespace SourceHook
    24 {
    25 	template<>
    26 	int HashFunction<int>(const int & k)
    27 	{
    28 		return k;
    29 	}
    30 	template<>
    31 	int Compare<int>(const int & k1, const int & k2)
    32 	{
    33 		if (k1 == k2)
    34 			return 0;
    35 		if (k1 > k2)
    36 			return 1;
    37 		if (k1 < k2)
    38 			return -1;
    39 		return 0;
    40 	}
    41 
    42 	namespace Impl
    43 	{
    44 		//////////////////////////////////////////////////////////////////////////
    45 		// CVfnPtrList
    46 		//////////////////////////////////////////////////////////////////////////
    47 
    48 		CVfnPtr *CVfnPtrList::GetVfnPtr(void *vfnptr)
    49 		{
    50 			iterator iter = find(vfnptr);
    51 			if (iter == end())
    52 			{
    53 				// No vfnptr info object found
    54 				// --> create a new one
    55 				CVfnPtr newVfnPtr(vfnptr);
    56 				if (newVfnPtr.Init())
    57 				{
    58 					push_back(newVfnPtr);
    59 
    60 					return &(back());
    61 				}
    62 				else
    63 				{
    64 					// Initialization failed.
    65 					return NULL;
    66 				}
    67 			}
    68 			else
    69 			{
    70 				return &(*iter);
    71 			}
    72 		}
    73 
    74 		//////////////////////////////////////////////////////////////////////////
    75 		// CSourceHookImpl
    76 		//////////////////////////////////////////////////////////////////////////
    77 		
    78 
    79 		CSourceHookImpl::CSourceHookImpl()
    80 		{
    81 		}
    82 		CSourceHookImpl::~CSourceHookImpl()
    83 		{
    84 			CompleteShutdown();
    85 		}
    86 
    87 		int CSourceHookImpl::GetIfaceVersion()
    88 		{
    89 			return SH_IFACE_VERSION;
    90 		}
    91 
    92 		int CSourceHookImpl::GetImplVersion()
    93 		{
    94 			return SH_IMPL_VERSION;
    95 		}
    96 
    97 		int CSourceHookImpl::AddHook(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
    98 			ISHDelegate *handler, bool post)
    99 		{
   100 			if (mode != Hook_Normal && mode != Hook_VP && mode != Hook_DVP)
   101 				return 0;
   102 
   103 			// Get info about hook manager
   104 			CHookManager hookManager(plug, myHookMan);
   105 			if (!hookManager)
   106 				return 0;
   107 
   108 			void *adjustediface = NULL;
   109 			void **cur_vtptr = NULL;
   110 			void *cur_vfnptr = NULL;
   111 
   112 			// find vfnptr
   113 			switch (mode)
   114 			{
   115 			case Hook_Normal:
   116 				adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
   117 
   118 				cur_vtptr = *reinterpret_cast<void***>(
   119 					reinterpret_cast<char*>(adjustediface) + hookManager.GetVtblOffs());
   120 				cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + hookManager.GetVtblIdx());
   121 				break;
   122 
   123 			case Hook_VP:
   124 
   125 				adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
   126 
   127 				cur_vtptr = *reinterpret_cast<void***>(
   128 					reinterpret_cast<char*>(adjustediface) + hookManager.GetVtblOffs());
   129 				cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + hookManager.GetVtblIdx());
   130 
   131 				adjustediface = NULL;
   132 				break;
   133 
   134 			case Hook_DVP:
   135 				adjustediface = NULL;
   136 
   137 				cur_vtptr = reinterpret_cast<void**>(iface);
   138 				cur_vfnptr = cur_vtptr + hookManager.GetVtblIdx();
   139 
   140 				break;
   141 			}
   142 
   143 			CVfnPtr *vfnPtr = m_VfnPtrs.GetVfnPtr(cur_vfnptr);
   144 			if (!vfnPtr)
   145 			{
   146 				// Could not create the vfnptr info object.
   147 				// This could be because a thunk generation on GCC
   148 				// has failed. See sourcehook_impl_cvfnptr.cpp
   149 				// for details.
   150 				return false;
   151 			}
   152 
   153 			vfnPtr->AddHookMan(m_HookManList.GetHookMan(hookManager));
   154 			CIface &ifaceinst = vfnPtr->GetIface(adjustediface);
   155 
   156 			// Add the hook
   157 			CHook hook(plug, thisptr_offs, handler, 
   158 
   159 				m_HookIDMan.New(hookManager.GetProto(), hookManager.GetVtblOffs(), hookManager.GetVtblIdx(),
   160 					cur_vfnptr, adjustediface, plug, thisptr_offs, handler, post)
   161 
   162 				);
   163 
   164 			if (post)
   165 				ifaceinst.GetPostHookList().push_back(hook);
   166 			else
   167 				ifaceinst.GetPreHookList().push_back(hook);
   168 
   169 			return hook.GetID();
   170 		}
   171 
   172 		bool CSourceHookImpl::RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   173 			ISHDelegate *handler, bool post)
   174 		{
   175 			// Get info about hook manager and compute adjustediface
   176 			CHookManager tmpHookMan(plug, myHookMan);
   177 
   178 			void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface)+thisptr_offs);
   179 
   180 			// Loop through all hooks and remove those which match:
   181 			//  hookman, vfnptr, iface, plug, adjusted iface, this ptr offs, handler, post
   182 			CVector<int> removehooks;
   183 			m_HookIDMan.FindAllHooks(removehooks, tmpHookMan.GetProto(), tmpHookMan.GetVtblOffs(), tmpHookMan.GetVtblIdx(),
   184 				adjustediface, plug, thisptr_offs, handler, post);
   185 
   186 			if (removehooks.empty())
   187 				return false;
   188 
   189 			bool status = false;
   190 
   191 			for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
   192 			{
   193 				if (RemoveHookByID(*iter))
   194 					status = true;
   195 			}
   196 			return status; 
   197 		}
   198 
   199 		bool CSourceHookImpl::RemoveHookByID(int hookid)
   200 		{
   201 			const CHookIDManager::Entry *hentry;
   202 
   203 			hentry = m_HookIDMan.QueryHook(hookid);
   204 			if (!hentry)
   205 			{
   206 				// hookid doesn't exist !
   207 				return false;
   208 			}
   209 
   210 			// find vfnptr
   211 			List<CVfnPtr>::iterator vfnptr_iter = m_VfnPtrs.find(hentry->vfnptr);
   212 			if (vfnptr_iter == m_VfnPtrs.end())
   213 				return false;
   214 
   215 			// find iface
   216 			List<CIface>::iterator iface_iter = vfnptr_iter->GetIfaceList().find(hentry->adjustediface);
   217 			if (iface_iter == vfnptr_iter->GetIfaceList().end())
   218 				return false;
   219 
   220 			// find hook
   221 			List<CHook> &hooks = hentry->post ? iface_iter->GetPostHookList() : iface_iter->GetPreHookList();
   222 			List<CHook>::iterator hook_iter = hooks.find(hookid);
   223 			if (hook_iter == hooks.end())
   224 				return false;
   225 
   226 			hook_iter->GetHandler()->DeleteThis();
   227 
   228 			// Iterator movage!
   229 			List<CHook>::iterator oldhookiter = hook_iter;
   230 			hook_iter = hooks.erase(hook_iter);
   231 
   232 			for (CStack<CHookContext>::iterator ctx_iter = m_ContextStack.begin();
   233 				ctx_iter != m_ContextStack.end(); ++ctx_iter)
   234 			{
   235 				ctx_iter->HookRemoved(oldhookiter, hook_iter);
   236 			}
   237 
   238 			if (iface_iter->GetPreHookList().empty() && iface_iter->GetPostHookList().empty())
   239 			{
   240 				// -> Kill all contexts that use it!
   241 				for (CStack<CHookContext>::iterator ctx_iter = m_ContextStack.begin();
   242 					ctx_iter != m_ContextStack.end(); ++ctx_iter)
   243 				{
   244 					ctx_iter->IfaceRemoved(&(*iface_iter));
   245 				}
   246 
   247 				// There are no hooks on this iface anymore...
   248 				iface_iter = vfnptr_iter->GetIfaceList().erase(iface_iter);
   249 
   250 				if (vfnptr_iter->GetIfaceList().empty())
   251 				{
   252 					// No ifaces at all -> Deactivate the hook
   253 
   254 					for (CStack<CHookContext>::iterator ctx_iter = m_ContextStack.begin();
   255 						ctx_iter != m_ContextStack.end(); ++ctx_iter)
   256 					{
   257 						ctx_iter->VfnPtrRemoved(&(*vfnptr_iter));
   258 					}
   259 
   260 					RevertAndRemoveVfnPtr(vfnptr_iter);
   261 				}
   262 			}
   263 
   264 			m_HookIDMan.Remove(hookid);
   265 			return true;
   266 		}
   267 
   268 		List<CVfnPtr>::iterator CSourceHookImpl::RevertAndRemoveVfnPtr(List<CVfnPtr>::iterator vfnptr_iter)
   269 		{
   270 			ICleanupTask *cleanupTask = vfnptr_iter->GetCleanupTask();
   271 
   272 			// Some vfnptrs require cleanup.
   273 			// Concrete case: on GCC, when the original vtable entry is not even
   274 			// we generate an even-aligned thunk to call the original function.
   275 			// If the vfnptr is being removed from a pre hook on the vfnptr
   276 			// we have to delay the cleanup of this thunk until the hook loop is done
   277 			// (because the orig function call mechanism is going to use the thunk).
   278 
   279 			if (cleanupTask != NULL)
   280 			{
   281 				// If this vfnptr is in use in one of the hook loops running at the moment
   282 				// Schedule it for removal on the DEEPEST hook loop.
   283 
   284 				size_t numOfContexts = m_ContextStack.size();
   285 				// m_ContextStack.at(0) is the deepest hook context
   286 				// m_ContextStack.at(size-1) = m_ContextStack.front is the uppermost
   287 
   288 				bool cleanupImmedieately = true;
   289 
   290 				CVfnPtr *vfnPtrObjAddr = &(*vfnptr_iter);
   291 				for (size_t i = 0; i < numOfContexts; ++i)
   292 				{
   293 					CHookContext &context = m_ContextStack.at(i);
   294 					if (context.pVfnPtr == vfnPtrObjAddr)
   295 					{
   296 						// Found a hook context using this vfnptr at the moment.
   297 						context.m_CleanupTask = cleanupTask;
   298 						cleanupImmedieately = false;			// Delay the cleanup
   299 						break;
   300 					}
   301 				}
   302 
   303 				if (cleanupImmedieately)
   304 				{
   305 					cleanupTask->CleanupAndDeleteThis();
   306 				}
   307 			}
   308 
   309 			// Do the work
   310 			vfnptr_iter->Revert();
   311 			return m_VfnPtrs.erase(vfnptr_iter);
   312 		}
   313 
   314 		void CSourceHookImpl::SetRes(META_RES res)
   315 		{
   316 			*m_ContextStack.front().pCurRes = res;
   317 		}
   318 
   319 		META_RES CSourceHookImpl::GetPrevRes()
   320 		{
   321 			return *m_ContextStack.front().pPrevRes;
   322 		}
   323 
   324 		META_RES CSourceHookImpl::GetStatus()
   325 		{
   326 			return *m_ContextStack.front().pStatus;
   327 
   328 		}
   329 
   330 		const void *CSourceHookImpl::GetOrigRet()
   331 		{
   332 			return m_ContextStack.front().pOrigRet;
   333 		}
   334 
   335 		const void *CSourceHookImpl::GetOverrideRet()
   336 		{
   337 			return (*m_ContextStack.front().pStatus < MRES_OVERRIDE) ?
   338 				NULL : m_ContextStack.front().pOverrideRet;
   339 		}
   340 
   341 		void *CSourceHookImpl::GetIfacePtr()
   342 		{
   343 			// If in recall: return last one
   344 			if (m_ContextStack.front().m_State >= CHookContext::State_Recall_Pre &&
   345 				m_ContextStack.front().m_State <= CHookContext::State_Recall_PostVP)
   346 			{
   347 				return m_ContextStack.second().pIfacePtr;
   348 			}
   349 			else
   350 			{
   351 				return m_ContextStack.front().pIfacePtr;
   352 			}
   353 		}
   354 
   355 		void *CSourceHookImpl::GetOverrideRetPtr()
   356 		{
   357 			return m_ContextStack.front().pOverrideRet;
   358 		}
   359 
   360 		void CSourceHookImpl::UnloadPlugin(Plugin plug)
   361 		{
   362 			// 1) Remove all hooks by this plugin
   363 
   364 			CVector<int> removehooks;
   365 			m_HookIDMan.FindAllHooks(removehooks, plug);
   366 
   367 			for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
   368 				RemoveHookByID(*iter);
   369 
   370 			// 2) Remove all hook managers
   371 			for (CHookManList::iterator iter = m_HookManList.begin(); iter != m_HookManList.end(); )
   372 			{
   373 				if (iter->GetOwnerPlugin() == plug)
   374 					iter = RemoveHookManager(iter);
   375 				else
   376 					++iter;
   377 			}
   378 		}
   379 
   380 		CHookManList::iterator CSourceHookImpl::RemoveHookManager(CHookManList::iterator hookman_iter)
   381 		{
   382 			// 2) Remove it
   383 			for (CVfnPtrList::iterator vfnptr_iter = m_VfnPtrs.begin();
   384 				vfnptr_iter != m_VfnPtrs.end();)
   385 			{
   386 				if (!vfnptr_iter->HookManRemoved(&(*hookman_iter)))
   387 				{
   388 					// This vfnptr has no more hook managers
   389 					// and asks to be removed.
   390 
   391 					m_HookIDMan.RemoveAll(vfnptr_iter->GetPtr());
   392 
   393 					vfnptr_iter = RevertAndRemoveVfnPtr(vfnptr_iter);
   394 				}
   395 				else
   396 				{
   397 					++vfnptr_iter;
   398 				}
   399 			}
   400 
   401 			return m_HookManList.erase(hookman_iter);
   402 		}
   403 
   404 		void CSourceHookImpl::RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc)
   405 		{
   406 			// Find the hook manager
   407 			CHookManList::iterator hookman_iter = m_HookManList.find(CHookManager::Descriptor(plug, pubFunc));
   408 
   409 			if (hookman_iter != m_HookManList.end())
   410 			{
   411 				RemoveHookManager(hookman_iter);
   412 			}
   413 		}
   414 
   415 		void CSourceHookImpl::SetIgnoreHooks(void *vfnptr)
   416 		{
   417 			CHookContext ctx;
   418 			ctx.m_State = CHookContext::State_Ignore;
   419 			m_ContextStack.push(ctx);
   420 		}
   421 
   422 		void CSourceHookImpl::ResetIgnoreHooks(void *vfnptr)
   423 		{
   424 			if (!m_ContextStack.empty() && m_ContextStack.front().m_State == CHookContext::State_Ignore)
   425 			{
   426 				// Actually use EndContext
   427 				// instead of m_ContextStack.pop directly
   428 				// because it runs the cleanup task if neccesary
   429 				EndContext(&(m_ContextStack.front()));
   430 			}
   431 		}
   432 
   433 		void *CSourceHookImpl::GetOrigVfnPtrEntry(void *vfnptr)
   434 		{
   435 			for (CVfnPtrList::iterator vfnptr_iter = m_VfnPtrs.begin(); vfnptr_iter != m_VfnPtrs.end(); ++vfnptr_iter)
   436 			{
   437 				if (vfnptr_iter->GetPtr() == vfnptr)
   438 					return vfnptr_iter->GetOrigEntry();
   439 			}
   440 			return NULL;
   441 		}
   442 
   443 		void CSourceHookImpl::DoRecall()
   444 		{
   445 			CHookContext newCtx;
   446 			CHookContext &curCtx = m_ContextStack.front();
   447 
   448 
   449 			newCtx.m_State = curCtx.m_State + (CHookContext::State_Recall_Pre - CHookContext::State_Pre);
   450 			if (newCtx.m_State == CHookContext::State_Recall_Post || newCtx.m_State == CHookContext::State_Recall_PostVP)
   451 			{
   452 				// Also save orig ret
   453 				newCtx.pOrigRet = curCtx.pOrigRet;
   454 			}
   455 
   456 			// The hookfunc usually does this, but it won't have a chance to see it, 
   457 			// so for recalls, we update status here if it's required
   458 			if (*curCtx.pCurRes > *curCtx.pStatus) 
   459 				*curCtx.pStatus = *curCtx.pCurRes;
   460 
   461 			newCtx.pStatus = curCtx.pStatus;
   462 			newCtx.pOverrideRet = curCtx.pOverrideRet;
   463 			newCtx.pPrevRes = curCtx.pPrevRes;
   464 			newCtx.m_Iter = curCtx.m_Iter;
   465 
   466 			// Take this with us!
   467 			newCtx.pCurRes = curCtx.pCurRes;
   468 
   469 			m_ContextStack.push(newCtx);
   470 			curCtx.m_State = CHookContext::State_Dead;
   471 		}
   472 
   473 		IHookContext *CSourceHookImpl::SetupHookLoop(IHookManagerInfo *hi, void *vfnptr, void *thisptr, void **origCallAddr, META_RES *statusPtr,
   474 			META_RES *prevResPtr, META_RES *curResPtr, const void *origRetPtr, void *overrideRetPtr)
   475 		{
   476 			CHookContext *pCtx = NULL;
   477 			CHookContext *oldctx = m_ContextStack.empty() ? NULL : &m_ContextStack.front();
   478 			if (oldctx)
   479 			{
   480 				// SH_CALL
   481 				if (oldctx->m_State == CHookContext::State_Ignore)
   482 				{
   483 					*statusPtr = MRES_IGNORED;
   484 					oldctx->m_CallOrig = true;
   485 					oldctx->m_State = CHookContext::State_Dead;
   486 
   487 					List<CVfnPtr*> &vfnptr_list = static_cast<CHookManager*>(hi)->GetVfnPtrList();
   488 					List<CVfnPtr*>::iterator vfnptr_iter;
   489 					for (vfnptr_iter = vfnptr_list.begin();
   490 						vfnptr_iter != vfnptr_list.end(); ++vfnptr_iter)
   491 					{
   492 						if (**vfnptr_iter == vfnptr)
   493 							break;
   494 					}
   495 					if (vfnptr_iter == vfnptr_list.end())
   496 					{
   497 						SH_ASSERT(false, ("How can a hook exist on a vfnptr which we don't have in our db?!"));
   498 					}
   499 					else
   500 					{
   501 						*origCallAddr = (*vfnptr_iter)->GetOrigCallAddr();
   502 						oldctx->pVfnPtr = *vfnptr_iter;
   503 					}
   504 
   505 					oldctx->pOrigRet = origRetPtr;
   506 					return oldctx;
   507 				}
   508 				// Recall
   509 				else if (oldctx->m_State >= CHookContext::State_Recall_Pre &&
   510 					oldctx->m_State <= CHookContext::State_Recall_PostVP)
   511 				{
   512 					pCtx = oldctx;
   513 
   514 					*statusPtr = *(oldctx->pStatus);
   515 					*prevResPtr = *(oldctx->pPrevRes);
   516 
   517 					pCtx->m_Iter = oldctx->m_Iter;
   518 
   519 					// Only have possibility of calling the orig func in pre recall mode
   520 					pCtx->m_CallOrig = (oldctx->m_State == CHookContext::State_Recall_Pre || 
   521 						oldctx->m_State == CHookContext::State_Recall_PreVP);
   522 
   523 					overrideRetPtr = pCtx->pOverrideRet;
   524 
   525 					// When the status is low so there's no override return value and we're in a post recall,
   526 					// give it the orig return value as override return value.
   527 					if (pCtx->m_State == CHookContext::State_Recall_Post ||
   528 						pCtx->m_State == CHookContext::State_Recall_PostVP)
   529 					{
   530 						origRetPtr = oldctx->pOrigRet;
   531 						if (*statusPtr < MRES_OVERRIDE)
   532 							overrideRetPtr = const_cast<void*>(pCtx->pOrigRet);
   533 					}
   534 				}
   535 			}
   536 			if (!pCtx)
   537 			{
   538 				pCtx = m_ContextStack.make_next();
   539 				pCtx->m_State = CHookContext::State_Born;
   540 				pCtx->m_CallOrig = true;
   541 			}
   542 
   543 			pCtx->pIface = NULL;
   544 
   545 			List<CVfnPtr*> &vfnptr_list = static_cast<CHookManager*>(hi)->GetVfnPtrList();
   546 			List<CVfnPtr*>::iterator vfnptr_iter; 
   547 			for (vfnptr_iter = vfnptr_list.begin();
   548 				vfnptr_iter != vfnptr_list.end(); ++vfnptr_iter)
   549 			{
   550 				if (**vfnptr_iter == vfnptr)
   551 					break;
   552 			}
   553 			if (vfnptr_iter == vfnptr_list.end())
   554 			{
   555 				pCtx->m_State = CHookContext::State_Dead;
   556 			}
   557 			else
   558 			{
   559 				pCtx->pVfnPtr = *vfnptr_iter;
   560 				*origCallAddr = pCtx->pVfnPtr->GetOrigCallAddr();
   561 				pCtx->pIface = pCtx->pVfnPtr->FindIface(thisptr);
   562 			}
   563 
   564 			pCtx->pStatus = statusPtr;
   565 			pCtx->pPrevRes = prevResPtr;
   566 			pCtx->pCurRes = curResPtr;
   567 			pCtx->pThisPtr = thisptr;
   568 			pCtx->pOverrideRet = overrideRetPtr;
   569 			pCtx->pOrigRet = origRetPtr;
   570 
   571 			return pCtx;
   572 		}
   573 
   574 		void CSourceHookImpl::EndContext(IHookContext *pCtx)
   575 		{
   576 			// Do clean up task, if any is associated with this context
   577 			m_ContextStack.front().DoCleanupTaskAndDeleteIt();
   578 			// Then remove it
   579 			m_ContextStack.pop();
   580 		}
   581 
   582 		void CSourceHookImpl::CompleteShutdown()
   583 		{
   584 			CVector<int> removehooks;
   585 			m_HookIDMan.FindAllHooks(removehooks);
   586 
   587 			for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
   588 				RemoveHookByID(*iter);
   589 		}
   590 
   591 		void CSourceHookImpl::PausePlugin(Plugin plug)
   592 		{
   593 			CVector<int> pausehooks;
   594 			m_HookIDMan.FindAllHooks(pausehooks, plug);
   595 
   596 			for (CVector<int>::iterator iter = pausehooks.begin(); iter != pausehooks.end(); ++iter)
   597 				PauseHookByID(*iter);
   598 		}
   599 
   600 		void CSourceHookImpl::UnpausePlugin(Plugin plug)
   601 		{
   602 			CVector<int> unpausehooks;
   603 			m_HookIDMan.FindAllHooks(unpausehooks, plug);
   604 
   605 			for (CVector<int>::iterator iter = unpausehooks.begin(); iter != unpausehooks.end(); ++iter)
   606 				UnpauseHookByID(*iter);
   607 		}
   608 
   609 		bool CSourceHookImpl::PauseHookByID(int hookid)
   610 		{
   611 			return SetHookPaused(hookid, true);
   612 		}
   613 
   614 		bool CSourceHookImpl::UnpauseHookByID(int hookid)
   615 		{
   616 			return SetHookPaused(hookid, false);
   617 		}
   618 
   619 		bool CSourceHookImpl::SetHookPaused(int hookid, bool paused)
   620 		{
   621 			const CHookIDManager::Entry *hentry;
   622 
   623 			hentry = m_HookIDMan.QueryHook(hookid);
   624 			if (!hentry)
   625 			{
   626 				// hookid doesn't exist !
   627 				return false;
   628 			}
   629 
   630 			// find vfnptr
   631 			List<CVfnPtr>::iterator vfnptr_iter = m_VfnPtrs.find(hentry->vfnptr);
   632 			if (vfnptr_iter == m_VfnPtrs.end())
   633 				return false;
   634 
   635 			// find iface
   636 			List<CIface>::iterator iface_iter = vfnptr_iter->GetIfaceList().find(hentry->adjustediface);
   637 			if (iface_iter == vfnptr_iter->GetIfaceList().end())
   638 				return false;
   639 
   640 			// find hook
   641 			List<CHook> &hooks = hentry->post ? iface_iter->GetPostHookList() : iface_iter->GetPreHookList();
   642 			List<CHook>::iterator hook_iter = hooks.find(hookid);
   643 			if (hook_iter == hooks.end())
   644 				return false;
   645 
   646 			hook_iter->SetPaused(paused);
   647 			return true;
   648 		}
   649 
   650 		//////////////////////////////////////////////////////////////////////////
   651 		// CHookContext
   652 		//////////////////////////////////////////////////////////////////////////
   653 		ISHDelegate *CHookContext::GetNext()
   654 		{
   655 			CIface *pVPIface;
   656 			switch (m_State)
   657 			{
   658 			case State_Dead:
   659 				return NULL;
   660 
   661 			case State_Born:
   662 				m_Iter = List<CHook>::iterator();
   663 				m_State = State_Pre;
   664 
   665 				// fall-through
   666 			case State_Recall_Pre:
   667 				m_State = State_Pre;
   668 			case State_Pre:
   669 				if (pIface)
   670 				{
   671 					if (!m_Iter)
   672 						m_Iter = pIface->GetPreHookList().begin();
   673 					else
   674 						++m_Iter;
   675 					SkipPaused(m_Iter, pIface->GetPreHookList());
   676 
   677 					if (m_Iter != pIface->GetPreHookList().end())
   678 					{
   679 						pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
   680 						return m_Iter->GetHandler();
   681 					}
   682 				}
   683 
   684 				// end of normal hooks -> VP
   685 				
   686 				m_State = State_PreVP;
   687 				m_Iter = List<CHook>::iterator();
   688 
   689 				// fall-through
   690 			case State_Recall_PreVP:
   691 				m_State = State_PreVP;
   692 			case State_PreVP:
   693 				pVPIface = pVfnPtr->FindIface(NULL);
   694 				if (pVPIface)
   695 				{
   696 					if (!m_Iter)
   697 						m_Iter = pVPIface->GetPreHookList().begin();
   698 					else
   699 						++m_Iter;
   700 					SkipPaused(m_Iter, pVPIface->GetPreHookList());
   701 
   702 					if (m_Iter != pVPIface->GetPreHookList())
   703 					{
   704 						pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
   705 						return m_Iter->GetHandler();
   706 					}
   707 				}
   708 
   709 				// end VP hooks -> orig call
   710 
   711 				m_State = State_OrigCall;
   712 				return NULL;
   713 				
   714 			case State_OrigCall:
   715 				m_Iter = List<CHook>::iterator();
   716 				m_State = State_Post;
   717 
   718 				// fall-through
   719 			case State_Post:
   720 				if (pIface)
   721 				{
   722 					if (!m_Iter)
   723 						m_Iter = pIface->GetPostHookList().begin();
   724 					else
   725 						++m_Iter;
   726 					SkipPaused(m_Iter, pIface->GetPostHookList());
   727 
   728 					if (m_Iter != pIface->GetPostHookList().end())
   729 					{
   730 						pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
   731 						return m_Iter->GetHandler();
   732 					}
   733 				}
   734 				// end of normal hooks -> VP
   735 
   736 				m_State = State_PostVP;
   737 				m_Iter = List<CHook>::iterator();
   738 
   739 				// fall-through
   740 			case State_PostVP:
   741 				pVPIface = pVfnPtr->FindIface(NULL);
   742 				if (pVPIface)
   743 				{
   744 					if (!m_Iter)
   745 						m_Iter = pVPIface->GetPostHookList().begin();
   746 					else
   747 						++m_Iter;
   748 					SkipPaused(m_Iter, pVPIface->GetPostHookList());
   749 
   750 					if (m_Iter != pVPIface->GetPostHookList())
   751 					{
   752 						pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
   753 						return m_Iter->GetHandler();
   754 					}
   755 				}
   756 
   757 				// end VP hooks -> done
   758 
   759 				m_State = State_Dead;
   760 				return NULL;
   761 
   762 			case State_Recall_Post:
   763 				// Skip pre hooks _and_ orig call
   764 				m_State = State_Post;
   765 				return NULL;
   766 
   767 			case State_Recall_PostVP:
   768 				// Skip pre hooks _and_ orig call
   769 				m_State = State_PostVP;
   770 				return NULL;
   771 			}
   772 			return NULL;
   773 		}
   774 
   775 		void *CHookContext::GetOverrideRetPtr()
   776 		{
   777 			return pOverrideRet;
   778 		}
   779 
   780 		const void *CHookContext::GetOrigRetPtr()
   781 		{
   782 			return pOrigRet;
   783 		}
   784 
   785 		bool CHookContext::ShouldCallOrig()
   786 		{
   787 			return m_CallOrig;
   788 		}
   789 
   790 		void CHookContext::HookRemoved(List<CHook>::iterator oldhookiter, List<CHook>::iterator nexthookiter)
   791 		{
   792 			if (m_Iter == oldhookiter)
   793 			{
   794 				m_Iter = nexthookiter;
   795 				--m_Iter;
   796 			}
   797 		}
   798 
   799 		void CHookContext::IfaceRemoved(CIface *iface)
   800 		{
   801 			if (pIface == iface)
   802 				pIface = NULL;
   803 		}
   804 
   805 		void CHookContext::VfnPtrRemoved(CVfnPtr *vfnptr)
   806 		{
   807 			if (pVfnPtr == vfnptr)
   808 			{
   809 				// Don't set pVfnPtr = NULL here!
   810 				// It may be used still.
   811 				// RevertAndRemoveVfnPtr uses it to find the hook context
   812 				// to which to attach the cleanup task of the vfnptr.
   813 
   814 				//pVfnPtr = NULL;
   815 				m_State = State_Dead;
   816 			}
   817 		}
   818 
   819 		void CHookContext::DoCleanupTaskAndDeleteIt()
   820 		{
   821 			if (m_CleanupTask != NULL)
   822 			{
   823 				m_CleanupTask->CleanupAndDeleteThis();
   824 			}
   825 		}
   826 	}
   827 }