core/sourcehook/sourcehook.cpp
changeset 768 e6394d70ced0
parent 733 2365240861a2
     1.1 --- a/core/sourcehook/sourcehook.cpp	Sat Oct 15 22:16:50 2011 -0700
     1.2 +++ b/core/sourcehook/sourcehook.cpp	Mon Dec 12 15:53:56 2011 -0600
     1.3 @@ -1,4 +1,5 @@
     1.4  /* ======== SourceHook ========
     1.5 +* vim: set ts=4 sw=4 tw=99 noet:
     1.6  * Copyright (C) 2004-2010 Metamod:Source Development Team
     1.7  * No warranties of any kind
     1.8  *
     1.9 @@ -357,7 +358,7 @@
    1.10  			return m_ContextStack.front().pOverrideRet;
    1.11  		}
    1.12  
    1.13 -		void CSourceHookImpl::UnloadPlugin(Plugin plug)
    1.14 +		void CSourceHookImpl::UnloadPlugin(Plugin plug, UnloadListener *listener)
    1.15  		{
    1.16  			// 1) Remove all hooks by this plugin
    1.17  
    1.18 @@ -375,6 +376,20 @@
    1.19  				else
    1.20  					++iter;
    1.21  			}
    1.22 +
    1.23 +			// Fix for bug 5034. It's pretty tricky to find whether hookmanagers owned by this
    1.24 +			// plugin are active. We could change how hook managers are tracked in SH, and lazily
    1.25 +			// free them as the context stack drops to 0, or we could change the pubfunc API to
    1.26 +			// know whether it's active or not. Rather than deal with this extra complexity, we
    1.27 +			// just conservatively wait until the context stack hits 0 before unloading.
    1.28 +			if (m_ContextStack.size() == 0)
    1.29 +			{
    1.30 +				listener->ReadyToUnload(plug);
    1.31 +			}
    1.32 +			else
    1.33 +			{
    1.34 +				m_PendingUnloads.push_back(new PendingUnload(listener, plug));
    1.35 +			}
    1.36  		}
    1.37  
    1.38  		CHookManList::iterator CSourceHookImpl::RemoveHookManager(CHookManList::iterator hookman_iter)
    1.39 @@ -571,12 +586,41 @@
    1.40  			return pCtx;
    1.41  		}
    1.42  
    1.43 +		void CSourceHookImpl::ResolvePendingUnloads(bool force)
    1.44 +		{
    1.45 +			List<PendingUnload *>::iterator iter = m_PendingUnloads.begin();
    1.46 +			while (iter != m_PendingUnloads.end())
    1.47 +			{
    1.48 +				PendingUnload *unload = *iter;
    1.49 +
    1.50 +				if (!force && !unload->deactivated())
    1.51 +				{
    1.52 +					// Unless being forced, wait one drop of the context stack
    1.53 +					// before actually unloading. Otherwise, we'll still return
    1.54 +					// to unloaded memory.
    1.55 +					unload->deactivate();
    1.56 +					iter++;
    1.57 +				}
    1.58 +				else
    1.59 +				{
    1.60 +					unload->listener()->ReadyToUnload(unload->plugin());
    1.61 +					delete unload;
    1.62 +					iter = m_PendingUnloads.erase(iter);
    1.63 +				}
    1.64 +			}
    1.65 +		}
    1.66 +
    1.67  		void CSourceHookImpl::EndContext(IHookContext *pCtx)
    1.68  		{
    1.69  			// Do clean up task, if any is associated with this context
    1.70  			m_ContextStack.front().DoCleanupTaskAndDeleteIt();
    1.71  			// Then remove it
    1.72  			m_ContextStack.pop();
    1.73 +
    1.74 +			// If we've reached 0 contexts and there are pending unloads,
    1.75 +			// resolve them now.
    1.76 +			if (m_ContextStack.size() == 0 && m_PendingUnloads.size() != 0)
    1.77 +				ResolvePendingUnloads();
    1.78  		}
    1.79  
    1.80  		void CSourceHookImpl::CompleteShutdown()