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))
290 *reinterpret_cast<void**>(m_Ptr) = newValue;
295 CIface &CVfnPtr::GetIface(void *iface)
297 List<CIface>::iterator iter = m_IfaceList.find(iface);
298 if (iter == m_IfaceList.end())
300 CIface newIface(iface);
303 m_IfaceList.push_front(newIface);
304 return m_IfaceList.front();
308 m_IfaceList.push_back(newIface);
309 return m_IfaceList.back();
318 //////////////////////////////////////////////////////////////////////////
320 //////////////////////////////////////////////////////////////////////////
321 CHookIDManager::CHookIDManager()
325 int CHookIDManager::New(const CProto &proto, int vtbl_offs, int vtbl_idx, void *vfnptr,
326 void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
328 Entry tmp(proto, vtbl_offs, vtbl_idx, vfnptr, adjustediface, plug, thisptr_offs, handler, post);
330 size_t cursize = m_Entries.size();
331 for (size_t i = 0; i < cursize; ++i)
333 if (m_Entries[i].isfree)
336 return static_cast<int>(i) + 1;
340 m_Entries.push_back(tmp);
341 return static_cast<int>(m_Entries.size()); // return size() because hookid = id+1 anyway
344 bool CHookIDManager::Remove(int hookid)
346 int realid = hookid - 1;
347 if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
350 m_Entries[realid].isfree = true;
352 // :TODO: remove free ids from back sometimes ??
357 const CHookIDManager::Entry * CHookIDManager::QueryHook(int hookid)
359 int realid = hookid - 1;
360 if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
363 return &m_Entries[realid];
366 void CHookIDManager::FindAllHooks(CVector<int> &output, const CProto &proto, int vtbl_offs,
367 int vtbl_idx, void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
369 // oh my god, a lot of parameters...
370 size_t cursize = m_Entries.size();
371 for (size_t i = 0; i < cursize; ++i)
373 if (!m_Entries[i].isfree && m_Entries[i].proto == proto && m_Entries[i].vtbl_offs == vtbl_offs &&
374 m_Entries[i].vtbl_idx == vtbl_idx && m_Entries[i].adjustediface == adjustediface && m_Entries[i].plug == plug &&
375 m_Entries[i].thisptr_offs == thisptr_offs && m_Entries[i].handler->IsEqual(handler) && m_Entries[i].post == post)
377 output.push_back(static_cast<int>(i) + 1);
382 void CHookIDManager::FindAllHooks(CVector<int> &output)
384 size_t cursize = m_Entries.size();
385 for (size_t i = 0; i < cursize; ++i)
387 if (!m_Entries[i].isfree)
388 output.push_back(static_cast<int>(i) + 1);
392 void CHookIDManager::FindAllHooks(CVector<int> &output, Plugin plug)
394 size_t cursize = m_Entries.size();
395 for (size_t i = 0; i < cursize; ++i)
397 if (!m_Entries[i].isfree && m_Entries[i].plug == plug)
398 output.push_back(static_cast<int>(i) + 1);
403 //////////////////////////////////////////////////////////////////////////
405 //////////////////////////////////////////////////////////////////////////
408 CSourceHookImpl::CSourceHookImpl()
411 CSourceHookImpl::~CSourceHookImpl()
415 int CSourceHookImpl::GetIfaceVersion()
417 return SH_IFACE_VERSION;
420 int CSourceHookImpl::GetImplVersion()
422 return SH_IMPL_VERSION;
425 int CSourceHookImpl::AddHook(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
426 ISHDelegate *handler, bool post)
428 if (mode != Hook_Normal && mode != Hook_VP && mode != Hook_DVP)
431 // Get info about hook manager
432 CHookManager hookManager(plug, myHookMan);
436 // Make sure that the container exists
437 CHookManagerContainer &container = m_HookManContainers.GetContainer(hookManager.GetVtblOffs(),
438 hookManager.GetVtblIdx(), hookManager.GetProto());
440 container.AddHookManager(hookManager);
444 // For some weird reason, the container is empty!
445 SH_ASSERT(0, ("No hookman found - but we've just added one!"));
449 CHookManager &hookMan = container.GetActiveHookManager();
451 void *adjustediface = NULL;
452 void **cur_vtptr = NULL;
453 void *cur_vfnptr = NULL;
459 adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
461 cur_vtptr = *reinterpret_cast<void***>(
462 reinterpret_cast<char*>(adjustediface) + hookMan.GetVtblOffs());
463 cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + hookMan.GetVtblIdx());
468 adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
470 cur_vtptr = *reinterpret_cast<void***>(
471 reinterpret_cast<char*>(adjustediface) + hookMan.GetVtblOffs());
472 cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + hookMan.GetVtblIdx());
474 adjustediface = NULL;
478 adjustediface = NULL;
480 cur_vtptr = reinterpret_cast<void**>(iface);
481 cur_vfnptr = cur_vtptr + hookMan.GetVtblIdx();
486 CVfnPtr &vfnPtr = hookMan.GetVfnPtr(cur_vfnptr);
487 CIface &ifaceinst = vfnPtr.GetIface(adjustediface);
490 CHook hook(plug, thisptr_offs, handler,
492 m_HookIDMan.New(hookMan.GetProto(), hookMan.GetVtblOffs(), hookMan.GetVtblIdx(),
493 cur_vfnptr, adjustediface, plug, thisptr_offs, handler, post)
498 ifaceinst.GetPostHookList().push_back(hook);
500 ifaceinst.GetPreHookList().push_back(hook);
505 bool CSourceHookImpl::RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
506 ISHDelegate *handler, bool post)
508 // Get info about hook manager and compute adjustediface
509 CHookManager tmpHookMan(plug, myHookMan);
511 void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface)+thisptr_offs);
513 // Loop through all hooks and remove those which match:
514 // hookman, vfnptr, iface, plug, adjusted iface, this ptr offs, handler, post
515 CVector<int> removehooks;
516 m_HookIDMan.FindAllHooks(removehooks, tmpHookMan.GetProto(), tmpHookMan.GetVtblOffs(), tmpHookMan.GetVtblIdx(),
517 adjustediface, plug, thisptr_offs, handler, post);
519 if (removehooks.empty())
524 for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
526 if (RemoveHookByID(*iter))
532 bool CSourceHookImpl::RemoveHookByID(int hookid)
534 const CHookIDManager::Entry *hentry;
536 hentry = m_HookIDMan.QueryHook(hookid);
539 // hookid doesn't exist !
544 CHookManContainerList::iterator hmcl_iter = m_HookManContainers.find(
545 CHookManagerContainer::Descriptor(hentry->vtbl_offs, hentry->vtbl_idx, hentry->proto));
547 if (hmcl_iter == m_HookManContainers.end() || !(*hmcl_iter))
550 CHookManager &hookman = hmcl_iter->GetActiveHookManager();
553 List<CVfnPtr>::iterator vfnptr_iter = hookman.GetVfnPtrList().find(hentry->vfnptr);
554 if (vfnptr_iter == hookman.GetVfnPtrList().end())
558 List<CIface>::iterator iface_iter = vfnptr_iter->GetIfaceList().find(hentry->adjustediface);
559 if (iface_iter == vfnptr_iter->GetIfaceList().end())
563 List<CHook> &hooks = hentry->post ? iface_iter->GetPostHookList() : iface_iter->GetPreHookList();
564 List<CHook>::iterator hook_iter = hooks.find(hookid);
565 if (hook_iter == hooks.end())
568 hook_iter->GetHandler()->DeleteThis();
571 List<CHook>::iterator oldhookiter = hook_iter;
572 hook_iter = hooks.erase(hook_iter);
574 for (CStack<CHookContext>::iterator ctx_iter = m_ContextStack.begin();
575 ctx_iter != m_ContextStack.end(); ++ctx_iter)
577 ctx_iter->HookRemoved(oldhookiter, hook_iter);
580 if (iface_iter->GetPreHookList().empty() && iface_iter->GetPostHookList().empty())
582 // -> Kill all contexts that use it!
583 for (CStack<CHookContext>::iterator ctx_iter = m_ContextStack.begin();
584 ctx_iter != m_ContextStack.end(); ++ctx_iter)
586 ctx_iter->IfaceRemoved(&(*iface_iter));
589 // There are no hooks on this iface anymore...
590 iface_iter = vfnptr_iter->GetIfaceList().erase(iface_iter);
592 if (vfnptr_iter->GetIfaceList().empty())
594 // No ifaces at all -> Deactivate the hook
596 for (CStack<CHookContext>::iterator ctx_iter = m_ContextStack.begin();
597 ctx_iter != m_ContextStack.end(); ++ctx_iter)
599 ctx_iter->VfnPtrRemoved(&(*vfnptr_iter));
602 // Only patch the vfnptr back if the module is still in memory
603 // If it's not, do not remove stuff like we did before
604 // First off we did it wrong (shutdown the whole hookman, uh..) and secondly applications may be
605 // confused by RemoveHook returning false then (yeah, I know, I made this one up, no one checks for RemoveHook error)
606 if (ModuleInMemory(reinterpret_cast<char*>(vfnptr_iter->GetPtr()), SH_PTRSIZE))
608 vfnptr_iter->Revert();
611 hookman.GetVfnPtrList().erase(vfnptr_iter);
613 if (hookman.GetVfnPtrList().empty())
615 // Unregister the hook manager
616 hookman.Unregister();
621 m_HookIDMan.Remove(hookid);
625 void CSourceHookImpl::SetRes(META_RES res)
627 *m_ContextStack.front().pCurRes = res;
630 META_RES CSourceHookImpl::GetPrevRes()
632 return *m_ContextStack.front().pPrevRes;
635 META_RES CSourceHookImpl::GetStatus()
637 return *m_ContextStack.front().pStatus;
641 const void *CSourceHookImpl::GetOrigRet()
643 return m_ContextStack.front().pOrigRet;
646 const void *CSourceHookImpl::GetOverrideRet()
648 return (*m_ContextStack.front().pStatus < MRES_OVERRIDE) ?
649 NULL : m_ContextStack.front().pOverrideRet;
652 void *CSourceHookImpl::GetIfacePtr()
654 // If in recall: return last one
655 if (m_ContextStack.front().m_State >= CHookContext::State_Recall_Pre &&
656 m_ContextStack.front().m_State <= CHookContext::State_Recall_PostVP)
658 return m_ContextStack.second().pIfacePtr;
662 return m_ContextStack.front().pIfacePtr;
666 void *CSourceHookImpl::GetOverrideRetPtr()
668 return m_ContextStack.front().pOverrideRet;
671 void CSourceHookImpl::UnloadPlugin(Plugin plug)
673 // 1) Remove all hooks by this plugin
675 CVector<int> removehooks;
676 m_HookIDMan.FindAllHooks(removehooks, plug);
678 for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
679 RemoveHookByID(*iter);
681 // 2) Remove all hook managers
682 m_HookManContainers.RemoveHookMans(plug);
685 void CSourceHookImpl::RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc)
687 // 1) Remove all its hooks
688 CVector<int> removehooks;
690 // Get info about hook manager
691 CHookManager hookManager(plug, pubFunc);
695 // Make sure that the container exists
696 CHookManagerContainer &container = m_HookManContainers.GetContainer(hookManager.GetVtblOffs(),
697 hookManager.GetVtblIdx(), hookManager.GetProto());
699 CHookManagerContainer::iterator cont_iter = container.find(CHookManager::Descriptor(plug, pubFunc));
701 for (List<CVfnPtr>::iterator vfnptr_iter = cont_iter->GetVfnPtrList().begin();
702 vfnptr_iter != cont_iter->GetVfnPtrList().end(); ++vfnptr_iter)
704 for (List<CIface>::iterator iface_iter = vfnptr_iter->GetIfaceList().begin();
705 iface_iter != vfnptr_iter->GetIfaceList().end(); ++iface_iter)
707 List<CHook>::iterator hook_iter;
708 for (hook_iter = iface_iter->GetPreHookList().begin();
709 hook_iter != iface_iter->GetPreHookList().end(); ++hook_iter)
710 removehooks.push_back(hook_iter->GetID());
712 for (hook_iter = iface_iter->GetPostHookList().begin();
713 hook_iter != iface_iter->GetPostHookList().end(); ++hook_iter)
714 removehooks.push_back(hook_iter->GetID());
718 for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
719 RemoveHookByID(*iter);
722 container.RemoveHookMans(plug, pubFunc);
725 void CSourceHookImpl::SetIgnoreHooks(void *vfnptr)
728 ctx.m_State = CHookContext::State_Ignore;
729 m_ContextStack.push(ctx);
732 void CSourceHookImpl::ResetIgnoreHooks(void *vfnptr)
734 if (!m_ContextStack.empty() && m_ContextStack.front().m_State == CHookContext::State_Ignore)
735 m_ContextStack.pop();
738 void *CSourceHookImpl::GetOrigVfnPtrEntry(void *vfnptr)
740 for (CHookManContainerList::iterator hmcl_iter = m_HookManContainers.begin();
741 hmcl_iter != m_HookManContainers.end(); ++hmcl_iter)
743 for (CHookManagerContainer::iterator hookmaniter = hmcl_iter->begin();
744 hookmaniter != hmcl_iter->end(); ++hookmaniter)
746 for (List<CVfnPtr>::iterator vfnptr_iter = hookmaniter->GetVfnPtrList().begin();
747 vfnptr_iter != hookmaniter->GetVfnPtrList().end(); ++vfnptr_iter)
749 if (vfnptr_iter->GetPtr() == vfnptr)
750 return vfnptr_iter->GetOrigEntry();
757 void CSourceHookImpl::DoRecall()
760 CHookContext &curCtx = m_ContextStack.front();
763 newCtx.m_State = curCtx.m_State + (CHookContext::State_Recall_Pre - CHookContext::State_Pre);
764 if (newCtx.m_State == CHookContext::State_Recall_Post || newCtx.m_State == CHookContext::State_Recall_PostVP)
766 // Also save orig ret
767 newCtx.pOrigRet = curCtx.pOrigRet;
770 // The hookfunc usually does this, but it won't have a chance to see it,
771 // so for recalls, we update status here if it's required
772 if (*curCtx.pCurRes > *curCtx.pStatus)
773 *curCtx.pStatus = *curCtx.pCurRes;
775 newCtx.pStatus = curCtx.pStatus;
776 newCtx.pOverrideRet = curCtx.pOverrideRet;
777 newCtx.pPrevRes = curCtx.pPrevRes;
778 newCtx.m_Iter = curCtx.m_Iter;
780 // Take this with us!
781 newCtx.pCurRes = curCtx.pCurRes;
783 m_ContextStack.push(newCtx);
784 curCtx.m_State = CHookContext::State_Dead;
787 IHookContext *CSourceHookImpl::SetupHookLoop(IHookManagerInfo *hi, void *vfnptr, void *thisptr, void **origentry, META_RES *statusPtr,
788 META_RES *prevResPtr, META_RES *curResPtr, const void *origRetPtr, void *overrideRetPtr)
790 CHookContext *pCtx = NULL;
791 CHookContext *oldctx = m_ContextStack.empty() ? NULL : &m_ContextStack.front();
795 if (oldctx->m_State == CHookContext::State_Ignore)
797 *statusPtr = MRES_IGNORED;
798 oldctx->m_CallOrig = true;
799 oldctx->m_State = CHookContext::State_Dead;
801 List<CVfnPtr> &vfnptrList = static_cast<CHookManager*>(hi)->GetVfnPtrList();
802 List<CVfnPtr>::iterator vfnptr_iter = vfnptrList.find(vfnptr);
803 if (vfnptr_iter == vfnptrList.end())
809 *origentry = vfnptr_iter->GetOrigEntry();
812 oldctx->pOrigRet = origRetPtr;
816 else if (oldctx->m_State >= CHookContext::State_Recall_Pre &&
817 oldctx->m_State <= CHookContext::State_Recall_PostVP)
821 *statusPtr = *(oldctx->pStatus);
822 *prevResPtr = *(oldctx->pPrevRes);
824 pCtx->m_Iter = oldctx->m_Iter;
826 // Only have possibility of calling the orig func in pre recall mode
827 pCtx->m_CallOrig = (oldctx->m_State == CHookContext::State_Recall_Pre ||
828 oldctx->m_State == CHookContext::State_Recall_PreVP);
830 overrideRetPtr = pCtx->pOverrideRet;
832 // When the status is low so there's no override return value and we're in a post recall,
833 // give it the orig return value as override return value.
834 if (pCtx->m_State == CHookContext::State_Recall_Post ||
835 pCtx->m_State == CHookContext::State_Recall_PostVP)
837 origRetPtr = oldctx->pOrigRet;
838 if (*statusPtr < MRES_OVERRIDE)
839 overrideRetPtr = const_cast<void*>(pCtx->pOrigRet);
845 pCtx = m_ContextStack.make_next();
846 pCtx->m_State = CHookContext::State_Born;
847 pCtx->m_CallOrig = true;
852 List<CVfnPtr> &vfnptrList = static_cast<CHookManager*>(hi)->GetVfnPtrList();
853 List<CVfnPtr>::iterator vfnptr_iter = vfnptrList.find(vfnptr);
854 if (vfnptr_iter == vfnptrList.end())
856 pCtx->m_State = CHookContext::State_Dead;
860 pCtx->pVfnPtr = &(*vfnptr_iter);
861 *origentry = vfnptr_iter->GetOrigEntry();
862 pCtx->pIface = vfnptr_iter->FindIface(thisptr);
865 pCtx->pStatus = statusPtr;
866 pCtx->pPrevRes = prevResPtr;
867 pCtx->pCurRes = curResPtr;
868 pCtx->pThisPtr = thisptr;
869 pCtx->pOverrideRet = overrideRetPtr;
870 pCtx->pOrigRet = origRetPtr;
875 void CSourceHookImpl::EndContext(IHookContext *pCtx)
877 m_ContextStack.pop();
880 void CSourceHookImpl::CompleteShutdown()
882 CVector<int> removehooks;
883 m_HookIDMan.FindAllHooks(removehooks);
885 for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
886 RemoveHookByID(*iter);
889 void CSourceHookImpl::PausePlugin(Plugin plug)
891 CVector<int> pausehooks;
892 m_HookIDMan.FindAllHooks(pausehooks, plug);
894 for (CVector<int>::iterator iter = pausehooks.begin(); iter != pausehooks.end(); ++iter)
895 PauseHookByID(*iter);
898 void CSourceHookImpl::UnpausePlugin(Plugin plug)
900 CVector<int> unpausehooks;
901 m_HookIDMan.FindAllHooks(unpausehooks, plug);
903 for (CVector<int>::iterator iter = unpausehooks.begin(); iter != unpausehooks.end(); ++iter)
904 UnpauseHookByID(*iter);
907 bool CSourceHookImpl::PauseHookByID(int hookid)
909 return SetHookPaused(hookid, true);
912 bool CSourceHookImpl::UnpauseHookByID(int hookid)
914 return SetHookPaused(hookid, false);
917 bool CSourceHookImpl::SetHookPaused(int hookid, bool paused)
919 const CHookIDManager::Entry *hentry;
921 hentry = m_HookIDMan.QueryHook(hookid);
924 // hookid doesn't exist !
929 CHookManContainerList::iterator hmcl_iter = m_HookManContainers.find(
930 CHookManagerContainer::Descriptor(hentry->vtbl_offs, hentry->vtbl_idx, hentry->proto));
932 if (hmcl_iter == m_HookManContainers.end() || !(*hmcl_iter))
935 CHookManager &hookman = hmcl_iter->GetActiveHookManager();
938 List<CVfnPtr>::iterator vfnptr_iter = hookman.GetVfnPtrList().find(hentry->vfnptr);
939 if (vfnptr_iter == hookman.GetVfnPtrList().end())
943 List<CIface>::iterator iface_iter = vfnptr_iter->GetIfaceList().find(hentry->adjustediface);
944 if (iface_iter == vfnptr_iter->GetIfaceList().end())
948 List<CHook> &hooks = hentry->post ? iface_iter->GetPostHookList() : iface_iter->GetPreHookList();
949 List<CHook>::iterator hook_iter = hooks.find(hookid);
950 if (hook_iter == hooks.end())
953 hook_iter->SetPaused(paused);
957 //////////////////////////////////////////////////////////////////////////
959 //////////////////////////////////////////////////////////////////////////
960 ISHDelegate *CHookContext::GetNext()
969 m_Iter = List<CHook>::iterator();
973 case State_Recall_Pre:
979 m_Iter = pIface->GetPreHookList().begin();
982 SkipPaused(m_Iter, pIface->GetPreHookList());
984 if (m_Iter != pIface->GetPreHookList().end())
986 pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
987 return m_Iter->GetHandler();
991 // end of normal hooks -> VP
993 m_State = State_PreVP;
994 m_Iter = List<CHook>::iterator();
997 case State_Recall_PreVP:
998 m_State = State_PreVP;
1000 pVPIface = pVfnPtr->FindIface(NULL);
1004 m_Iter = pVPIface->GetPreHookList().begin();
1007 SkipPaused(m_Iter, pVPIface->GetPreHookList());
1009 if (m_Iter != pVPIface->GetPreHookList())
1011 pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
1012 return m_Iter->GetHandler();
1016 // end VP hooks -> orig call
1018 m_State = State_OrigCall;
1021 case State_OrigCall:
1022 m_Iter = List<CHook>::iterator();
1023 m_State = State_Post;
1030 m_Iter = pIface->GetPostHookList().begin();
1033 SkipPaused(m_Iter, pIface->GetPostHookList());
1035 if (m_Iter != pIface->GetPostHookList().end())
1037 pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
1038 return m_Iter->GetHandler();
1041 // end of normal hooks -> VP
1043 m_State = State_PostVP;
1044 m_Iter = List<CHook>::iterator();
1048 pVPIface = pVfnPtr->FindIface(NULL);
1052 m_Iter = pVPIface->GetPostHookList().begin();
1055 SkipPaused(m_Iter, pVPIface->GetPostHookList());
1057 if (m_Iter != pVPIface->GetPostHookList())
1059 pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
1060 return m_Iter->GetHandler();
1064 // end VP hooks -> done
1066 m_State = State_Dead;
1069 case State_Recall_Post:
1070 // Skip pre hooks _and_ orig call
1071 m_State = State_Post;
1074 case State_Recall_PostVP:
1075 // Skip pre hooks _and_ orig call
1076 m_State = State_PostVP;
1082 void *CHookContext::GetOverrideRetPtr()
1084 return pOverrideRet;
1087 const void *CHookContext::GetOrigRetPtr()
1092 bool CHookContext::ShouldCallOrig()
1097 void CHookContext::HookRemoved(List<CHook>::iterator oldhookiter, List<CHook>::iterator nexthookiter)
1099 if (m_Iter == oldhookiter)
1101 m_Iter = nexthookiter;
1106 void CHookContext::IfaceRemoved(CIface *iface)
1108 if (pIface == iface)
1112 void CHookContext::VfnPtrRemoved(CVfnPtr *vfnptr)
1114 if (pVfnPtr == vfnptr)
1117 m_State = State_Dead;