One bugfix, addition of META_RETURN_(VALUE_)NEWPARAMS (implemented using recalls), tests now support compiling against an older version of sourcehook.h (through the sourcehook_test.h file) while main.cpp which now also acts as a CSourceHookImpl factory compiles against the new version.
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 SourceHook::HashFunction<int>(const int & k)
32 int SourceHook::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 (HookManInfoList::iterator hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end(); ++hmil_iter)
72 if (hmil_iter->m_Plug != plug)
74 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
75 vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
77 for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
78 iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
80 List<HookInfo>::iterator hook_iter;
81 TMP_CHECK_LIST(m_PreHooks);
82 TMP_CHECK_LIST(m_PostHooks);
90 void CSourceHookImpl::UnloadPlugin(Plugin plug)
92 // 1) Manually remove all hooks by this plugin
93 List<RemoveHookInfo> hookstoremove;
94 HookManInfoList::iterator hmil_iter;
96 #define TMP_CHECK_LIST(name, ispost) \
97 for (hook_iter = iface_iter->name.m_List.begin(); hook_iter != iface_iter->name.m_List.end(); ++hook_iter) \
98 if (hook_iter->plug == plug) \
99 hookstoremove.push_back(RemoveHookInfo(hook_iter->plug, iface_iter->m_Ptr, \
100 hook_iter->thisptr_offs, hmil_iter->m_Func, hook_iter->handler, ispost))
102 for (hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end(); ++hmil_iter)
104 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
105 vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
107 for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
108 iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
110 List<HookInfo>::iterator hook_iter;
111 TMP_CHECK_LIST(m_PreHooks, false);
112 TMP_CHECK_LIST(m_PostHooks, true);
116 #undef TMP_CHECK_LIST
118 for (List<RemoveHookInfo>::iterator rmiter = hookstoremove.begin(); rmiter != hookstoremove.end(); ++rmiter)
121 // 2) Other plugins may use hook managers in this plugin.
122 // Get a list of hook managers that are in this plugin and are used by other plugins
123 // Delete all hook managers that are in this plugin
125 HookManInfoList tmphookmans;
127 for (hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end();
128 erase ? hmil_iter=m_HookMans.erase(hmil_iter) : ++hmil_iter)
130 if (hmil_iter->m_Plug == plug)
132 if (!hmil_iter->m_VfnPtrs.empty())
134 // All hooks by this plugin are already removed
135 // So if there is a vfnptr, it has to be used by an other plugin
136 tmphookmans.push_back(*hmil_iter);
144 // For each hook manager that is used in an other plugin:
145 for (hmil_iter = tmphookmans.begin(); hmil_iter != tmphookmans.end(); ++hmil_iter)
147 // Find a suitable hook manager in an other plugin
148 HookManInfoList::iterator newHookMan = FindHookMan(m_HookMans.begin(), m_HookMans.end(),
149 hmil_iter->m_Proto, hmil_iter->m_VtblOffs, hmil_iter->m_VtblIdx);
151 // This should _never_ happen.
152 // If there is a hook from an other plugin, the plugin must have provided a hook manager as well.
153 SH_ASSERT(newHookMan != m_HookMans.end(),
154 ("Could not find a suitable hook manager in an other plugin!"));
156 // AddHook should make sure that every plugin only has _one_ hook manager for _one_ proto/vi/vo
157 SH_ASSERT(newHookMan->m_Plug != plug, ("New hook manager from same plugin!"));
159 // The first hook manager should be always used - so the new hook manager has to be empty
160 SH_ASSERT(newHookMan->m_VfnPtrs.empty(), ("New hook manager not empty!"));
162 // Move the vfnptrs from the old hook manager to the new one
163 newHookMan->m_VfnPtrs = hmil_iter->m_VfnPtrs;
165 // Unregister the old one, register the new one
166 hmil_iter->m_Func(HA_Unregister, NULL);
167 newHookMan->m_Func(HA_Register, &(*newHookMan));
169 // zOMG BAIL, here is part of what you wanted:
171 // Go through all vfnptrs in this hookman and patch them to point to the new manager's handler!
173 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = newHookMan->m_VfnPtrs.begin();
174 vfnptr_iter != newHookMan->m_VfnPtrs.end(); ++vfnptr_iter)
176 // And DEREFERENCE newHookMan->m_HookfuncVfnptr!
177 // otherwise it will be executing the vtable... had to find out the hard way
178 *reinterpret_cast<void**>(vfnptr_iter->m_Ptr) = *reinterpret_cast<void**>(newHookMan->m_HookfuncVfnptr);
181 // That should fix it, bail!
185 void CSourceHookImpl::RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc)
189 CHookManagerInfo tmp;
190 if (pubFunc(HA_GetInfo, &tmp) != 0)
193 HookManInfoList::iterator hmil_iter = FindHookMan(m_HookMans.begin(), m_HookMans.end(),
194 tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx);
196 if (hmil_iter == m_HookMans.end())
202 bool stillInUse = false;
203 List<RemoveHookInfo> hookstoremove;
205 // Iterate through all of the hook manager's hooks. Remove all hooks from the hookman's plugin.
206 #define TMP_CHECK_LIST(name, ispost) \
207 for (hook_iter = iface_iter->name.m_List.begin(); hook_iter != iface_iter->name.m_List.end(); ++hook_iter) \
208 if (hook_iter->plug == plug) \
209 hookstoremove.push_back(RemoveHookInfo(hook_iter->plug, iface_iter->m_Ptr, \
210 hook_iter->thisptr_offs, hmil_iter->m_Func, hook_iter->handler, ispost)); \
214 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
215 vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
217 for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
218 iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
220 List<HookInfo>::iterator hook_iter;
221 TMP_CHECK_LIST(m_PreHooks, false);
222 TMP_CHECK_LIST(m_PostHooks, true);
225 #undef TMP_CHECK_LIST
227 for (List<RemoveHookInfo>::iterator rmiter = hookstoremove.begin(); rmiter != hookstoremove.end(); ++rmiter)
230 CHookManagerInfo info = *hmil_iter;
232 // Unlink the hook manager from the list
233 m_HookMans.erase(hmil_iter);
235 // If there were any hooks from other plugins, find a new suitable hook manager.
238 // Find a suitable hook manager in an other plugin
239 HookManInfoList::iterator newHookMan = FindHookMan(m_HookMans.begin(), m_HookMans.end(),
240 info.m_Proto, info.m_VtblOffs, info.m_VtblIdx);
242 // This should _never_ happen.
243 // If there is a hook from an other plugin, the plugin must have provided a hook manager as well.
244 SH_ASSERT(newHookMan != m_HookMans.end(),
245 ("Could not find a suitable hook manager in an other plugin!"));
247 // AddHook should make sure that every plugin only has _one_ hook manager for _one_ proto/vi/vo
248 SH_ASSERT(newHookMan->m_Plug != plug, ("New hook manager from same plugin!"));
250 // The first hook manager should be always used - so the new hook manager has to be empty
251 SH_ASSERT(newHookMan->m_VfnPtrs.empty(), ("New hook manager not empty!"));
253 // Move the vfnptrs from the old hook manager to the new one
254 newHookMan->m_VfnPtrs = info.m_VfnPtrs;
256 // Unregister the old one, register the new one
257 info.m_Func(HA_Unregister, NULL);
258 newHookMan->m_Func(HA_Register, &(*newHookMan));
260 // Go through all vfnptrs in this hookman and patch them to point to the new manager's handler!
262 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = newHookMan->m_VfnPtrs.begin();
263 vfnptr_iter != newHookMan->m_VfnPtrs.end(); ++vfnptr_iter)
265 // And DEREFERENCE newHookMan->m_HookfuncVfnptr!
266 // otherwise it will be executing the vtable... had to find out the hard way
267 *reinterpret_cast<void**>(vfnptr_iter->m_Ptr) = *reinterpret_cast<void**>(newHookMan->m_HookfuncVfnptr);
272 void CSourceHookImpl::CompleteShutdown()
274 List<RemoveHookInfo> hookstoremove;
275 #define TMP_CHECK_LIST(name, ispost) \
276 for (hook_iter = iface_iter->name.m_List.begin(); hook_iter != iface_iter->name.m_List.end(); ++hook_iter) \
277 hookstoremove.push_back(RemoveHookInfo(hook_iter->plug, iface_iter->m_Ptr, \
278 hook_iter->thisptr_offs, hmil_iter->m_Func, hook_iter->handler, ispost))
279 for (HookManInfoList::iterator hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end(); ++hmil_iter)
281 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
282 vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
284 for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
285 iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
287 List<HookInfo>::iterator hook_iter;
288 TMP_CHECK_LIST(m_PreHooks, false);
289 TMP_CHECK_LIST(m_PostHooks, true);
293 #undef TMP_CHECK_LIST
295 for (List<RemoveHookInfo>::iterator rmiter = hookstoremove.begin(); rmiter != hookstoremove.end(); ++rmiter)
301 bool CSourceHookImpl::AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
303 void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
305 // 1) Get info about the hook manager
306 CHookManagerInfo tmp;
307 if (myHookMan(HA_GetInfo, &tmp) != 0)
310 void **cur_vtptr = *reinterpret_cast<void***>(
311 reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs);
312 void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.m_VtblIdx);
314 // Add the proposed hook manager to the _end_ of the list if the plugin doesn't have a hook manager
315 // with this proto/vo/vi registered
316 HookManInfoList::iterator hkmi_iter;
317 for (hkmi_iter = m_HookMans.begin(); hkmi_iter != m_HookMans.end(); ++hkmi_iter)
319 if (hkmi_iter->m_Plug == plug && ProtosEquiv(hkmi_iter->m_Proto, tmp.m_Proto) &&
320 hkmi_iter->m_VtblOffs == tmp.m_VtblOffs && hkmi_iter->m_VtblIdx == tmp.m_VtblIdx)
323 if (hkmi_iter == m_HookMans.end())
325 // No such hook manager from this plugin yet, add it!
326 tmp.m_Func = myHookMan;
328 m_HookMans.push_back(tmp);
331 // Then, search for a suitable hook manager (from the beginning)
332 HookManInfoList::iterator hookman = FindHookMan(m_HookMans.begin(), m_HookMans.end(), tmp.m_Proto,
333 tmp.m_VtblOffs, tmp.m_VtblIdx);
334 SH_ASSERT(hookman != m_HookMans.end(), ("No hookman found - but if there was none, we've just added one!"));
336 // Check whether there is already an other hook manager for the same vfnptr but with other values before
337 // If yes, it means that the two are incompatible, so return false.
338 for (hkmi_iter = m_HookMans.begin(); hkmi_iter != m_HookMans.end(); ++hkmi_iter)
340 if (hkmi_iter != hookman)
342 if (hkmi_iter->m_VfnPtrs.find(cur_vfnptr) != hkmi_iter->m_VfnPtrs.end())
347 // Tell it to store the pointer if it's not already active
348 if (hookman->m_VfnPtrs.empty())
349 hookman->m_Func(HA_Register, &(*hookman));
351 CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->m_VfnPtrs.find(cur_vfnptr);
353 if (vfnptr_iter == hookman->m_VfnPtrs.end())
356 CVfnPtr vfp(cur_vfnptr);
358 // Alter vtable entry
359 if (!SetMemAccess(cur_vtptr, sizeof(void*) * (tmp.m_VtblIdx + 1), SH_MEM_READ | SH_MEM_WRITE))
362 *reinterpret_cast<void**>(cur_vfnptr) = *reinterpret_cast<void**>(hookman->m_HookfuncVfnptr);
364 hookman->m_VfnPtrs.push_back(vfp);
366 // Make vfnptr_iter point to the new element
367 vfnptr_iter = hookman->m_VfnPtrs.end();
370 // Now that it is done, check whether we have to update any callclasses
371 ApplyCallClassPatches(adjustediface, tmp.m_VtblOffs, tmp.m_VtblIdx, vfp.m_OrigEntry);
374 CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.find(adjustediface);
375 if (iface_iter == vfnptr_iter->m_Ifaces.end())
378 vfnptr_iter->m_Ifaces.push_back(CIface(adjustediface));
380 // Make iface_iter point to the new element
381 iface_iter = vfnptr_iter->m_Ifaces.end();
387 hookinfo.handler = handler;
388 hookinfo.plug = plug;
389 hookinfo.paused = false;
390 hookinfo.thisptr_offs = thisptr_offs;
392 iface_iter->m_PostHooks.m_List.push_back(hookinfo);
394 iface_iter->m_PreHooks.m_List.push_back(hookinfo);
399 bool CSourceHookImpl::RemoveHook(RemoveHookInfo info)
401 return RemoveHook(info.plug, info.iface, info.thisptr_offs, info.hookman, info.handler, info.post);
404 bool CSourceHookImpl::RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
406 void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface)+thisptr_offs);
407 CHookManagerInfo tmp;
408 if (myHookMan(HA_GetInfo, &tmp) != 0)
411 // Find the hook manager and the hook
412 HookManInfoList::iterator hookman = FindHookMan(m_HookMans.begin(), m_HookMans.end(),
413 tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx);
414 if (hookman == m_HookMans.end())
417 if (!ModuleInMemory(reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs,
418 sizeof(void*) * (tmp.m_VtblIdx + 1)))
420 // The module the vtable was in is already unloaded.
421 hookman->m_VfnPtrs.clear();
422 hookman->m_Func(HA_Unregister, NULL);
426 void **cur_vtptr = *reinterpret_cast<void***>(
427 reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs);
428 void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.m_VtblIdx);
430 CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->m_VfnPtrs.find(cur_vfnptr);
432 if (vfnptr_iter == hookman->m_VfnPtrs.end())
435 for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
436 iface_iter != vfnptr_iter->m_Ifaces.end();)
438 List<HookInfo> &hooks =
439 post ? iface_iter->m_PostHooks.m_List : iface_iter->m_PreHooks.m_List;
442 for (List<HookInfo>::iterator hookiter = hooks.begin();
443 hookiter != hooks.end(); )
445 erase = hookiter->plug == plug && hookiter->handler->IsEqual(handler) &&
446 hookiter->thisptr_offs == thisptr_offs;
449 hookiter->handler->DeleteThis(); // Make the _plugin_ delete the handler object
451 // Move all iterators pointing at this
452 List<HookInfo>::iterator oldhookiter = hookiter;
453 hookiter = hooks.erase(hookiter);
454 List<HookInfo>::iterator newhookiter = hookiter;
455 --newhookiter; // The hook loop will ++ it then
456 CHookList::CIter *pItIter;
457 for (pItIter = iface_iter->m_PreHooks.m_UsedIters; pItIter; pItIter = pItIter->m_pNext)
458 if (pItIter->m_Iter == oldhookiter)
459 pItIter->m_Iter = newhookiter;
464 if (iface_iter->m_PostHooks.m_List.empty() && iface_iter->m_PreHooks.m_List.empty())
466 // There are no hooks on this iface anymore...
467 for (HookLoopInfoStack::iterator hli_iter = m_HLIStack.begin();
468 hli_iter != m_HLIStack.end(); ++hli_iter)
470 if (hli_iter->pCurIface == static_cast<IIface*>(&(*iface_iter)))
471 hli_iter->shouldContinue = false;
474 iface_iter = vfnptr_iter->m_Ifaces.erase(iface_iter);
475 if (vfnptr_iter->m_Ifaces.empty())
477 // No ifaces at all -> Deactivate the hook
478 *reinterpret_cast<void**>(vfnptr_iter->m_Ptr) = vfnptr_iter->m_OrigEntry;
480 hookman->m_VfnPtrs.erase(vfnptr_iter);
482 // Remove callclass patch
483 for (Impl_CallClassList::iterator cciter = m_CallClasses.begin(); cciter != m_CallClasses.end(); ++cciter)
484 if (cciter->m_Ptr == adjustediface)
485 cciter->RemoveCallClassPatch(tmp.m_VtblOffs, tmp.m_VtblIdx);
487 if (hookman->m_VfnPtrs.empty())
489 // Unregister the hook manager
490 hookman->m_Func(HA_Unregister, NULL);
493 // Don't try to continue looping through ifaces
494 // - the list is already invalid
504 GenericCallClass *CSourceHookImpl::GetCallClass(void *iface, size_t size)
506 for (Impl_CallClassList::iterator cciter = m_CallClasses.begin(); cciter != m_CallClasses.end(); ++cciter)
508 if (cciter->m_Ptr == iface && cciter->m_ObjSize == size)
510 ++cciter->m_RefCounter;
517 CCallClassImpl tmp(iface, size);
518 ApplyCallClassPatches(tmp);
519 m_CallClasses.push_back(tmp);
520 return &m_CallClasses.back();
523 void CSourceHookImpl::ReleaseCallClass(GenericCallClass *ptr)
525 Impl_CallClassList::iterator iter = m_CallClasses.find(ptr);
526 if (iter == m_CallClasses.end())
528 --iter->m_RefCounter;
529 if (iter->m_RefCounter < 1)
530 m_CallClasses.erase(iter);
533 void CSourceHookImpl::ApplyCallClassPatches(CCallClassImpl &cc)
535 for (HookManInfoList::iterator hookman = m_HookMans.begin(); hookman != m_HookMans.end(); ++hookman)
537 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->m_VfnPtrs.begin();
538 vfnptr_iter != hookman->m_VfnPtrs.end(); ++vfnptr_iter)
540 for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
541 iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
543 if (iface_iter->m_Ptr >= cc.m_Ptr &&
544 iface_iter->m_Ptr < (reinterpret_cast<char*>(cc.m_Ptr) + cc.m_ObjSize))
546 cc.ApplyCallClassPatch(static_cast<int>(reinterpret_cast<char*>(iface_iter->m_Ptr) -
547 reinterpret_cast<char*>(cc.m_Ptr)) + hookman->m_VtblOffs,
548 hookman->m_VtblIdx, vfnptr_iter->m_OrigEntry);
555 void CSourceHookImpl::ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry)
557 for (Impl_CallClassList::iterator cc_iter = m_CallClasses.begin(); cc_iter != m_CallClasses.end();
560 if (ifaceptr >= cc_iter->m_Ptr &&
561 ifaceptr < (reinterpret_cast<char*>(cc_iter->m_Ptr) + cc_iter->m_ObjSize))
563 cc_iter->ApplyCallClassPatch(static_cast<int>(reinterpret_cast<char*>(ifaceptr) -
564 reinterpret_cast<char*>(cc_iter->m_Ptr)) + vtbl_offs, vtbl_idx, orig_entry);
569 void CSourceHookImpl::RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx)
571 for (Impl_CallClassList::iterator cc_iter = m_CallClasses.begin(); cc_iter != m_CallClasses.end();
574 if (ifaceptr >= cc_iter->m_Ptr &&
575 ifaceptr < (reinterpret_cast<char*>(cc_iter->m_Ptr) + cc_iter->m_ObjSize))
577 cc_iter->RemoveCallClassPatch(vtbl_offs, vtbl_idx);
582 CSourceHookImpl::HookManInfoList::iterator CSourceHookImpl::FindHookMan(HookManInfoList::iterator begin,
583 HookManInfoList::iterator end, const char *proto, int vtblofs, int vtblidx)
585 HookManInfoList::iterator hookmaniter;
586 for (hookmaniter = m_HookMans.begin(); hookmaniter != m_HookMans.end(); ++hookmaniter)
588 if (ProtosEquiv(hookmaniter->m_Proto, proto) && hookmaniter->m_VtblOffs == vtblofs &&
589 hookmaniter->m_VtblIdx == vtblidx)
595 void CSourceHookImpl::SetPluginPaused(Plugin plug, bool paused)
597 // Go through all hook managers, all interfaces, and set the status of all hooks of this plugin to paused
598 for (HookManInfoList::iterator hookmaniter = m_HookMans.begin(); hookmaniter != m_HookMans.end(); ++hookmaniter)
599 for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookmaniter->m_VfnPtrs.begin();
600 vfnptr_iter != hookmaniter->m_VfnPtrs.end(); ++vfnptr_iter)
601 for (CVfnPtr::IfaceListIter ifaceiter = vfnptr_iter->m_Ifaces.begin();
602 ifaceiter != vfnptr_iter->m_Ifaces.end(); ++ifaceiter)
604 for (List<HookInfo>::iterator hookiter = ifaceiter->m_PreHooks.m_List.begin();
605 hookiter != ifaceiter->m_PreHooks.m_List.end(); ++hookiter)
606 if (plug == hookiter->plug)
607 hookiter->paused = paused;
609 for (List<HookInfo>::iterator hookiter = ifaceiter->m_PostHooks.m_List.begin();
610 hookiter != ifaceiter->m_PostHooks.m_List.end(); ++hookiter)
611 if (plug == hookiter->plug)
612 hookiter->paused = paused;
615 void CSourceHookImpl::PausePlugin(Plugin plug)
617 SetPluginPaused(plug, true);
620 void CSourceHookImpl::UnpausePlugin(Plugin plug)
622 SetPluginPaused(plug, false);
625 void CSourceHookImpl::HookLoopBegin(IIface *pIface)
628 hli.pCurIface = pIface;
629 hli.shouldContinue = true;
631 m_HLIStack.push(hli);
634 void CSourceHookImpl::HookLoopEnd()
639 void CSourceHookImpl::SetRes(META_RES res)
641 *m_HLIStack.front().pCurRes = res;
644 META_RES CSourceHookImpl::GetPrevRes()
646 return *m_HLIStack.front().pPrevRes;
649 META_RES CSourceHookImpl::GetStatus()
651 return *m_HLIStack.front().pStatus;
654 const void *CSourceHookImpl::GetOrigRet()
656 return m_HLIStack.front().pOrigRet;
659 const void *CSourceHookImpl::GetOverrideRet()
661 // pOverrideRet is always set since recalls were introduced
662 // GetOverrideRetPtr was added; a function which always returns pOverrideRet
663 // so that RETURN_META_VALUE_NEWPARAMS can provide an override result
665 // This means that we have to filter GetOverrideRet:
666 // If the status variable is < MRES_OVERRIDE, return NULL.
668 return (*m_HLIStack.front().pStatus < MRES_OVERRIDE) ?
669 NULL : m_HLIStack.front().pOverrideRet;
672 void *CSourceHookImpl::GetOverrideRetPtr()
674 // As described in the comment above: always return pOverrideRet
675 return m_HLIStack.front().pOverrideRet;
678 void *CSourceHookImpl::GetIfacePtr()
680 return *m_HLIStack.front().pIfacePtrPtr;
683 void CSourceHookImpl::SetCurResPtr(META_RES *mres)
685 m_HLIStack.front().pCurRes = mres;
688 void CSourceHookImpl::SetPrevResPtr(META_RES *mres)
690 m_HLIStack.front().pPrevRes = mres;
692 // If we're recalling, drag the previous mres value to the new hookfunc
693 if (m_HLIStack.size() > 1 && m_HLIStack.second().recall)
694 *mres = *m_HLIStack.second().pPrevRes;
697 void CSourceHookImpl::SetStatusPtr(META_RES *mres)
699 m_HLIStack.front().pStatus = mres;
701 // If we're recalling, drag the previous mres value to the new hookfunc
702 if (m_HLIStack.size() > 1 && m_HLIStack.second().recall)
703 *mres = *m_HLIStack.second().pStatus;
706 void CSourceHookImpl::SetIfacePtrPtr(void **pp)
708 m_HLIStack.front().pIfacePtrPtr = pp;
711 void CSourceHookImpl::SetOrigRetPtr(const void *ptr)
713 m_HLIStack.front().pOrigRet = ptr;
715 void CSourceHookImpl::SetOverrideRetPtr(void *ptr)
717 m_HLIStack.front().pOverrideRet = ptr;
720 // New function which does all of the above + more :)
721 void *CSourceHookImpl::SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
722 void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr)
724 HookLoopInfo &hli = m_HLIStack.front();
725 hli.pStatus = statusPtr;
726 hli.pPrevRes = prevResPtr;
727 hli.pCurRes = curResPtr;
728 hli.pIfacePtrPtr = ifacePtrPtr;
729 hli.pOrigRet = origRetPtr;
731 // Handle some recall stuff
732 if (m_HLIStack.size() > 1 && m_HLIStack.second().recall)
734 HookLoopInfo &other = m_HLIStack.second();
735 *statusPtr = *other.pStatus;
736 *prevResPtr = *other.pStatus;
737 hli.pOverrideRet = other.pOverrideRet;
740 hli.pOverrideRet = overrideRetPtr;
742 // Tell the hook func which override ret ptr to use
743 return hli.pOverrideRet;
746 bool CSourceHookImpl::ShouldContinue()
748 // If recall is true, we shall not continue either.
749 // This is because, if it's true and ShouldContinue is called, it suggests that the
750 // actual recall is done and that we are back in the original handler which shall return
753 return m_HLIStack.front().shouldContinue && !m_HLIStack.front().recall;
756 void CSourceHookImpl::DoRecall()
758 if (!m_HLIStack.empty())
760 m_HLIStack.front().recall = true;
761 CHookList *mlist = static_cast<CHookList*>(m_HLIStack.front().pCurIface->GetPreHooks());
762 mlist->m_Recall = true;
764 // The hookfunc usually do this, but it won't have a chance to see it,
765 // so for recalls, we update status here if it's required
766 if (*m_HLIStack.front().pCurRes > *m_HLIStack.front().pStatus)
767 *m_HLIStack.front().pStatus = *m_HLIStack.front().pCurRes;
772 ////////////////////////////
774 ////////////////////////////
775 CSourceHookImpl::CCallClassImpl::CCallClassImpl(void *ptr, size_t size)
776 : m_Ptr(ptr), m_ObjSize(size), m_RefCounter(1)
779 CSourceHookImpl::CCallClassImpl::~CCallClassImpl()
783 void *CSourceHookImpl::CCallClassImpl::GetThisPtr()
787 void *CSourceHookImpl::CCallClassImpl::GetOrigFunc(int vtbloffs, int vtblidx)
789 OrigVTables::iterator iter = m_VT.find(vtbloffs);
790 if (iter != m_VT.end() && iter->val.size() > (size_t)vtblidx)
792 return iter->val[vtblidx];
797 void CSourceHookImpl::CCallClassImpl::ApplyCallClassPatch(int vtbl_offs, int vtbl_idx, void *orig_entry)
799 OrigFuncs &tmpvec = m_VT[vtbl_offs];
800 if (tmpvec.size() <= (size_t)vtbl_idx)
801 tmpvec.resize(vtbl_idx+1);
802 tmpvec[vtbl_idx] = orig_entry;
804 void CSourceHookImpl::CCallClassImpl::RemoveCallClassPatch(int vtbl_offs, int vtbl_idx)
806 OrigVTables::iterator iter = m_VT.find(vtbl_offs);
807 if (iter != m_VT.end())
809 if (iter->val.size() > (size_t)vtbl_idx)
811 iter->val[vtbl_idx] = 0;
813 OrigFuncs &of = iter->val;
814 OrigFuncs::iterator lastused = of.end();
815 for (OrigFuncs::iterator viter = of.begin(); viter != of.end(); ++viter)
820 if (lastused == of.end())
822 // No used element => Remove the whole vector
826 of.resize(lastused - of.begin() + 1);
831 bool CSourceHookImpl::ProtosEquiv(const char *p1, const char *p2)
834 Old protos look like this:
841 "attrib|param1_type|param2_type
845 "attrib|ret_type|param1_type"
847 "attrib|ret_type|param2_type"
849 New protos are in fact pointers to the ProtoInfo sturcture (see sourcehook.h for details)
851 Old protos _never_ begin with a null character
852 New protos _always_ begin with a null character
855 if (*p1 && *p2) // Case1: Both old
857 // As in old versions
858 return strcmp(p1, p2) == 0;
860 else if (!*p1 && !*p2) // Case2: Both new
862 const ProtoInfo *pi1 = reinterpret_cast<const ProtoInfo*>(p1);
863 const ProtoInfo *pi2 = reinterpret_cast<const ProtoInfo*>(p2);
865 if (pi1->retTypeSize == pi2->retTypeSize &&
866 pi1->numOfParams == pi2->numOfParams)
868 // params[0] is 0 for "normal" functions and -1 for vararg functions
869 // params[1] is size of first parameter
870 // params[2] is size of second parameter ...
871 for (int i = 0; i <= pi1->numOfParams; ++i)
873 if (pi1->params[i] != pi2->params[i])
883 else // Case3: Mixed old/new
890 ////////////////////////////
892 ////////////////////////////
893 CSourceHookImpl::CHookManagerInfo::~CHookManagerInfo()
897 IVfnPtr *CSourceHookImpl::CHookManagerInfo::FindVfnPtr(void *vfnptr)
899 VfnPtrListIter iter = m_VfnPtrs.find(vfnptr);
900 return iter == m_VfnPtrs.end() ? NULL : &(*iter);
902 void CSourceHookImpl::CHookManagerInfo::SetInfo(int vtbl_offs, int vtbl_idx, const char *proto)
904 m_VtblOffs = vtbl_offs;
905 m_VtblIdx = vtbl_idx;
908 void CSourceHookImpl::CHookManagerInfo::SetHookfuncVfnptr(void *hookfunc_vfnptr)
910 m_HookfuncVfnptr = hookfunc_vfnptr;
913 ////////////////////////////
915 ////////////////////////////
918 // If you get a crash here, the ptr passed is invalid
919 // This usually means a SH_DECL_MANUALHOOK* with wrong thisptroffs/vtbloffs/vtblidx
920 CSourceHookImpl::CVfnPtr::CVfnPtr(void *ptr) : m_Ptr(ptr), m_OrigEntry(*reinterpret_cast<void**>(ptr))
923 CSourceHookImpl::CVfnPtr::~CVfnPtr()
927 void *CSourceHookImpl::CVfnPtr::GetVfnPtr()
932 void *CSourceHookImpl::CVfnPtr::GetOrigEntry()
937 IIface *CSourceHookImpl::CVfnPtr::FindIface(void *ptr)
939 IfaceListIter iter = m_Ifaces.find(ptr);
940 return iter == m_Ifaces.end() ? NULL : &(*iter);
943 ////////////////////////////
945 ////////////////////////////
946 CSourceHookImpl::CIface::CIface(void *ptr) : m_Ptr(ptr)
949 CSourceHookImpl::CIface::~CIface()
953 void *CSourceHookImpl::CIface::GetPtr()
958 IHookList *CSourceHookImpl::CIface::GetPreHooks()
962 IHookList *CSourceHookImpl::CIface::GetPostHooks()
967 ////////////////////////////
969 ////////////////////////////
971 CSourceHookImpl::CHookList::CHookList() : m_FreeIters(NULL), m_UsedIters(NULL),
975 CSourceHookImpl::CHookList::CHookList(const CHookList &other) : m_List(other.m_List),
976 m_FreeIters(NULL), m_UsedIters(NULL), m_Recall(false)
980 CSourceHookImpl::CHookList::~CHookList()
984 CIter *iter = m_FreeIters->m_pNext;
990 CIter *iter = m_UsedIters->m_pNext;
995 IHookList::IIter *CSourceHookImpl::CHookList::GetIter()
1001 m_FreeIters = ret->m_pNext;
1006 ret = new CIter(this);
1009 // Muuuh, if we're recalling, it shall be a copy of the last iterator, incremented by one
1010 if (m_Recall && m_UsedIters)
1012 ret->Set(m_UsedIters); // m_UsedIters is the last returned and not released iterator
1013 ret->Next(); // Use next instead of directly incrementing its m_Iter:
1014 // skips paused plugins
1017 ret->m_pNext = m_UsedIters;
1018 ret->m_pPrev = NULL;
1020 m_UsedIters->m_pPrev = ret;
1027 void CSourceHookImpl::CHookList::ReleaseIter(IIter *pIter)
1029 CIter *pIter2 = static_cast<CIter*>(pIter);
1031 // Unlink from m_UsedIters
1033 if (pIter2->m_pNext)
1034 pIter2->m_pNext->m_pPrev = pIter2->m_pPrev;
1035 if (pIter2->m_pPrev)
1036 pIter2->m_pPrev->m_pNext = pIter2->m_pNext;
1037 if (pIter2 == m_UsedIters)
1040 // Link to m_FreeIters
1042 pIter2->m_pNext = m_FreeIters;
1044 m_FreeIters = pIter2;
1046 // Reset recall state.
1050 CSourceHookImpl::CHookList::CIter::CIter(CHookList *pList) : m_pList(pList), m_pNext(NULL)
1054 CSourceHookImpl::CHookList::CIter::~CIter()
1058 void CSourceHookImpl::CHookList::CIter::Set(CIter *pOther)
1060 m_Iter = pOther->m_Iter;
1063 void CSourceHookImpl::CHookList::CIter::GoToBegin()
1065 m_Iter = m_pList->m_List.begin();
1069 bool CSourceHookImpl::CHookList::CIter::End()
1073 return m_Iter == m_pList->m_List.end();
1075 void CSourceHookImpl::CHookList::CIter::Next()
1082 void CSourceHookImpl::CHookList::CIter::Clear()
1086 void CSourceHookImpl::CHookList::CIter::SkipPaused()
1088 while (m_Iter != m_pList->m_List.end() && m_Iter->paused)
1092 ISHDelegate *CSourceHookImpl::CHookList::CIter::Handler()
1094 return m_Iter->handler;
1096 int CSourceHookImpl::CHookList::CIter::ThisPtrOffs()
1098 return m_Iter->thisptr_offs;