sourcehook/sourcehook_hookmangen.cpp
branchhookman_autogen
changeset 455 deec66cea8cc
parent 452 2efe13dab607
child 456 9f19985c76aa
     1.1 --- a/sourcehook/sourcehook_hookmangen.cpp	Fri Nov 09 21:14:37 2007 +0000
     1.2 +++ b/sourcehook/sourcehook_hookmangen.cpp	Sat Nov 10 16:10:55 2007 +0000
     1.3 @@ -15,6 +15,7 @@
     1.4  // http://www.angelcode.com/dev/callconv/callconv.html
     1.5  // http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_6/CH06-1.html
     1.6  
     1.7 +#include "sourcehook_impl.h"
     1.8  #include "sourcehook_hookmangen.h"
     1.9  #include "sourcehook_hookmangen_x86.h"
    1.10  #include "sh_memory.h"
    1.11 @@ -47,7 +48,7 @@
    1.12  		}
    1.13  
    1.14  		GenContext::GenContext(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx, ISourceHook *pSHPtr)
    1.15 -			: m_Proto(proto), m_VtblOffs(vtbl_offs), m_VtblIdx(vtbl_idx), m_SHPtr(pSHPtr),
    1.16 +			: m_GeneratedPubFunc(NULL), m_Proto(proto), m_VtblOffs(vtbl_offs), m_VtblIdx(vtbl_idx), m_SHPtr(pSHPtr),
    1.17  			  m_pHI(NULL), m_HookfuncVfnptr(NULL), m_RegCounter(0)
    1.18  		{
    1.19  			m_pHI = new void*;
    1.20 @@ -1652,5 +1653,91 @@
    1.21  			GenerateHookFunc();
    1.22  			return fastdelegate::detail::horrible_cast<HookManagerPubFunc>(GeneratePubFunc());
    1.23  		}
    1.24 +
    1.25 +		HookManagerPubFunc GenContext::GetPubFunc()
    1.26 +		{
    1.27 +			if (m_GeneratedPubFunc == 0)
    1.28 +				m_GeneratedPubFunc = Generate();
    1.29 +
    1.30 +			return m_GeneratedPubFunc;
    1.31 +		}
    1.32 +
    1.33 +		bool GenContext::Equal(const CProto &proto, int vtbl_offs, int vtbl_idx)
    1.34 +		{
    1.35 +			return (m_Proto.ExactlyEqual(proto) && m_VtblOffs == vtbl_offs && m_VtblIdx == vtbl_idx);
    1.36 +		}
    1.37 +
    1.38 +		bool GenContext::Equal(HookManagerPubFunc other)
    1.39 +		{
    1.40 +			return m_GeneratedPubFunc == other;
    1.41 +		}
    1.42 +
    1.43 +		// *********************************** class GenContextContainer
    1.44 +		CHookManagerAutoGen::CHookManagerAutoGen(ISourceHook *pSHPtr) : m_pSHPtr(pSHPtr)
    1.45 +		{
    1.46 +		}
    1.47 +
    1.48 +		CHookManagerAutoGen::~CHookManagerAutoGen()
    1.49 +		{
    1.50 +			for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
    1.51 +			{
    1.52 +				delete iter->m_GenContext;
    1.53 +			}
    1.54 +		}
    1.55 +
    1.56 +		int CHookManagerAutoGen::GetIfaceVersion()
    1.57 +		{
    1.58 +			return SH_HOOKMANAUTOGEN_IFACE_VERSION;
    1.59 +		}
    1.60 +
    1.61 +		int CHookManagerAutoGen::GetImplVersion()
    1.62 +		{
    1.63 +			return SH_HOOKMANAUTOGEN_IMPL_VERSION;
    1.64 +		}
    1.65 +
    1.66 +		HookManagerPubFunc CHookManagerAutoGen::MakeHookMan(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx)
    1.67 +		{
    1.68 +			CProto mproto(proto);
    1.69 +			for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
    1.70 +			{
    1.71 +				if (iter->m_GenContext->Equal(mproto, vtbl_offs, vtbl_idx))
    1.72 +				{
    1.73 +					iter->m_RefCnt++;
    1.74 +					return iter->m_GenContext->GetPubFunc();
    1.75 +				}
    1.76 +			}
    1.77 +
    1.78 +			// Not found yet -> new one
    1.79 +			StoredContext sctx;
    1.80 +			sctx.m_RefCnt = 1;
    1.81 +			sctx.m_GenContext = new GenContext(proto, vtbl_offs, vtbl_idx, m_pSHPtr);
    1.82 +
    1.83 +			if (sctx.m_GenContext->GetPubFunc() == NULL)
    1.84 +			{
    1.85 +				return NULL;
    1.86 +			}
    1.87 +			else
    1.88 +			{
    1.89 +				m_Contexts.push_back(sctx);
    1.90 +				return sctx.m_GenContext->GetPubFunc();
    1.91 +			}
    1.92 +		}
    1.93 +
    1.94 +		void CHookManagerAutoGen::ReleaseHookMan(HookManagerPubFunc pubFunc)
    1.95 +		{
    1.96 +			for (List<StoredContext>::iterator iter = m_Contexts.begin(); iter != m_Contexts.end(); ++iter)
    1.97 +			{
    1.98 +				if (iter->m_GenContext->Equal(pubFunc))
    1.99 +				{
   1.100 +					iter->m_RefCnt--;
   1.101 +					if (iter->m_RefCnt == 0)
   1.102 +					{
   1.103 +						delete iter->m_GenContext;
   1.104 +						m_Contexts.erase(iter);
   1.105 +					}
   1.106 +					break;
   1.107 +				}
   1.108 +			}
   1.109 +		}
   1.110  	}
   1.111  }