sourcehook/sourcehook.cpp
changeset 162 a95db73792e5
parent 157 064f77cac7ab
child 163 377bdd31c28d
     1.1 --- a/sourcehook/sourcehook.cpp	Thu Dec 22 21:00:54 2005 +0000
     1.2 +++ b/sourcehook/sourcehook.cpp	Fri Dec 23 11:58:11 2005 +0000
     1.3 @@ -627,6 +627,7 @@
     1.4  		HookLoopInfo hli;
     1.5  		hli.pCurIface = pIface;
     1.6  		hli.shouldContinue = true;
     1.7 +		hli.recall = false;
     1.8  		m_HLIStack.push(hli);
     1.9  	}
    1.10  
    1.11 @@ -657,6 +658,20 @@
    1.12  
    1.13  	const void *CSourceHookImpl::GetOverrideRet()
    1.14  	{
    1.15 +		// pOverrideRet is always set since recalls were introduced
    1.16 +		// GetOverrideRetPtr was added; a function which always returns pOverrideRet
    1.17 +		// so that RETURN_META_VALUE_NEWPARAMS can provide an override result
    1.18 +
    1.19 +		// This means that we have to filter GetOverrideRet:
    1.20 +		//  If the status variable is < MRES_OVERRIDE, return NULL.
    1.21 +
    1.22 +		return (*m_HLIStack.front().pStatus < MRES_OVERRIDE) ?
    1.23 +			NULL : m_HLIStack.front().pOverrideRet;
    1.24 +	}
    1.25 +
    1.26 +	void *CSourceHookImpl::GetOverrideRetPtr()
    1.27 +	{
    1.28 +		// As described in the comment above: always return pOverrideRet
    1.29  		return m_HLIStack.front().pOverrideRet;
    1.30  	}
    1.31  
    1.32 @@ -673,11 +688,19 @@
    1.33  	void CSourceHookImpl::SetPrevResPtr(META_RES *mres)
    1.34  	{
    1.35  		m_HLIStack.front().pPrevRes = mres;
    1.36 +
    1.37 +		// If we're recalling, drag the previous mres value to the new hookfunc
    1.38 +		if (m_HLIStack.size() > 1 && m_HLIStack.second().recall)
    1.39 +			*mres = *m_HLIStack.second().pPrevRes;
    1.40  	}
    1.41  
    1.42  	void CSourceHookImpl::SetStatusPtr(META_RES *mres)
    1.43  	{
    1.44  		m_HLIStack.front().pStatus = mres;
    1.45 +
    1.46 +		// If we're recalling, drag the previous mres value to the new hookfunc
    1.47 +		if (m_HLIStack.size() > 1 && m_HLIStack.second().recall)
    1.48 +			*mres = *m_HLIStack.second().pStatus;
    1.49  	}
    1.50  
    1.51  	void CSourceHookImpl::SetIfacePtrPtr(void **pp)
    1.52 @@ -689,13 +712,61 @@
    1.53  	{
    1.54  		m_HLIStack.front().pOrigRet = ptr;
    1.55  	}
    1.56 -	void CSourceHookImpl::SetOverrideRetPtr(const void *ptr)
    1.57 +	void CSourceHookImpl::SetOverrideRetPtr(void *ptr)
    1.58  	{
    1.59  		m_HLIStack.front().pOverrideRet = ptr;
    1.60  	}
    1.61 +
    1.62 +	// New function which does all of the above + more :)
    1.63 +	void *CSourceHookImpl::SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
    1.64 +		void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr)
    1.65 +	{
    1.66 +		HookLoopInfo &hli = m_HLIStack.front();
    1.67 +		hli.pStatus = statusPtr;
    1.68 +		hli.pPrevRes = prevResPtr;
    1.69 +		hli.pCurRes = curResPtr;
    1.70 +		hli.pIfacePtrPtr = ifacePtrPtr;
    1.71 +		hli.pOrigRet = origRetPtr;
    1.72 +
    1.73 +		// Handle some recall stuff
    1.74 +		if (m_HLIStack.size() > 1 && m_HLIStack.second().recall)
    1.75 +		{
    1.76 +			HookLoopInfo &other = m_HLIStack.second();
    1.77 +			*statusPtr = *other.pStatus;
    1.78 +			*prevResPtr = *other.pStatus;
    1.79 +			hli.pOverrideRet = other.pOverrideRet;
    1.80 +		}
    1.81 +		else
    1.82 +			hli.pOverrideRet = overrideRetPtr;
    1.83 +
    1.84 +		// Tell the hook func which override ret ptr to use
    1.85 +		return hli.pOverrideRet;
    1.86 +	}
    1.87 +
    1.88  	bool CSourceHookImpl::ShouldContinue()
    1.89  	{
    1.90 -		return m_HLIStack.front().shouldContinue;
    1.91 +		// If recall is true, we shall not continue either.
    1.92 +		// This is because, if it's true and ShouldContinue is called, it suggests that the
    1.93 +		// actual recall is done and that we are back in the original handler which shall return
    1.94 +		// immediately.
    1.95 +
    1.96 +		return m_HLIStack.front().shouldContinue && !m_HLIStack.front().recall;
    1.97 +	}
    1.98 +
    1.99 +	void CSourceHookImpl::DoRecall()
   1.100 +	{
   1.101 +		if (!m_HLIStack.empty())
   1.102 +		{
   1.103 +			m_HLIStack.front().recall = true;
   1.104 +			CHookList *mlist = static_cast<CHookList*>(m_HLIStack.front().pCurIface->GetPreHooks());
   1.105 +			mlist->m_Recall = true;
   1.106 +
   1.107 +			// The hookfunc usually do this, but it won't have a chance to see it, 
   1.108 +			// so for recalls, we update status here if it's required
   1.109 +			if (*m_HLIStack.front().pCurRes > *m_HLIStack.front().pStatus) 
   1.110 +				*m_HLIStack.front().pStatus = *m_HLIStack.front().pCurRes;
   1.111 +
   1.112 +		}
   1.113  	}
   1.114  
   1.115  	////////////////////////////
   1.116 @@ -897,12 +968,15 @@
   1.117  	// CHookList
   1.118  	////////////////////////////
   1.119  
   1.120 -	CSourceHookImpl::CHookList::CHookList() : m_FreeIters(NULL), m_UsedIters(NULL)
   1.121 +	CSourceHookImpl::CHookList::CHookList() : m_FreeIters(NULL), m_UsedIters(NULL),
   1.122 +		m_Recall(false)
   1.123  	{
   1.124  	}
   1.125 -	CSourceHookImpl::CHookList::CHookList(const CHookList &other) : m_List(other.m_List), m_FreeIters(NULL), m_UsedIters(NULL)
   1.126 +	CSourceHookImpl::CHookList::CHookList(const CHookList &other) : m_List(other.m_List),
   1.127 +		m_FreeIters(NULL), m_UsedIters(NULL), m_Recall(false)
   1.128  	{
   1.129  	}
   1.130 +
   1.131  	CSourceHookImpl::CHookList::~CHookList()
   1.132  	{
   1.133  		while (m_FreeIters)
   1.134 @@ -932,12 +1006,22 @@
   1.135  			ret = new CIter(this);
   1.136  		}
   1.137  		
   1.138 +		// Muuuh, if we're recalling, it shall be a copy of the last iterator, incremented by one
   1.139 +		if (m_Recall && m_UsedIters)
   1.140 +		{
   1.141 +			ret->Set(m_UsedIters);		// m_UsedIters is the last returned and not released iterator
   1.142 +			ret->Next();				// Use next instead of directly incrementing its m_Iter:
   1.143 +										// skips paused plugins
   1.144 +		}
   1.145 +
   1.146  		ret->m_pNext = m_UsedIters;
   1.147  		ret->m_pPrev = NULL;
   1.148  		if (m_UsedIters)
   1.149  			m_UsedIters->m_pPrev = ret;
   1.150  		m_UsedIters = ret;
   1.151  
   1.152 +		m_Recall = false;
   1.153 +
   1.154  		return ret;
   1.155  	}
   1.156  	void CSourceHookImpl::CHookList::ReleaseIter(IIter *pIter)
   1.157 @@ -958,6 +1042,9 @@
   1.158  		pIter2->m_pNext = m_FreeIters;
   1.159  
   1.160  		m_FreeIters = pIter2;
   1.161 +
   1.162 +		// Reset recall state.
   1.163 +		m_Recall = false;
   1.164  	}
   1.165  
   1.166  	CSourceHookImpl::CHookList::CIter::CIter(CHookList *pList) : m_pList(pList), m_pNext(NULL)
   1.167 @@ -968,6 +1055,11 @@
   1.168  	{
   1.169  	}
   1.170  
   1.171 +	void CSourceHookImpl::CHookList::CIter::Set(CIter *pOther)
   1.172 +	{
   1.173 +		m_Iter = pOther->m_Iter;
   1.174 +	}
   1.175 +
   1.176  	void CSourceHookImpl::CHookList::CIter::GoToBegin()
   1.177  	{
   1.178  		m_Iter = m_pList->m_List.begin();