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