Fixed some issues with getting the user messages when mod was compiled with frame pointer.
Added support for Dystopia through an ugly hack until more information known.
Code has been further uglified.
1 /* ======== SourceHook ========
2 * Copyright (C) 2004-2005 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)
42 CSourceHookImpl::CSourceHookImpl()
45 CSourceHookImpl::~CSourceHookImpl()
49 int CSourceHookImpl::GetIfaceVersion()
51 return SH_IFACE_VERSION;
54 int CSourceHookImpl::GetImplVersion()
56 return SH_IMPL_VERSION;
59 bool CSourceHookImpl::IsPluginInUse(Plugin plug)
61 // Iterate through all hook managers which are in this plugin
62 // Iterate through their vfnptrs, ifaces, hooks
63 // If a hook from an other plugin is found, return true
64 // Return false otherwise
65 #define TMP_CHECK_LIST(name) \
66 for (hook_iter = iface_iter->name.m_List.begin(); hook_iter != iface_iter->name.m_List.end(); ++hook_iter) \
67 if (hook_iter->plug == plug) \
70 for (HookManContList::iterator hmcl_iter = m_HookMans.begin();
71 hmcl_iter != m_HookMans.end(); ++hmcl_iter)
73 for (CHookManagerContainer::iterator hmil_iter = hmcl_iter->begin();
74 hmil_iter != hmcl_iter->end(); ++hmil_iter)
76 if (hmil_iter->m_Plug != plug)
78 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
79 vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
81 for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
82 iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
84 List<HookInfo>::iterator hook_iter;
85 TMP_CHECK_LIST(m_PreHooks);
86 TMP_CHECK_LIST(m_PostHooks);
96 void CSourceHookImpl::UnloadPlugin(Plugin plug)
98 // 1) Manually remove all hooks by this plugin
99 // 2) Manually remove all hook mans by this plugin
101 List<RemoveHookInfo> hookstoremove;
102 List<RemoveHookManInfo> hookmanstoremove;
103 HookManInfoList::iterator hmil_iter;
105 #define TMP_CHECK_LIST(name, ispost) \
106 for (hook_iter = iface_iter->name.m_List.begin(); hook_iter != iface_iter->name.m_List.end(); ++hook_iter) \
107 if (hook_iter->plug == plug) \
108 hookstoremove.push_back(RemoveHookInfo(hook_iter->plug, iface_iter->m_Ptr, \
109 hook_iter->thisptr_offs, hmil_iter->m_Func, hook_iter->handler, ispost)); \
111 for (HookManContList::iterator hmcl_iter = m_HookMans.begin();
112 hmcl_iter != m_HookMans.end(); ++hmcl_iter)
114 for (CHookManagerContainer::iterator hmil_iter = hmcl_iter->begin();
115 hmil_iter != hmcl_iter->end(); ++hmil_iter)
117 if (hmil_iter->m_Plug == plug)
118 hookmanstoremove.push_back(RemoveHookManInfo(plug, hmil_iter->m_Func));
120 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
121 vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
123 for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
124 iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
126 List<HookInfo>::iterator hook_iter;
127 TMP_CHECK_LIST(m_PreHooks, false);
128 TMP_CHECK_LIST(m_PostHooks, true);
133 #undef TMP_CHECK_LIST
135 for (List<RemoveHookInfo>::iterator rmiter = hookstoremove.begin(); rmiter != hookstoremove.end(); ++rmiter)
138 for (List<RemoveHookManInfo>::iterator rmiter = hookmanstoremove.begin(); rmiter != hookmanstoremove.end(); ++rmiter)
139 RemoveHookManager(*rmiter);
142 void CSourceHookImpl::RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc)
146 CHookManagerInfo tmp;
147 if (pubFunc(HA_GetInfo, &tmp) != 0)
151 HookManContList::iterator hmcl_iter = m_HookMans.find(
152 CHookManagerContainer::HMCI(tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx));
154 if (hmcl_iter == m_HookMans.end())
160 CHookManagerContainer::iterator hmil_iter = hmcl_iter->find(
161 CHookManagerInfo::Descriptor(plug, pubFunc));
163 if (hmil_iter == hmcl_iter->end())
169 bool stillInUse = false;
170 List<RemoveHookInfo> hookstoremove;
172 // Iterate through all of the hook manager's hooks. Remove all hooks from the hookman's plugin.
173 #define TMP_CHECK_LIST(name, ispost) \
174 for (hook_iter = iface_iter->name.m_List.begin(); hook_iter != iface_iter->name.m_List.end(); ++hook_iter) \
175 if (hook_iter->plug == plug) \
176 hookstoremove.push_back(RemoveHookInfo(hook_iter->plug, iface_iter->m_Ptr, \
177 hook_iter->thisptr_offs, hmil_iter->m_Func, hook_iter->handler, ispost)); \
181 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
182 vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
184 for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
185 iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
187 List<HookInfo>::iterator hook_iter;
188 TMP_CHECK_LIST(m_PreHooks, false);
189 TMP_CHECK_LIST(m_PostHooks, true);
192 #undef TMP_CHECK_LIST
194 for (List<RemoveHookInfo>::iterator rmiter = hookstoremove.begin(); rmiter != hookstoremove.end(); ++rmiter)
197 CHookManagerInfo info = *hmil_iter;
199 // Unlink the hook manager from the list
200 hmcl_iter->erase(hmil_iter);
202 // If there were any hooks from other plugins, find a new suitable hook manager.
205 // Find a suitable hook manager in an other plugin
206 hmil_iter = hmcl_iter->begin();
208 // This should _never_ happen.
209 // If there is a hook from an other plugin, the plugin must have provided a hook manager as well.
210 SH_ASSERT(hmil_iter != hmcl_iter->end(),
211 ("Could not find a suitable hook manager in an other plugin!"));
213 // AddHook should make sure that every plugin only has _one_ hook manager for _one_ proto/vi/vo
214 SH_ASSERT(hmil_iter->m_Plug != plug, ("New hook manager from same plugin!"));
216 // The first hook manager should be always used - so the new hook manager has to be empty
217 SH_ASSERT(hmil_iter->m_VfnPtrs.empty(), ("New hook manager not empty!"));
219 // Move the vfnptrs from the old hook manager to the new one
220 hmil_iter->m_VfnPtrs = info.m_VfnPtrs;
222 // Unregister the old one, register the new one
223 info.m_Func(HA_Unregister, NULL);
224 hmil_iter->m_Func(HA_Register, &(*hmil_iter));
226 // Go through all vfnptrs in this hookman and patch them to point to the new manager's handler!
228 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
229 vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
231 // And DEREFERENCE newHookMan->m_HookfuncVfnptr!
232 // otherwise it will be executing the vtable... had to find out the hard way
233 *reinterpret_cast<void**>(vfnptr_iter->m_Ptr) = *reinterpret_cast<void**>(hmil_iter->m_HookfuncVfnptr);
238 void CSourceHookImpl::RemoveHookManager(RemoveHookManInfo info)
240 RemoveHookManager(info.plug, info.hookman);
243 void CSourceHookImpl::CompleteShutdown()
247 List<RemoveHookInfo> hookstoremove;
249 #define TMP_CHECK_LIST(name, ispost) \
250 for (hook_iter = iface_iter->name.m_List.begin(); hook_iter != iface_iter->name.m_List.end(); ++hook_iter) \
251 hookstoremove.push_back(RemoveHookInfo(hook_iter->plug, iface_iter->m_Ptr, \
252 hook_iter->thisptr_offs, hmil_iter->m_Func, hook_iter->handler, ispost))
253 for (HookManContList::iterator hmcl_iter = m_HookMans.begin();
254 hmcl_iter != m_HookMans.end(); ++hmcl_iter)
256 for (CHookManagerContainer::iterator hmil_iter = hmcl_iter->begin();
257 hmil_iter != hmcl_iter->end(); ++hmil_iter)
259 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
260 vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
262 for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
263 iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
265 List<HookInfo>::iterator hook_iter;
266 TMP_CHECK_LIST(m_PreHooks, false);
267 TMP_CHECK_LIST(m_PostHooks, true);
272 #undef TMP_CHECK_LIST
274 for (List<RemoveHookInfo>::iterator rmiter = hookstoremove.begin(); rmiter != hookstoremove.end(); ++rmiter)
280 bool CSourceHookImpl::AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
282 void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
284 // 1) Get info about the hook manager
285 CHookManagerInfo tmp;
286 if (myHookMan(HA_GetInfo, &tmp) != 0)
289 tmp.m_Func = myHookMan;
292 CHookManagerContainer::HMCI hmci(tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx);
294 void **cur_vtptr = *reinterpret_cast<void***>(
295 reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs);
296 void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.m_VtblIdx);
298 // Add the container if not already added
299 HookManContList::iterator hmcl_iter = m_HookMans.find(hmci);
300 if (hmcl_iter == m_HookMans.end())
302 m_HookMans.push_back(CHookManagerContainer(hmci));
303 hmcl_iter = m_HookMans.end();
307 hmcl_iter->AddHookManager(plug, tmp);
309 CHookManagerContainer::iterator hookman = hmcl_iter->begin();
310 SH_ASSERT(hookman != hmcl_iter->end(), ("No hookman found - but we've just added one!"));
312 // Check whether any other container holds a hook manager which holds this vfnptr
313 // If yes, it means that the two are incompatible, so return false.
314 for (HookManContList::iterator hmcl_iter2 = m_HookMans.begin();
315 hmcl_iter2 != m_HookMans.end(); ++hmcl_iter2)
317 if (hmcl_iter2 != hmcl_iter)
319 for (CHookManagerContainer::iterator hmil_iter = hmcl_iter2->begin();
320 hmil_iter != hmcl_iter2->end(); ++hmil_iter)
322 if (hmil_iter->m_VfnPtrs.find(cur_vfnptr) != hmil_iter->m_VfnPtrs.end())
328 // Tell it to store the pointer if it's not already active
329 if (hookman->m_VfnPtrs.empty())
330 hookman->m_Func(HA_Register, &(*hookman));
332 CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->m_VfnPtrs.find(cur_vfnptr);
334 if (vfnptr_iter == hookman->m_VfnPtrs.end())
337 CVfnPtr vfp(cur_vfnptr);
339 // Alter vtable entry
340 if (!SetMemAccess(cur_vtptr, sizeof(void*) * (tmp.m_VtblIdx + 1), SH_MEM_READ | SH_MEM_WRITE))
343 *reinterpret_cast<void**>(cur_vfnptr) = *reinterpret_cast<void**>(hookman->m_HookfuncVfnptr);
345 hookman->m_VfnPtrs.push_back(vfp);
347 // Make vfnptr_iter point to the new element
348 vfnptr_iter = hookman->m_VfnPtrs.end();
351 // Now that it is done, check whether we have to update any callclasses
352 ApplyCallClassPatches(adjustediface, tmp.m_VtblOffs, tmp.m_VtblIdx, vfp.m_OrigEntry);
355 CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.find(adjustediface);
356 if (iface_iter == vfnptr_iter->m_Ifaces.end())
359 vfnptr_iter->m_Ifaces.push_back(CIface(adjustediface));
361 // Make iface_iter point to the new element
362 iface_iter = vfnptr_iter->m_Ifaces.end();
368 hookinfo.handler = handler;
369 hookinfo.plug = plug;
370 hookinfo.paused = false;
371 hookinfo.thisptr_offs = thisptr_offs;
373 iface_iter->m_PostHooks.m_List.push_back(hookinfo);
375 iface_iter->m_PreHooks.m_List.push_back(hookinfo);
380 bool CSourceHookImpl::RemoveHook(RemoveHookInfo info)
382 return RemoveHook(info.plug, info.iface, info.thisptr_offs, info.hookman, info.handler, info.post);
385 bool CSourceHookImpl::RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
387 void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface)+thisptr_offs);
388 CHookManagerInfo tmp;
389 if (myHookMan(HA_GetInfo, &tmp) != 0)
392 // Find the hook manager and the hook
394 HookManContList::iterator hmcl_iter = m_HookMans.find(
395 CHookManagerContainer::HMCI(tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx));
396 if (hmcl_iter == m_HookMans.end() || hmcl_iter->empty())
399 CHookManagerContainer::iterator hookman = hmcl_iter->begin();
401 if (!ModuleInMemory(reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs,
402 sizeof(void*) * (tmp.m_VtblIdx + 1)))
404 // The module the vtable was in is already unloaded.
405 hookman->m_VfnPtrs.clear();
406 hookman->m_Func(HA_Unregister, NULL);
410 void **cur_vtptr = *reinterpret_cast<void***>(
411 reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs);
412 void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.m_VtblIdx);
414 CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->m_VfnPtrs.find(cur_vfnptr);
416 if (vfnptr_iter == hookman->m_VfnPtrs.end())
419 for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
420 iface_iter != vfnptr_iter->m_Ifaces.end();)
422 if (iface_iter->m_Ptr != adjustediface)
427 List<HookInfo> &hooks =
428 post ? iface_iter->m_PostHooks.m_List : iface_iter->m_PreHooks.m_List;
431 for (List<HookInfo>::iterator hookiter = hooks.begin();
432 hookiter != hooks.end(); )
434 erase = hookiter->plug == plug && hookiter->handler->IsEqual(handler) &&
435 hookiter->thisptr_offs == thisptr_offs;
438 hookiter->handler->DeleteThis(); // Make the _plugin_ delete the handler object
440 // Move all iterators pointing at this
441 List<HookInfo>::iterator oldhookiter = hookiter;
442 hookiter = hooks.erase(hookiter);
443 List<HookInfo>::iterator newhookiter = hookiter;
444 --newhookiter; // The hook loop will ++ it then
445 CHookList::CIter *pItIter;
446 for (pItIter = iface_iter->m_PreHooks.m_UsedIters; pItIter; pItIter = pItIter->m_pNext)
447 if (pItIter->m_Iter == oldhookiter)
448 pItIter->m_Iter = newhookiter;
453 if (iface_iter->m_PostHooks.m_List.empty() && iface_iter->m_PreHooks.m_List.empty())
455 // There are no hooks on this iface anymore...
456 for (HookLoopInfoStack::iterator hli_iter = m_HLIStack.begin();
457 hli_iter != m_HLIStack.end(); ++hli_iter)
459 if (hli_iter->pCurIface == static_cast<IIface*>(&(*iface_iter)))
460 hli_iter->shouldContinue = false;
463 iface_iter = vfnptr_iter->m_Ifaces.erase(iface_iter);
464 if (vfnptr_iter->m_Ifaces.empty())
466 // No ifaces at all -> Deactivate the hook
467 *reinterpret_cast<void**>(vfnptr_iter->m_Ptr) = vfnptr_iter->m_OrigEntry;
469 hookman->m_VfnPtrs.erase(vfnptr_iter);
471 // Remove callclass patch
472 for (Impl_CallClassList::iterator cciter = m_CallClasses.begin(); cciter != m_CallClasses.end(); ++cciter)
473 if (cciter->m_Ptr == adjustediface)
474 cciter->RemoveCallClassPatch(tmp.m_VtblOffs, tmp.m_VtblIdx);
476 if (hookman->m_VfnPtrs.empty())
478 // Unregister the hook manager
479 hookman->m_Func(HA_Unregister, NULL);
482 // Don't try to continue looping through ifaces
483 // - the list is already invalid
493 GenericCallClass *CSourceHookImpl::GetCallClass(void *iface, size_t size)
495 for (Impl_CallClassList::iterator cciter = m_CallClasses.begin(); cciter != m_CallClasses.end(); ++cciter)
497 if (cciter->m_Ptr == iface && cciter->m_ObjSize == size)
499 ++cciter->m_RefCounter;
506 CCallClassImpl tmp(iface, size);
507 ApplyCallClassPatches(tmp);
508 m_CallClasses.push_back(tmp);
509 return &m_CallClasses.back();
512 void CSourceHookImpl::ReleaseCallClass(GenericCallClass *ptr)
514 Impl_CallClassList::iterator iter = m_CallClasses.find(ptr);
515 if (iter == m_CallClasses.end())
517 --iter->m_RefCounter;
518 if (iter->m_RefCounter < 1)
519 m_CallClasses.erase(iter);
522 void CSourceHookImpl::ApplyCallClassPatches(CCallClassImpl &cc)
524 for (HookManContList::iterator hmcl_iter = m_HookMans.begin();
525 hmcl_iter != m_HookMans.end(); ++hmcl_iter)
527 for (CHookManagerContainer::iterator hookman = hmcl_iter->begin();
528 hookman != hmcl_iter->end(); ++hookman)
530 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->m_VfnPtrs.begin();
531 vfnptr_iter != hookman->m_VfnPtrs.end(); ++vfnptr_iter)
533 for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
534 iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
536 if (iface_iter->m_Ptr >= cc.m_Ptr &&
537 iface_iter->m_Ptr < (reinterpret_cast<char*>(cc.m_Ptr) + cc.m_ObjSize))
539 cc.ApplyCallClassPatch(static_cast<int>(reinterpret_cast<char*>(iface_iter->m_Ptr) -
540 reinterpret_cast<char*>(cc.m_Ptr)) + hookman->m_VtblOffs,
541 hookman->m_VtblIdx, vfnptr_iter->m_OrigEntry);
549 void CSourceHookImpl::ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry)
551 for (Impl_CallClassList::iterator cc_iter = m_CallClasses.begin(); cc_iter != m_CallClasses.end();
554 if (ifaceptr >= cc_iter->m_Ptr &&
555 ifaceptr < (reinterpret_cast<char*>(cc_iter->m_Ptr) + cc_iter->m_ObjSize))
557 cc_iter->ApplyCallClassPatch(static_cast<int>(reinterpret_cast<char*>(ifaceptr) -
558 reinterpret_cast<char*>(cc_iter->m_Ptr)) + vtbl_offs, vtbl_idx, orig_entry);
563 void CSourceHookImpl::RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx)
565 for (Impl_CallClassList::iterator cc_iter = m_CallClasses.begin(); cc_iter != m_CallClasses.end();
568 if (ifaceptr >= cc_iter->m_Ptr &&
569 ifaceptr < (reinterpret_cast<char*>(cc_iter->m_Ptr) + cc_iter->m_ObjSize))
571 cc_iter->RemoveCallClassPatch(vtbl_offs, vtbl_idx);
576 void CSourceHookImpl::SetPluginPaused(Plugin plug, bool paused)
578 // Go through all hook managers, all interfaces, and set the status of all hooks of this plugin to paused
579 for (HookManContList::iterator hmcl_iter = m_HookMans.begin();
580 hmcl_iter != m_HookMans.end(); ++hmcl_iter)
582 for (CHookManagerContainer::iterator hookmaniter = hmcl_iter->begin();
583 hookmaniter != hmcl_iter->end(); ++hookmaniter)
585 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookmaniter->m_VfnPtrs.begin();
586 vfnptr_iter != hookmaniter->m_VfnPtrs.end(); ++vfnptr_iter)
588 for (CVfnPtr::IfaceListIter ifaceiter = vfnptr_iter->m_Ifaces.begin();
589 ifaceiter != vfnptr_iter->m_Ifaces.end(); ++ifaceiter)
591 for (List<HookInfo>::iterator hookiter = ifaceiter->m_PreHooks.m_List.begin();
592 hookiter != ifaceiter->m_PreHooks.m_List.end(); ++hookiter)
593 if (plug == hookiter->plug)
594 hookiter->paused = paused;
596 for (List<HookInfo>::iterator hookiter = ifaceiter->m_PostHooks.m_List.begin();
597 hookiter != ifaceiter->m_PostHooks.m_List.end(); ++hookiter)
598 if (plug == hookiter->plug)
599 hookiter->paused = paused;
605 void CSourceHookImpl::PausePlugin(Plugin plug)
607 SetPluginPaused(plug, true);
610 void CSourceHookImpl::UnpausePlugin(Plugin plug)
612 SetPluginPaused(plug, false);
615 void CSourceHookImpl::HookLoopBegin(IIface *pIface)
618 hli.pCurIface = pIface;
619 hli.shouldContinue = true;
620 hli.recall = HookLoopInfo::Recall_No;
622 static_cast<CIface*>(pIface)->m_PreHooks.RQFlagReset();
623 static_cast<CIface*>(pIface)->m_PostHooks.RQFlagReset();
625 m_HLIStack.push(hli);
628 void CSourceHookImpl::HookLoopEnd()
630 // When in a post recall... make sure status is high enough so that it returns the orig ret.
631 if (m_HLIStack.size() > 1 && m_HLIStack.second().recall == HookLoopInfo::Recall_Post2)
632 *m_HLIStack.front().pStatus = MRES_SUPERCEDE; // THAT'LL TEACH THEM!
636 void CSourceHookImpl::SetRes(META_RES res)
638 *m_HLIStack.front().pCurRes = res;
641 META_RES CSourceHookImpl::GetPrevRes()
643 return *m_HLIStack.front().pPrevRes;
646 META_RES CSourceHookImpl::GetStatus()
648 return *m_HLIStack.front().pStatus;
651 const void *CSourceHookImpl::GetOrigRet()
653 // When in a post recall... return the orig ret of the previous hookloop.
654 if (m_HLIStack.size() > 1 && m_HLIStack.second().recall == HookLoopInfo::Recall_Post2)
655 return m_HLIStack.second().pOrigRet;
657 return m_HLIStack.front().pOrigRet;
660 const void *CSourceHookImpl::GetOverrideRet()
662 // pOverrideRet is always set since recalls were introduced
663 // GetOverrideRetPtr was added; a function which always returns pOverrideRet
664 // so that RETURN_META_VALUE_NEWPARAMS can provide an override result
666 // This means that we have to filter GetOverrideRet:
667 // If the status variable is < MRES_OVERRIDE, return NULL.
669 return (*m_HLIStack.front().pStatus < MRES_OVERRIDE) ?
670 NULL : m_HLIStack.front().pOverrideRet;
673 void *CSourceHookImpl::GetOverrideRetPtr()
675 // As described in the comment above: always return pOverrideRet
676 return m_HLIStack.front().pOverrideRet;
679 void *CSourceHookImpl::GetIfacePtr()
681 return *m_HLIStack.front().pIfacePtrPtr;
684 void CSourceHookImpl::SetCurResPtr(META_RES *mres)
686 m_HLIStack.front().pCurRes = mres;
689 void CSourceHookImpl::SetPrevResPtr(META_RES *mres)
691 m_HLIStack.front().pPrevRes = mres;
693 // If we're recalling, drag the previous mres value to the new hookfunc
694 if (m_HLIStack.size() > 1 && m_HLIStack.second().recall)
695 *mres = *m_HLIStack.second().pPrevRes;
698 void CSourceHookImpl::SetStatusPtr(META_RES *mres)
700 m_HLIStack.front().pStatus = mres;
702 // If we're recalling, drag the previous mres value to the new hookfunc
703 if (m_HLIStack.size() > 1 && m_HLIStack.second().recall)
704 *mres = *m_HLIStack.second().pStatus;
707 void CSourceHookImpl::SetIfacePtrPtr(void **pp)
709 m_HLIStack.front().pIfacePtrPtr = pp;
712 void CSourceHookImpl::SetOrigRetPtr(const void *ptr)
714 m_HLIStack.front().pOrigRet = ptr;
716 void CSourceHookImpl::SetOverrideRetPtr(void *ptr)
718 m_HLIStack.front().pOverrideRet = ptr;
721 // New function which does all of the above + more :)
722 void *CSourceHookImpl::SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
723 void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr)
725 HookLoopInfo &hli = m_HLIStack.front();
726 hli.pStatus = statusPtr;
727 hli.pPrevRes = prevResPtr;
728 hli.pCurRes = curResPtr;
729 hli.pIfacePtrPtr = ifacePtrPtr;
730 hli.pOrigRet = origRetPtr;
732 // Handle some recall stuff
733 if (m_HLIStack.size() > 1 && m_HLIStack.second().recall)
735 HookLoopInfo &other = m_HLIStack.second();
736 *statusPtr = *other.pStatus;
737 *prevResPtr = *other.pStatus;
739 // When the status is low so there's no override return value and we're in a post recall,
740 // give it the orig return value as override return value.
741 if (*statusPtr < MRES_OVERRIDE && other.recall == HookLoopInfo::Recall_Post1)
742 hli.pOverrideRet = const_cast<void*>(other.pOrigRet);
743 else // Otherwise, transfer override ret normally
744 hli.pOverrideRet = other.pOverrideRet;
747 hli.pOverrideRet = overrideRetPtr;
749 // Tell the hook func which override ret ptr to use
750 return hli.pOverrideRet;
753 bool CSourceHookImpl::ShouldContinue()
755 // If recall is true, we shall not continue either.
756 // This is because, if it's true and ShouldContinue is called, it suggests that the
757 // actual recall is done and that we are back in the original handler which shall return
761 // The second element on the stack has recall set to Recall_Post1.
762 // This means that we want to skip this part and the original function calling thing, so
763 // we store the status mres value, set status to MRES_SUPERCEDE, and return false.
764 // We also set the recall flag to Recall_Post2, so the next time the thing calls us, we
765 // can return true (so that the thing goes into post hooks).
766 if (m_HLIStack.size() > 1)
768 if (m_HLIStack.second().recall == HookLoopInfo::Recall_Post1)
770 m_HLIStack.front().temporaryStatus = *m_HLIStack.front().pStatus;
771 *m_HLIStack.front().pStatus = MRES_SUPERCEDE;
772 m_HLIStack.second().recall = HookLoopInfo::Recall_Post2;
775 else if (m_HLIStack.second().recall == HookLoopInfo::Recall_Post2)
777 *m_HLIStack.front().pStatus = m_HLIStack.front().temporaryStatus;
778 return m_HLIStack.front().shouldContinue;
781 return m_HLIStack.front().shouldContinue && !m_HLIStack.front().recall;
784 void CSourceHookImpl::DoRecall()
786 if (!m_HLIStack.empty())
788 // Also watch out for post recalls! Described at the beginning of sourcehook_impl.h
789 m_HLIStack.front().recall =
790 static_cast<CIface*>(m_HLIStack.front().pCurIface)->m_PostHooks.RQFlagGet() ?
791 HookLoopInfo::Recall_Post1 : HookLoopInfo::Recall_Pre;
793 CHookList *mlist = static_cast<CHookList*>(m_HLIStack.front().recall == HookLoopInfo::Recall_Pre ?
794 m_HLIStack.front().pCurIface->GetPreHooks() : m_HLIStack.front().pCurIface->GetPostHooks());
795 mlist->m_Recall = true;
797 // The hookfunc usually does this, but it won't have a chance to see it,
798 // so for recalls, we update status here if it's required
799 if (*m_HLIStack.front().pCurRes > *m_HLIStack.front().pStatus)
800 *m_HLIStack.front().pStatus = *m_HLIStack.front().pCurRes;
805 ////////////////////////////
807 ////////////////////////////
808 CSourceHookImpl::CCallClassImpl::CCallClassImpl(void *ptr, size_t size)
809 : m_Ptr(ptr), m_ObjSize(size), m_RefCounter(1)
812 CSourceHookImpl::CCallClassImpl::~CCallClassImpl()
816 void *CSourceHookImpl::CCallClassImpl::GetThisPtr()
820 void *CSourceHookImpl::CCallClassImpl::GetOrigFunc(int vtbloffs, int vtblidx)
822 OrigVTables::iterator iter = m_VT.find(vtbloffs);
823 if (iter != m_VT.end() && iter->val.size() > (size_t)vtblidx)
825 return iter->val[vtblidx];
830 void CSourceHookImpl::CCallClassImpl::ApplyCallClassPatch(int vtbl_offs, int vtbl_idx, void *orig_entry)
832 OrigFuncs &tmpvec = m_VT[vtbl_offs];
833 if (tmpvec.size() <= (size_t)vtbl_idx)
834 tmpvec.resize(vtbl_idx+1, NULL);
835 tmpvec[vtbl_idx] = orig_entry;
837 void CSourceHookImpl::CCallClassImpl::RemoveCallClassPatch(int vtbl_offs, int vtbl_idx)
839 OrigVTables::iterator iter = m_VT.find(vtbl_offs);
840 if (iter != m_VT.end())
842 if (iter->val.size() > (size_t)vtbl_idx)
844 iter->val[vtbl_idx] = 0;
846 OrigFuncs &of = iter->val;
847 OrigFuncs::iterator lastused = of.end();
848 for (OrigFuncs::iterator viter = of.begin(); viter != of.end(); ++viter)
853 if (lastused == of.end())
855 // No used element => Remove the whole vector
859 of.resize(lastused - of.begin() + 1);
864 ////////////////////////////
866 ////////////////////////////
867 CSourceHookImpl::CHookManagerInfo::CHookManagerInfo() : m_HookManVersion(0)
870 CSourceHookImpl::CHookManagerInfo::~CHookManagerInfo()
874 IVfnPtr *CSourceHookImpl::CHookManagerInfo::FindVfnPtr(void *vfnptr)
876 VfnPtrListIter iter = m_VfnPtrs.find(vfnptr);
877 return iter == m_VfnPtrs.end() ? NULL : &(*iter);
879 void CSourceHookImpl::CHookManagerInfo::SetInfo(int vtbl_offs, int vtbl_idx, const char *proto)
881 m_VtblOffs = vtbl_offs;
882 m_VtblIdx = vtbl_idx;
885 void CSourceHookImpl::CHookManagerInfo::SetHookfuncVfnptr(void *hookfunc_vfnptr)
887 m_HookfuncVfnptr = hookfunc_vfnptr;
889 void CSourceHookImpl::CHookManagerInfo::SetVersion(int version)
891 m_HookManVersion = version;
893 ////////////////////////////
895 ////////////////////////////
898 // If you get a crash here, the ptr passed is invalid
899 // This usually means a SH_DECL_MANUALHOOK* with wrong thisptroffs/vtbloffs/vtblidx
900 CSourceHookImpl::CVfnPtr::CVfnPtr(void *ptr) : m_Ptr(ptr), m_OrigEntry(*reinterpret_cast<void**>(ptr))
903 CSourceHookImpl::CVfnPtr::~CVfnPtr()
907 void *CSourceHookImpl::CVfnPtr::GetVfnPtr()
912 void *CSourceHookImpl::CVfnPtr::GetOrigEntry()
917 IIface *CSourceHookImpl::CVfnPtr::FindIface(void *ptr)
919 IfaceListIter iter = m_Ifaces.find(ptr);
920 return iter == m_Ifaces.end() ? NULL : &(*iter);
923 ////////////////////////////
925 ////////////////////////////
926 CSourceHookImpl::CIface::CIface(void *ptr) : m_Ptr(ptr)
929 CSourceHookImpl::CIface::~CIface()
933 void *CSourceHookImpl::CIface::GetPtr()
938 IHookList *CSourceHookImpl::CIface::GetPreHooks()
942 IHookList *CSourceHookImpl::CIface::GetPostHooks()
947 ////////////////////////////
949 ////////////////////////////
951 CSourceHookImpl::CHookList::CHookList() : m_FreeIters(NULL), m_UsedIters(NULL),
955 CSourceHookImpl::CHookList::CHookList(const CHookList &other) : m_List(other.m_List),
956 m_FreeIters(NULL), m_UsedIters(NULL), m_Recall(false)
960 CSourceHookImpl::CHookList::~CHookList()
964 CIter *iter = m_FreeIters->m_pNext;
970 CIter *iter = m_UsedIters->m_pNext;
975 IHookList::IIter *CSourceHookImpl::CHookList::GetIter()
983 m_FreeIters = ret->m_pNext;
988 ret = new CIter(this);
991 // Muuuh, if we're recalling, it shall be a copy of the last iterator, incremented by one
992 if (m_Recall && m_UsedIters)
994 ret->Set(m_UsedIters); // m_UsedIters is the last returned and not released iterator
995 ret->Next(); // Use next instead of directly incrementing its m_Iter:
996 // skips paused plugins
999 ret->m_pNext = m_UsedIters;
1000 ret->m_pPrev = NULL;
1002 m_UsedIters->m_pPrev = ret;
1009 void CSourceHookImpl::CHookList::ReleaseIter(IIter *pIter)
1011 CIter *pIter2 = static_cast<CIter*>(pIter);
1013 // Unlink from m_UsedIters
1015 if (pIter2->m_pNext)
1016 pIter2->m_pNext->m_pPrev = pIter2->m_pPrev;
1017 if (pIter2->m_pPrev)
1018 pIter2->m_pPrev->m_pNext = pIter2->m_pNext;
1019 if (pIter2 == m_UsedIters)
1022 // Link to m_FreeIters
1024 pIter2->m_pNext = m_FreeIters;
1026 m_FreeIters = pIter2;
1028 // Reset recall state.
1032 CSourceHookImpl::CHookList::CIter::CIter(CHookList *pList) : m_pList(pList), m_pNext(NULL)
1036 CSourceHookImpl::CHookList::CIter::~CIter()
1040 void CSourceHookImpl::CHookList::CIter::Set(CIter *pOther)
1042 m_Iter = pOther->m_Iter;
1045 void CSourceHookImpl::CHookList::CIter::GoToBegin()
1047 m_Iter = m_pList->m_List.begin();
1051 bool CSourceHookImpl::CHookList::CIter::End()
1055 return m_Iter == m_pList->m_List.end();
1057 void CSourceHookImpl::CHookList::CIter::Next()
1064 void CSourceHookImpl::CHookList::CIter::Clear()
1068 void CSourceHookImpl::CHookList::CIter::SkipPaused()
1070 while (m_Iter != m_pList->m_List.end() && m_Iter->paused)
1074 ISHDelegate *CSourceHookImpl::CHookList::CIter::Handler()
1076 return m_Iter->handler;
1078 int CSourceHookImpl::CHookList::CIter::ThisPtrOffs()
1080 return m_Iter->thisptr_offs;
1083 //////////////////////////////////////////////////////////////////////////
1085 //////////////////////////////////////////////////////////////////////////
1086 char *CSourceHookImpl::CProto::DupProto(const char *p)
1091 size_t len = strlen(p);
1092 newproto = new char[len+1];
1093 memcpy(newproto, p, len+1);
1095 const ProtoInfo *pi1 = reinterpret_cast<const ProtoInfo *>(p);
1096 int *ar_copy = new int[pi1->numOfParams + 1];
1097 for (int i = 0; i <= pi1->numOfParams; i++)
1098 ar_copy[i] = pi1->params[i];
1099 ProtoInfo *copy = new ProtoInfo(pi1->retTypeSize, pi1->numOfParams, ar_copy);
1100 newproto = reinterpret_cast<char *>(copy);
1106 void CSourceHookImpl::CProto::FreeProto(char *prot)
1112 ProtoInfo *pi = reinterpret_cast<ProtoInfo *>(prot);
1113 delete [] pi->params;
1118 bool CSourceHookImpl::CProto::Equal(const char *p1, const char *p2)
1120 if (*p1 && *p2) // Case1: Both old
1122 // As in old versions
1123 return strcmp(p1, p2) == 0;
1125 else if (!*p1 && !*p2) // Case2: Both new
1127 const ProtoInfo *pi1 = reinterpret_cast<const ProtoInfo*>(p1);
1128 const ProtoInfo *pi2 = reinterpret_cast<const ProtoInfo*>(p2);
1130 if (pi1->retTypeSize == pi2->retTypeSize &&
1131 pi1->numOfParams == pi2->numOfParams)
1133 // params[0] is 0 for "normal" functions and -1 for vararg functions
1134 // params[1] is size of first parameter
1135 // params[2] is size of second parameter ...
1136 for (int i = 0; i <= pi1->numOfParams; ++i)
1138 if (pi1->params[i] != pi2->params[i])
1148 else // Case3: Mixed old/new
1156 //////////////////////////////////////////////////////////////////////////
1157 // CHookManagerContainer
1158 //////////////////////////////////////////////////////////////////////////
1159 void CSourceHookImpl::CHookManagerContainer::AddHookManager(Plugin plug, const CHookManagerInfo &hookman)
1162 // Check whether such a hook manager already exists; if yes, ignore.
1163 for (iter = begin(); iter != end(); ++iter)
1165 if (iter->m_Plug == plug && iter->m_Func == hookman.m_Func)
1169 // It doesn't -> add it. Add it to the end of its version group.
1170 for (iter = begin(); iter != end(); ++iter)
1172 if (iter->m_HookManVersion < hookman.m_HookManVersion)
1176 bool isBeginning = iter == begin();
1178 insert(iter, hookman);
1180 // If we inserted it at the beginning and if the dam breaks open many years too soon
1181 // and if there is more than one hookman and if the second one isn't empty, transfer
1182 // hooks from second to first
1184 if (isBeginning && size() > 1)
1187 iterator second = iter;
1190 if (!second->m_VfnPtrs.empty())
1192 // Move the vfnptrs from the old hook manager to the new one
1193 iter->m_VfnPtrs = second->m_VfnPtrs;
1194 second->m_VfnPtrs.clear();
1196 // Unregister the old one, register the new one
1197 second->m_Func(HA_Unregister, NULL);
1198 iter->m_Func(HA_Register, &(*iter));
1200 // Go through all vfnptrs in this hookman and patch them to point to the new manager's handler!
1202 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = iter->m_VfnPtrs.begin();
1203 vfnptr_iter != iter->m_VfnPtrs.end(); ++vfnptr_iter)
1205 // And DEREFERENCE newHookMan->m_HookfuncVfnptr!
1206 // otherwise it will be executing the vtable... had to find out the hard way
1207 *reinterpret_cast<void**>(vfnptr_iter->m_Ptr) = *reinterpret_cast<void**>(iter->m_HookfuncVfnptr);