sourcehook/sourcehook.cpp
changeset 163 377bdd31c28d
parent 162 a95db73792e5
child 177 114eaa11dca2
     1.1 --- a/sourcehook/sourcehook.cpp	Fri Dec 23 11:58:11 2005 +0000
     1.2 +++ b/sourcehook/sourcehook.cpp	Fri Dec 23 23:00:55 2005 +0000
     1.3 @@ -67,22 +67,28 @@
     1.4  		if (hook_iter->plug == plug) \
     1.5  			return true;
     1.6  
     1.7 -		for (HookManInfoList::iterator hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end(); ++hmil_iter)
     1.8 +		for (HookManContList::iterator hmcl_iter = m_HookMans.begin();
     1.9 +			hmcl_iter != m_HookMans.end(); ++hmcl_iter)
    1.10  		{
    1.11 -			if (hmil_iter->m_Plug != plug)
    1.12 -				continue;
    1.13 -			for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
    1.14 -				vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
    1.15 +			for (CHookManagerContainer::iterator hmil_iter = hmcl_iter->begin();
    1.16 +				hmil_iter != hmcl_iter->end(); ++hmil_iter)
    1.17  			{
    1.18 -				for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
    1.19 -					iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
    1.20 +				if (hmil_iter->m_Plug != plug)
    1.21 +					continue;
    1.22 +				for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
    1.23 +					vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
    1.24  				{
    1.25 -					List<HookInfo>::iterator hook_iter;
    1.26 -					TMP_CHECK_LIST(m_PreHooks);
    1.27 -					TMP_CHECK_LIST(m_PostHooks);
    1.28 +					for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
    1.29 +						iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
    1.30 +					{
    1.31 +						List<HookInfo>::iterator hook_iter;
    1.32 +						TMP_CHECK_LIST(m_PreHooks);
    1.33 +						TMP_CHECK_LIST(m_PostHooks);
    1.34 +					}
    1.35  				}
    1.36  			}
    1.37  		}
    1.38 +		
    1.39  #undef TMP_CHECK_LIST
    1.40  		return false;
    1.41  	}
    1.42 @@ -90,26 +96,37 @@
    1.43  	void CSourceHookImpl::UnloadPlugin(Plugin plug)
    1.44  	{
    1.45  		// 1) Manually remove all hooks by this plugin
    1.46 +		// 2) Manually remove all hook mans by this plugin
    1.47 +
    1.48  		List<RemoveHookInfo> hookstoremove;
    1.49 +		List<RemoveHookManInfo> hookmanstoremove;
    1.50  		HookManInfoList::iterator hmil_iter;
    1.51  
    1.52  #define TMP_CHECK_LIST(name, ispost) \
    1.53  	for (hook_iter = iface_iter->name.m_List.begin(); hook_iter != iface_iter->name.m_List.end(); ++hook_iter) \
    1.54  		if (hook_iter->plug == plug) \
    1.55  			hookstoremove.push_back(RemoveHookInfo(hook_iter->plug, iface_iter->m_Ptr, \
    1.56 -			hook_iter->thisptr_offs, hmil_iter->m_Func, hook_iter->handler, ispost))
    1.57 +			hook_iter->thisptr_offs, hmil_iter->m_Func, hook_iter->handler, ispost)); \
    1.58  
    1.59 -		for (hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end(); ++hmil_iter)
    1.60 +		for (HookManContList::iterator hmcl_iter = m_HookMans.begin();
    1.61 +			hmcl_iter != m_HookMans.end(); ++hmcl_iter)
    1.62  		{
    1.63 -			for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
    1.64 -				vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
    1.65 +			for (CHookManagerContainer::iterator hmil_iter = hmcl_iter->begin();
    1.66 +				hmil_iter != hmcl_iter->end(); ++hmil_iter)
    1.67  			{
    1.68 -				for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
    1.69 -					iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
    1.70 +				if (hmil_iter->m_Plug == plug)
    1.71 +					hookmanstoremove.push_back(RemoveHookManInfo(plug, hmil_iter->m_Func));
    1.72 +
    1.73 +				for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
    1.74 +					vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
    1.75  				{
    1.76 -					List<HookInfo>::iterator hook_iter;
    1.77 -					TMP_CHECK_LIST(m_PreHooks, false);
    1.78 -					TMP_CHECK_LIST(m_PostHooks, true);
    1.79 +					for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
    1.80 +						iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
    1.81 +					{
    1.82 +						List<HookInfo>::iterator hook_iter;
    1.83 +						TMP_CHECK_LIST(m_PreHooks, false);
    1.84 +						TMP_CHECK_LIST(m_PostHooks, true);
    1.85 +					}
    1.86  				}
    1.87  			}
    1.88  		}
    1.89 @@ -118,68 +135,8 @@
    1.90  		for (List<RemoveHookInfo>::iterator rmiter = hookstoremove.begin(); rmiter != hookstoremove.end(); ++rmiter)
    1.91  			RemoveHook(*rmiter);
    1.92  
    1.93 -		// 2) Other plugins may use hook managers in this plugin.
    1.94 -		// Get a list of hook managers that are in this plugin and are used by other plugins
    1.95 -		// Delete all hook managers that are in this plugin
    1.96 -
    1.97 -		HookManInfoList tmphookmans;
    1.98 -		bool erase = false;
    1.99 -		for (hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end();
   1.100 -			erase ? hmil_iter=m_HookMans.erase(hmil_iter) : ++hmil_iter)
   1.101 -		{
   1.102 -			if (hmil_iter->m_Plug == plug)
   1.103 -			{
   1.104 -				if (!hmil_iter->m_VfnPtrs.empty())
   1.105 -				{
   1.106 -					// All hooks by this plugin are already removed
   1.107 -					// So if there is a vfnptr, it has to be used by an other plugin
   1.108 -					tmphookmans.push_back(*hmil_iter);
   1.109 -				}
   1.110 -				erase = true;
   1.111 -			}
   1.112 -			else
   1.113 -				erase = false;
   1.114 -		}
   1.115 -
   1.116 -		// For each hook manager that is used in an other plugin:
   1.117 -		for (hmil_iter = tmphookmans.begin(); hmil_iter != tmphookmans.end(); ++hmil_iter)
   1.118 -		{
   1.119 -			// Find a suitable hook manager in an other plugin
   1.120 -			HookManInfoList::iterator newHookMan = FindHookMan(m_HookMans.begin(), m_HookMans.end(),
   1.121 -				hmil_iter->m_Proto, hmil_iter->m_VtblOffs, hmil_iter->m_VtblIdx);
   1.122 -
   1.123 -			// This should _never_ happen.
   1.124 -			// If there is a hook from an other plugin, the plugin must have provided a hook manager as well.
   1.125 -			SH_ASSERT(newHookMan != m_HookMans.end(),
   1.126 -				("Could not find a suitable hook manager in an other plugin!"));
   1.127 -
   1.128 -			// AddHook should make sure that every plugin only has _one_ hook manager for _one_ proto/vi/vo
   1.129 -			SH_ASSERT(newHookMan->m_Plug != plug, ("New hook manager from same plugin!"));
   1.130 -
   1.131 -			// The first hook manager should be always used - so the new hook manager has to be empty
   1.132 -			SH_ASSERT(newHookMan->m_VfnPtrs.empty(), ("New hook manager not empty!"));
   1.133 -
   1.134 -			// Move the vfnptrs from the old hook manager to the new one
   1.135 -			newHookMan->m_VfnPtrs = hmil_iter->m_VfnPtrs;
   1.136 -
   1.137 -			// Unregister the old one, register the new one
   1.138 -			hmil_iter->m_Func(HA_Unregister, NULL);
   1.139 -			newHookMan->m_Func(HA_Register, &(*newHookMan));
   1.140 -
   1.141 -			// zOMG BAIL, here is part of what you wanted:
   1.142 -
   1.143 -			// Go through all vfnptrs in this hookman and patch them to point to the new manager's handler!
   1.144 -			// or whatever
   1.145 -			for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = newHookMan->m_VfnPtrs.begin();
   1.146 -				vfnptr_iter != newHookMan->m_VfnPtrs.end(); ++vfnptr_iter)
   1.147 -			{
   1.148 -				// And DEREFERENCE newHookMan->m_HookfuncVfnptr!
   1.149 -				// otherwise it will be executing the vtable... had to find out the hard way
   1.150 -				*reinterpret_cast<void**>(vfnptr_iter->m_Ptr) = *reinterpret_cast<void**>(newHookMan->m_HookfuncVfnptr);
   1.151 -			}
   1.152 -
   1.153 -			// That should fix it, bail!
   1.154 -		}
   1.155 +		for (List<RemoveHookManInfo>::iterator rmiter = hookmanstoremove.begin(); rmiter != hookmanstoremove.end(); ++rmiter)
   1.156 +			RemoveHookManager(*rmiter);
   1.157  	}
   1.158  
   1.159  	void CSourceHookImpl::RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc)
   1.160 @@ -190,12 +147,22 @@
   1.161  		if (pubFunc(HA_GetInfo, &tmp) != 0)
   1.162  			return;
   1.163  
   1.164 -		HookManInfoList::iterator hmil_iter = FindHookMan(m_HookMans.begin(), m_HookMans.end(),
   1.165 -			tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx);
   1.166 +		
   1.167 +		HookManContList::iterator hmcl_iter = m_HookMans.find(
   1.168 +			CHookManagerContainer::HMCI(tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx));
   1.169  
   1.170 -		if (hmil_iter == m_HookMans.end())
   1.171 +		if (hmcl_iter == m_HookMans.end())
   1.172  		{
   1.173 -			 // Moo ?
   1.174 +			// Moo?
   1.175 +			return;
   1.176 +		}
   1.177 +
   1.178 +		CHookManagerContainer::iterator hmil_iter = hmcl_iter->find(
   1.179 +			CHookManagerInfo::Descriptor(plug, pubFunc));
   1.180 +
   1.181 +		if (hmil_iter == hmcl_iter->end())
   1.182 +		{
   1.183 +			// Moo?
   1.184  			return;
   1.185  		}
   1.186  
   1.187 @@ -230,63 +197,75 @@
   1.188  		CHookManagerInfo info = *hmil_iter;
   1.189  
   1.190  		// Unlink the hook manager from the list
   1.191 -		m_HookMans.erase(hmil_iter);
   1.192 +		hmcl_iter->erase(hmil_iter);
   1.193  
   1.194  		// If there were any hooks from other plugins, find a new suitable hook manager.
   1.195  		if (stillInUse)
   1.196  		{
   1.197  			// Find a suitable hook manager in an other plugin
   1.198 -			HookManInfoList::iterator newHookMan = FindHookMan(m_HookMans.begin(), m_HookMans.end(),
   1.199 -				info.m_Proto, info.m_VtblOffs, info.m_VtblIdx);
   1.200 +			hmil_iter = hmcl_iter->begin();
   1.201  
   1.202  			// This should _never_ happen.
   1.203  			// If there is a hook from an other plugin, the plugin must have provided a hook manager as well.
   1.204 -			SH_ASSERT(newHookMan != m_HookMans.end(),
   1.205 +			SH_ASSERT(hmil_iter != hmcl_iter->end(),
   1.206  				("Could not find a suitable hook manager in an other plugin!"));
   1.207  
   1.208  			// AddHook should make sure that every plugin only has _one_ hook manager for _one_ proto/vi/vo
   1.209 -			SH_ASSERT(newHookMan->m_Plug != plug, ("New hook manager from same plugin!"));
   1.210 +			SH_ASSERT(hmil_iter->m_Plug != plug, ("New hook manager from same plugin!"));
   1.211  
   1.212  			// The first hook manager should be always used - so the new hook manager has to be empty
   1.213 -			SH_ASSERT(newHookMan->m_VfnPtrs.empty(), ("New hook manager not empty!"));
   1.214 +			SH_ASSERT(hmil_iter->m_VfnPtrs.empty(), ("New hook manager not empty!"));
   1.215  
   1.216  			// Move the vfnptrs from the old hook manager to the new one
   1.217 -			newHookMan->m_VfnPtrs = info.m_VfnPtrs;
   1.218 +			hmil_iter->m_VfnPtrs = info.m_VfnPtrs;
   1.219  
   1.220  			// Unregister the old one, register the new one
   1.221  			info.m_Func(HA_Unregister, NULL);
   1.222 -			newHookMan->m_Func(HA_Register, &(*newHookMan));
   1.223 +			hmil_iter->m_Func(HA_Register, &(*hmil_iter));
   1.224  
   1.225  			// Go through all vfnptrs in this hookman and patch them to point to the new manager's handler!
   1.226  			// or whatever
   1.227 -			for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = newHookMan->m_VfnPtrs.begin();
   1.228 -				vfnptr_iter != newHookMan->m_VfnPtrs.end(); ++vfnptr_iter)
   1.229 +			for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
   1.230 +				vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
   1.231  			{
   1.232  				// And DEREFERENCE newHookMan->m_HookfuncVfnptr!
   1.233  				// otherwise it will be executing the vtable... had to find out the hard way
   1.234 -				*reinterpret_cast<void**>(vfnptr_iter->m_Ptr) = *reinterpret_cast<void**>(newHookMan->m_HookfuncVfnptr);
   1.235 +				*reinterpret_cast<void**>(vfnptr_iter->m_Ptr) = *reinterpret_cast<void**>(hmil_iter->m_HookfuncVfnptr);
   1.236  			}
   1.237  		}
   1.238  	}
   1.239  
   1.240 +	void CSourceHookImpl::RemoveHookManager(RemoveHookManInfo info)
   1.241 +	{
   1.242 +		RemoveHookManager(info.plug, info.hookman);
   1.243 +	}
   1.244 +
   1.245  	void CSourceHookImpl::CompleteShutdown()
   1.246  	{
   1.247 +		// Remove all hooks
   1.248 +
   1.249  		List<RemoveHookInfo> hookstoremove;
   1.250 +
   1.251  #define TMP_CHECK_LIST(name, ispost) \
   1.252  	for (hook_iter = iface_iter->name.m_List.begin(); hook_iter != iface_iter->name.m_List.end(); ++hook_iter) \
   1.253  		hookstoremove.push_back(RemoveHookInfo(hook_iter->plug, iface_iter->m_Ptr, \
   1.254  			hook_iter->thisptr_offs, hmil_iter->m_Func, hook_iter->handler, ispost))
   1.255 -		for (HookManInfoList::iterator hmil_iter = m_HookMans.begin(); hmil_iter != m_HookMans.end(); ++hmil_iter)
   1.256 +		for (HookManContList::iterator hmcl_iter = m_HookMans.begin();
   1.257 +			hmcl_iter != m_HookMans.end(); ++hmcl_iter)
   1.258  		{
   1.259 -			for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
   1.260 -				vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
   1.261 +			for (CHookManagerContainer::iterator hmil_iter = hmcl_iter->begin();
   1.262 +				hmil_iter != hmcl_iter->end(); ++hmil_iter)
   1.263  			{
   1.264 -				for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
   1.265 -					iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
   1.266 +				for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
   1.267 +					vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
   1.268  				{
   1.269 -					List<HookInfo>::iterator hook_iter;
   1.270 -					TMP_CHECK_LIST(m_PreHooks, false);
   1.271 -					TMP_CHECK_LIST(m_PostHooks, true);
   1.272 +					for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
   1.273 +						iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
   1.274 +					{
   1.275 +						List<HookInfo>::iterator hook_iter;
   1.276 +						TMP_CHECK_LIST(m_PreHooks, false);
   1.277 +						TMP_CHECK_LIST(m_PostHooks, true);
   1.278 +					}
   1.279  				}
   1.280  			}
   1.281  		}
   1.282 @@ -307,40 +286,42 @@
   1.283  		if (myHookMan(HA_GetInfo, &tmp) != 0)
   1.284  			return false;
   1.285  
   1.286 +		tmp.m_Func = myHookMan;
   1.287 +		tmp.m_Plug = plug;
   1.288 +
   1.289 +		CHookManagerContainer::HMCI hmci(tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx);
   1.290 +
   1.291  		void **cur_vtptr = *reinterpret_cast<void***>(
   1.292  			reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs);
   1.293  		void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.m_VtblIdx);
   1.294  
   1.295 -		// Add the proposed hook manager to the _end_ of the list if the plugin doesn't have a hook manager
   1.296 -		// with this proto/vo/vi registered
   1.297 -		HookManInfoList::iterator hkmi_iter;
   1.298 -		for (hkmi_iter = m_HookMans.begin(); hkmi_iter != m_HookMans.end(); ++hkmi_iter)
   1.299 +		// Add the container if not already added
   1.300 +		HookManContList::iterator hmcl_iter = m_HookMans.find(hmci);
   1.301 +		if (hmcl_iter == m_HookMans.end())
   1.302  		{
   1.303 -			if (hkmi_iter->m_Plug == plug && ProtosEquiv(hkmi_iter->m_Proto, tmp.m_Proto) &&
   1.304 -				hkmi_iter->m_VtblOffs == tmp.m_VtblOffs && hkmi_iter->m_VtblIdx == tmp.m_VtblIdx)
   1.305 -				break;
   1.306 -		}
   1.307 -		if (hkmi_iter == m_HookMans.end())
   1.308 -		{
   1.309 -			// No such hook manager from this plugin yet, add it!
   1.310 -			tmp.m_Func = myHookMan;
   1.311 -			tmp.m_Plug = plug;
   1.312 -			m_HookMans.push_back(tmp);
   1.313 +			m_HookMans.push_back(CHookManagerContainer(hmci));
   1.314 +			hmcl_iter = m_HookMans.end();
   1.315 +			--hmcl_iter;
   1.316  		}
   1.317  
   1.318 -		// Then, search for a suitable hook manager (from the beginning)
   1.319 -		HookManInfoList::iterator hookman = FindHookMan(m_HookMans.begin(), m_HookMans.end(), tmp.m_Proto,
   1.320 -			tmp.m_VtblOffs, tmp.m_VtblIdx);
   1.321 -		SH_ASSERT(hookman != m_HookMans.end(), ("No hookman found - but if there was none, we've just added one!"));
   1.322 +		hmcl_iter->AddHookManager(plug, tmp);
   1.323  
   1.324 -		// Check whether there is already an other hook manager for the same vfnptr but with other values before
   1.325 +		CHookManagerContainer::iterator hookman = hmcl_iter->begin();
   1.326 +		SH_ASSERT(hookman != hmcl_iter->end(), ("No hookman found - but we've just added one!"));
   1.327 +
   1.328 +		// Check whether any other container holds a hook manager which holds this vfnptr
   1.329  		// If yes, it means that the two are incompatible, so return false.
   1.330 -		for (hkmi_iter = m_HookMans.begin(); hkmi_iter != m_HookMans.end(); ++hkmi_iter)
   1.331 +		for (HookManContList::iterator hmcl_iter2 = m_HookMans.begin();
   1.332 +			hmcl_iter2 != m_HookMans.end(); ++hmcl_iter2)
   1.333  		{
   1.334 -			if (hkmi_iter != hookman)
   1.335 +			if (hmcl_iter2 != hmcl_iter)
   1.336  			{
   1.337 -				if (hkmi_iter->m_VfnPtrs.find(cur_vfnptr) != hkmi_iter->m_VfnPtrs.end())
   1.338 -					return false;
   1.339 +				for (CHookManagerContainer::iterator hmil_iter = hmcl_iter2->begin();
   1.340 +					hmil_iter != hmcl_iter2->end(); ++hmil_iter)
   1.341 +				{
   1.342 +					if (hmil_iter->m_VfnPtrs.find(cur_vfnptr) != hmil_iter->m_VfnPtrs.end())
   1.343 +						return false;
   1.344 +				}
   1.345  			}
   1.346  		}
   1.347  
   1.348 @@ -409,10 +390,13 @@
   1.349  			return false;
   1.350  
   1.351  		// Find the hook manager and the hook
   1.352 -		HookManInfoList::iterator hookman = FindHookMan(m_HookMans.begin(), m_HookMans.end(),
   1.353 -			tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx);
   1.354 -		if (hookman == m_HookMans.end())
   1.355 +
   1.356 +		HookManContList::iterator hmcl_iter = m_HookMans.find(
   1.357 +			CHookManagerContainer::HMCI(tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx));
   1.358 +		if (hmcl_iter == m_HookMans.end() || hmcl_iter->empty())
   1.359  			return false;
   1.360 +		
   1.361 +		CHookManagerContainer::iterator hookman = hmcl_iter->begin();
   1.362  
   1.363  		if (!ModuleInMemory(reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs,
   1.364  			sizeof(void*) * (tmp.m_VtblIdx + 1)))
   1.365 @@ -532,20 +516,25 @@
   1.366  
   1.367  	void CSourceHookImpl::ApplyCallClassPatches(CCallClassImpl &cc)
   1.368  	{
   1.369 -		for (HookManInfoList::iterator hookman = m_HookMans.begin(); hookman != m_HookMans.end(); ++hookman)
   1.370 +		for (HookManContList::iterator hmcl_iter = m_HookMans.begin();
   1.371 +			hmcl_iter != m_HookMans.end(); ++hmcl_iter)
   1.372  		{
   1.373 -			for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->m_VfnPtrs.begin();
   1.374 -				vfnptr_iter != hookman->m_VfnPtrs.end(); ++vfnptr_iter)
   1.375 +			for (CHookManagerContainer::iterator hookman = hmcl_iter->begin();
   1.376 +				hookman != hmcl_iter->end(); ++hookman)
   1.377  			{
   1.378 -				for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
   1.379 -					iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
   1.380 +				for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->m_VfnPtrs.begin();
   1.381 +					vfnptr_iter != hookman->m_VfnPtrs.end(); ++vfnptr_iter)
   1.382  				{
   1.383 -					if (iface_iter->m_Ptr >= cc.m_Ptr &&
   1.384 -						iface_iter->m_Ptr < (reinterpret_cast<char*>(cc.m_Ptr) + cc.m_ObjSize))
   1.385 +					for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
   1.386 +						iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
   1.387  					{
   1.388 -						cc.ApplyCallClassPatch(static_cast<int>(reinterpret_cast<char*>(iface_iter->m_Ptr) -
   1.389 -							reinterpret_cast<char*>(cc.m_Ptr)) + hookman->m_VtblOffs,
   1.390 -							hookman->m_VtblIdx, vfnptr_iter->m_OrigEntry);
   1.391 +						if (iface_iter->m_Ptr >= cc.m_Ptr &&
   1.392 +							iface_iter->m_Ptr < (reinterpret_cast<char*>(cc.m_Ptr) + cc.m_ObjSize))
   1.393 +						{
   1.394 +							cc.ApplyCallClassPatch(static_cast<int>(reinterpret_cast<char*>(iface_iter->m_Ptr) -
   1.395 +								reinterpret_cast<char*>(cc.m_Ptr)) + hookman->m_VtblOffs,
   1.396 +								hookman->m_VtblIdx, vfnptr_iter->m_OrigEntry);
   1.397 +						}
   1.398  					}
   1.399  				}
   1.400  			}
   1.401 @@ -579,38 +568,34 @@
   1.402  		}
   1.403  	}
   1.404  
   1.405 -	CSourceHookImpl::HookManInfoList::iterator CSourceHookImpl::FindHookMan(HookManInfoList::iterator begin,
   1.406 -		HookManInfoList::iterator end, const char *proto, int vtblofs, int vtblidx)
   1.407 -	{
   1.408 -		HookManInfoList::iterator hookmaniter;
   1.409 -		for (hookmaniter = m_HookMans.begin(); hookmaniter != m_HookMans.end(); ++hookmaniter)
   1.410 -		{
   1.411 -			if (ProtosEquiv(hookmaniter->m_Proto, proto) && hookmaniter->m_VtblOffs == vtblofs &&
   1.412 -				hookmaniter->m_VtblIdx == vtblidx)
   1.413 -				break;
   1.414 -		}
   1.415 -		return hookmaniter;
   1.416 -	}
   1.417 -
   1.418  	void CSourceHookImpl::SetPluginPaused(Plugin plug, bool paused)
   1.419  	{
   1.420  		// Go through all hook managers, all interfaces, and set the status of all hooks of this plugin to paused
   1.421 -		for (HookManInfoList::iterator hookmaniter = m_HookMans.begin(); hookmaniter != m_HookMans.end(); ++hookmaniter)
   1.422 -			for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookmaniter->m_VfnPtrs.begin();
   1.423 -				vfnptr_iter != hookmaniter->m_VfnPtrs.end(); ++vfnptr_iter)
   1.424 -				for (CVfnPtr::IfaceListIter ifaceiter = vfnptr_iter->m_Ifaces.begin();
   1.425 -					ifaceiter != vfnptr_iter->m_Ifaces.end(); ++ifaceiter)
   1.426 +		for (HookManContList::iterator hmcl_iter = m_HookMans.begin();
   1.427 +			hmcl_iter != m_HookMans.end(); ++hmcl_iter)
   1.428 +		{
   1.429 +			for (CHookManagerContainer::iterator hookmaniter = hmcl_iter->begin();
   1.430 +				hookmaniter != hmcl_iter->end(); ++hookmaniter)
   1.431 +			{
   1.432 +				for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookmaniter->m_VfnPtrs.begin();
   1.433 +					vfnptr_iter != hookmaniter->m_VfnPtrs.end(); ++vfnptr_iter)
   1.434  				{
   1.435 -					for (List<HookInfo>::iterator hookiter = ifaceiter->m_PreHooks.m_List.begin();
   1.436 -						hookiter != ifaceiter->m_PreHooks.m_List.end(); ++hookiter)
   1.437 -						if (plug == hookiter->plug)
   1.438 -							hookiter->paused = paused;
   1.439 +					for (CVfnPtr::IfaceListIter ifaceiter = vfnptr_iter->m_Ifaces.begin();
   1.440 +						ifaceiter != vfnptr_iter->m_Ifaces.end(); ++ifaceiter)
   1.441 +					{
   1.442 +						for (List<HookInfo>::iterator hookiter = ifaceiter->m_PreHooks.m_List.begin();
   1.443 +							hookiter != ifaceiter->m_PreHooks.m_List.end(); ++hookiter)
   1.444 +							if (plug == hookiter->plug)
   1.445 +								hookiter->paused = paused;
   1.446  
   1.447 -					for (List<HookInfo>::iterator hookiter = ifaceiter->m_PostHooks.m_List.begin();
   1.448 -						hookiter != ifaceiter->m_PostHooks.m_List.end(); ++hookiter)
   1.449 -						if (plug == hookiter->plug)
   1.450 -							hookiter->paused = paused;
   1.451 +						for (List<HookInfo>::iterator hookiter = ifaceiter->m_PostHooks.m_List.begin();
   1.452 +							hookiter != ifaceiter->m_PostHooks.m_List.end(); ++hookiter)
   1.453 +							if (plug == hookiter->plug)
   1.454 +								hookiter->paused = paused;
   1.455 +					}
   1.456  				}
   1.457 +			}
   1.458 +		}
   1.459  	}
   1.460  	void CSourceHookImpl::PausePlugin(Plugin plug)
   1.461  	{
   1.462 @@ -828,68 +813,12 @@
   1.463  		}
   1.464  	}
   1.465  
   1.466 -	bool CSourceHookImpl::ProtosEquiv(const char *p1, const char *p2)
   1.467 -	{
   1.468 -		/*
   1.469 -		 Old protos look like this:
   1.470 -
   1.471 -		 0_void:
   1.472 -			"attrib"
   1.473 -		 1_void:
   1.474 -			"attrib|param1_type"
   1.475 -		 2_void:
   1.476 -			"attrib|param1_type|param2_type
   1.477 -		 0:
   1.478 -			"attrib|ret_type"
   1.479 -		 1:
   1.480 -			"attrib|ret_type|param1_type"
   1.481 -		 2:
   1.482 -			"attrib|ret_type|param2_type"
   1.483 -
   1.484 -		New protos are in fact pointers to the ProtoInfo sturcture (see sourcehook.h for details)
   1.485 -
   1.486 -		 Old protos _never_ begin with a null character
   1.487 -		 New protos _always_ begin with a null character
   1.488 -		*/
   1.489 -
   1.490 -		if (*p1 && *p2)			// Case1: Both old
   1.491 -		{
   1.492 -			// As in old versions
   1.493 -			return strcmp(p1, p2) == 0;
   1.494 -		}
   1.495 -		else if (!*p1 && !*p2)	// Case2: Both new
   1.496 -		{
   1.497 -			const ProtoInfo *pi1 = reinterpret_cast<const ProtoInfo*>(p1);
   1.498 -			const ProtoInfo *pi2 = reinterpret_cast<const ProtoInfo*>(p2);
   1.499 -
   1.500 -			if (pi1->retTypeSize == pi2->retTypeSize &&
   1.501 -				pi1->numOfParams == pi2->numOfParams)
   1.502 -			{
   1.503 -				// params[0] is 0 for "normal" functions and -1 for vararg functions
   1.504 -				// params[1] is size of first parameter
   1.505 -				// params[2] is size of second parameter ...
   1.506 -				for (int i = 0; i <= pi1->numOfParams; ++i)
   1.507 -				{
   1.508 -					if (pi1->params[i] != pi2->params[i])
   1.509 -						return false;
   1.510 -				}
   1.511 -				return true;
   1.512 -			}
   1.513 -			else
   1.514 -			{
   1.515 -				return false;
   1.516 -			}
   1.517 -		}
   1.518 -		else					// Case3: Mixed old/new
   1.519 -		{
   1.520 -			// Trust the user
   1.521 -			return true;
   1.522 -		}
   1.523 -	}
   1.524 -
   1.525  	////////////////////////////
   1.526  	// CHookManagerInfo
   1.527  	////////////////////////////
   1.528 +	CSourceHookImpl::CHookManagerInfo::CHookManagerInfo() : m_HookManVersion(0)
   1.529 +	{
   1.530 +	}
   1.531  	CSourceHookImpl::CHookManagerInfo::~CHookManagerInfo()
   1.532  	{
   1.533  	}
   1.534 @@ -909,7 +838,10 @@
   1.535  	{
   1.536  		m_HookfuncVfnptr = hookfunc_vfnptr;
   1.537  	}
   1.538 -
   1.539 +	void CSourceHookImpl::CHookManagerInfo::SetVersion(int version)
   1.540 +	{
   1.541 +		m_HookManVersion = version;
   1.542 +	}
   1.543  	////////////////////////////
   1.544  	// CVfnPtr
   1.545  	////////////////////////////
   1.546 @@ -1097,4 +1029,102 @@
   1.547  	{
   1.548  		return m_Iter->thisptr_offs;
   1.549  	}
   1.550 +
   1.551 +	//////////////////////////////////////////////////////////////////////////
   1.552 +	// CProto
   1.553 +	//////////////////////////////////////////////////////////////////////////
   1.554 +	bool CSourceHookImpl::CProto::Equal(const char *p1, const char *p2)
   1.555 +	{
   1.556 +		if (*p1 && *p2)			// Case1: Both old
   1.557 +		{
   1.558 +			// As in old versions
   1.559 +			return strcmp(p1, p2) == 0;
   1.560 +		}
   1.561 +		else if (!*p1 && !*p2)	// Case2: Both new
   1.562 +		{
   1.563 +			const ProtoInfo *pi1 = reinterpret_cast<const ProtoInfo*>(p1);
   1.564 +			const ProtoInfo *pi2 = reinterpret_cast<const ProtoInfo*>(p2);
   1.565 +
   1.566 +			if (pi1->retTypeSize == pi2->retTypeSize &&
   1.567 +				pi1->numOfParams == pi2->numOfParams)
   1.568 +			{
   1.569 +				// params[0] is 0 for "normal" functions and -1 for vararg functions
   1.570 +				// params[1] is size of first parameter
   1.571 +				// params[2] is size of second parameter ...
   1.572 +				for (int i = 0; i <= pi1->numOfParams; ++i)
   1.573 +				{
   1.574 +					if (pi1->params[i] != pi2->params[i])
   1.575 +						return false;
   1.576 +				}
   1.577 +				return true;
   1.578 +			}
   1.579 +			else
   1.580 +			{
   1.581 +				return false;
   1.582 +			}
   1.583 +		}
   1.584 +		else					// Case3: Mixed old/new
   1.585 +		{
   1.586 +			// Trust the user
   1.587 +			return true;
   1.588 +		}
   1.589 +	}
   1.590 +
   1.591 +
   1.592 +	//////////////////////////////////////////////////////////////////////////
   1.593 +	// CHookManagerContainer
   1.594 +	//////////////////////////////////////////////////////////////////////////
   1.595 +	void CSourceHookImpl::CHookManagerContainer::AddHookManager(Plugin plug, const CHookManagerInfo &hookman)
   1.596 +	{
   1.597 +		iterator iter;
   1.598 +		// Check whether such a hook manager already exists; if yes, ignore.
   1.599 +		for (iter = begin(); iter != end(); ++iter)
   1.600 +		{
   1.601 +			if (iter->m_Plug == plug && iter->m_Func == hookman.m_Func)
   1.602 +				return;
   1.603 +		}
   1.604 +
   1.605 +		// It doesn't -> add it. Add it to the end of its version group.
   1.606 +		for (iter = begin(); iter != end(); ++iter)
   1.607 +		{
   1.608 +			if (iter->m_HookManVersion < hookman.m_HookManVersion)
   1.609 +				break;
   1.610 +		}
   1.611 +
   1.612 +		bool isBeginning = iter == begin();
   1.613 +
   1.614 +		insert(iter, hookman);
   1.615 +
   1.616 +		// If we inserted it at the beginning and if the dam breaks open many years too soon
   1.617 +		// and if there is more than one hookman and if the second one isn't empty, transfer
   1.618 +		// hooks from second to first
   1.619 +
   1.620 +		if (isBeginning && size() > 1)
   1.621 +		{
   1.622 +			iter = begin();
   1.623 +			iterator second = iter;
   1.624 +			++second;
   1.625 +
   1.626 +			if (!second->m_VfnPtrs.empty())
   1.627 +			{
   1.628 +				// Move the vfnptrs from the old hook manager to the new one
   1.629 +				iter->m_VfnPtrs = second->m_VfnPtrs;
   1.630 +				second->m_VfnPtrs.clear();
   1.631 +
   1.632 +				// Unregister the old one, register the new one
   1.633 +				second->m_Func(HA_Unregister, NULL);
   1.634 +				iter->m_Func(HA_Register, &(*iter));
   1.635 +
   1.636 +				// Go through all vfnptrs in this hookman and patch them to point to the new manager's handler!
   1.637 +				// or whatever
   1.638 +				for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = iter->m_VfnPtrs.begin();
   1.639 +					vfnptr_iter != iter->m_VfnPtrs.end(); ++vfnptr_iter)
   1.640 +				{
   1.641 +					// And DEREFERENCE newHookMan->m_HookfuncVfnptr!
   1.642 +					// otherwise it will be executing the vtable... had to find out the hard way
   1.643 +					*reinterpret_cast<void**>(vfnptr_iter->m_Ptr) = *reinterpret_cast<void**>(iter->m_HookfuncVfnptr);
   1.644 +				}
   1.645 +			}
   1.646 +		}
   1.647 +	}
   1.648  }