1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/sourcehook/sourcehook.cpp Fri Nov 14 04:04:02 2008 -0600
1.3 @@ -0,0 +1,1093 @@
1.4 +/* ======== SourceHook ========
1.5 +* Copyright (C) 2004-2008 Metamod:Source Development Team
1.6 +* No warranties of any kind
1.7 +*
1.8 +* License: zlib/libpng
1.9 +*
1.10 +* Author(s): Pavol "PM OnoTo" Marko
1.11 +* Contributors: Scott "Damaged Soul" Ehlert
1.12 +* ============================
1.13 +*/
1.14 +
1.15 +/**
1.16 + * @file sourcehook.cpp
1.17 + * @brief Contains the implementation of the SourceHook API
1.18 +*/
1.19 +
1.20 +#if defined __GNUC__
1.21 +#include <stdint.h>
1.22 +#endif
1.23 +
1.24 +#include "sourcehook_impl.h"
1.25 +
1.26 +namespace SourceHook
1.27 +{
1.28 + template<>
1.29 + int HashFunction<int>(const int & k)
1.30 + {
1.31 + return k;
1.32 + }
1.33 + template<>
1.34 + int Compare<int>(const int & k1, const int & k2)
1.35 + {
1.36 + if (k1 == k2)
1.37 + return 0;
1.38 + if (k1 > k2)
1.39 + return 1;
1.40 + if (k1 < k2)
1.41 + return -1;
1.42 + return 0;
1.43 + }
1.44 +
1.45 + namespace Impl
1.46 + {
1.47 + //////////////////////////////////////////////////////////////////////////
1.48 + // CProto
1.49 + //////////////////////////////////////////////////////////////////////////
1.50 + void CProto::Fill(const ProtoInfo *pProto)
1.51 + {
1.52 + if (pProto == NULL)
1.53 + m_Version = -1;
1.54 +
1.55 + m_ParamsPassInfo.clear();
1.56 +
1.57 + if (pProto->paramsPassInfo[0].size == 0)
1.58 + {
1.59 + // Version 1
1.60 + m_Version = 0;
1.61 + m_Convention = pProto->convention;
1.62 + m_NumOfParams = pProto->numOfParams;
1.63 +
1.64 + m_RetPassInfo.size = pProto->retPassInfo.size;
1.65 + m_RetPassInfo.type = pProto->retPassInfo.type;
1.66 + m_RetPassInfo.flags = GetRealFlags(pProto->retPassInfo);
1.67 +
1.68 + m_RetPassInfo.pNormalCtor = NULL;
1.69 + m_RetPassInfo.pCopyCtor = NULL;
1.70 + m_RetPassInfo.pDtor = NULL;
1.71 + m_RetPassInfo.pAssignOperator = NULL;
1.72 +
1.73 +
1.74 + m_ParamsPassInfo.resize(pProto->numOfParams);
1.75 +
1.76 + for (int i = 1; i <= pProto->numOfParams; ++i)
1.77 + {
1.78 + m_ParamsPassInfo[i-1].size = pProto->paramsPassInfo[i].size;
1.79 + m_ParamsPassInfo[i-1].type = pProto->paramsPassInfo[i].type;
1.80 + m_ParamsPassInfo[i-1].flags = GetRealFlags(pProto->paramsPassInfo[i]);
1.81 +
1.82 + m_ParamsPassInfo[i-1].pNormalCtor = NULL;
1.83 + m_ParamsPassInfo[i-1].pCopyCtor = NULL;
1.84 + m_ParamsPassInfo[i-1].pDtor = NULL;
1.85 + m_ParamsPassInfo[i-1].pAssignOperator = NULL;
1.86 + }
1.87 + }
1.88 + else if (pProto->paramsPassInfo[0].size == 1)
1.89 + {
1.90 + // Version 2
1.91 + m_Version = 1;
1.92 + m_Convention = pProto->convention;
1.93 + m_NumOfParams = pProto->numOfParams;
1.94 +
1.95 + m_RetPassInfo.size = pProto->retPassInfo.size;
1.96 + m_RetPassInfo.type = pProto->retPassInfo.type;
1.97 + m_RetPassInfo.flags = pProto->retPassInfo.flags;
1.98 +
1.99 + m_RetPassInfo.pNormalCtor = pProto->retPassInfo2.pNormalCtor;
1.100 + m_RetPassInfo.pCopyCtor = pProto->retPassInfo2.pCopyCtor;
1.101 + m_RetPassInfo.pDtor = pProto->retPassInfo2.pDtor;
1.102 + m_RetPassInfo.pAssignOperator = pProto->retPassInfo2.pAssignOperator;
1.103 +
1.104 + m_ParamsPassInfo.resize(pProto->numOfParams);
1.105 +
1.106 + for (int i = 1; i <= pProto->numOfParams; ++i)
1.107 + {
1.108 + m_ParamsPassInfo[i-1].size = pProto->paramsPassInfo[i].size;
1.109 + m_ParamsPassInfo[i-1].type = pProto->paramsPassInfo[i].type;
1.110 + m_ParamsPassInfo[i-1].flags = pProto->paramsPassInfo[i].flags;
1.111 +
1.112 + m_ParamsPassInfo[i-1].pNormalCtor = pProto->paramsPassInfo2[i].pNormalCtor;
1.113 + m_ParamsPassInfo[i-1].pCopyCtor = pProto->paramsPassInfo2[i].pCopyCtor;
1.114 + m_ParamsPassInfo[i-1].pDtor = pProto->paramsPassInfo2[i].pDtor;
1.115 + m_ParamsPassInfo[i-1].pAssignOperator = pProto->paramsPassInfo2[i].pAssignOperator;
1.116 + }
1.117 + }
1.118 + else
1.119 + {
1.120 + // Unknown
1.121 + m_Version = -1;
1.122 + }
1.123 + }
1.124 +
1.125 + // Basic compat test
1.126 + // Other than this, we assume that the plugins know what they're doing
1.127 + bool CProto::operator == (const CProto &other) const
1.128 + {
1.129 + if (m_Version < 0 || other.GetVersion() < 0)
1.130 + return false;
1.131 +
1.132 + if (m_NumOfParams != other.GetNumOfParams())
1.133 + return false;
1.134 +
1.135 + if (m_Convention != ProtoInfo::CallConv_Unknown && other.GetConvention() != ProtoInfo::CallConv_Unknown &&
1.136 + m_Convention != other.GetConvention())
1.137 + return false;
1.138 +
1.139 + if (GetRealSize(GetRet()) != GetRealSize(other.GetRet()))
1.140 + return false;
1.141 +
1.142 + for (int i = 0; i < m_NumOfParams; ++i)
1.143 + {
1.144 + if (GetRealSize(GetParam(i)) != GetRealSize(other.GetParam(i)))
1.145 + return false;
1.146 + if (GetParam(i).type != PassInfo::PassType_Unknown && other.GetParam(i).type != PassInfo::PassType_Unknown)
1.147 + {
1.148 + if (GetParam(i).type != other.GetParam(i).type)
1.149 + return false;
1.150 + if (GetParam(i).flags != other.GetParam(i).flags)
1.151 + return false;
1.152 + }
1.153 + }
1.154 +
1.155 + return true;
1.156 + }
1.157 +
1.158 + bool CProto::ExactlyEqual(const CProto &other) const
1.159 + {
1.160 + if (m_Version != other.m_Version ||
1.161 + m_NumOfParams != other.m_NumOfParams ||
1.162 + m_Convention != other.m_Convention ||
1.163 + GetRet() != other.GetRet())
1.164 + {
1.165 + return false;
1.166 + }
1.167 +
1.168 + for (int i = 0; i < m_NumOfParams; ++i)
1.169 + {
1.170 + if(GetParam(i) != other.GetParam(i))
1.171 + return false;
1.172 + }
1.173 +
1.174 + return true;
1.175 + }
1.176 +
1.177 + //////////////////////////////////////////////////////////////////////////
1.178 + // CHookManager
1.179 + //////////////////////////////////////////////////////////////////////////
1.180 + void CHookManager::SetInfo(int hookman_version, int vtbloffs, int vtblidx,
1.181 + ProtoInfo *proto, void *hookfunc_vfnptr)
1.182 + {
1.183 + m_Version = hookman_version;
1.184 + m_VtblOffs = vtbloffs;
1.185 + m_VtblIdx = vtblidx;
1.186 + m_Proto = proto;
1.187 + m_HookfuncVfnptr = hookfunc_vfnptr;
1.188 + }
1.189 +
1.190 + //////////////////////////////////////////////////////////////////////////
1.191 + // CVfnPtrList
1.192 + //////////////////////////////////////////////////////////////////////////
1.193 +
1.194 + CVfnPtr &CVfnPtrList::GetVfnPtr(void *vfnptr)
1.195 + {
1.196 + iterator iter = find(vfnptr);
1.197 + if (iter == end())
1.198 + {
1.199 + CVfnPtr newVfnPtr(vfnptr);
1.200 + push_back(newVfnPtr);
1.201 +
1.202 + return back();
1.203 + }
1.204 + else
1.205 + {
1.206 + return *iter;
1.207 + }
1.208 + }
1.209 + //////////////////////////////////////////////////////////////////////////
1.210 + // CVfnPtr
1.211 + //////////////////////////////////////////////////////////////////////////
1.212 +
1.213 + void CVfnPtr::AddHookMan(CHookManager *pHookMan)
1.214 + {
1.215 + List<CHookManager*>::iterator iter;
1.216 +
1.217 + // Don't accept invalid hook managers
1.218 + if (!*pHookMan)
1.219 + return;
1.220 +
1.221 + // Check whether this hook manager already exists; if yes, ignore.
1.222 + iter = m_HookMans.find(pHookMan);
1.223 + if (iter != m_HookMans.end())
1.224 + return;
1.225 +
1.226 + // It doesn't -> add it. Add it to the end of its version group.
1.227 + for (iter = m_HookMans.begin(); iter != m_HookMans.end(); ++iter)
1.228 + {
1.229 + if ((*iter)->GetVersion() < pHookMan->GetVersion())
1.230 + break;
1.231 + }
1.232 +
1.233 + bool isBeginning = iter == m_HookMans.begin();
1.234 +
1.235 + m_HookMans.insert(iter, pHookMan);
1.236 +
1.237 + if (isBeginning)
1.238 + {
1.239 + pHookMan->IncrRef(this);
1.240 + if (m_HookMans.size() > 1)
1.241 + {
1.242 + // If another hookman was used until now but this one is better
1.243 + // (which it is because it's the first -> it has a higher version)
1.244 + // -> switch!
1.245 +
1.246 + List<CHookManager*>::iterator second = m_HookMans.begin();
1.247 + ++second;
1.248 +
1.249 + (*second)->DecrRef(this);
1.250 + }
1.251 +
1.252 + // Make sure that this vfnptr points at it
1.253 + Patch(pHookMan->GetHookFunc());
1.254 + }
1.255 + }
1.256 +
1.257 + bool CVfnPtr::HookManRemoved(CHookManager *pHookMan)
1.258 + {
1.259 + // Don't accept invalid hook managers
1.260 + if (!*pHookMan)
1.261 + return true;
1.262 +
1.263 + List<CHookManager*>::iterator iter = m_HookMans.find(pHookMan);
1.264 + if (iter == m_HookMans.end())
1.265 + return true; // Didn't exist here anyway
1.266 +
1.267 + if (iter == m_HookMans.begin())
1.268 + {
1.269 + // It is the first one!
1.270 + pHookMan->DecrRef(this);
1.271 + m_HookMans.erase(iter);
1.272 +
1.273 + if (m_HookMans.empty())
1.274 + return false; // No more hookmans -> let SH delete us
1.275 +
1.276 + // Activate second -> now first hookman
1.277 + m_HookMans.front()->IncrRef(this);
1.278 + Patch(m_HookMans.front()->GetHookFunc());
1.279 + }
1.280 + else
1.281 + {
1.282 + m_HookMans.erase(iter);
1.283 + }
1.284 + return true;
1.285 + }
1.286 +
1.287 + bool CVfnPtr::Patch(void *newValue)
1.288 + {
1.289 + if (!SetMemAccess(m_Ptr, sizeof(void*), SH_MEM_READ | SH_MEM_WRITE))
1.290 + {
1.291 + return false;
1.292 + }
1.293 +
1.294 + *reinterpret_cast<void**>(m_Ptr) = newValue;
1.295 +
1.296 + return true;
1.297 + }
1.298 +
1.299 + CIface &CVfnPtr::GetIface(void *iface)
1.300 + {
1.301 + List<CIface>::iterator iter = m_IfaceList.find(iface);
1.302 + if (iter == m_IfaceList.end())
1.303 + {
1.304 + CIface newIface(iface);
1.305 + if (iface == NULL)
1.306 + {
1.307 + m_IfaceList.push_front(newIface);
1.308 + return m_IfaceList.front();
1.309 + }
1.310 + else
1.311 + {
1.312 + m_IfaceList.push_back(newIface);
1.313 + return m_IfaceList.back();
1.314 + }
1.315 + }
1.316 + else
1.317 + {
1.318 + return *iter;
1.319 + }
1.320 + }
1.321 +
1.322 + //////////////////////////////////////////////////////////////////////////
1.323 + // CHookIdManager
1.324 + //////////////////////////////////////////////////////////////////////////
1.325 + CHookIDManager::CHookIDManager()
1.326 + {
1.327 + }
1.328 +
1.329 + int CHookIDManager::New(const CProto &proto, int vtbl_offs, int vtbl_idx, void *vfnptr,
1.330 + void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
1.331 + {
1.332 + Entry tmp(proto, vtbl_offs, vtbl_idx, vfnptr, adjustediface, plug, thisptr_offs, handler, post);
1.333 +
1.334 + size_t cursize = m_Entries.size();
1.335 + for (size_t i = 0; i < cursize; ++i)
1.336 + {
1.337 + if (m_Entries[i].isfree)
1.338 + {
1.339 + m_Entries[i] = tmp;
1.340 + return static_cast<int>(i) + 1;
1.341 + }
1.342 + }
1.343 +
1.344 + m_Entries.push_back(tmp);
1.345 + return static_cast<int>(m_Entries.size()); // return size() because hookid = id+1 anyway
1.346 + }
1.347 +
1.348 + bool CHookIDManager::Remove(int hookid)
1.349 + {
1.350 + int realid = hookid - 1;
1.351 + if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
1.352 + return false;
1.353 +
1.354 + m_Entries[realid].isfree = true;
1.355 +
1.356 + // :TODO: remove free ids from back sometimes ??
1.357 +
1.358 + return true;
1.359 + }
1.360 +
1.361 + const CHookIDManager::Entry * CHookIDManager::QueryHook(int hookid)
1.362 + {
1.363 + int realid = hookid - 1;
1.364 + if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
1.365 + return NULL;
1.366 +
1.367 + return &m_Entries[realid];
1.368 + }
1.369 +
1.370 + void CHookIDManager::FindAllHooks(CVector<int> &output, const CProto &proto, int vtbl_offs,
1.371 + int vtbl_idx, void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
1.372 + {
1.373 + // oh my god, a lot of parameters...
1.374 + size_t cursize = m_Entries.size();
1.375 + for (size_t i = 0; i < cursize; ++i)
1.376 + {
1.377 + if (!m_Entries[i].isfree && m_Entries[i].proto == proto && m_Entries[i].vtbl_offs == vtbl_offs &&
1.378 + m_Entries[i].vtbl_idx == vtbl_idx && m_Entries[i].adjustediface == adjustediface && m_Entries[i].plug == plug &&
1.379 + m_Entries[i].thisptr_offs == thisptr_offs && m_Entries[i].handler->IsEqual(handler) && m_Entries[i].post == post)
1.380 + {
1.381 + output.push_back(static_cast<int>(i) + 1);
1.382 + }
1.383 + }
1.384 + }
1.385 +
1.386 + void CHookIDManager::FindAllHooks(CVector<int> &output)
1.387 + {
1.388 + size_t cursize = m_Entries.size();
1.389 + for (size_t i = 0; i < cursize; ++i)
1.390 + {
1.391 + if (!m_Entries[i].isfree)
1.392 + output.push_back(static_cast<int>(i) + 1);
1.393 + }
1.394 + }
1.395 +
1.396 + void CHookIDManager::FindAllHooks(CVector<int> &output, Plugin plug)
1.397 + {
1.398 + size_t cursize = m_Entries.size();
1.399 + for (size_t i = 0; i < cursize; ++i)
1.400 + {
1.401 + if (!m_Entries[i].isfree && m_Entries[i].plug == plug)
1.402 + output.push_back(static_cast<int>(i) + 1);
1.403 + }
1.404 + }
1.405 +
1.406 +
1.407 + void CHookIDManager::RemoveAll(void *vfnptr)
1.408 + {
1.409 + size_t cursize = m_Entries.size();
1.410 + for (size_t i = 0; i < cursize; ++i)
1.411 + {
1.412 + if (!m_Entries[i].isfree && m_Entries[i].vfnptr == vfnptr)
1.413 + m_Entries[i].isfree = true;
1.414 + }
1.415 + }
1.416 +
1.417 + //////////////////////////////////////////////////////////////////////////
1.418 + // CSourceHookImpl
1.419 + //////////////////////////////////////////////////////////////////////////
1.420 +
1.421 +
1.422 + CSourceHookImpl::CSourceHookImpl()
1.423 + {
1.424 + }
1.425 + CSourceHookImpl::~CSourceHookImpl()
1.426 + {
1.427 + CompleteShutdown();
1.428 + }
1.429 +
1.430 + int CSourceHookImpl::GetIfaceVersion()
1.431 + {
1.432 + return SH_IFACE_VERSION;
1.433 + }
1.434 +
1.435 + int CSourceHookImpl::GetImplVersion()
1.436 + {
1.437 + return SH_IMPL_VERSION;
1.438 + }
1.439 +
1.440 + int CSourceHookImpl::AddHook(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
1.441 + ISHDelegate *handler, bool post)
1.442 + {
1.443 + if (mode != Hook_Normal && mode != Hook_VP && mode != Hook_DVP)
1.444 + return 0;
1.445 +
1.446 + // Get info about hook manager
1.447 + CHookManager hookManager(plug, myHookMan);
1.448 + if (!hookManager)
1.449 + return 0;
1.450 +
1.451 + void *adjustediface = NULL;
1.452 + void **cur_vtptr = NULL;
1.453 + void *cur_vfnptr = NULL;
1.454 +
1.455 + // find vfnptr
1.456 + switch (mode)
1.457 + {
1.458 + case Hook_Normal:
1.459 + adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
1.460 +
1.461 + cur_vtptr = *reinterpret_cast<void***>(
1.462 + reinterpret_cast<char*>(adjustediface) + hookManager.GetVtblOffs());
1.463 + cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + hookManager.GetVtblIdx());
1.464 + break;
1.465 +
1.466 + case Hook_VP:
1.467 +
1.468 + adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
1.469 +
1.470 + cur_vtptr = *reinterpret_cast<void***>(
1.471 + reinterpret_cast<char*>(adjustediface) + hookManager.GetVtblOffs());
1.472 + cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + hookManager.GetVtblIdx());
1.473 +
1.474 + adjustediface = NULL;
1.475 + break;
1.476 +
1.477 + case Hook_DVP:
1.478 + adjustediface = NULL;
1.479 +
1.480 + cur_vtptr = reinterpret_cast<void**>(iface);
1.481 + cur_vfnptr = cur_vtptr + hookManager.GetVtblIdx();
1.482 +
1.483 + break;
1.484 + }
1.485 +
1.486 + CVfnPtr &vfnPtr = m_VfnPtrs.GetVfnPtr(cur_vfnptr);
1.487 + vfnPtr.AddHookMan(m_HookManList.GetHookMan(hookManager));
1.488 + CIface &ifaceinst = vfnPtr.GetIface(adjustediface);
1.489 +
1.490 + // Add the hook
1.491 + CHook hook(plug, thisptr_offs, handler,
1.492 +
1.493 + m_HookIDMan.New(hookManager.GetProto(), hookManager.GetVtblOffs(), hookManager.GetVtblIdx(),
1.494 + cur_vfnptr, adjustediface, plug, thisptr_offs, handler, post)
1.495 +
1.496 + );
1.497 +
1.498 + if (post)
1.499 + ifaceinst.GetPostHookList().push_back(hook);
1.500 + else
1.501 + ifaceinst.GetPreHookList().push_back(hook);
1.502 +
1.503 + return hook.GetID();
1.504 + }
1.505 +
1.506 + bool CSourceHookImpl::RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
1.507 + ISHDelegate *handler, bool post)
1.508 + {
1.509 + // Get info about hook manager and compute adjustediface
1.510 + CHookManager tmpHookMan(plug, myHookMan);
1.511 +
1.512 + void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface)+thisptr_offs);
1.513 +
1.514 + // Loop through all hooks and remove those which match:
1.515 + // hookman, vfnptr, iface, plug, adjusted iface, this ptr offs, handler, post
1.516 + CVector<int> removehooks;
1.517 + m_HookIDMan.FindAllHooks(removehooks, tmpHookMan.GetProto(), tmpHookMan.GetVtblOffs(), tmpHookMan.GetVtblIdx(),
1.518 + adjustediface, plug, thisptr_offs, handler, post);
1.519 +
1.520 + if (removehooks.empty())
1.521 + return false;
1.522 +
1.523 + bool status = false;
1.524 +
1.525 + for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
1.526 + {
1.527 + if (RemoveHookByID(*iter))
1.528 + status = true;
1.529 + }
1.530 + return status;
1.531 + }
1.532 +
1.533 + bool CSourceHookImpl::RemoveHookByID(int hookid)
1.534 + {
1.535 + const CHookIDManager::Entry *hentry;
1.536 +
1.537 + hentry = m_HookIDMan.QueryHook(hookid);
1.538 + if (!hentry)
1.539 + {
1.540 + // hookid doesn't exist !
1.541 + return false;
1.542 + }
1.543 +
1.544 + // find vfnptr
1.545 + List<CVfnPtr>::iterator vfnptr_iter = m_VfnPtrs.find(hentry->vfnptr);
1.546 + if (vfnptr_iter == m_VfnPtrs.end())
1.547 + return false;
1.548 +
1.549 + // find iface
1.550 + List<CIface>::iterator iface_iter = vfnptr_iter->GetIfaceList().find(hentry->adjustediface);
1.551 + if (iface_iter == vfnptr_iter->GetIfaceList().end())
1.552 + return false;
1.553 +
1.554 + // find hook
1.555 + List<CHook> &hooks = hentry->post ? iface_iter->GetPostHookList() : iface_iter->GetPreHookList();
1.556 + List<CHook>::iterator hook_iter = hooks.find(hookid);
1.557 + if (hook_iter == hooks.end())
1.558 + return false;
1.559 +
1.560 + hook_iter->GetHandler()->DeleteThis();
1.561 +
1.562 + // Iterator movage!
1.563 + List<CHook>::iterator oldhookiter = hook_iter;
1.564 + hook_iter = hooks.erase(hook_iter);
1.565 +
1.566 + for (CStack<CHookContext>::iterator ctx_iter = m_ContextStack.begin();
1.567 + ctx_iter != m_ContextStack.end(); ++ctx_iter)
1.568 + {
1.569 + ctx_iter->HookRemoved(oldhookiter, hook_iter);
1.570 + }
1.571 +
1.572 + if (iface_iter->GetPreHookList().empty() && iface_iter->GetPostHookList().empty())
1.573 + {
1.574 + // -> Kill all contexts that use it!
1.575 + for (CStack<CHookContext>::iterator ctx_iter = m_ContextStack.begin();
1.576 + ctx_iter != m_ContextStack.end(); ++ctx_iter)
1.577 + {
1.578 + ctx_iter->IfaceRemoved(&(*iface_iter));
1.579 + }
1.580 +
1.581 + // There are no hooks on this iface anymore...
1.582 + iface_iter = vfnptr_iter->GetIfaceList().erase(iface_iter);
1.583 +
1.584 + if (vfnptr_iter->GetIfaceList().empty())
1.585 + {
1.586 + // No ifaces at all -> Deactivate the hook
1.587 +
1.588 + for (CStack<CHookContext>::iterator ctx_iter = m_ContextStack.begin();
1.589 + ctx_iter != m_ContextStack.end(); ++ctx_iter)
1.590 + {
1.591 + ctx_iter->VfnPtrRemoved(&(*vfnptr_iter));
1.592 + }
1.593 +
1.594 + vfnptr_iter->Revert();
1.595 +
1.596 + m_VfnPtrs.erase(vfnptr_iter);
1.597 + }
1.598 + }
1.599 +
1.600 + m_HookIDMan.Remove(hookid);
1.601 + return true;
1.602 + }
1.603 +
1.604 + void CSourceHookImpl::SetRes(META_RES res)
1.605 + {
1.606 + *m_ContextStack.front().pCurRes = res;
1.607 + }
1.608 +
1.609 + META_RES CSourceHookImpl::GetPrevRes()
1.610 + {
1.611 + return *m_ContextStack.front().pPrevRes;
1.612 + }
1.613 +
1.614 + META_RES CSourceHookImpl::GetStatus()
1.615 + {
1.616 + return *m_ContextStack.front().pStatus;
1.617 +
1.618 + }
1.619 +
1.620 + const void *CSourceHookImpl::GetOrigRet()
1.621 + {
1.622 + return m_ContextStack.front().pOrigRet;
1.623 + }
1.624 +
1.625 + const void *CSourceHookImpl::GetOverrideRet()
1.626 + {
1.627 + return (*m_ContextStack.front().pStatus < MRES_OVERRIDE) ?
1.628 + NULL : m_ContextStack.front().pOverrideRet;
1.629 + }
1.630 +
1.631 + void *CSourceHookImpl::GetIfacePtr()
1.632 + {
1.633 + // If in recall: return last one
1.634 + if (m_ContextStack.front().m_State >= CHookContext::State_Recall_Pre &&
1.635 + m_ContextStack.front().m_State <= CHookContext::State_Recall_PostVP)
1.636 + {
1.637 + return m_ContextStack.second().pIfacePtr;
1.638 + }
1.639 + else
1.640 + {
1.641 + return m_ContextStack.front().pIfacePtr;
1.642 + }
1.643 + }
1.644 +
1.645 + void *CSourceHookImpl::GetOverrideRetPtr()
1.646 + {
1.647 + return m_ContextStack.front().pOverrideRet;
1.648 + }
1.649 +
1.650 + void CSourceHookImpl::UnloadPlugin(Plugin plug)
1.651 + {
1.652 + // 1) Remove all hooks by this plugin
1.653 +
1.654 + CVector<int> removehooks;
1.655 + m_HookIDMan.FindAllHooks(removehooks, plug);
1.656 +
1.657 + for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
1.658 + RemoveHookByID(*iter);
1.659 +
1.660 + // 2) Remove all hook managers
1.661 + for (CHookManList::iterator iter = m_HookManList.begin(); iter != m_HookManList.end(); )
1.662 + {
1.663 + if (iter->GetOwnerPlugin() == plug)
1.664 + iter = RemoveHookManager(iter);
1.665 + else
1.666 + ++iter;
1.667 + }
1.668 + }
1.669 +
1.670 + CHookManList::iterator CSourceHookImpl::RemoveHookManager(CHookManList::iterator hookman_iter)
1.671 + {
1.672 + // 2) Remove it
1.673 + for (CVfnPtrList::iterator vfnptr_iter = m_VfnPtrs.begin();
1.674 + vfnptr_iter != m_VfnPtrs.end();)
1.675 + {
1.676 + if (!vfnptr_iter->HookManRemoved(&(*hookman_iter)))
1.677 + {
1.678 + // This vfnptr has no more hook managers
1.679 + // and asks to be removed.
1.680 +
1.681 + vfnptr_iter->Revert();
1.682 +
1.683 + m_HookIDMan.RemoveAll(vfnptr_iter->GetPtr());
1.684 + vfnptr_iter = m_VfnPtrs.erase(vfnptr_iter);
1.685 + }
1.686 + else
1.687 + {
1.688 + ++vfnptr_iter;
1.689 + }
1.690 + }
1.691 +
1.692 + return m_HookManList.erase(hookman_iter);
1.693 + }
1.694 +
1.695 + void CSourceHookImpl::RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc)
1.696 + {
1.697 + // Find the hook manager
1.698 + CHookManList::iterator hookman_iter = m_HookManList.find(CHookManager::Descriptor(plug, pubFunc));
1.699 +
1.700 + if (hookman_iter != m_HookManList.end())
1.701 + {
1.702 + RemoveHookManager(hookman_iter);
1.703 + }
1.704 + }
1.705 +
1.706 + void CSourceHookImpl::SetIgnoreHooks(void *vfnptr)
1.707 + {
1.708 + CHookContext ctx;
1.709 + ctx.m_State = CHookContext::State_Ignore;
1.710 + m_ContextStack.push(ctx);
1.711 + }
1.712 +
1.713 + void CSourceHookImpl::ResetIgnoreHooks(void *vfnptr)
1.714 + {
1.715 + if (!m_ContextStack.empty() && m_ContextStack.front().m_State == CHookContext::State_Ignore)
1.716 + m_ContextStack.pop();
1.717 + }
1.718 +
1.719 + void *CSourceHookImpl::GetOrigVfnPtrEntry(void *vfnptr)
1.720 + {
1.721 + for (CVfnPtrList::iterator vfnptr_iter = m_VfnPtrs.begin(); vfnptr_iter != m_VfnPtrs.end(); ++vfnptr_iter)
1.722 + {
1.723 + if (vfnptr_iter->GetPtr() == vfnptr)
1.724 + return vfnptr_iter->GetOrigEntry();
1.725 + }
1.726 + return NULL;
1.727 + }
1.728 +
1.729 + void CSourceHookImpl::DoRecall()
1.730 + {
1.731 + CHookContext newCtx;
1.732 + CHookContext &curCtx = m_ContextStack.front();
1.733 +
1.734 +
1.735 + newCtx.m_State = curCtx.m_State + (CHookContext::State_Recall_Pre - CHookContext::State_Pre);
1.736 + if (newCtx.m_State == CHookContext::State_Recall_Post || newCtx.m_State == CHookContext::State_Recall_PostVP)
1.737 + {
1.738 + // Also save orig ret
1.739 + newCtx.pOrigRet = curCtx.pOrigRet;
1.740 + }
1.741 +
1.742 + // The hookfunc usually does this, but it won't have a chance to see it,
1.743 + // so for recalls, we update status here if it's required
1.744 + if (*curCtx.pCurRes > *curCtx.pStatus)
1.745 + *curCtx.pStatus = *curCtx.pCurRes;
1.746 +
1.747 + newCtx.pStatus = curCtx.pStatus;
1.748 + newCtx.pOverrideRet = curCtx.pOverrideRet;
1.749 + newCtx.pPrevRes = curCtx.pPrevRes;
1.750 + newCtx.m_Iter = curCtx.m_Iter;
1.751 +
1.752 + // Take this with us!
1.753 + newCtx.pCurRes = curCtx.pCurRes;
1.754 +
1.755 + m_ContextStack.push(newCtx);
1.756 + curCtx.m_State = CHookContext::State_Dead;
1.757 + }
1.758 +
1.759 + IHookContext *CSourceHookImpl::SetupHookLoop(IHookManagerInfo *hi, void *vfnptr, void *thisptr, void **origentry, META_RES *statusPtr,
1.760 + META_RES *prevResPtr, META_RES *curResPtr, const void *origRetPtr, void *overrideRetPtr)
1.761 + {
1.762 + CHookContext *pCtx = NULL;
1.763 + CHookContext *oldctx = m_ContextStack.empty() ? NULL : &m_ContextStack.front();
1.764 + if (oldctx)
1.765 + {
1.766 + // SH_CALL
1.767 + if (oldctx->m_State == CHookContext::State_Ignore)
1.768 + {
1.769 + *statusPtr = MRES_IGNORED;
1.770 + oldctx->m_CallOrig = true;
1.771 + oldctx->m_State = CHookContext::State_Dead;
1.772 +
1.773 + List<CVfnPtr*> &vfnptr_list = static_cast<CHookManager*>(hi)->GetVfnPtrList();
1.774 + List<CVfnPtr*>::iterator vfnptr_iter;
1.775 + for (vfnptr_iter = vfnptr_list.begin();
1.776 + vfnptr_iter != vfnptr_list.end(); ++vfnptr_iter)
1.777 + {
1.778 + if (**vfnptr_iter == vfnptr)
1.779 + break;
1.780 + }
1.781 + if (vfnptr_iter == vfnptr_list.end())
1.782 + {
1.783 + SH_ASSERT(false, ("How can a hook exist on a vfnptr which we don't have in our db?!"));
1.784 + }
1.785 + else
1.786 + {
1.787 + *origentry = (*vfnptr_iter)->GetOrigEntry();
1.788 + }
1.789 +
1.790 + oldctx->pOrigRet = origRetPtr;
1.791 + return oldctx;
1.792 + }
1.793 + // Recall
1.794 + else if (oldctx->m_State >= CHookContext::State_Recall_Pre &&
1.795 + oldctx->m_State <= CHookContext::State_Recall_PostVP)
1.796 + {
1.797 + pCtx = oldctx;
1.798 +
1.799 + *statusPtr = *(oldctx->pStatus);
1.800 + *prevResPtr = *(oldctx->pPrevRes);
1.801 +
1.802 + pCtx->m_Iter = oldctx->m_Iter;
1.803 +
1.804 + // Only have possibility of calling the orig func in pre recall mode
1.805 + pCtx->m_CallOrig = (oldctx->m_State == CHookContext::State_Recall_Pre ||
1.806 + oldctx->m_State == CHookContext::State_Recall_PreVP);
1.807 +
1.808 + overrideRetPtr = pCtx->pOverrideRet;
1.809 +
1.810 + // When the status is low so there's no override return value and we're in a post recall,
1.811 + // give it the orig return value as override return value.
1.812 + if (pCtx->m_State == CHookContext::State_Recall_Post ||
1.813 + pCtx->m_State == CHookContext::State_Recall_PostVP)
1.814 + {
1.815 + origRetPtr = oldctx->pOrigRet;
1.816 + if (*statusPtr < MRES_OVERRIDE)
1.817 + overrideRetPtr = const_cast<void*>(pCtx->pOrigRet);
1.818 + }
1.819 + }
1.820 + }
1.821 + if (!pCtx)
1.822 + {
1.823 + pCtx = m_ContextStack.make_next();
1.824 + pCtx->m_State = CHookContext::State_Born;
1.825 + pCtx->m_CallOrig = true;
1.826 + }
1.827 +
1.828 + pCtx->pIface = NULL;
1.829 +
1.830 + List<CVfnPtr*> &vfnptr_list = static_cast<CHookManager*>(hi)->GetVfnPtrList();
1.831 + List<CVfnPtr*>::iterator vfnptr_iter;
1.832 + for (vfnptr_iter = vfnptr_list.begin();
1.833 + vfnptr_iter != vfnptr_list.end(); ++vfnptr_iter)
1.834 + {
1.835 + if (**vfnptr_iter == vfnptr)
1.836 + break;
1.837 + }
1.838 + if (vfnptr_iter == vfnptr_list.end())
1.839 + {
1.840 + pCtx->m_State = CHookContext::State_Dead;
1.841 + }
1.842 + else
1.843 + {
1.844 + pCtx->pVfnPtr = *vfnptr_iter;
1.845 + *origentry = pCtx->pVfnPtr->GetOrigEntry();
1.846 + pCtx->pIface = pCtx->pVfnPtr->FindIface(thisptr);
1.847 + }
1.848 +
1.849 + pCtx->pStatus = statusPtr;
1.850 + pCtx->pPrevRes = prevResPtr;
1.851 + pCtx->pCurRes = curResPtr;
1.852 + pCtx->pThisPtr = thisptr;
1.853 + pCtx->pOverrideRet = overrideRetPtr;
1.854 + pCtx->pOrigRet = origRetPtr;
1.855 +
1.856 + return pCtx;
1.857 + }
1.858 +
1.859 + void CSourceHookImpl::EndContext(IHookContext *pCtx)
1.860 + {
1.861 + m_ContextStack.pop();
1.862 + }
1.863 +
1.864 + void CSourceHookImpl::CompleteShutdown()
1.865 + {
1.866 + CVector<int> removehooks;
1.867 + m_HookIDMan.FindAllHooks(removehooks);
1.868 +
1.869 + for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
1.870 + RemoveHookByID(*iter);
1.871 + }
1.872 +
1.873 + void CSourceHookImpl::PausePlugin(Plugin plug)
1.874 + {
1.875 + CVector<int> pausehooks;
1.876 + m_HookIDMan.FindAllHooks(pausehooks, plug);
1.877 +
1.878 + for (CVector<int>::iterator iter = pausehooks.begin(); iter != pausehooks.end(); ++iter)
1.879 + PauseHookByID(*iter);
1.880 + }
1.881 +
1.882 + void CSourceHookImpl::UnpausePlugin(Plugin plug)
1.883 + {
1.884 + CVector<int> unpausehooks;
1.885 + m_HookIDMan.FindAllHooks(unpausehooks, plug);
1.886 +
1.887 + for (CVector<int>::iterator iter = unpausehooks.begin(); iter != unpausehooks.end(); ++iter)
1.888 + UnpauseHookByID(*iter);
1.889 + }
1.890 +
1.891 + bool CSourceHookImpl::PauseHookByID(int hookid)
1.892 + {
1.893 + return SetHookPaused(hookid, true);
1.894 + }
1.895 +
1.896 + bool CSourceHookImpl::UnpauseHookByID(int hookid)
1.897 + {
1.898 + return SetHookPaused(hookid, false);
1.899 + }
1.900 +
1.901 + bool CSourceHookImpl::SetHookPaused(int hookid, bool paused)
1.902 + {
1.903 + const CHookIDManager::Entry *hentry;
1.904 +
1.905 + hentry = m_HookIDMan.QueryHook(hookid);
1.906 + if (!hentry)
1.907 + {
1.908 + // hookid doesn't exist !
1.909 + return false;
1.910 + }
1.911 +
1.912 + // find vfnptr
1.913 + List<CVfnPtr>::iterator vfnptr_iter = m_VfnPtrs.find(hentry->vfnptr);
1.914 + if (vfnptr_iter == m_VfnPtrs.end())
1.915 + return false;
1.916 +
1.917 + // find iface
1.918 + List<CIface>::iterator iface_iter = vfnptr_iter->GetIfaceList().find(hentry->adjustediface);
1.919 + if (iface_iter == vfnptr_iter->GetIfaceList().end())
1.920 + return false;
1.921 +
1.922 + // find hook
1.923 + List<CHook> &hooks = hentry->post ? iface_iter->GetPostHookList() : iface_iter->GetPreHookList();
1.924 + List<CHook>::iterator hook_iter = hooks.find(hookid);
1.925 + if (hook_iter == hooks.end())
1.926 + return false;
1.927 +
1.928 + hook_iter->SetPaused(paused);
1.929 + return true;
1.930 + }
1.931 +
1.932 + //////////////////////////////////////////////////////////////////////////
1.933 + // CHookContext
1.934 + //////////////////////////////////////////////////////////////////////////
1.935 + ISHDelegate *CHookContext::GetNext()
1.936 + {
1.937 + CIface *pVPIface;
1.938 + switch (m_State)
1.939 + {
1.940 + case State_Dead:
1.941 + return NULL;
1.942 +
1.943 + case State_Born:
1.944 + m_Iter = List<CHook>::iterator();
1.945 + m_State = State_Pre;
1.946 +
1.947 + // fall-through
1.948 + case State_Recall_Pre:
1.949 + m_State = State_Pre;
1.950 + case State_Pre:
1.951 + if (pIface)
1.952 + {
1.953 + if (!m_Iter)
1.954 + m_Iter = pIface->GetPreHookList().begin();
1.955 + else
1.956 + ++m_Iter;
1.957 + SkipPaused(m_Iter, pIface->GetPreHookList());
1.958 +
1.959 + if (m_Iter != pIface->GetPreHookList().end())
1.960 + {
1.961 + pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
1.962 + return m_Iter->GetHandler();
1.963 + }
1.964 + }
1.965 +
1.966 + // end of normal hooks -> VP
1.967 +
1.968 + m_State = State_PreVP;
1.969 + m_Iter = List<CHook>::iterator();
1.970 +
1.971 + // fall-through
1.972 + case State_Recall_PreVP:
1.973 + m_State = State_PreVP;
1.974 + case State_PreVP:
1.975 + pVPIface = pVfnPtr->FindIface(NULL);
1.976 + if (pVPIface)
1.977 + {
1.978 + if (!m_Iter)
1.979 + m_Iter = pVPIface->GetPreHookList().begin();
1.980 + else
1.981 + ++m_Iter;
1.982 + SkipPaused(m_Iter, pVPIface->GetPreHookList());
1.983 +
1.984 + if (m_Iter != pVPIface->GetPreHookList())
1.985 + {
1.986 + pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
1.987 + return m_Iter->GetHandler();
1.988 + }
1.989 + }
1.990 +
1.991 + // end VP hooks -> orig call
1.992 +
1.993 + m_State = State_OrigCall;
1.994 + return NULL;
1.995 +
1.996 + case State_OrigCall:
1.997 + m_Iter = List<CHook>::iterator();
1.998 + m_State = State_Post;
1.999 +
1.1000 + // fall-through
1.1001 + case State_Post:
1.1002 + if (pIface)
1.1003 + {
1.1004 + if (!m_Iter)
1.1005 + m_Iter = pIface->GetPostHookList().begin();
1.1006 + else
1.1007 + ++m_Iter;
1.1008 + SkipPaused(m_Iter, pIface->GetPostHookList());
1.1009 +
1.1010 + if (m_Iter != pIface->GetPostHookList().end())
1.1011 + {
1.1012 + pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
1.1013 + return m_Iter->GetHandler();
1.1014 + }
1.1015 + }
1.1016 + // end of normal hooks -> VP
1.1017 +
1.1018 + m_State = State_PostVP;
1.1019 + m_Iter = List<CHook>::iterator();
1.1020 +
1.1021 + // fall-through
1.1022 + case State_PostVP:
1.1023 + pVPIface = pVfnPtr->FindIface(NULL);
1.1024 + if (pVPIface)
1.1025 + {
1.1026 + if (!m_Iter)
1.1027 + m_Iter = pVPIface->GetPostHookList().begin();
1.1028 + else
1.1029 + ++m_Iter;
1.1030 + SkipPaused(m_Iter, pVPIface->GetPostHookList());
1.1031 +
1.1032 + if (m_Iter != pVPIface->GetPostHookList())
1.1033 + {
1.1034 + pIfacePtr = reinterpret_cast<void*>(reinterpret_cast<char*>(pThisPtr) - m_Iter->GetThisPointerOffset());
1.1035 + return m_Iter->GetHandler();
1.1036 + }
1.1037 + }
1.1038 +
1.1039 + // end VP hooks -> done
1.1040 +
1.1041 + m_State = State_Dead;
1.1042 + return NULL;
1.1043 +
1.1044 + case State_Recall_Post:
1.1045 + // Skip pre hooks _and_ orig call
1.1046 + m_State = State_Post;
1.1047 + return NULL;
1.1048 +
1.1049 + case State_Recall_PostVP:
1.1050 + // Skip pre hooks _and_ orig call
1.1051 + m_State = State_PostVP;
1.1052 + return NULL;
1.1053 + }
1.1054 + return NULL;
1.1055 + }
1.1056 +
1.1057 + void *CHookContext::GetOverrideRetPtr()
1.1058 + {
1.1059 + return pOverrideRet;
1.1060 + }
1.1061 +
1.1062 + const void *CHookContext::GetOrigRetPtr()
1.1063 + {
1.1064 + return pOrigRet;
1.1065 + }
1.1066 +
1.1067 + bool CHookContext::ShouldCallOrig()
1.1068 + {
1.1069 + return m_CallOrig;
1.1070 + }
1.1071 +
1.1072 + void CHookContext::HookRemoved(List<CHook>::iterator oldhookiter, List<CHook>::iterator nexthookiter)
1.1073 + {
1.1074 + if (m_Iter == oldhookiter)
1.1075 + {
1.1076 + m_Iter = nexthookiter;
1.1077 + --m_Iter;
1.1078 + }
1.1079 + }
1.1080 +
1.1081 + void CHookContext::IfaceRemoved(CIface *iface)
1.1082 + {
1.1083 + if (pIface == iface)
1.1084 + pIface = NULL;
1.1085 + }
1.1086 +
1.1087 + void CHookContext::VfnPtrRemoved(CVfnPtr *vfnptr)
1.1088 + {
1.1089 + if (pVfnPtr == vfnptr)
1.1090 + {
1.1091 + pVfnPtr = NULL;
1.1092 + m_State = State_Dead;
1.1093 + }
1.1094 + }
1.1095 + }
1.1096 +}