1 /* ======== SourceHook ========
2 * Copyright (C) 2004-2007 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): Pavol "PM OnoTo" Marko
8 * Contributors: Scott "Damaged Soul" Ehlert
9 * ============================
13 * @file sourcehook.cpp
14 * @brief Contains the implementation of the SourceHook API
21 #include "sourcehook_impl.h"
22 #include "sh_memory.h"
27 int HashFunction<int>(const int & k)
32 int Compare<int>(const int & k1, const int & k2)
45 //////////////////////////////////////////////////////////////////////////
47 //////////////////////////////////////////////////////////////////////////
48 ProtoInfo *CProto::DupProto(const ProtoInfo *src)
53 PassInfo *newParamsPassInfo = new PassInfo[src->numOfParams + 1];
54 for (int i = 0; i <= src->numOfParams; ++i)
55 newParamsPassInfo[i] = src->paramsPassInfo[i];
57 ProtoInfo *newProto = new ProtoInfo;
58 newProto->retPassInfo = src->retPassInfo;
59 newProto->paramsPassInfo = newParamsPassInfo;
60 newProto->numOfParams = src->numOfParams;
61 newProto->convention = src->convention;
66 void CProto::FreeProto(ProtoInfo *prot)
71 delete [] prot->paramsPassInfo;
75 bool CProto::Equal(const ProtoInfo *p1, const ProtoInfo *p2)
80 if (p1->numOfParams != p2->numOfParams)
83 if (p1->convention != ProtoInfo::CallConv_Unknown && p2->convention != ProtoInfo::CallConv_Unknown &&
84 p1->convention != p2->convention)
87 if (p1->retPassInfo.size != p2->retPassInfo.size)
90 // Skip params[0] : dummy
91 for (int i = 1; i <= p1->numOfParams; ++i)
93 if (p1->paramsPassInfo[i].size != p2->paramsPassInfo[i].size)
95 if (p1->paramsPassInfo[i].type != PassInfo::PassType_Unknown && p2->paramsPassInfo[i].type != PassInfo::PassType_Unknown)
97 if (p1->paramsPassInfo[i].type != p2->paramsPassInfo[i].type)
99 if (p1->paramsPassInfo[i].flags != p2->paramsPassInfo[i].flags)
108 //////////////////////////////////////////////////////////////////////////
109 // CHookManContainerList
110 //////////////////////////////////////////////////////////////////////////
111 CHookManagerContainer &CHookManContainerList::GetContainer(int vtbloffs, int vtblidx, const CProto &proto)
113 iterator iter = find(CHookManagerContainer::Descriptor(vtbloffs, vtblidx, proto));
116 CHookManagerContainer newCont(vtbloffs, vtblidx, proto);
126 void CHookManContainerList::RemoveHookMans(Plugin plug)
128 for (iterator iter = begin(); iter != end(); ++iter)
129 iter->RemoveHookMans(plug);
132 void CHookManContainerList::RemoveHookMans(Plugin plug, HookManagerPubFunc pubFunc)
134 for (iterator iter = begin(); iter != end(); ++iter)
135 iter->RemoveHookMans(plug, pubFunc);
138 //////////////////////////////////////////////////////////////////////////
139 // CHookManagerContainer
140 //////////////////////////////////////////////////////////////////////////
142 bool CHookManagerContainer::AddHookManager(const CHookManager &hookMan)
146 // Don't accept invalid hook managers
150 // Check whether such a hook manager already exists; if yes, ignore.
151 iter = find(hookMan);
154 // but make sure that it's activated (ms_HI stored)
159 // It doesn't -> add it. Add it to the end of its version group.
160 for (iter = begin(); iter != end(); ++iter)
162 if (iter->GetVersion() < hookMan.GetVersion())
166 bool isBeginning = iter == begin();
168 insert(iter, hookMan);
176 // If the next hookman isn't empty (there were hooks before, but this hookman is better)
177 // transfer its vfnptrs and deactivate it
179 iterator second = iter;
182 if (!second->GetVfnPtrList().empty())
184 // Move the vfnptrs from the old hook manager to the new one
185 iter->GetVfnPtrList() = second->GetVfnPtrList();
186 second->GetVfnPtrList().clear();
188 // Unregister the old one, register the new one
189 second->Unregister();
191 // Go through all vfnptrs in this hookman and patch them to point to the new manager's handler!
192 // :TODO: change this to something like for_all :X
193 for (List<CVfnPtr>::iterator vfnptr_iter = iter->GetVfnPtrList().begin();
194 vfnptr_iter != iter->GetVfnPtrList().end(); ++vfnptr_iter)
196 vfnptr_iter->Patch(iter->GetHookFunc());
201 // activate the new active hookman
208 void CHookManagerContainer::RemoveHookMans(Plugin plug, HookManagerPubFunc pubFunc)
210 bool erasethis = false;
212 for (iterator iter = begin(); iter != end(); erasethis ? (iter = erase(iter)) : ++iter)
215 if (iter->GetOwnerPlugin() == plug
216 && (pubFunc == NULL ? true : (iter->GetPubFunc() == pubFunc)))
218 if (iter == begin() && !iter->GetVfnPtrList().empty())
220 // The hook manager is active
221 // -> Transfer its hooks to the next hook manager (which _has_ to exist)
222 SH_ASSERT(size() > 1, ("No more hook managers!?"));
224 iterator second = iter;
227 SH_ASSERT(second->GetOwnerPlugin() != plug, ("New hook manager from same plugin!"));
229 second->GetVfnPtrList() = iter->GetVfnPtrList();
230 iter->GetVfnPtrList().clear();
235 // Go through all vfnptrs in this hookman and patch them to point to the new manager's handler!
236 // :TODO: change this to something like for_all :X
237 for (List<CVfnPtr>::iterator vfnptr_iter = second->GetVfnPtrList().begin();
238 vfnptr_iter != second->GetVfnPtrList().end(); ++vfnptr_iter)
240 vfnptr_iter->Patch(second->GetHookFunc());
248 //////////////////////////////////////////////////////////////////////////
250 //////////////////////////////////////////////////////////////////////////
251 void CHookManager::SetInfo(int hookman_version, int vtbloffs, int vtblidx,
252 ProtoInfo *proto, void *hookfunc_vfnptr)
254 m_Version = hookman_version;
255 m_VtblOffs = vtbloffs;
258 m_HookfuncVfnptr = hookfunc_vfnptr;
261 CVfnPtr &CHookManager::GetVfnPtr(void *vfnptr)
263 List<CVfnPtr>::iterator iter = m_VfnPtrList.find(vfnptr);
264 if (iter == m_VfnPtrList.end())
266 CVfnPtr newVfnPtr(vfnptr);
267 m_VfnPtrList.push_back(newVfnPtr);
269 newVfnPtr.Patch(GetHookFunc());
271 return m_VfnPtrList.back();
279 //////////////////////////////////////////////////////////////////////////
281 //////////////////////////////////////////////////////////////////////////
283 bool CVfnPtr::Patch(void *newValue)
285 if (!SetMemAccess(m_Ptr, sizeof(void*), SH_MEM_READ | SH_MEM_WRITE))
288 *reinterpret_cast<void**>(m_Ptr) = newValue;
293 CIface &CVfnPtr::GetIface(void *iface)
295 List<CIface>::iterator iter = m_IfaceList.find(iface);
296 if (iter == m_IfaceList.end())
298 CIface newIface(iface);
301 m_IfaceList.push_front(newIface);
302 return m_IfaceList.front();
306 m_IfaceList.push_back(newIface);
307 return m_IfaceList.back();
316 //////////////////////////////////////////////////////////////////////////
318 //////////////////////////////////////////////////////////////////////////
319 CHookIDManager::CHookIDManager()
323 int CHookIDManager::New(const CProto &proto, int vtbl_offs, int vtbl_idx, void *vfnptr,
324 void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
326 Entry tmp(proto, vtbl_offs, vtbl_idx, vfnptr, adjustediface, plug, thisptr_offs, handler, post);
328 size_t cursize = m_Entries.size();
329 for (size_t i = 0; i < cursize; ++i)
331 if (m_Entries[i].isfree)
334 return static_cast<int>(i) + 1;
338 m_Entries.push_back(tmp);
339 return static_cast<int>(m_Entries.size()); // return size() because hookid = id+1 anyway
342 bool CHookIDManager::Remove(int hookid)
344 int realid = hookid - 1;
345 if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
348 m_Entries[realid].isfree = true;
350 // :TODO: remove free ids from back sometimes ??
355 const CHookIDManager::Entry * CHookIDManager::QueryHook(int hookid)
357 int realid = hookid - 1;
358 if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
361 return &m_Entries[realid];
364 void CHookIDManager::FindAllHooks(CVector<int> &output, const CProto &proto, int vtbl_offs,
365 int vtbl_idx, void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
367 // oh my god, a lot of parameters...
368 size_t cursize = m_Entries.size();
369 for (size_t i = 0; i < cursize; ++i)
371 if (!m_Entries[i].isfree && m_Entries[i].proto == proto && m_Entries[i].vtbl_offs == vtbl_offs &&
372 m_Entries[i].vtbl_idx == vtbl_idx && m_Entries[i].adjustediface == adjustediface && m_Entries[i].plug == plug &&
373 m_Entries[i].thisptr_offs == thisptr_offs && m_Entries[i].handler->IsEqual(handler) && m_Entries[i].post == post)
375 output.push_back(static_cast<int>(i) + 1);
380 void CHookIDManager::FindAllHooks(CVector<int> &output)
382 size_t cursize = m_Entries.size();
383 for (size_t i = 0; i < cursize; ++i)
385 if (!m_Entries[i].isfree)
386 output.push_back(static_cast<int>(i) + 1);
390 void CHookIDManager::FindAllHooks(CVector<int> &output, Plugin plug)
392 size_t cursize = m_Entries.size();
393 for (size_t i = 0; i < cursize; ++i)
395 if (!m_Entries[i].isfree && m_Entries[i].plug == plug)
396 output.push_back(static_cast<int>(i) + 1);
401 //////////////////////////////////////////////////////////////////////////
403 //////////////////////////////////////////////////////////////////////////
406 CSourceHookImpl::CSourceHookImpl()
409 CSourceHookImpl::~CSourceHookImpl()
413 int CSourceHookImpl::GetIfaceVersion()
415 return SH_IFACE_VERSION;
418 int CSourceHookImpl::GetImplVersion()
420 return SH_IMPL_VERSION;
423 int CSourceHookImpl::AddHook(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
424 ISHDelegate *handler, bool post)
426 if (mode != Hook_Normal && mode != Hook_VP && mode != Hook_DVP)
429 // Get info about hook manager
430 CHookManager hookManager(plug, myHookMan);
434 // Make sure that the container exists
435 CHookManagerContainer &container = m_HookManContainers.GetContainer(hookManager.GetVtblOffs(),
436 hookManager.GetVtblIdx(), hookManager.GetProto());
438 container.AddHookManager(hookManager);
442 // For some weird reason, the container is empty!
443 SH_ASSERT(0, ("No hookman found - but we've just added one!"));
447 CHookManager &hookMan = container.GetActiveHookManager();
449 void *adjustediface = NULL;
450 void **cur_vtptr = NULL;
451 void *cur_vfnptr = NULL;
457 adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
459 cur_vtptr = *reinterpret_cast<void***>(
460 reinterpret_cast<char*>(adjustediface) + hookMan.GetVtblOffs());
461 cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + hookMan.GetVtblIdx());
466 adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
468 cur_vtptr = *reinterpret_cast<void***>(
469 reinterpret_cast<char*>(adjustediface) + hookMan.GetVtblOffs());
470 cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + hookMan.GetVtblIdx());
472 adjustediface = NULL;
476 adjustediface = NULL;
478 cur_vtptr = reinterpret_cast<void**>(iface);
479 cur_vfnptr = cur_vtptr + hookMan.GetVtblIdx();
484 CVfnPtr &vfnPtr = hookMan.GetVfnPtr(cur_vfnptr);
485 CIface &ifaceinst = vfnPtr.GetIface(adjustediface);
488 CHook hook(plug, thisptr_offs, handler,
490 m_HookIDMan.New(hookMan.GetProto(), hookMan.GetVtblOffs(), hookMan.GetVtblIdx(),
491 cur_vfnptr, adjustediface, plug, thisptr_offs, handler, post)
496 ifaceinst.GetPostHookList().push_back(hook);
498 ifaceinst.GetPreHookList().push_back(hook);
503 bool CSourceHookImpl::RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
504 ISHDelegate *handler, bool post)
506 // Get info about hook manager and compute adjustediface
507 CHookManager tmpHookMan(plug, myHookMan);
509 void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface)+thisptr_offs);
511 // Loop through all hooks and remove those which match:
512 // hookman, vfnptr, iface, plug, adjusted iface, this ptr offs, handler, post
513 CVector<int> removehooks;
514 m_HookIDMan.FindAllHooks(removehooks, tmpHookMan.GetProto(), tmpHookMan.GetVtblOffs(), tmpHookMan.GetVtblIdx(),
515 adjustediface, plug, thisptr_offs, handler, post);
517 if (removehooks.empty())
522 for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
524 if (RemoveHookByID(*iter))
530 bool CSourceHookImpl::RemoveHookByID(int hookid)
532 const CHookIDManager::Entry *hentry;
534 hentry = m_HookIDMan.QueryHook(hookid);
537 // hookid doesn't exist !
542 CHookManContainerList::iterator hmcl_iter = m_HookManContainers.find(
543 CHookManagerContainer::Descriptor(hentry->vtbl_offs, hentry->vtbl_idx, hentry->proto));
545 if (hmcl_iter == m_HookManContainers.end() || !(*hmcl_iter))
548 CHookManager &hookman = hmcl_iter->GetActiveHookManager();
551 List<CVfnPtr>::iterator vfnptr_iter = hookman.GetVfnPtrList().find(hentry->vfnptr);
552 if (vfnptr_iter == hookman.GetVfnPtrList().end())
556 List<CIface>::iterator iface_iter = vfnptr_iter->GetIfaceList().find(hentry->adjustediface);
557 if (iface_iter == vfnptr_iter->GetIfaceList().end())
561 List<CHook> &hooks = hentry->post ? iface_iter->GetPostHookList() : iface_iter->GetPreHookList();
562 List<CHook>::iterator hook_iter = hooks.find(hookid);
563 if (hook_iter == hooks.end())
566 hook_iter->GetHandler()->DeleteThis();
569 List<CHook>::iterator oldhookiter = hook_iter;
570 hook_iter = hooks.erase(hook_iter);
572 for (CStack<CHookContext>::iterator ctx_iter = m_ContextStack.begin();
573 ctx_iter != m_ContextStack.end(); ++ctx_iter)
575 ctx_iter->HookRemoved(oldhookiter, hook_iter);
578 if (iface_iter->GetPreHookList().empty() && iface_iter->GetPostHookList().empty())
580 // -> Kill all contexts that use it!
581 for (CStack<CHookContext>::iterator ctx_iter = m_ContextStack.begin();
582 ctx_iter != m_ContextStack.end(); ++ctx_iter)
584 ctx_iter->IfaceRemoved(&(*iface_iter));
587 // There are no hooks on this iface anymore...
588 iface_iter = vfnptr_iter->GetIfaceList().erase(iface_iter);
590 if (vfnptr_iter->GetIfaceList().empty())
592 // No ifaces at all -> Deactivate the hook
594 for (CStack<CHookContext>::iterator ctx_iter = m_ContextStack.begin();
595 ctx_iter != m_ContextStack.end(); ++ctx_iter)
597 ctx_iter->VfnPtrRemoved(&(*vfnptr_iter));
600 // Only patch the vfnptr back if the module is still in memory
601 // If it's not, do not remove stuff like we did before
602 // First off we did it wrong (shutdown the whole hookman, uh..) and secondly applications may be
603 // confused by RemoveHook returning false then (yeah, I know, I made this one up, no one checks for RemoveHook error)
604 if (ModuleInMemory(reinterpret_cast<char*>(vfnptr_iter->GetPtr()), SH_PTRSIZE))
606 vfnptr_iter->Revert();
609 hookman.GetVfnPtrList().erase(vfnptr_iter);
611 if (hookman.GetVfnPtrList().empty())
613 // Unregister the hook manager
614 hookman.Unregister();
619 m_HookIDMan.Remove(hookid);
623 void CSourceHookImpl::SetRes(META_RES res)
625 *m_ContextStack.front().pCurRes = res;
628 META_RES CSourceHookImpl::GetPrevRes()
630 return *m_ContextStack.front().pPrevRes;
633 META_RES CSourceHookImpl::GetStatus()
635 return *m_ContextStack.front().pStatus;
639 const void *CSourceHookImpl::GetOrigRet()
641 return m_ContextStack.front().pOrigRet;
644 const void *CSourceHookImpl::GetOverrideRet()
646 return (*m_ContextStack.front().pStatus < MRES_OVERRIDE) ?
647 NULL : m_ContextStack.front().pOverrideRet;
650 void *CSourceHookImpl::GetIfacePtr()
652 // If in recall: return last one
653 if (m_ContextStack.front().m_State >= CHookContext::State_Recall_Pre &&
654 m_ContextStack.front().m_State <= CHookContext::State_Recall_PostVP)
656 return m_ContextStack.second().pIfacePtr;
660 return m_ContextStack.front().pIfacePtr;
664 void *CSourceHookImpl::GetOverrideRetPtr()
666 return m_ContextStack.front().pOverrideRet;
669 void CSourceHookImpl::UnloadPlugin(Plugin plug)
671 // 1) Remove all hooks by this plugin
673 CVector<int> removehooks;
674 m_HookIDMan.FindAllHooks(removehooks, plug);
676 for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
677 RemoveHookByID(*iter);
679 // 2) Remove all hook managers
680 m_HookManContainers.RemoveHookMans(plug);
683 void CSourceHookImpl::RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc)
685 // 1) Remove all its hooks
686 CVector<int> removehooks;
688 // Get info about hook manager
689 CHookManager hookManager(plug, pubFunc);
693 // Make sure that the container exists
694 CHookManagerContainer &container = m_HookManContainers.GetContainer(hookManager.GetVtblOffs(),
695 hookManager.GetVtblIdx(), hookManager.GetProto());
697 CHookManagerContainer::iterator cont_iter = container.find(CHookManager::Descriptor(plug, pubFunc));
699 for (List<CVfnPtr>::iterator vfnptr_iter = cont_iter->GetVfnPtrList().begin();
700 vfnptr_iter != cont_iter->GetVfnPtrList().end(); ++vfnptr_iter)
702 for (List<CIface>::iterator iface_iter = vfnptr_iter->GetIfaceList().begin();
703 iface_iter != vfnptr_iter->GetIfaceList().end(); ++iface_iter)
705 List<CHook>::iterator hook_iter;
706 for (hook_iter = iface_iter->GetPreHookList().begin();
707 hook_iter != iface_iter->GetPreHookList().end(); ++hook_iter)
708 removehooks.push_back(hook_iter->GetID());
710 for (hook_iter = iface_iter->GetPostHookList().begin();
711 hook_iter != iface_iter->GetPostHookList().end(); ++hook_iter)
712 removehooks.push_back(hook_iter->GetID());
716 for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
717 RemoveHookByID(*iter);
720 container.RemoveHookMans(plug, pubFunc);
723 void CSourceHookImpl::SetIgnoreHooks(void *vfnptr)
726 ctx.m_State = CHookContext::State_Ignore;
727 m_ContextStack.push(ctx);
730 void CSourceHookImpl::ResetIgnoreHooks(void *vfnptr)
732 if (!m_ContextStack.empty() && m_ContextStack.front().m_State == CHookContext::State_Ignore)
733 m_ContextStack.pop();
736 void CSourceHookImpl::DoRecall()
739 CHookContext &curCtx = m_ContextStack.front();
742 newCtx.m_State = curCtx.m_State + (CHookContext::State_Recall_Pre - CHookContext::State_Pre);
743 if (newCtx.m_State == CHookContext::State_Recall_Post || newCtx.m_State == CHookContext::State_Recall_PostVP)
745 // Also save orig ret
746 newCtx.pOrigRet = curCtx.pOrigRet;
749 // The hookfunc usually does this, but it won't have a chance to see it,
750 // so for recalls, we update status here if it's required
751 if (*curCtx.pCurRes > *curCtx.pStatus)
752 *curCtx.pStatus = *curCtx.pCurRes;
754 newCtx.pStatus = curCtx.pStatus;
755 newCtx.pOverrideRet = curCtx.pOverrideRet;
756 newCtx.pPrevRes = curCtx.pPrevRes;
757 newCtx.m_Iter = curCtx.m_Iter;
759 // Take this with us!
760 newCtx.pCurRes = curCtx.pCurRes;
762 m_ContextStack.push(newCtx);
763 curCtx.m_State = CHookContext::State_Dead;
766 IHookContext *CSourceHookImpl::SetupHookLoop(IHookManagerInfo *hi, void *vfnptr, void *thisptr, void **origentry, META_RES *statusPtr,
767 META_RES *prevResPtr, META_RES *curResPtr, const void *origRetPtr, void *overrideRetPtr)
769 CHookContext *pCtx = NULL;
770 CHookContext *oldctx = m_ContextStack.empty() ? NULL : &m_ContextStack.front();
774 if (oldctx->m_State == CHookContext::State_Ignore)
776 *statusPtr = MRES_IGNORED;
777 oldctx->m_State = CHookContext::State_Dead;
779 List<CVfnPtr> &vfnptrList = static_cast<CHookManager*>(hi)->GetVfnPtrList();
780 List<CVfnPtr>::iterator vfnptr_iter = vfnptrList.find(vfnptr);
781 if (vfnptr_iter == vfnptrList.end())
787 *origentry = vfnptr_iter->GetOrigEntry();
790 oldctx->pOrigRet = origRetPtr;
794 else if (oldctx->m_State >= CHookContext::State_Recall_Pre &&
795 oldctx->m_State <= CHookContext::State_Recall_PostVP)
799 *statusPtr = *(oldctx->pStatus);
800 *prevResPtr = *(oldctx->pPrevRes);
802 pCtx->m_Iter = oldctx->m_Iter;
804 // Only have possibility of calling the orig func in pre recall mode
805 pCtx->m_CallOrig = (oldctx->m_State == CHookContext::State_Recall_Pre ||
806 oldctx->m_State == CHookContext::State_Recall_PreVP);
808 overrideRetPtr = pCtx->pOverrideRet;
810 // When the status is low so there's no override return value and we're in a post recall,
811 // give it the orig return value as override return value.
812 if (pCtx->m_State == CHookContext::State_Recall_Post ||
813 pCtx->m_State == CHookContext::State_Recall_PostVP)
815 origRetPtr = oldctx->pOrigRet;
816 if (*statusPtr < MRES_OVERRIDE)
817 overrideRetPtr = const_cast<void*>(pCtx->pOrigRet);
823 pCtx = m_ContextStack.make_next();
824 pCtx->m_State = CHookContext::State_Born;
825 pCtx->m_CallOrig = true;
830 List<CVfnPtr> &vfnptrList = static_cast<CHookManager*>(hi)->GetVfnPtrList();
831 List<CVfnPtr>::iterator vfnptr_iter = vfnptrList.find(vfnptr);
832 if (vfnptr_iter == vfnptrList.end())
834 pCtx->m_State = CHookContext::State_Dead;
838 pCtx->pVfnPtr = &(*vfnptr_iter);
839 *origentry = vfnptr_iter->GetOrigEntry();
840 pCtx->pIface = vfnptr_iter->FindIface(thisptr);
843 pCtx->pStatus = statusPtr;
844 pCtx->pPrevRes = prevResPtr;
845 pCtx->pCurRes = curResPtr;
846 pCtx->pThisPtr = thisptr;
847 pCtx->pOverrideRet = overrideRetPtr;
848 pCtx->pOrigRet = origRetPtr;
853 void CSourceHookImpl::EndContext(IHookContext *pCtx)
855 m_ContextStack.pop();
858 void CSourceHookImpl::CompleteShutdown()
860 CVector<int> removehooks;
861 m_HookIDMan.FindAllHooks(removehooks);
863 for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
864 RemoveHookByID(*iter);
867 void CSourceHookImpl::PausePlugin(Plugin plug)
869 CVector<int> pausehooks;
870 m_HookIDMan.FindAllHooks(pausehooks, plug);
872 for (CVector<int>::iterator iter = pausehooks.begin(); iter != pausehooks.end(); ++iter)
873 PauseHookByID(*iter);
876 void CSourceHookImpl::UnpausePlugin(Plugin plug)
878 CVector<int> unpausehooks;
879 m_HookIDMan.FindAllHooks(unpausehooks, plug);
881 for (CVector<int>::iterator iter = unpausehooks.begin(); iter != unpausehooks.end(); ++iter)
882 UnpauseHookByID(*iter);
885 bool CSourceHookImpl::PauseHookByID(int hookid)
887 return SetHookPaused(hookid, true);
890 bool CSourceHookImpl::UnpauseHookByID(int hookid)
892 return SetHookPaused(hookid, false);
895 bool CSourceHookImpl::SetHookPaused(int hookid, bool paused)
897 const CHookIDManager::Entry *hentry;
899 hentry = m_HookIDMan.QueryHook(hookid);
902 // hookid doesn't exist !
907 CHookManContainerList::iterator hmcl_iter = m_HookManContainers.find(
908 CHookManagerContainer::Descriptor(hentry->vtbl_offs, hentry->vtbl_idx, hentry->proto));
910 if (hmcl_iter == m_HookManContainers.end() || !(*hmcl_iter))
913 CHookManager &hookman = hmcl_iter->GetActiveHookManager();
916 List<CVfnPtr>::iterator vfnptr_iter = hookman.GetVfnPtrList().find(hentry->vfnptr);
917 if (vfnptr_iter == hookman.GetVfnPtrList().end())
921 List<CIface>::iterator iface_iter = vfnptr_iter->GetIfaceList().find(hentry->adjustediface);
922 if (iface_iter == vfnptr_iter->GetIfaceList().end())
926 List<CHook> &hooks = hentry->post ? iface_iter->GetPostHookList() : iface_iter->GetPreHookList();
927 List<CHook>::iterator hook_iter = hooks.find(hookid);
928 if (hook_iter == hooks.end())
931 hook_iter->SetPaused(paused);
935 //////////////////////////////////////////////////////////////////////////
937 //////////////////////////////////////////////////////////////////////////
938 ISHDelegate *CHookContext::GetNext()
948 m_Iter = List<CHook>::iterator();
952 case State_Recall_Pre:
958 m_Iter = pIface->GetPreHookList().begin();
961 SkipPaused(m_Iter, pIface->GetPreHookList());
963 if (m_Iter != pIface->GetPreHookList().end())
965 pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
966 return m_Iter->GetHandler();
970 // end of normal hooks -> VP
972 m_State = State_PreVP;
973 m_Iter = List<CHook>::iterator();
976 case State_Recall_PreVP:
977 m_State = State_PreVP;
979 pVPIface = pVfnPtr->FindIface(NULL);
983 m_Iter = pVPIface->GetPreHookList().begin();
986 SkipPaused(m_Iter, pVPIface->GetPreHookList());
988 if (m_Iter != pVPIface->GetPreHookList())
990 pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
991 return m_Iter->GetHandler();
995 // end VP hooks -> orig call
997 m_State = State_OrigCall;
1000 case State_OrigCall:
1001 m_Iter = List<CHook>::iterator();
1002 m_State = State_Post;
1009 m_Iter = pIface->GetPostHookList().begin();
1012 SkipPaused(m_Iter, pIface->GetPostHookList());
1014 if (m_Iter != pIface->GetPostHookList().end())
1016 pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
1017 return m_Iter->GetHandler();
1020 // end of normal hooks -> VP
1022 m_State = State_PostVP;
1023 m_Iter = List<CHook>::iterator();
1027 pVPIface = pVfnPtr->FindIface(NULL);
1031 m_Iter = pVPIface->GetPostHookList().begin();
1034 SkipPaused(m_Iter, pVPIface->GetPostHookList());
1036 if (m_Iter != pVPIface->GetPostHookList())
1038 pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
1039 return m_Iter->GetHandler();
1043 // end VP hooks -> done
1045 m_State = State_Dead;
1048 case State_Recall_Post:
1049 // Skip pre hooks _and_ orig call
1050 m_State = State_Post;
1053 case State_Recall_PostVP:
1054 // Skip pre hooks _and_ orig call
1055 m_State = State_PostVP;
1061 void *CHookContext::GetOverrideRetPtr()
1063 return pOverrideRet;
1066 const void *CHookContext::GetOrigRetPtr()
1071 bool CHookContext::ShouldCallOrig()
1076 void CHookContext::HookRemoved(List<CHook>::iterator oldhookiter, List<CHook>::iterator nexthookiter)
1078 if (m_Iter == oldhookiter)
1080 m_Iter = nexthookiter;
1085 void CHookContext::IfaceRemoved(CIface *iface)
1087 if (pIface == iface)
1091 void CHookContext::VfnPtrRemoved(CVfnPtr *vfnptr)
1093 if (pVfnPtr == vfnptr)
1096 m_State = State_Dead;