sourcehook/sourcehook.cpp
changeset 1 586787053c73
child 6 4803fd7d28bb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sourcehook/sourcehook.cpp	Thu Apr 14 12:54:13 2005 +0000
     1.3 @@ -0,0 +1,553 @@
     1.4 +/* ======== SourceHook ========
     1.5 +* By PM
     1.6 +* No warranties of any kind
     1.7 +* ============================
     1.8 +*/
     1.9 +
    1.10 +/**
    1.11 +*	@file sourcehook.cpp
    1.12 +*	@brief Contains the implementation of the SourceHook API
    1.13 +*/
    1.14 +
    1.15 +#include <algorithm>
    1.16 +#include "sourcehook_impl.h"
    1.17 +
    1.18 +#if SH_SYS == SH_SYS_WIN32
    1.19 +# include <windows.h>
    1.20 +#elif SH_SYS == SH_SYS_LINUX
    1.21 +# include <sys/mman.h>
    1.22 +# include <sys/limits.h>
    1.23 +# include <unistd.h>
    1.24 +# ifndef PAGESIZE
    1.25 +#  define PAGESIZE 4096
    1.26 +# endif
    1.27 +#endif
    1.28 +
    1.29 +#if SH_RUNTIME_CODEGEN == 1
    1.30 +# if SH_COMP == SH_COMP_GCC
    1.31 +#  define SH_CCC_CODESIZE 15
    1.32 +	//	 :TODO:
    1.33 +	//void SH_CCC_MakeGate(void *origthisptr, unsigned char* &ptr, void *origvtable, int i)
    1.34 +	//{
    1.35 +	//}
    1.36 +# elif SH_COMP == SH_COMP_MSVC
    1.37 +#  define SH_CCC_CODESIZE 19
    1.38 +	void SH_CCC_MakeGate(void *origthisptr, void *ccthisptr, unsigned char* ptr, void *origfunc)
    1.39 +	{
    1.40 +		// --Subtract old this pointer to get the offset
    1.41 +		// 00417EDF 81 E9 A1 7B 33 01 sub         ecx,1337BA1h 
    1.42 +		*ptr++ = 0x81;
    1.43 +		*ptr++ = 0xE9;
    1.44 +		*reinterpret_cast<void**>(ptr) = ccthisptr;
    1.45 +		ptr += sizeof(void*);
    1.46 +		// --Add it to the new this pointer
    1.47 +		// 00417EE5 81 C1 37 13 37 13 add         ecx,13371337h 
    1.48 +		*ptr++ = 0x81;
    1.49 +		*ptr++ = 0xC1;
    1.50 +		*reinterpret_cast<void**>(ptr) = origthisptr;
    1.51 +		ptr += sizeof(void*);
    1.52 +		// --Prepare address
    1.53 +		// 00417EEB B8 EF BE AD DE   mov         eax,0DEADBEEFh 
    1.54 +		*ptr++ = 0xB8;
    1.55 +		*reinterpret_cast<void**>(ptr) = origfunc;
    1.56 +		ptr += sizeof(void*);
    1.57 +		// -- Do actual jump
    1.58 +		// 00417EF0 FF E0            jmp         eax  
    1.59 +		*ptr++ = 0xFF;
    1.60 +		*ptr++ = 0xE0;
    1.61 +	}
    1.62 +# endif
    1.63 +#endif
    1.64 +
    1.65 +
    1.66 +namespace SourceHook
    1.67 +{
    1.68 +	CSourceHookImpl::CSourceHookImpl()
    1.69 +	{
    1.70 +		// Get page size
    1.71 +#if SH_SYS == SH_SYS_WIN32
    1.72 +		SYSTEM_INFO sysinfo;
    1.73 +		GetSystemInfo(&sysinfo);
    1.74 +		m_PageSize = sysinfo.dwPageSize;
    1.75 +#elif SH_SYS == SH_SYS_LINUX
    1.76 +		m_PageSize = PAGESIZE;
    1.77 +#else
    1.78 +# error Unsupported system
    1.79 +#endif
    1.80 +	}
    1.81 +
    1.82 +	CSourceHookImpl::~CSourceHookImpl()
    1.83 +	{
    1.84 +
    1.85 +	}
    1.86 +
    1.87 +	bool CSourceHookImpl::IsPluginInUse(Plugin plug)
    1.88 +	{
    1.89 +		// Iterate through all hookers which are in this plugin
    1.90 +		// Iterate through their hooks
    1.91 +		// If a hook from an other plugin is found, return true
    1.92 +		// Return false otherwise
    1.93 +
    1.94 +		for (HookerInfoList::iterator iter = m_Hookers.begin(); iter != m_Hookers.end(); ++iter)
    1.95 +		{
    1.96 +			if (iter->plug == plug && !iter->ifaces.empty())
    1.97 +			{
    1.98 +				for (std::list<HookerInfo::Iface>::iterator iter2 = iter->ifaces.begin(); iter2 != iter->ifaces.end(); ++iter2)
    1.99 +				{
   1.100 +					std::list<HookerInfo::Iface::Hook>::iterator iter3;
   1.101 +					for (iter3 = iter2->hooks_pre.begin(); iter3 != iter2->hooks_pre.end(); ++iter3)
   1.102 +						if (iter3->plug == plug)
   1.103 +							return true;
   1.104 +					for (iter3 = iter2->hooks_post.begin(); iter3 != iter2->hooks_post.end(); ++iter3)
   1.105 +						if (iter3->plug == plug)
   1.106 +							return true;
   1.107 +				}
   1.108 +			}
   1.109 +		}
   1.110 +		return false;
   1.111 +	}
   1.112 +
   1.113 +	void CSourceHookImpl::UnloadPlugin(Plugin plug)
   1.114 +	{
   1.115 +		// Get a list of hookers that are in this plugin and are used by other plugins
   1.116 +
   1.117 +		HookerInfoList tmphookers;
   1.118 +		bool erase = false;
   1.119 +		for (HookerInfoList::iterator iter = m_Hookers.begin(); iter != m_Hookers.end();
   1.120 +			erase ? iter=m_Hookers.erase(iter) : ++iter)
   1.121 +		{
   1.122 +			if (iter->plug == plug && !iter->ifaces.empty())
   1.123 +			{
   1.124 +				bool found = false;
   1.125 +				for (std::list<HookerInfo::Iface>::iterator iter2 = iter->ifaces.begin();
   1.126 +					iter2 != iter->ifaces.end(); ++iter2)
   1.127 +				{
   1.128 +					for (std::list<HookerInfo::Iface::Hook>::iterator iter3 = iter2->hooks_pre.begin();
   1.129 +						iter3 != iter2->hooks_pre.end(); ++iter3)
   1.130 +					{
   1.131 +						if (iter3->plug != plug)
   1.132 +						{
   1.133 +							found = true;
   1.134 +							break;
   1.135 +						}
   1.136 +					}
   1.137 +					for (iter3 = iter2->hooks_post.begin(); iter3 != iter2->hooks_post.end(); ++iter3)
   1.138 +					{
   1.139 +						if (iter3->plug != plug)
   1.140 +						{
   1.141 +							found = true;
   1.142 +							break;
   1.143 +						}
   1.144 +					}
   1.145 +					if (found)
   1.146 +					{
   1.147 +						tmphookers.push_back(*iter);
   1.148 +						break;
   1.149 +					}
   1.150 +				}
   1.151 +				erase = true;
   1.152 +			}
   1.153 +			else
   1.154 +				erase = false;
   1.155 +		}
   1.156 +		
   1.157 +		// For each hooker:
   1.158 +		for (HookerInfoList::iterator iter = tmphookers.begin(); iter != tmphookers.end(); ++iter)
   1.159 +		{
   1.160 +			// Shutdown all ifaces and remove the hooks from this plugin from their lists
   1.161 +			for (std::list<HookerInfo::Iface>::iterator iter2 = iter->ifaces.begin(); iter2 != iter->ifaces.end();
   1.162 +				erase ? iter2 = iter->ifaces.erase(iter2) : ++iter2)
   1.163 +			{
   1.164 +				*(reinterpret_cast<void**>(reinterpret_cast<char*>(iter2->ptr) + iter->vtbl_offs) 
   1.165 +					+ iter->vtbl_idx) = iter2->orig_entry;
   1.166 +
   1.167 +				for (std::list<HookerInfo::Iface::Hook>::iterator iter3 = iter2->hooks_pre.begin(); iter3 != iter2->hooks_pre.end();
   1.168 +					iter3->plug == plug ? iter3=iter2->hooks_pre.erase(iter3) : ++iter3) {}
   1.169 +				for (std::list<HookerInfo::Iface::Hook>::iterator iter3 = iter2->hooks_post.begin(); iter3 != iter2->hooks_post.end();
   1.170 +					iter3->plug == plug ? iter3=iter2->hooks_post.erase(iter3) : ++iter3) {}
   1.171 +				
   1.172 +				erase = iter2->hooks_pre.empty() && iter2->hooks_post.empty();
   1.173 +			}
   1.174 +			
   1.175 +			if (iter->ifaces.empty())
   1.176 +			{
   1.177 +				// Nothing more to do; no more ifaces to re-register
   1.178 +				continue;
   1.179 +			}
   1.180 +
   1.181 +			// 2) Find a suitable hooker in an other plugin
   1.182 +			HookerInfoList::iterator newHooker = FindHooker(m_Hookers.begin(), m_Hookers.end(),
   1.183 +				iter->proto, iter->vtbl_offs, iter->vtbl_idx, iter->thisptr_offs);
   1.184 +			if (newHooker == m_Hookers.end())
   1.185 +			{
   1.186 +				// This should _never_ happen.
   1.187 +				// If there is a hook from an other plugin, the plugin must have provided a hooker as well.
   1.188 +				SH_ASSERT(0);
   1.189 +			}
   1.190 +
   1.191 +			// AddHook should make sure that every plugin only has _one_ hooker for _one_ proto/vi/vo
   1.192 +			SH_ASSERT(newHooker->plug != plug);
   1.193 +
   1.194 +			// 3) Register it!
   1.195 +			newHooker->func(HA_Register, &(*iter));
   1.196 +			for (std::list<HookerInfo::Iface>::iterator iter2 = iter->ifaces.begin(); iter2 != iter->ifaces.end();
   1.197 +				++iter2)
   1.198 +			{
   1.199 +				reinterpret_cast<void**>(reinterpret_cast<char*>(iter2->ptr) + iter->vtbl_offs)[iter->vtbl_idx] =
   1.200 +					reinterpret_cast<void**>(reinterpret_cast<char*>(iter->hookfunc_inst) + iter->hookfunc_vtbl_offs)[iter->hookfunc_vtbl_idx];
   1.201 +			}
   1.202 +		}
   1.203 +	}
   1.204 +
   1.205 +	void CSourceHookImpl::CompleteShutdown()
   1.206 +	{
   1.207 +		// Go through all hookers and shut down all interfaces
   1.208 +		for (HookerInfoList::iterator iter = m_Hookers.begin(); iter != m_Hookers.end(); ++iter)
   1.209 +		{
   1.210 +			for (std::list<HookerInfo::Iface>::iterator iter2 = iter->ifaces.begin(); iter2 != iter->ifaces.end(); ++iter2)
   1.211 +			{
   1.212 +				// Note that we can pass iter->func as "myHooker" because it is only used
   1.213 +				// to retreive data
   1.214 +				for (std::list<HookerInfo::Iface::Hook>::iterator iter3 = iter2->hooks_pre.begin(); iter3 != iter2->hooks_pre.end(); ++iter3)
   1.215 +					RemoveHook(iter3->plug, iter2->ptr, iter->func, iter3->handler, false);
   1.216 +				for (std::list<HookerInfo::Iface::Hook>::iterator iter3 = iter2->hooks_post.begin(); iter3 != iter2->hooks_post.end(); ++iter3)
   1.217 +					RemoveHook(iter3->plug, iter2->ptr, iter->func, iter3->handler, true);
   1.218 +			}
   1.219 +		}
   1.220 +	}
   1.221 +
   1.222 +	bool CSourceHookImpl::AddHook(Plugin plug, void *iface, int ifacesize, Hooker myHooker, ISHDelegate *handler, bool post)
   1.223 +	{
   1.224 +		// 1) Get info about the hooker
   1.225 +		HookerInfo tmp;
   1.226 +		if (myHooker(HA_GetInfo, &tmp) != 0)
   1.227 +			return false;
   1.228 +
   1.229 +		// Add the proposed hooker to the _end_ of the list if the plugin doesn't have a hooker with this proto/vo/vi registered
   1.230 +		HookerInfoList::iterator hookeriter;
   1.231 +		for (hookeriter = m_Hookers.begin(); hookeriter != m_Hookers.end(); ++hookeriter)
   1.232 +		{
   1.233 +			if (hookeriter->plug == plug && strcmp(hookeriter->proto, tmp.proto) == 0 &&
   1.234 +				hookeriter->vtbl_offs == tmp.vtbl_offs && hookeriter->vtbl_idx == tmp.vtbl_idx &&
   1.235 +				hookeriter->thisptr_offs == tmp.thisptr_offs)
   1.236 +				break;
   1.237 +		}
   1.238 +		if (hookeriter == m_Hookers.end())
   1.239 +		{
   1.240 +			// No such hooker from this plugin yet, add it!
   1.241 +			tmp.func = myHooker;
   1.242 +			tmp.plug = plug;
   1.243 +			m_Hookers.push_back(tmp);
   1.244 +		}
   1.245 +
   1.246 +		// Then, search for a suitable hooker (from the beginning)
   1.247 +		HookerInfoList::iterator hooker = FindHooker(m_Hookers.begin(), m_Hookers.end(), tmp.proto, tmp.vtbl_offs, tmp.vtbl_idx, tmp.thisptr_offs);
   1.248 +		SH_ASSERT(hooker != m_Hookers.end());
   1.249 +
   1.250 +		// Tell it to store the pointer if it's not already active
   1.251 +		if (hooker->ifaces.empty())
   1.252 +			hooker->func(HA_Register, &(*hooker));
   1.253 +
   1.254 +		std::list<HookerInfo::Iface>::iterator ifsiter = std::find(hooker->ifaces.begin(), hooker->ifaces.end(), iface);
   1.255 +
   1.256 +		if (ifsiter == hooker->ifaces.end())
   1.257 +		{
   1.258 +			HookerInfo::Iface ifs;
   1.259 +			ifs.ptr = iface;
   1.260 +			ifs.callclass = GetCallClass(iface, ifacesize);
   1.261 +			void *vtableptr = *reinterpret_cast<void**>(reinterpret_cast<char*>(iface) + hooker->vtbl_offs);
   1.262 +			SetMemAccess(vtableptr, sizeof(void*) * hooker->vtbl_idx, SH_MEM_READ | SH_MEM_WRITE);
   1.263 +			ifs.orig_entry = (*reinterpret_cast<void***>(reinterpret_cast<char*>(iface) + hooker->vtbl_offs))[hooker->vtbl_idx];
   1.264 +			(*reinterpret_cast<void***>(reinterpret_cast<char*>(iface) + hooker->vtbl_offs))[hooker->vtbl_idx] =
   1.265 +				(*reinterpret_cast<void***>(reinterpret_cast<char*>(hooker->hookfunc_inst) + hooker->hookfunc_vtbl_offs))[hooker->hookfunc_vtbl_idx];
   1.266 +			hooker->ifaces.push_back(ifs);
   1.267 +			ifsiter = hooker->ifaces.end();
   1.268 +			--ifsiter;
   1.269 +		}
   1.270 +		HookerInfo::Iface::Hook hookinfo;
   1.271 +		hookinfo.handler = handler;
   1.272 +		hookinfo.plug = plug;
   1.273 +		if (post)
   1.274 +   			ifsiter->hooks_post.push_back(hookinfo);
   1.275 +		else
   1.276 +			ifsiter->hooks_pre.push_back(hookinfo);
   1.277 +
   1.278 +
   1.279 +		// Now that it is done, check whether we have to update any callclasses
   1.280 +		for (Impl_CallClassList::iterator cciter = m_CallClasses.begin(); cciter != m_CallClasses.end(); ++cciter)
   1.281 +		{
   1.282 +			if (cciter->iface == iface)
   1.283 +			{
   1.284 +				ApplyCallClassPatch(*cciter, tmp.vtbl_offs, tmp.vtbl_idx, ifsiter->orig_entry);
   1.285 +
   1.286 +				// We can assume that there is no more callclass with the same iface
   1.287 +				break;
   1.288 +			}
   1.289 +		}
   1.290 +
   1.291 +		return true;
   1.292 +	}
   1.293 +
   1.294 +	bool CSourceHookImpl::RemoveHook(Plugin plug, void *iface, Hooker myHooker, ISHDelegate *handler, bool post)
   1.295 +	{
   1.296 +		HookerInfo tmp;
   1.297 +		if (myHooker(HA_GetInfo, &tmp) != 0)
   1.298 +			return false;
   1.299 +
   1.300 +		// Find the hooker and the hook
   1.301 +		HookerInfoList::iterator hooker = FindHooker(m_Hookers.begin(), m_Hookers.end(), 
   1.302 +			tmp.proto, tmp.vtbl_offs, tmp.vtbl_idx, tmp.thisptr_offs);
   1.303 +		if (hooker == m_Hookers.end())
   1.304 +			return false;
   1.305 +
   1.306 +		for (std::list<HookerInfo::Iface>::iterator ifaceiter = hooker->ifaces.begin(); ifaceiter != hooker->ifaces.end(); ++ifaceiter)
   1.307 +		{
   1.308 +			if (ifaceiter->ptr == iface)
   1.309 +			{
   1.310 +				std::list<HookerInfo::Iface::Hook> &hooks = post ? ifaceiter->hooks_post : ifaceiter->hooks_pre;
   1.311 +				bool erase;
   1.312 +				for (std::list<HookerInfo::Iface::Hook>::iterator hookiter = hooks.begin();
   1.313 +					hookiter != hooks.end(); erase ? hookiter = hooks.erase(hookiter) : ++hookiter)
   1.314 +				{
   1.315 +					erase = hookiter->plug == plug && hookiter->handler->IsEqual(handler);
   1.316 +					if (erase)
   1.317 +						hookiter->handler->DeleteThis();			// Make the _plugin_ delete the handler object
   1.318 +				}
   1.319 +				if (ifaceiter->hooks_post.empty() && ifaceiter->hooks_pre.empty())
   1.320 +				{
   1.321 +					// Deactivate the hook
   1.322 +					(*reinterpret_cast<void***>(reinterpret_cast<char*>(ifaceiter->ptr) +
   1.323 +						hooker->vtbl_offs))[hooker->vtbl_idx]= ifaceiter->orig_entry;
   1.324 +
   1.325 +					// Release the callclass
   1.326 +					ReleaseCallClass(ifaceiter->callclass);
   1.327 +
   1.328 +					// Remove the iface info
   1.329 +					hooker->ifaces.erase(ifaceiter);
   1.330 +
   1.331 +					if (hooker->ifaces.empty())
   1.332 +						hooker->func(HA_Unregister, NULL);
   1.333 +				}
   1.334 +				// :TODO: Better return value? Or none?
   1.335 +				return true;
   1.336 +			}
   1.337 +		}
   1.338 +		return false;
   1.339 +	}
   1.340 +
   1.341 +	void *CSourceHookImpl::GetCallClass(void *iface, size_t size)
   1.342 +	{
   1.343 +		for (Impl_CallClassList::iterator cciter = m_CallClasses.begin(); cciter != m_CallClasses.end(); ++cciter)
   1.344 +		{
   1.345 +			if (cciter->iface == iface)
   1.346 +			{
   1.347 +				++cciter->refcounter;
   1.348 +				return cciter->ptr;
   1.349 +			}
   1.350 +		}
   1.351 +
   1.352 +		// The layout of callclasses is:
   1.353 +		//  Copy of the class; vtable entries pointing to gates
   1.354 +		//  Pointer to the corresponding CallClass object
   1.355 +		//  Copy of the class; vtable entries pointing to original functions
   1.356 +
   1.357 +		CallClass tmp;
   1.358 +		char *ptr = new char[size * 2 + sizeof(void*)];
   1.359 +		tmp.ptr = reinterpret_cast<void*>(ptr);
   1.360 +		memcpy(reinterpret_cast<void*>(ptr), iface, size);
   1.361 +		memcpy(reinterpret_cast<void*>(ptr + size + sizeof(void*)), iface, size);
   1.362 +		tmp.iface = iface;
   1.363 +		tmp.size = size;
   1.364 +		tmp.refcounter = 1;
   1.365 +
   1.366 +		// Go through _all_ hooks and apply any needed patches
   1.367 +		for (HookerInfoList::iterator hooker = m_Hookers.begin(); hooker != m_Hookers.end(); ++hooker)
   1.368 +		{
   1.369 +			for (std::list<HookerInfo::Iface>::iterator ifaceiter = hooker->ifaces.begin(); ifaceiter != hooker->ifaces.end(); ++ifaceiter)
   1.370 +			{
   1.371 +				if (ifaceiter->ptr == iface)
   1.372 +				{
   1.373 +					if (!ApplyCallClassPatch(tmp, hooker->vtbl_offs, hooker->vtbl_idx, ifaceiter->orig_entry))
   1.374 +					{
   1.375 +						FreeCallClass(tmp);
   1.376 +						return NULL;
   1.377 +					}
   1.378 +				}
   1.379 +			}
   1.380 +		}
   1.381 +		m_CallClasses.push_back(tmp);
   1.382 +		// The object has to be followed by the pointer to the vtable info object
   1.383 +		*reinterpret_cast<void**>(ptr + size) = &m_CallClasses.back();
   1.384 +		return tmp.ptr;
   1.385 +	}
   1.386 +
   1.387 +	void CSourceHookImpl::ReleaseCallClass(void *ptr)
   1.388 +	{
   1.389 +		Impl_CallClassList::iterator iter = std::find(m_CallClasses.begin(), m_CallClasses.end(), ptr);
   1.390 +		if (iter == m_CallClasses.end())
   1.391 +			return;
   1.392 +		--iter->refcounter;
   1.393 +		if (iter->refcounter < 1)
   1.394 +		{
   1.395 +			FreeCallClass(*iter);
   1.396 +			m_CallClasses.erase(iter);
   1.397 +		}
   1.398 +	}
   1.399 +
   1.400 +	void CSourceHookImpl::FreeCallClass(CallClass &cc)
   1.401 +	{
   1.402 +		for (CallClass::VTableList::iterator vtbliter = cc.vtables.begin(); vtbliter != cc.vtables.end(); ++vtbliter)
   1.403 +		{
   1.404 +#if SH_RUNTIME_CODEGEN == 1
   1.405 +			// Delete generated callgates
   1.406 +			for (std::list<int>::iterator cgiter = vtbliter->patches.begin(); cgiter != vtbliter->patches.end(); ++cgiter)
   1.407 +			{
   1.408 +				char *cgptr = reinterpret_cast<char**>(vtbliter->ptr)[*cgiter];
   1.409 +				delete [] cgptr;
   1.410 +			}
   1.411 +#endif
   1.412 +			delete [] reinterpret_cast<char*>(vtbliter->ptr);
   1.413 +		}
   1.414 +		delete [] reinterpret_cast<char*>(cc.ptr);
   1.415 +	}
   1.416 +
   1.417 +	bool CSourceHookImpl::ApplyCallClassPatch(CallClass &cc, int vtbl_offs, int vtbl_idx, void *orig_entry)
   1.418 +	{
   1.419 +		char *ptr = reinterpret_cast<char*>(cc.ptr);
   1.420 +		void *vtable = *reinterpret_cast<void**>(ptr + vtbl_offs);
   1.421 +
   1.422 +		// Check whether we already know that vtable
   1.423 +		CallClass::VTableList::iterator vtbliter = std::find(cc.vtables.begin(),
   1.424 +			cc.vtables.end(), reinterpret_cast<void*>(vtable));
   1.425 +		int actvtablesize=MAX_VTABLE_LEN;
   1.426 +		if (vtbliter == cc.vtables.end())
   1.427 +		{
   1.428 +			CallClass::VTable newvtableinfo;
   1.429 +			int pagesnum = (MAX_VTABLE_LEN % m_PageSize == 0) ? MAX_VTABLE_LEN / m_PageSize : MAX_VTABLE_LEN / m_PageSize + 1;
   1.430 +			// Set all pages in the range to readwrite
   1.431 +			// :TODO: Fix this!
   1.432 +			char *pagebegin = reinterpret_cast<char*>(vtable) - (reinterpret_cast<int>(vtable) % m_PageSize);
   1.433 +			for (int ipage = 0; ipage < pagesnum; ++ipage)
   1.434 +			{
   1.435 +				if (!SetMemAccess(reinterpret_cast<void*>(pagebegin + ipage * m_PageSize), 1,
   1.436 +					SH_MEM_READ | SH_MEM_WRITE))
   1.437 +				{
   1.438 +					// We can't go here anymore
   1.439 +					actvtablesize = static_cast<int>((pagebegin + ipage * m_PageSize) - reinterpret_cast<char*>(vtable));
   1.440 +					break;
   1.441 +				}
   1.442 +			}
   1.443 +			if (actvtablesize < 1)
   1.444 +			{
   1.445 +				// We can't access the vtable -> Quit
   1.446 +				return false;
   1.447 +			}
   1.448 +			// Create a new vtable
   1.449 +			newvtableinfo.ptr = reinterpret_cast<void*>(new char[actvtablesize]);
   1.450 +			// Fill it with the information from the old vtable
   1.451 +			memcpy(newvtableinfo.ptr, vtable, actvtablesize);
   1.452 +			newvtableinfo.actsize = actvtablesize;
   1.453 +			// Set the pointer in the object and add it to the list of already known vtables
   1.454 +			*reinterpret_cast<void**>(ptr + vtbl_offs) = newvtableinfo.ptr;
   1.455 +			cc.vtables.push_back(newvtableinfo);
   1.456 +
   1.457 +			vtbliter = cc.vtables.end();
   1.458 +			--vtbliter;
   1.459 +			// :TODO: When not codegen, patch the other vtable?
   1.460 +		}
   1.461 +
   1.462 +		// Check whether we already have this patch
   1.463 +		if (std::find(vtbliter->patches.begin(), vtbliter->patches.end(), vtbl_idx) == vtbliter->patches.end())
   1.464 +		{
   1.465 +			// No -> apply it
   1.466 +
   1.467 +			// Get a call gate
   1.468 +			void *callgate = NULL;
   1.469 +#if SH_RUNTIME_CODEGEN == 1
   1.470 +			unsigned char *cggen = new unsigned char[SH_CCC_CODESIZE];
   1.471 +			SH_CCC_MakeGate(cc.iface, cc.ptr, cggen, orig_entry);
   1.472 +			callgate = (void*)cggen;
   1.473 +			SetMemAccess(callgate, SH_CCC_CODESIZE, SH_MEM_READ | SH_MEM_WRITE | SH_MEM_EXEC);
   1.474 +#else
   1.475 +			// :TODO:
   1.476 +#error Not supported yet!
   1.477 +#endif
   1.478 +			vtbliter->patches.push_back(vtbl_idx);
   1.479 +			reinterpret_cast<void**>(vtbliter->ptr)[vtbl_idx] = callgate;
   1.480 +		}
   1.481 +		return true;
   1.482 +	}
   1.483 +
   1.484 +
   1.485 +	CSourceHookImpl::HookerInfoList::iterator CSourceHookImpl::FindHooker(HookerInfoList::iterator begin,
   1.486 +		HookerInfoList::iterator end, const char *proto, int vtblofs, int vtblidx, int thisptrofs)
   1.487 +	{
   1.488 +		for (HookerInfoList::iterator hookeriter = m_Hookers.begin(); hookeriter != m_Hookers.end(); ++hookeriter)
   1.489 +		{
   1.490 +			if (strcmp(hookeriter->proto, proto) == 0 && hookeriter->vtbl_offs == vtblofs && hookeriter->vtbl_idx == vtblidx &&
   1.491 +				hookeriter->thisptr_offs == thisptrofs)
   1.492 +				break;
   1.493 +		}
   1.494 +		return hookeriter;
   1.495 +	}
   1.496 +
   1.497 +
   1.498 +	void CSourceHookImpl::SetRes(META_RES res)
   1.499 +	{
   1.500 +		m_CurRes = res;
   1.501 +	}
   1.502 +
   1.503 +	META_RES CSourceHookImpl::GetPrevRes()
   1.504 +	{
   1.505 +		return m_PrevRes;
   1.506 +	}
   1.507 +
   1.508 +	META_RES CSourceHookImpl::GetStatus()
   1.509 +	{
   1.510 +		return m_Status;
   1.511 +	}
   1.512 +
   1.513 +	const void *CSourceHookImpl::GetOrigRet()
   1.514 +	{
   1.515 +		return m_OrigRet;
   1.516 +	}
   1.517 +
   1.518 +	const void *CSourceHookImpl::GetOverrideRet()
   1.519 +	{
   1.520 +		return m_OverrideRet;
   1.521 +	}
   1.522 +
   1.523 +	META_RES &CSourceHookImpl::GetCurResRef()
   1.524 +	{
   1.525 +		return m_CurRes;
   1.526 +	}
   1.527 +
   1.528 +	META_RES &CSourceHookImpl::GetPrevResRef()
   1.529 +	{
   1.530 +		return m_PrevRes;
   1.531 +	}
   1.532 +
   1.533 +	META_RES &CSourceHookImpl::GetStatusRef()
   1.534 +	{
   1.535 +		return m_Status;
   1.536 +	}
   1.537 +
   1.538 +	void CSourceHookImpl::SetOrigRet(const void *ptr)
   1.539 +	{
   1.540 +		m_OrigRet = ptr;
   1.541 +	}
   1.542 +
   1.543 +	void CSourceHookImpl::SetOverrideRet(const void *ptr)
   1.544 +	{
   1.545 +		m_OverrideRet = ptr;
   1.546 +	}
   1.547 +
   1.548 +	void CSourceHookImpl::SetIfacePtr(void *ptr)
   1.549 +	{
   1.550 +		m_IfacePtr = ptr;
   1.551 +	}
   1.552 +	void *CSourceHookImpl::GetIfacePtr()
   1.553 +	{
   1.554 +		return m_IfacePtr;
   1.555 +	}
   1.556 +}
   1.557 \ No newline at end of file