sourcehook/sourcehook.cpp
changeset 324 257f0837a885
parent 294 0d87bc8606f8
child 328 3a04ef79edf1
     1.1 --- a/sourcehook/sourcehook.cpp	Mon May 07 18:33:34 2007 +0000
     1.2 +++ b/sourcehook/sourcehook.cpp	Mon May 07 18:35:59 2007 +0000
     1.3 @@ -277,14 +277,21 @@
     1.4  		m_HookMans.clear();
     1.5  	}
     1.6  
     1.7 +
     1.8  	bool CSourceHookImpl::AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
     1.9  	{
    1.10 +		return AddHookNew(plug, Hook_Normal, iface, thisptr_offs, myHookMan, handler, post) != 0 ? true : false;
    1.11 +	}
    1.12 +
    1.13 +	int CSourceHookImpl::AddHookNew(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
    1.14 +		ISHDelegate *handler, bool post)
    1.15 +	{
    1.16  		void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
    1.17  
    1.18  		// 1) Get info about the hook manager
    1.19  		CHookManagerInfo tmp;
    1.20  		if (myHookMan(HA_GetInfo, &tmp) != 0)
    1.21 -			return false;
    1.22 +			return 0;
    1.23  
    1.24  		tmp.m_Func = myHookMan;
    1.25  		tmp.m_Plug = plug;
    1.26 @@ -320,7 +327,7 @@
    1.27  					hmil_iter != hmcl_iter2->end(); ++hmil_iter)
    1.28  				{
    1.29  					if (hmil_iter->m_VfnPtrs.find(cur_vfnptr) != hmil_iter->m_VfnPtrs.end())
    1.30 -						return false;
    1.31 +						return 0;
    1.32  				}
    1.33  			}
    1.34  		}
    1.35 @@ -338,7 +345,7 @@
    1.36  
    1.37  			// Alter vtable entry
    1.38  			if (!SetMemAccess(cur_vtptr, sizeof(void*) * (tmp.m_VtblIdx + 1), SH_MEM_READ | SH_MEM_WRITE))
    1.39 -				return false;
    1.40 +				return 0;
    1.41  
    1.42  			*reinterpret_cast<void**>(cur_vfnptr) = *reinterpret_cast<void**>(hookman->m_HookfuncVfnptr);
    1.43  
    1.44 @@ -352,6 +359,10 @@
    1.45  			ApplyCallClassPatches(adjustediface, tmp.m_VtblOffs, tmp.m_VtblIdx, vfp.m_OrigEntry);
    1.46  		}
    1.47  
    1.48 +		// If it's a VP hook, set adjustediface to NULL now ( see the comments at the beginning of sourcehook_impl.h )
    1.49 +		if (mode == Hook_VP)
    1.50 +			adjustediface = NULL;
    1.51 +
    1.52  		CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.find(adjustediface);
    1.53  		if (iface_iter == vfnptr_iter->m_Ifaces.end())
    1.54  		{
    1.55 @@ -361,6 +372,34 @@
    1.56  			// Make iface_iter point to the new element
    1.57  			iface_iter = vfnptr_iter->m_Ifaces.end();
    1.58  			--iface_iter;
    1.59 +
    1.60 +			// If this is a VP-Hook-NULL interface, go through all other interfaces of this vfnptr and tell them!
    1.61 +			if (adjustediface == NULL)
    1.62 +			{
    1.63 +				for (CVfnPtr::IfaceListIter iface_iter2 = vfnptr_iter->m_Ifaces.begin();
    1.64 +					iface_iter2 != vfnptr_iter->m_Ifaces.end(); ++iface_iter2)
    1.65 +				{
    1.66 +					if (*iface_iter2 != NULL)
    1.67 +					{
    1.68 +						iface_iter2->m_PreHooks.SetVPList(&iface_iter->m_PreHooks.m_List);
    1.69 +						iface_iter2->m_PostHooks.SetVPList(&iface_iter->m_PostHooks.m_List);
    1.70 +					}
    1.71 +				}
    1.72 +			}
    1.73 +			else
    1.74 +			{
    1.75 +				// If this is a new non-VP-Hook-NULL interface, look for a non-VP-Hook-NULL interface!
    1.76 +				for (CVfnPtr::IfaceListIter iface_iter2 = vfnptr_iter->m_Ifaces.begin();
    1.77 +					iface_iter2 != vfnptr_iter->m_Ifaces.end(); ++iface_iter2)
    1.78 +				{
    1.79 +					if (*iface_iter2 == NULL)
    1.80 +					{
    1.81 +						iface_iter->m_PreHooks.SetVPList(&iface_iter2->m_PreHooks.m_List);
    1.82 +						iface_iter->m_PostHooks.SetVPList(&iface_iter2->m_PostHooks.m_List);
    1.83 +						break;	// There can only be one!
    1.84 +					}
    1.85 +				}
    1.86 +			}
    1.87  		}
    1.88  
    1.89  		// Add the hook
    1.90 @@ -369,11 +408,136 @@
    1.91  		hookinfo.plug = plug;
    1.92  		hookinfo.paused = false;
    1.93  		hookinfo.thisptr_offs = thisptr_offs;
    1.94 +		hookinfo.hookid = m_HookIDMan.New(tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx, cur_vfnptr,
    1.95 +			adjustediface, plug, thisptr_offs, handler, post);
    1.96 +
    1.97  		if (post)
    1.98  			iface_iter->m_PostHooks.m_List.push_back(hookinfo);
    1.99  		else
   1.100  			iface_iter->m_PreHooks.m_List.push_back(hookinfo);
   1.101  
   1.102 +		return hookinfo.hookid;
   1.103 +	}
   1.104 +
   1.105 +	bool CSourceHookImpl::RemoveHookByID(Plugin plug, int hookid)
   1.106 +	{
   1.107 +		const CHookIDManager::Entry *hentry;
   1.108 +
   1.109 +		hentry = m_HookIDMan.QueryHook(hookid);
   1.110 +		if (!hentry)
   1.111 +		{
   1.112 +			// hookid doesn't exist !
   1.113 +			return false;
   1.114 +		}
   1.115 +
   1.116 +		HookManContList::iterator hmcl_iter = m_HookMans.find(
   1.117 +			CHookManagerContainer::HMCI(hentry->proto.GetProto(), hentry->vtbl_offs, hentry->vtbl_idx));
   1.118 +		if (hmcl_iter == m_HookMans.end() || hmcl_iter->empty())
   1.119 +			return false;
   1.120 +		CHookManagerContainer::iterator hookman = hmcl_iter->begin();
   1.121 +
   1.122 +		// Find vfnptr
   1.123 +		CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->m_VfnPtrs.find(hentry->vfnptr);
   1.124 +		if (vfnptr_iter == hookman->m_VfnPtrs.end())
   1.125 +			return false;
   1.126 +
   1.127 +		// Find iface
   1.128 +		CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.find(hentry->adjustediface);
   1.129 +		if (iface_iter == vfnptr_iter->m_Ifaces.end())
   1.130 +			return false;
   1.131 +
   1.132 +		// Find hook
   1.133 +		List<HookInfo> &hooks = hentry->post ? iface_iter->m_PostHooks.m_List : iface_iter->m_PreHooks.m_List;
   1.134 +
   1.135 +		List<HookInfo>::iterator hookiter = hooks.find(hookid);
   1.136 +		if (hookiter == hooks.end())
   1.137 +			return false;
   1.138 +
   1.139 +		hookiter->handler->DeleteThis();
   1.140 +
   1.141 +		// Move all iterators pointing at this
   1.142 +		List<HookInfo>::iterator oldhookiter = hookiter;
   1.143 +		hookiter = hooks.erase(hookiter);
   1.144 +		List<HookInfo>::iterator newhookiter = hookiter;
   1.145 +		--newhookiter; // The hook loop will ++ it then
   1.146 +		CHookList::CIter *pItIter;
   1.147 +		
   1.148 +		for (pItIter = iface_iter->m_PreHooks.m_UsedIters; pItIter; pItIter = pItIter->m_pNext)
   1.149 +			if (pItIter->m_Iter == oldhookiter)
   1.150 +				pItIter->m_Iter = newhookiter;
   1.151 +
   1.152 +		// If this is VP-Hook-NULL interface, also check all other interfaces of this vfnptr
   1.153 +		if (*iface_iter == NULL)
   1.154 +		{
   1.155 +			for (CVfnPtr::IfaceListIter iface_iter2 = vfnptr_iter->m_Ifaces.begin();
   1.156 +				iface_iter2 != vfnptr_iter->m_Ifaces.end(); ++iface_iter2)
   1.157 +			{
   1.158 +				if (*iface_iter2 != NULL)
   1.159 +				{
   1.160 +					for (pItIter = iface_iter2->m_PreHooks.m_UsedIters; pItIter; pItIter = pItIter->m_pNext)
   1.161 +						if (pItIter->m_Iter == oldhookiter)
   1.162 +							pItIter->m_Iter = newhookiter;
   1.163 +				}
   1.164 +			}
   1.165 +		}
   1.166 +
   1.167 +		if (iface_iter->m_PostHooks.m_List.empty() && iface_iter->m_PreHooks.m_List.empty())
   1.168 +		{
   1.169 +			// If this is a VP-Hook-NULL interface, go through all other interfaces of this vfnptr and tell them!
   1.170 +			if (*iface_iter == NULL)
   1.171 +			{
   1.172 +				for (CVfnPtr::IfaceListIter iface_iter2 = vfnptr_iter->m_Ifaces.begin();
   1.173 +					iface_iter2 != vfnptr_iter->m_Ifaces.end(); ++iface_iter2)
   1.174 +				{
   1.175 +					if (iface_iter2->m_Ptr != NULL)
   1.176 +					{
   1.177 +						iface_iter2->m_PreHooks.ClearVPList();
   1.178 +						iface_iter2->m_PostHooks.ClearVPList();
   1.179 +					}
   1.180 +				}
   1.181 +			}
   1.182 +
   1.183 +
   1.184 +			// There are no hooks on this iface anymore...
   1.185 +			for (HookLoopInfoStack::iterator hli_iter = m_HLIStack.begin();
   1.186 +				hli_iter != m_HLIStack.end(); ++hli_iter)
   1.187 +			{
   1.188 +				if (hli_iter->pCurIface == static_cast<IIface*>(&(*iface_iter)))
   1.189 +					hli_iter->shouldContinue = false;
   1.190 +			}
   1.191 +
   1.192 +			iface_iter = vfnptr_iter->m_Ifaces.erase(iface_iter);
   1.193 +			if (vfnptr_iter->m_Ifaces.empty())
   1.194 +			{
   1.195 +				// No ifaces at all -> Deactivate the hook
   1.196 +
   1.197 +				// Only patch the vfnptr back if the module is still in memory
   1.198 +				// If it's not, do not remove stuff like we did before
   1.199 +				// First off we did it wrong (shutdown the whole hookman, uh..) and secondly applications may be
   1.200 +				// confused by RemoveHook returning false then (yeah, I know, I made this one up, no one checks for RemoveHook error)
   1.201 +				if (ModuleInMemory(reinterpret_cast<char*>(vfnptr_iter->m_Ptr), SH_PTRSIZE))
   1.202 +				{
   1.203 +					*reinterpret_cast<void**>(vfnptr_iter->m_Ptr) = vfnptr_iter->m_OrigEntry;
   1.204 +				}
   1.205 +
   1.206 +				hookman->m_VfnPtrs.erase(vfnptr_iter);
   1.207 +
   1.208 +				// Remove callclass patch
   1.209 +				for (Impl_CallClassList::iterator cciter = m_CallClasses.begin(); cciter != m_CallClasses.end(); ++cciter)
   1.210 +					if (cciter->m_Ptr == hentry->adjustediface)
   1.211 +						cciter->RemoveCallClassPatch(hentry->vtbl_offs, hentry->vtbl_idx);
   1.212 +
   1.213 +				if (hookman->m_VfnPtrs.empty())
   1.214 +				{
   1.215 +					// Unregister the hook manager
   1.216 +					hookman->m_Func(HA_Unregister, NULL);
   1.217 +				}
   1.218 +			}
   1.219 +		}
   1.220 +
   1.221 +		// Don't forget to remove the hookid from m_HookIDMan
   1.222 +		m_HookIDMan.Remove(hookid);
   1.223 +
   1.224  		return true;
   1.225  	}
   1.226  
   1.227 @@ -384,110 +548,29 @@
   1.228  
   1.229  	bool CSourceHookImpl::RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
   1.230  	{
   1.231 -		void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface)+thisptr_offs);
   1.232 +		// Get info about hook manager and compute adjustediface
   1.233  		CHookManagerInfo tmp;
   1.234  		if (myHookMan(HA_GetInfo, &tmp) != 0)
   1.235  			return false;
   1.236  
   1.237 -		// Find the hook manager and the hook
   1.238 -
   1.239 -		HookManContList::iterator hmcl_iter = m_HookMans.find(
   1.240 -			CHookManagerContainer::HMCI(tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx));
   1.241 -		if (hmcl_iter == m_HookMans.end() || hmcl_iter->empty())
   1.242 -			return false;
   1.243 -		
   1.244 -		CHookManagerContainer::iterator hookman = hmcl_iter->begin();
   1.245 +		void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface)+thisptr_offs);
   1.246  
   1.247 -		if (!ModuleInMemory(reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs,
   1.248 -			sizeof(void*) * (tmp.m_VtblIdx + 1)))
   1.249 -		{
   1.250 -			// The module the vtable was in is already unloaded.
   1.251 -			hookman->m_VfnPtrs.clear();
   1.252 -			hookman->m_Func(HA_Unregister, NULL);
   1.253 -			return true;
   1.254 -		}
   1.255 +		// Loop through all hooks and remove those which match:
   1.256 +		//  hookman, vfnptr, iface, plug, adjusted iface, this ptr offs, handler, post
   1.257 +		CVector<int> removehooks;
   1.258 +		m_HookIDMan.FindAllHooks(removehooks, tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx, adjustediface, plug, thisptr_offs, handler, post);
   1.259  
   1.260 -		void **cur_vtptr = *reinterpret_cast<void***>(
   1.261 -			reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs);
   1.262 -		void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.m_VtblIdx);
   1.263 -
   1.264 -		CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->m_VfnPtrs.find(cur_vfnptr);
   1.265 -
   1.266 -		if (vfnptr_iter == hookman->m_VfnPtrs.end())
   1.267 +		if (removehooks.empty())
   1.268  			return false;
   1.269  
   1.270 -		for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
   1.271 -			iface_iter != vfnptr_iter->m_Ifaces.end();)
   1.272 -		{
   1.273 -			if (iface_iter->m_Ptr != adjustediface)
   1.274 -			{
   1.275 -				iface_iter++;
   1.276 -				continue;
   1.277 -			}
   1.278 -			List<HookInfo> &hooks =
   1.279 -				post ? iface_iter->m_PostHooks.m_List : iface_iter->m_PreHooks.m_List;
   1.280 -
   1.281 -			bool erase;
   1.282 -			for (List<HookInfo>::iterator hookiter = hooks.begin();
   1.283 -				hookiter != hooks.end(); )
   1.284 -			{
   1.285 -				erase = hookiter->plug == plug && hookiter->handler->IsEqual(handler) &&
   1.286 -					hookiter->thisptr_offs == thisptr_offs;
   1.287 -				if (erase)
   1.288 -				{
   1.289 -					hookiter->handler->DeleteThis();			// Make the _plugin_ delete the handler object
   1.290 +		bool status = false;
   1.291  
   1.292 -					// Move all iterators pointing at this
   1.293 -					List<HookInfo>::iterator oldhookiter = hookiter;
   1.294 -					hookiter = hooks.erase(hookiter);
   1.295 -					List<HookInfo>::iterator newhookiter = hookiter;
   1.296 -					--newhookiter; // The hook loop will ++ it then
   1.297 -					CHookList::CIter *pItIter;
   1.298 -					for (pItIter = iface_iter->m_PreHooks.m_UsedIters; pItIter; pItIter = pItIter->m_pNext)
   1.299 -						if (pItIter->m_Iter == oldhookiter)
   1.300 -							pItIter->m_Iter = newhookiter;
   1.301 -				}
   1.302 -				else
   1.303 -					++hookiter;
   1.304 -			}
   1.305 -			if (iface_iter->m_PostHooks.m_List.empty() && iface_iter->m_PreHooks.m_List.empty())
   1.306 -			{
   1.307 -				// There are no hooks on this iface anymore...
   1.308 -				for (HookLoopInfoStack::iterator hli_iter = m_HLIStack.begin();
   1.309 -					hli_iter != m_HLIStack.end(); ++hli_iter)
   1.310 -				{
   1.311 -					if (hli_iter->pCurIface == static_cast<IIface*>(&(*iface_iter)))
   1.312 -						hli_iter->shouldContinue = false;
   1.313 -				}
   1.314 -
   1.315 -				iface_iter = vfnptr_iter->m_Ifaces.erase(iface_iter);
   1.316 -				if (vfnptr_iter->m_Ifaces.empty())
   1.317 -				{
   1.318 -					// No ifaces at all -> Deactivate the hook
   1.319 -					*reinterpret_cast<void**>(vfnptr_iter->m_Ptr) = vfnptr_iter->m_OrigEntry;
   1.320 -
   1.321 -					hookman->m_VfnPtrs.erase(vfnptr_iter);
   1.322 -
   1.323 -					// Remove callclass patch
   1.324 -					for (Impl_CallClassList::iterator cciter = m_CallClasses.begin(); cciter != m_CallClasses.end(); ++cciter)
   1.325 -						if (cciter->m_Ptr == adjustediface)
   1.326 -							cciter->RemoveCallClassPatch(tmp.m_VtblOffs, tmp.m_VtblIdx);
   1.327 -
   1.328 -					if (hookman->m_VfnPtrs.empty())
   1.329 -					{
   1.330 -						// Unregister the hook manager
   1.331 -						hookman->m_Func(HA_Unregister, NULL);
   1.332 -					}
   1.333 -
   1.334 -					// Don't try to continue looping through ifaces
   1.335 -					// - the list is already invalid
   1.336 -					return true;
   1.337 -				}
   1.338 -			}
   1.339 -			else
   1.340 -				++iface_iter;
   1.341 +		for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
   1.342 +		{
   1.343 +			if (RemoveHookByID(plug, *iter))
   1.344 +				status = true;
   1.345  		}
   1.346 -		return true;
   1.347 +		return status; 
   1.348  	}
   1.349  
   1.350  	GenericCallClass *CSourceHookImpl::GetCallClass(void *iface, size_t size)
   1.351 @@ -614,7 +697,7 @@
   1.352  
   1.353  	void CSourceHookImpl::HookLoopBegin(IIface *pIface)
   1.354  	{
   1.355 -		HookLoopInfo hli;
   1.356 +		HookLoopInfo hli = {0};
   1.357  		hli.pCurIface = pIface;
   1.358  		hli.shouldContinue = true;
   1.359  		hli.recall = HookLoopInfo::Recall_No;
   1.360 @@ -917,7 +1000,17 @@
   1.361  	IIface *CSourceHookImpl::CVfnPtr::FindIface(void *ptr)
   1.362  	{
   1.363  		IfaceListIter iter = m_Ifaces.find(ptr);
   1.364 -		return iter == m_Ifaces.end() ? NULL : &(*iter);
   1.365 +		
   1.366 +		// If nothing is found, check for a NULL-interface (VP hooks only)
   1.367 +		if (iter == m_Ifaces.end())
   1.368 +		{
   1.369 +			iter = m_Ifaces.find((void*)0);
   1.370 +			return iter == m_Ifaces.end() ? NULL : &(*iter);
   1.371 +		}
   1.372 +		else
   1.373 +		{
   1.374 +			return &(*iter);
   1.375 +		}
   1.376  	}
   1.377  
   1.378  	////////////////////////////
   1.379 @@ -948,11 +1041,11 @@
   1.380  	// CHookList
   1.381  	////////////////////////////
   1.382  
   1.383 -	CSourceHookImpl::CHookList::CHookList() : m_FreeIters(NULL), m_UsedIters(NULL),
   1.384 +	CSourceHookImpl::CHookList::CHookList() : m_VPList(NULL), m_FreeIters(NULL), m_UsedIters(NULL),
   1.385  		m_Recall(false)
   1.386  	{
   1.387  	}
   1.388 -	CSourceHookImpl::CHookList::CHookList(const CHookList &other) : m_List(other.m_List),
   1.389 +	CSourceHookImpl::CHookList::CHookList(const CHookList &other) : m_VPList(other.m_VPList), m_List(other.m_List),
   1.390  		m_FreeIters(NULL), m_UsedIters(NULL), m_Recall(false)
   1.391  	{
   1.392  	}
   1.393 @@ -1029,9 +1122,36 @@
   1.394  		m_Recall = false;
   1.395  	}
   1.396  
   1.397 -	CSourceHookImpl::CHookList::CIter::CIter(CHookList *pList) : m_pList(pList), m_pNext(NULL)
   1.398 +	void CSourceHookImpl::CHookList::SetVPList(List<HookInfo> *newList)
   1.399  	{
   1.400 -		GoToBegin();
   1.401 +		m_VPList = newList;
   1.402 +
   1.403 +		// Update cached CIter objects
   1.404 +
   1.405 +		CIter *pTmp;
   1.406 +		pTmp = m_FreeIters;
   1.407 +		while (pTmp)
   1.408 +		{
   1.409 +			pTmp->m_Iter.SetListLeft(m_VPList);
   1.410 +			pTmp = pTmp->m_pNext;
   1.411 +		}
   1.412 +
   1.413 +		pTmp = m_UsedIters;
   1.414 +		while (pTmp)
   1.415 +		{
   1.416 +			pTmp->m_Iter.SetListLeft(m_VPList);
   1.417 +			pTmp = pTmp->m_pNext;
   1.418 +		}
   1.419 +	}
   1.420 +
   1.421 +	void CSourceHookImpl::CHookList::ClearVPList()
   1.422 +	{
   1.423 +		SetVPList(NULL);
   1.424 +	}
   1.425 +
   1.426 +	CSourceHookImpl::CHookList::CIter::CIter(CHookList *pList) : m_pList(pList), 
   1.427 +		m_Iter(pList->m_VPList, &m_pList->m_List), m_pNext(NULL)
   1.428 +	{
   1.429  	}
   1.430  	CSourceHookImpl::CHookList::CIter::~CIter()
   1.431  	{
   1.432 @@ -1044,7 +1164,7 @@
   1.433  
   1.434  	void CSourceHookImpl::CHookList::CIter::GoToBegin()
   1.435  	{
   1.436 -		m_Iter = m_pList->m_List.begin();
   1.437 +		m_Iter.GoToBegin();
   1.438  		SkipPaused();
   1.439  	}
   1.440  
   1.441 @@ -1052,7 +1172,7 @@
   1.442  	{
   1.443  		if (!m_pList)
   1.444  			return false;
   1.445 -		return m_Iter == m_pList->m_List.end();
   1.446 +		return m_Iter.End();;
   1.447  	}
   1.448  	void CSourceHookImpl::CHookList::CIter::Next()
   1.449  	{
   1.450 @@ -1067,7 +1187,7 @@
   1.451  	}
   1.452  	void CSourceHookImpl::CHookList::CIter::SkipPaused()
   1.453  	{
   1.454 -		while (m_Iter != m_pList->m_List.end() && m_Iter->paused)
   1.455 +		while (!m_Iter.End() && m_Iter->paused)
   1.456  			++m_Iter;
   1.457  	}
   1.458  
   1.459 @@ -1085,6 +1205,9 @@
   1.460  	//////////////////////////////////////////////////////////////////////////
   1.461  	char *CSourceHookImpl::CProto::DupProto(const char *p)
   1.462  	{
   1.463 +		if (!p)
   1.464 +			return NULL;
   1.465 +
   1.466  		char *newproto;
   1.467  		if (*p)
   1.468  		{
   1.469 @@ -1105,6 +1228,9 @@
   1.470  
   1.471  	void CSourceHookImpl::CProto::FreeProto(char *prot)
   1.472  	{
   1.473 +		if (!prot)
   1.474 +			return;
   1.475 +
   1.476  		if (*prot)
   1.477  		{
   1.478  			delete [] prot;
   1.479 @@ -1117,6 +1243,9 @@
   1.480  
   1.481  	bool CSourceHookImpl::CProto::Equal(const char *p1, const char *p2)
   1.482  	{
   1.483 +		if (!p1 || !p2)
   1.484 +			return false;
   1.485 +
   1.486  		if (*p1 && *p2)			// Case1: Both old
   1.487  		{
   1.488  			// As in old versions
   1.489 @@ -1209,4 +1338,68 @@
   1.490  			}
   1.491  		}
   1.492  	}
   1.493 +
   1.494 +	//////////////////////////////////////////////////////////////////////////
   1.495 +	// CHookIDManager
   1.496 +	//////////////////////////////////////////////////////////////////////////
   1.497 +	CSourceHookImpl::CHookIDManager::CHookIDManager()
   1.498 +	{
   1.499 +	}
   1.500 +
   1.501 +	int CSourceHookImpl::CHookIDManager::New(const CProto &proto, int vtbl_offs, int vtbl_idx, void *vfnptr,
   1.502 +		void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
   1.503 +	{
   1.504 +		Entry tmp(proto, vtbl_offs, vtbl_idx, vfnptr, adjustediface, plug, thisptr_offs, handler, post);
   1.505 +
   1.506 +		size_t cursize = m_Entries.size();
   1.507 +		for (size_t i = 0; i < cursize; ++i)
   1.508 +		{
   1.509 +			if (m_Entries[i].isfree)
   1.510 +			{
   1.511 +				m_Entries[i] = tmp;
   1.512 +				return static_cast<int>(i) + 1;
   1.513 +			}
   1.514 +		}
   1.515 +
   1.516 +		m_Entries.push_back(tmp);
   1.517 +		return static_cast<int>(m_Entries.size());		// return size() because hookid = id+1 anyway
   1.518 +	}
   1.519 +
   1.520 +	bool CSourceHookImpl::CHookIDManager::Remove(int hookid)
   1.521 +	{
   1.522 +		int realid = hookid - 1;
   1.523 +		if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
   1.524 +			return false;
   1.525 +
   1.526 +		m_Entries[realid].isfree = true;
   1.527 +
   1.528 +		// :TODO: remove free ids from back sometimes ??
   1.529 +
   1.530 +		return true;
   1.531 +	}
   1.532 +
   1.533 +	const CSourceHookImpl::CHookIDManager::Entry * CSourceHookImpl::CHookIDManager::QueryHook(int hookid)
   1.534 +	{
   1.535 +		int realid = hookid - 1;
   1.536 +		if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
   1.537 +			return NULL;
   1.538 +
   1.539 +		return &m_Entries[realid];
   1.540 +	}
   1.541 +
   1.542 +	void CSourceHookImpl::CHookIDManager::FindAllHooks(CVector<int> &output, const CProto &proto, int vtbl_offs,
   1.543 +		int vtbl_idx, void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
   1.544 +	{
   1.545 +		// oh my god, a lot of parameters...
   1.546 +		size_t cursize = m_Entries.size();
   1.547 +		for (size_t i = 0; i < cursize; ++i)
   1.548 +		{
   1.549 +			if (!m_Entries[i].isfree && m_Entries[i].proto == proto && m_Entries[i].vtbl_offs == vtbl_offs &&
   1.550 +				m_Entries[i].vtbl_idx == vtbl_idx && m_Entries[i].adjustediface == adjustediface && m_Entries[i].plug == plug &&
   1.551 +				m_Entries[i].thisptr_offs == thisptr_offs && m_Entries[i].handler->IsEqual(handler) && m_Entries[i].post == post)
   1.552 +			{
   1.553 +				output.push_back(static_cast<int>(i) + 1);
   1.554 +			}
   1.555 +		}
   1.556 +	} 
   1.557  }