1.1 --- a/core-legacy/sourcehook/sourcehook.cpp Fri Feb 27 00:19:50 2009 -0600
1.2 +++ b/core-legacy/sourcehook/sourcehook.cpp Wed Oct 28 23:14:32 2009 -0700
1.3 @@ -1,5 +1,5 @@
1.4 /* ======== SourceHook ========
1.5 -* Copyright (C) 2004-2008 Metamod:Source Development Team
1.6 +* Copyright (C) 2004-2007 Metamod:Source Development Team
1.7 * No warranties of any kind
1.8 *
1.9 * License: zlib/libpng
1.10 @@ -39,7 +39,7 @@
1.11 return -1;
1.12 return 0;
1.13 }
1.14 - CSourceHookImpl::CSourceHookImpl()
1.15 + CSourceHookImpl::CSourceHookImpl() : m_OneIgnore(NULL), m_IgnoreActive(false)
1.16 {
1.17 }
1.18 CSourceHookImpl::~CSourceHookImpl()
1.19 @@ -277,24 +277,32 @@
1.20 m_HookMans.clear();
1.21 }
1.22
1.23 +
1.24 bool CSourceHookImpl::AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
1.25 {
1.26 - void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
1.27 + return AddHookNew(plug, Hook_Normal, iface, thisptr_offs, myHookMan, handler, post) != 0 ? true : false;
1.28 + }
1.29 +
1.30 + int CSourceHookImpl::AddHookNew(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
1.31 + ISHDelegate *handler, bool post)
1.32 + {
1.33 + void *adjustediface = NULL;
1.34 + void **cur_vtptr = NULL;
1.35 + void *cur_vfnptr = NULL;
1.36 +
1.37 + if (mode != Hook_Normal && mode != Hook_VP && mode != Hook_DVP)
1.38 + return 0;
1.39
1.40 // 1) Get info about the hook manager
1.41 CHookManagerInfo tmp;
1.42 if (myHookMan(HA_GetInfo, &tmp) != 0)
1.43 - return false;
1.44 + return 0;
1.45
1.46 tmp.m_Func = myHookMan;
1.47 tmp.m_Plug = plug;
1.48
1.49 CHookManagerContainer::HMCI hmci(tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx);
1.50
1.51 - void **cur_vtptr = *reinterpret_cast<void***>(
1.52 - reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs);
1.53 - void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.m_VtblIdx);
1.54 -
1.55 // Add the container if not already added
1.56 HookManContList::iterator hmcl_iter = m_HookMans.find(hmci);
1.57 if (hmcl_iter == m_HookMans.end())
1.58 @@ -320,7 +328,7 @@
1.59 hmil_iter != hmcl_iter2->end(); ++hmil_iter)
1.60 {
1.61 if (hmil_iter->m_VfnPtrs.find(cur_vfnptr) != hmil_iter->m_VfnPtrs.end())
1.62 - return false;
1.63 + return 0;
1.64 }
1.65 }
1.66 }
1.67 @@ -329,16 +337,37 @@
1.68 if (hookman->m_VfnPtrs.empty())
1.69 hookman->m_Func(HA_Register, &(*hookman));
1.70
1.71 +
1.72 + // find vfnptr
1.73 + if (mode == Hook_Normal || mode == Hook_VP)
1.74 + {
1.75 + adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
1.76 +
1.77 + cur_vtptr = *reinterpret_cast<void***>(
1.78 + reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs);
1.79 + cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.m_VtblIdx);
1.80 +
1.81 + // For Hook_VP, adjustediface will be set to NULL later
1.82 + // because we first have to patch callclasses (callclasses are deprecated but left in for backwards compat)
1.83 + }
1.84 + else if (mode == Hook_DVP)
1.85 + {
1.86 + adjustediface = NULL;
1.87 +
1.88 + cur_vtptr = reinterpret_cast<void**>(iface);
1.89 + cur_vfnptr = cur_vtptr + tmp.m_VtblIdx;
1.90 + }
1.91 +
1.92 CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->m_VfnPtrs.find(cur_vfnptr);
1.93
1.94 if (vfnptr_iter == hookman->m_VfnPtrs.end())
1.95 {
1.96 // Add a new one
1.97 - CVfnPtr vfp(cur_vfnptr);
1.98 + CVfnPtr vfp(cur_vfnptr, &m_OneIgnore);
1.99
1.100 // Alter vtable entry
1.101 if (!SetMemAccess(cur_vtptr, sizeof(void*) * (tmp.m_VtblIdx + 1), SH_MEM_READ | SH_MEM_WRITE))
1.102 - return false;
1.103 + return 0;
1.104
1.105 *reinterpret_cast<void**>(cur_vfnptr) = *reinterpret_cast<void**>(hookman->m_HookfuncVfnptr);
1.106
1.107 @@ -352,6 +381,10 @@
1.108 ApplyCallClassPatches(adjustediface, tmp.m_VtblOffs, tmp.m_VtblIdx, vfp.m_OrigEntry);
1.109 }
1.110
1.111 + // If it's a VP hook, set adjustediface to NULL now ( see the comments at the beginning of sourcehook_impl.h )
1.112 + if (mode == Hook_VP)
1.113 + adjustediface = NULL;
1.114 +
1.115 CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.find(adjustediface);
1.116 if (iface_iter == vfnptr_iter->m_Ifaces.end())
1.117 {
1.118 @@ -361,6 +394,34 @@
1.119 // Make iface_iter point to the new element
1.120 iface_iter = vfnptr_iter->m_Ifaces.end();
1.121 --iface_iter;
1.122 +
1.123 + // If this is a VP-Hook-NULL interface, go through all other interfaces of this vfnptr and tell them!
1.124 + if (adjustediface == NULL)
1.125 + {
1.126 + for (CVfnPtr::IfaceListIter iface_iter2 = vfnptr_iter->m_Ifaces.begin();
1.127 + iface_iter2 != vfnptr_iter->m_Ifaces.end(); ++iface_iter2)
1.128 + {
1.129 + if (*iface_iter2 != NULL)
1.130 + {
1.131 + iface_iter2->m_PreHooks.SetVPList(&iface_iter->m_PreHooks.m_List);
1.132 + iface_iter2->m_PostHooks.SetVPList(&iface_iter->m_PostHooks.m_List);
1.133 + }
1.134 + }
1.135 + }
1.136 + else
1.137 + {
1.138 + // If this is a new non-VP-Hook-NULL interface, look for a non-VP-Hook-NULL interface!
1.139 + for (CVfnPtr::IfaceListIter iface_iter2 = vfnptr_iter->m_Ifaces.begin();
1.140 + iface_iter2 != vfnptr_iter->m_Ifaces.end(); ++iface_iter2)
1.141 + {
1.142 + if (*iface_iter2 == NULL)
1.143 + {
1.144 + iface_iter->m_PreHooks.SetVPList(&iface_iter2->m_PreHooks.m_List);
1.145 + iface_iter->m_PostHooks.SetVPList(&iface_iter2->m_PostHooks.m_List);
1.146 + break; // There can only be one!
1.147 + }
1.148 + }
1.149 + }
1.150 }
1.151
1.152 // Add the hook
1.153 @@ -369,11 +430,136 @@
1.154 hookinfo.plug = plug;
1.155 hookinfo.paused = false;
1.156 hookinfo.thisptr_offs = thisptr_offs;
1.157 + hookinfo.hookid = m_HookIDMan.New(tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx, cur_vfnptr,
1.158 + adjustediface, plug, thisptr_offs, handler, post);
1.159 +
1.160 if (post)
1.161 iface_iter->m_PostHooks.m_List.push_back(hookinfo);
1.162 else
1.163 iface_iter->m_PreHooks.m_List.push_back(hookinfo);
1.164
1.165 + return hookinfo.hookid;
1.166 + }
1.167 +
1.168 + bool CSourceHookImpl::RemoveHookByID(Plugin plug, int hookid)
1.169 + {
1.170 + const CHookIDManager::Entry *hentry;
1.171 +
1.172 + hentry = m_HookIDMan.QueryHook(hookid);
1.173 + if (!hentry)
1.174 + {
1.175 + // hookid doesn't exist !
1.176 + return false;
1.177 + }
1.178 +
1.179 + HookManContList::iterator hmcl_iter = m_HookMans.find(
1.180 + CHookManagerContainer::HMCI(hentry->proto.GetProto(), hentry->vtbl_offs, hentry->vtbl_idx));
1.181 + if (hmcl_iter == m_HookMans.end() || hmcl_iter->empty())
1.182 + return false;
1.183 + CHookManagerContainer::iterator hookman = hmcl_iter->begin();
1.184 +
1.185 + // Find vfnptr
1.186 + CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->m_VfnPtrs.find(hentry->vfnptr);
1.187 + if (vfnptr_iter == hookman->m_VfnPtrs.end())
1.188 + return false;
1.189 +
1.190 + // Find iface
1.191 + CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.find(hentry->adjustediface);
1.192 + if (iface_iter == vfnptr_iter->m_Ifaces.end())
1.193 + return false;
1.194 +
1.195 + // Find hook
1.196 + List<HookInfo> &hooks = hentry->post ? iface_iter->m_PostHooks.m_List : iface_iter->m_PreHooks.m_List;
1.197 +
1.198 + List<HookInfo>::iterator hookiter = hooks.find(hookid);
1.199 + if (hookiter == hooks.end())
1.200 + return false;
1.201 +
1.202 + hookiter->handler->DeleteThis();
1.203 +
1.204 + // Move all iterators pointing at this
1.205 + List<HookInfo>::iterator oldhookiter = hookiter;
1.206 + hookiter = hooks.erase(hookiter);
1.207 + List<HookInfo>::iterator newhookiter = hookiter;
1.208 + --newhookiter; // The hook loop will ++ it then
1.209 + CHookList::CIter *pItIter;
1.210 +
1.211 + for (pItIter = iface_iter->m_PreHooks.m_UsedIters; pItIter; pItIter = pItIter->m_pNext)
1.212 + if (pItIter->m_Iter == oldhookiter)
1.213 + pItIter->m_Iter = newhookiter;
1.214 +
1.215 + // If this is VP-Hook-NULL interface, also check all other interfaces of this vfnptr
1.216 + if (*iface_iter == NULL)
1.217 + {
1.218 + for (CVfnPtr::IfaceListIter iface_iter2 = vfnptr_iter->m_Ifaces.begin();
1.219 + iface_iter2 != vfnptr_iter->m_Ifaces.end(); ++iface_iter2)
1.220 + {
1.221 + if (*iface_iter2 != NULL)
1.222 + {
1.223 + for (pItIter = iface_iter2->m_PreHooks.m_UsedIters; pItIter; pItIter = pItIter->m_pNext)
1.224 + if (pItIter->m_Iter == oldhookiter)
1.225 + pItIter->m_Iter = newhookiter;
1.226 + }
1.227 + }
1.228 + }
1.229 +
1.230 + if (iface_iter->m_PostHooks.m_List.empty() && iface_iter->m_PreHooks.m_List.empty())
1.231 + {
1.232 + // If this is a VP-Hook-NULL interface, go through all other interfaces of this vfnptr and tell them!
1.233 + if (*iface_iter == NULL)
1.234 + {
1.235 + for (CVfnPtr::IfaceListIter iface_iter2 = vfnptr_iter->m_Ifaces.begin();
1.236 + iface_iter2 != vfnptr_iter->m_Ifaces.end(); ++iface_iter2)
1.237 + {
1.238 + if (iface_iter2->m_Ptr != NULL)
1.239 + {
1.240 + iface_iter2->m_PreHooks.ClearVPList();
1.241 + iface_iter2->m_PostHooks.ClearVPList();
1.242 + }
1.243 + }
1.244 + }
1.245 +
1.246 +
1.247 + // There are no hooks on this iface anymore...
1.248 + for (HookLoopInfoStack::iterator hli_iter = m_HLIStack.begin();
1.249 + hli_iter != m_HLIStack.end(); ++hli_iter)
1.250 + {
1.251 + if (hli_iter->pCurIface == static_cast<IIface*>(&(*iface_iter)))
1.252 + hli_iter->shouldContinue = false;
1.253 + }
1.254 +
1.255 + iface_iter = vfnptr_iter->m_Ifaces.erase(iface_iter);
1.256 + if (vfnptr_iter->m_Ifaces.empty())
1.257 + {
1.258 + // No ifaces at all -> Deactivate the hook
1.259 +
1.260 + // Only patch the vfnptr back if the module is still in memory
1.261 + // If it's not, do not remove stuff like we did before
1.262 + // First off we did it wrong (shutdown the whole hookman, uh..) and secondly applications may be
1.263 + // confused by RemoveHook returning false then (yeah, I know, I made this one up, no one checks for RemoveHook error)
1.264 + if (ModuleInMemory(reinterpret_cast<char*>(vfnptr_iter->m_Ptr), SH_PTRSIZE))
1.265 + {
1.266 + *reinterpret_cast<void**>(vfnptr_iter->m_Ptr) = vfnptr_iter->m_OrigEntry;
1.267 + }
1.268 +
1.269 + hookman->m_VfnPtrs.erase(vfnptr_iter);
1.270 +
1.271 + // Remove callclass patch
1.272 + for (Impl_CallClassList::iterator cciter = m_CallClasses.begin(); cciter != m_CallClasses.end(); ++cciter)
1.273 + if (cciter->m_Ptr == hentry->adjustediface)
1.274 + cciter->RemoveCallClassPatch(hentry->vtbl_offs, hentry->vtbl_idx);
1.275 +
1.276 + if (hookman->m_VfnPtrs.empty())
1.277 + {
1.278 + // Unregister the hook manager
1.279 + hookman->m_Func(HA_Unregister, NULL);
1.280 + }
1.281 + }
1.282 + }
1.283 +
1.284 + // Don't forget to remove the hookid from m_HookIDMan
1.285 + m_HookIDMan.Remove(hookid);
1.286 +
1.287 return true;
1.288 }
1.289
1.290 @@ -384,113 +570,32 @@
1.291
1.292 bool CSourceHookImpl::RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
1.293 {
1.294 - void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface)+thisptr_offs);
1.295 + // Get info about hook manager and compute adjustediface
1.296 CHookManagerInfo tmp;
1.297 if (myHookMan(HA_GetInfo, &tmp) != 0)
1.298 return false;
1.299
1.300 - // Find the hook manager and the hook
1.301 -
1.302 - HookManContList::iterator hmcl_iter = m_HookMans.find(
1.303 - CHookManagerContainer::HMCI(tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx));
1.304 - if (hmcl_iter == m_HookMans.end() || hmcl_iter->empty())
1.305 - return false;
1.306 -
1.307 - CHookManagerContainer::iterator hookman = hmcl_iter->begin();
1.308 + void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface)+thisptr_offs);
1.309
1.310 - if (!ModuleInMemory(reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs,
1.311 - sizeof(void*) * (tmp.m_VtblIdx + 1)))
1.312 - {
1.313 - // The module the vtable was in is already unloaded.
1.314 - hookman->m_VfnPtrs.clear();
1.315 - hookman->m_Func(HA_Unregister, NULL);
1.316 - return true;
1.317 - }
1.318 + // Loop through all hooks and remove those which match:
1.319 + // hookman, vfnptr, iface, plug, adjusted iface, this ptr offs, handler, post
1.320 + CVector<int> removehooks;
1.321 + m_HookIDMan.FindAllHooks(removehooks, tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx, adjustediface, plug, thisptr_offs, handler, post);
1.322
1.323 - void **cur_vtptr = *reinterpret_cast<void***>(
1.324 - reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs);
1.325 - void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.m_VtblIdx);
1.326 -
1.327 - CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->m_VfnPtrs.find(cur_vfnptr);
1.328 -
1.329 - if (vfnptr_iter == hookman->m_VfnPtrs.end())
1.330 + if (removehooks.empty())
1.331 return false;
1.332
1.333 - for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
1.334 - iface_iter != vfnptr_iter->m_Ifaces.end();)
1.335 - {
1.336 - if (iface_iter->m_Ptr != adjustediface)
1.337 - {
1.338 - iface_iter++;
1.339 - continue;
1.340 - }
1.341 - List<HookInfo> &hooks =
1.342 - post ? iface_iter->m_PostHooks.m_List : iface_iter->m_PreHooks.m_List;
1.343 -
1.344 - bool erase;
1.345 - for (List<HookInfo>::iterator hookiter = hooks.begin();
1.346 - hookiter != hooks.end(); )
1.347 - {
1.348 - erase = hookiter->plug == plug && hookiter->handler->IsEqual(handler) &&
1.349 - hookiter->thisptr_offs == thisptr_offs;
1.350 - if (erase)
1.351 - {
1.352 - hookiter->handler->DeleteThis(); // Make the _plugin_ delete the handler object
1.353 + bool status = false;
1.354
1.355 - // Move all iterators pointing at this
1.356 - List<HookInfo>::iterator oldhookiter = hookiter;
1.357 - hookiter = hooks.erase(hookiter);
1.358 - List<HookInfo>::iterator newhookiter = hookiter;
1.359 - --newhookiter; // The hook loop will ++ it then
1.360 - CHookList::CIter *pItIter;
1.361 - for (pItIter = iface_iter->m_PreHooks.m_UsedIters; pItIter; pItIter = pItIter->m_pNext)
1.362 - if (pItIter->m_Iter == oldhookiter)
1.363 - pItIter->m_Iter = newhookiter;
1.364 - }
1.365 - else
1.366 - ++hookiter;
1.367 - }
1.368 - if (iface_iter->m_PostHooks.m_List.empty() && iface_iter->m_PreHooks.m_List.empty())
1.369 - {
1.370 - // There are no hooks on this iface anymore...
1.371 - for (HookLoopInfoStack::iterator hli_iter = m_HLIStack.begin();
1.372 - hli_iter != m_HLIStack.end(); ++hli_iter)
1.373 - {
1.374 - if (hli_iter->pCurIface == static_cast<IIface*>(&(*iface_iter)))
1.375 - hli_iter->shouldContinue = false;
1.376 - }
1.377 -
1.378 - iface_iter = vfnptr_iter->m_Ifaces.erase(iface_iter);
1.379 - if (vfnptr_iter->m_Ifaces.empty())
1.380 - {
1.381 - // No ifaces at all -> Deactivate the hook
1.382 - *reinterpret_cast<void**>(vfnptr_iter->m_Ptr) = vfnptr_iter->m_OrigEntry;
1.383 -
1.384 - hookman->m_VfnPtrs.erase(vfnptr_iter);
1.385 -
1.386 - // Remove callclass patch
1.387 - for (Impl_CallClassList::iterator cciter = m_CallClasses.begin(); cciter != m_CallClasses.end(); ++cciter)
1.388 - if (cciter->m_Ptr == adjustediface)
1.389 - cciter->RemoveCallClassPatch(tmp.m_VtblOffs, tmp.m_VtblIdx);
1.390 -
1.391 - if (hookman->m_VfnPtrs.empty())
1.392 - {
1.393 - // Unregister the hook manager
1.394 - hookman->m_Func(HA_Unregister, NULL);
1.395 - }
1.396 -
1.397 - // Don't try to continue looping through ifaces
1.398 - // - the list is already invalid
1.399 - return true;
1.400 - }
1.401 - }
1.402 - else
1.403 - ++iface_iter;
1.404 + for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
1.405 + {
1.406 + if (RemoveHookByID(plug, *iter))
1.407 + status = true;
1.408 }
1.409 - return true;
1.410 + return status;
1.411 }
1.412
1.413 - GenericCallClass *CSourceHookImpl::GetCallClass(void *iface, size_t size)
1.414 + DeprecatedCallClass<void> *CSourceHookImpl::GetCallClass(void *iface, size_t size)
1.415 {
1.416 for (Impl_CallClassList::iterator cciter = m_CallClasses.begin(); cciter != m_CallClasses.end(); ++cciter)
1.417 {
1.418 @@ -509,7 +614,7 @@
1.419 return &m_CallClasses.back();
1.420 }
1.421
1.422 - void CSourceHookImpl::ReleaseCallClass(GenericCallClass *ptr)
1.423 + void CSourceHookImpl::ReleaseCallClass(DeprecatedCallClass<void> *ptr)
1.424 {
1.425 Impl_CallClassList::iterator iter = m_CallClasses.find(ptr);
1.426 if (iter == m_CallClasses.end())
1.427 @@ -614,7 +719,7 @@
1.428
1.429 void CSourceHookImpl::HookLoopBegin(IIface *pIface)
1.430 {
1.431 - HookLoopInfo hli;
1.432 + HookLoopInfo hli = {0};
1.433 hli.pCurIface = pIface;
1.434 hli.shouldContinue = true;
1.435 hli.recall = HookLoopInfo::Recall_No;
1.436 @@ -752,6 +857,11 @@
1.437
1.438 bool CSourceHookImpl::ShouldContinue()
1.439 {
1.440 + // If recall is true, we shall not continue either.
1.441 + // This is because, if it's true and ShouldContinue is called, it suggests that the
1.442 + // actual recall is done and that we are back in the original handler which shall return
1.443 + // immediately.
1.444 +
1.445 // Post-recalls:
1.446 // The second element on the stack has recall set to Recall_Post1.
1.447 // This means that we want to skip this part and the original function calling thing, so
1.448 @@ -773,21 +883,7 @@
1.449 return m_HLIStack.front().shouldContinue;
1.450 }
1.451 }
1.452 -
1.453 - // 16.01.2008: We used to return false for recalls here.
1.454 - // This caused the hook funcs to think that the whole iface has been destroyed
1.455 - // (the original purpose of shouldcontinue was to notify the hookfuncs that everything has been
1.456 - // invalidated by RemoveHook) so they did not release their iterator. -> Leaks
1.457 - // Now, GetIter sets the iterator to END so it still gets released but the hooks don't continue.
1.458 -
1.459 - // But: we have to return false in the post phase of pre recalls (ie. a pre hook made a recall).
1.460 - if (m_HLIStack.front().recall == HookLoopInfo::Recall_Pre &&
1.461 - static_cast<CIface*>(m_HLIStack.front().pCurIface)->m_PreHooks.RelFlagGet())
1.462 - {
1.463 - return false;
1.464 - }
1.465 -
1.466 - return m_HLIStack.front().shouldContinue;
1.467 + return m_HLIStack.front().shouldContinue && !m_HLIStack.front().recall;
1.468 }
1.469
1.470 void CSourceHookImpl::DoRecall()
1.471 @@ -811,6 +907,35 @@
1.472 }
1.473 }
1.474
1.475 + void CSourceHookImpl::SetIgnoreHooks(Plugin plug, void *vfnptr)
1.476 + {
1.477 + m_OneIgnore = vfnptr;
1.478 + }
1.479 +
1.480 + void CSourceHookImpl::ResetIgnoreHooks(Plugin plug, void *vfnptr)
1.481 + {
1.482 + m_OneIgnore = NULL;
1.483 + }
1.484 +
1.485 + void *CSourceHookImpl::GetOrigVfnPtrEntry(void *vfnptr)
1.486 + {
1.487 + for (HookManContList::iterator hmcl_iter = m_HookMans.begin();
1.488 + hmcl_iter != m_HookMans.end(); ++hmcl_iter)
1.489 + {
1.490 + for (CHookManagerContainer::iterator hookmaniter = hmcl_iter->begin();
1.491 + hookmaniter != hmcl_iter->end(); ++hookmaniter)
1.492 + {
1.493 + for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookmaniter->m_VfnPtrs.begin();
1.494 + vfnptr_iter != hookmaniter->m_VfnPtrs.end(); ++vfnptr_iter)
1.495 + {
1.496 + if (vfnptr_iter->m_Ptr == vfnptr)
1.497 + return vfnptr_iter->m_OrigEntry;
1.498 + }
1.499 + }
1.500 + }
1.501 + return NULL;
1.502 + }
1.503 +
1.504 ////////////////////////////
1.505 // CCallClassImpl
1.506 ////////////////////////////
1.507 @@ -906,7 +1031,8 @@
1.508
1.509 // If you get a crash here, the ptr passed is invalid
1.510 // This usually means a SH_DECL_MANUALHOOK* with wrong thisptroffs/vtbloffs/vtblidx
1.511 - CSourceHookImpl::CVfnPtr::CVfnPtr(void *ptr) : m_Ptr(ptr), m_OrigEntry(*reinterpret_cast<void**>(ptr))
1.512 + CSourceHookImpl::CVfnPtr::CVfnPtr(void *ptr, void **pOneIgnore) : m_Ptr(ptr),
1.513 + m_OrigEntry(*reinterpret_cast<void**>(ptr)), m_pOneIgnore(pOneIgnore)
1.514 {
1.515 }
1.516 CSourceHookImpl::CVfnPtr::~CVfnPtr()
1.517 @@ -925,8 +1051,24 @@
1.518
1.519 IIface *CSourceHookImpl::CVfnPtr::FindIface(void *ptr)
1.520 {
1.521 + if (m_Ptr == *m_pOneIgnore)
1.522 + {
1.523 + *m_pOneIgnore = NULL; // Only once!
1.524 + return NULL;
1.525 + }
1.526 +
1.527 IfaceListIter iter = m_Ifaces.find(ptr);
1.528 - return iter == m_Ifaces.end() ? NULL : &(*iter);
1.529 +
1.530 + // If nothing is found, check for a NULL-interface (VP hooks only)
1.531 + if (iter == m_Ifaces.end())
1.532 + {
1.533 + iter = m_Ifaces.find((void*)0);
1.534 + return iter == m_Ifaces.end() ? NULL : &(*iter);
1.535 + }
1.536 + else
1.537 + {
1.538 + return &(*iter);
1.539 + }
1.540 }
1.541
1.542 ////////////////////////////
1.543 @@ -957,11 +1099,11 @@
1.544 // CHookList
1.545 ////////////////////////////
1.546
1.547 - CSourceHookImpl::CHookList::CHookList() : m_FreeIters(NULL), m_UsedIters(NULL),
1.548 + CSourceHookImpl::CHookList::CHookList() : m_VPList(NULL), m_FreeIters(NULL), m_UsedIters(NULL),
1.549 m_Recall(false)
1.550 {
1.551 }
1.552 - CSourceHookImpl::CHookList::CHookList(const CHookList &other) : m_List(other.m_List),
1.553 + CSourceHookImpl::CHookList::CHookList(const CHookList &other) : m_VPList(other.m_VPList), m_List(other.m_List),
1.554 m_FreeIters(NULL), m_UsedIters(NULL), m_Recall(false)
1.555 {
1.556 }
1.557 @@ -1003,9 +1145,6 @@
1.558 ret->Set(m_UsedIters); // m_UsedIters is the last returned and not released iterator
1.559 ret->Next(); // Use next instead of directly incrementing its m_Iter:
1.560 // skips paused plugins
1.561 -
1.562 - // Set the last iterator to END!
1.563 - m_UsedIters->GoToEnd();
1.564 }
1.565
1.566 ret->m_pNext = m_UsedIters;
1.567 @@ -1020,8 +1159,6 @@
1.568 }
1.569 void CSourceHookImpl::CHookList::ReleaseIter(IIter *pIter)
1.570 {
1.571 - m_RelFlag = true;
1.572 -
1.573 CIter *pIter2 = static_cast<CIter*>(pIter);
1.574
1.575 // Unlink from m_UsedIters
1.576 @@ -1031,7 +1168,7 @@
1.577 if (pIter2->m_pPrev)
1.578 pIter2->m_pPrev->m_pNext = pIter2->m_pNext;
1.579 if (pIter2 == m_UsedIters)
1.580 - m_UsedIters = m_UsedIters->m_pNext;
1.581 + m_UsedIters = NULL;
1.582
1.583 // Link to m_FreeIters
1.584
1.585 @@ -1043,9 +1180,36 @@
1.586 m_Recall = false;
1.587 }
1.588
1.589 - CSourceHookImpl::CHookList::CIter::CIter(CHookList *pList) : m_pList(pList), m_pNext(NULL)
1.590 + void CSourceHookImpl::CHookList::SetVPList(List<HookInfo> *newList)
1.591 {
1.592 - GoToBegin();
1.593 + m_VPList = newList;
1.594 +
1.595 + // Update cached CIter objects
1.596 +
1.597 + CIter *pTmp;
1.598 + pTmp = m_FreeIters;
1.599 + while (pTmp)
1.600 + {
1.601 + pTmp->m_Iter.SetListLeft(m_VPList);
1.602 + pTmp = pTmp->m_pNext;
1.603 + }
1.604 +
1.605 + pTmp = m_UsedIters;
1.606 + while (pTmp)
1.607 + {
1.608 + pTmp->m_Iter.SetListLeft(m_VPList);
1.609 + pTmp = pTmp->m_pNext;
1.610 + }
1.611 + }
1.612 +
1.613 + void CSourceHookImpl::CHookList::ClearVPList()
1.614 + {
1.615 + SetVPList(NULL);
1.616 + }
1.617 +
1.618 + CSourceHookImpl::CHookList::CIter::CIter(CHookList *pList) : m_pList(pList),
1.619 + m_Iter(pList->m_VPList, &m_pList->m_List), m_pNext(NULL)
1.620 + {
1.621 }
1.622 CSourceHookImpl::CHookList::CIter::~CIter()
1.623 {
1.624 @@ -1058,24 +1222,19 @@
1.625
1.626 void CSourceHookImpl::CHookList::CIter::GoToBegin()
1.627 {
1.628 - m_Iter = m_pList->m_List.begin();
1.629 + m_Iter.GoToBegin();
1.630 SkipPaused();
1.631 }
1.632
1.633 - void CSourceHookImpl::CHookList::CIter::GoToEnd()
1.634 - {
1.635 - m_Iter = m_pList->m_List.end();
1.636 - }
1.637 -
1.638 bool CSourceHookImpl::CHookList::CIter::End()
1.639 {
1.640 if (!m_pList)
1.641 return false;
1.642 - return m_Iter == m_pList->m_List.end();
1.643 + return m_Iter.End();;
1.644 }
1.645 void CSourceHookImpl::CHookList::CIter::Next()
1.646 {
1.647 - if (!m_pList || m_Iter == m_pList->m_List.end())
1.648 + if (!m_pList)
1.649 return;
1.650 ++m_Iter;
1.651 SkipPaused();
1.652 @@ -1086,7 +1245,7 @@
1.653 }
1.654 void CSourceHookImpl::CHookList::CIter::SkipPaused()
1.655 {
1.656 - while (m_Iter != m_pList->m_List.end() && m_Iter->paused)
1.657 + while (!m_Iter.End() && m_Iter->paused)
1.658 ++m_Iter;
1.659 }
1.660
1.661 @@ -1104,6 +1263,9 @@
1.662 //////////////////////////////////////////////////////////////////////////
1.663 char *CSourceHookImpl::CProto::DupProto(const char *p)
1.664 {
1.665 + if (!p)
1.666 + return NULL;
1.667 +
1.668 char *newproto;
1.669 if (*p)
1.670 {
1.671 @@ -1124,6 +1286,9 @@
1.672
1.673 void CSourceHookImpl::CProto::FreeProto(char *prot)
1.674 {
1.675 + if (!prot)
1.676 + return;
1.677 +
1.678 if (*prot)
1.679 {
1.680 delete [] prot;
1.681 @@ -1136,6 +1301,9 @@
1.682
1.683 bool CSourceHookImpl::CProto::Equal(const char *p1, const char *p2)
1.684 {
1.685 + if (!p1 || !p2)
1.686 + return false;
1.687 +
1.688 if (*p1 && *p2) // Case1: Both old
1.689 {
1.690 // As in old versions
1.691 @@ -1228,4 +1396,68 @@
1.692 }
1.693 }
1.694 }
1.695 +
1.696 + //////////////////////////////////////////////////////////////////////////
1.697 + // CHookIDManager
1.698 + //////////////////////////////////////////////////////////////////////////
1.699 + CSourceHookImpl::CHookIDManager::CHookIDManager()
1.700 + {
1.701 + }
1.702 +
1.703 + int CSourceHookImpl::CHookIDManager::New(const CProto &proto, int vtbl_offs, int vtbl_idx, void *vfnptr,
1.704 + void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
1.705 + {
1.706 + Entry tmp(proto, vtbl_offs, vtbl_idx, vfnptr, adjustediface, plug, thisptr_offs, handler, post);
1.707 +
1.708 + size_t cursize = m_Entries.size();
1.709 + for (size_t i = 0; i < cursize; ++i)
1.710 + {
1.711 + if (m_Entries[i].isfree)
1.712 + {
1.713 + m_Entries[i] = tmp;
1.714 + return static_cast<int>(i) + 1;
1.715 + }
1.716 + }
1.717 +
1.718 + m_Entries.push_back(tmp);
1.719 + return static_cast<int>(m_Entries.size()); // return size() because hookid = id+1 anyway
1.720 + }
1.721 +
1.722 + bool CSourceHookImpl::CHookIDManager::Remove(int hookid)
1.723 + {
1.724 + int realid = hookid - 1;
1.725 + if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
1.726 + return false;
1.727 +
1.728 + m_Entries[realid].isfree = true;
1.729 +
1.730 + // :TODO: remove free ids from back sometimes ??
1.731 +
1.732 + return true;
1.733 + }
1.734 +
1.735 + const CSourceHookImpl::CHookIDManager::Entry * CSourceHookImpl::CHookIDManager::QueryHook(int hookid)
1.736 + {
1.737 + int realid = hookid - 1;
1.738 + if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
1.739 + return NULL;
1.740 +
1.741 + return &m_Entries[realid];
1.742 + }
1.743 +
1.744 + void CSourceHookImpl::CHookIDManager::FindAllHooks(CVector<int> &output, const CProto &proto, int vtbl_offs,
1.745 + int vtbl_idx, void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
1.746 + {
1.747 + // oh my god, a lot of parameters...
1.748 + size_t cursize = m_Entries.size();
1.749 + for (size_t i = 0; i < cursize; ++i)
1.750 + {
1.751 + if (!m_Entries[i].isfree && m_Entries[i].proto == proto && m_Entries[i].vtbl_offs == vtbl_offs &&
1.752 + m_Entries[i].vtbl_idx == vtbl_idx && m_Entries[i].adjustediface == adjustediface && m_Entries[i].plug == plug &&
1.753 + m_Entries[i].thisptr_offs == thisptr_offs && m_Entries[i].handler->IsEqual(handler) && m_Entries[i].post == post)
1.754 + {
1.755 + output.push_back(static_cast<int>(i) + 1);
1.756 + }
1.757 + }
1.758 + }
1.759 }