1.1 --- a/sourcehook/sourcehook.cpp Sat Oct 13 20:15:58 2007 +0000
1.2 +++ b/sourcehook/sourcehook.cpp Sun Oct 14 11:59:13 2007 +0000
1.3 @@ -19,7 +19,6 @@
1.4 #endif
1.5
1.6 #include "sourcehook_impl.h"
1.7 -#include "sh_memory.h"
1.8
1.9 namespace SourceHook
1.10 {
1.11 @@ -45,205 +44,113 @@
1.12 //////////////////////////////////////////////////////////////////////////
1.13 // CProto
1.14 //////////////////////////////////////////////////////////////////////////
1.15 - ProtoInfo *CProto::DupProto(const ProtoInfo *src)
1.16 + void CProto::Fill(const ProtoInfo *pProto)
1.17 {
1.18 - if (src == NULL)
1.19 - return NULL;
1.20 + if (pProto == NULL)
1.21 + m_Version = -1;
1.22 +
1.23 + m_ParamsPassInfo.clear();
1.24 +
1.25 + if (pProto->paramsPassInfo[0].size == 0)
1.26 + {
1.27 + // Version 1
1.28 + m_Version = 0;
1.29 + m_Convention = pProto->convention;
1.30 + m_NumOfParams = pProto->numOfParams;
1.31 +
1.32 + m_RetPassInfo.size = pProto->retPassInfo.size;
1.33 + m_RetPassInfo.type = pProto->retPassInfo.type;
1.34 + m_RetPassInfo.flags = GetRealFlags(pProto->retPassInfo);
1.35 +
1.36 + m_RetPassInfo.pNormalCtor = NULL;
1.37 + m_RetPassInfo.pCopyCtor = NULL;
1.38 + m_RetPassInfo.pDtor = NULL;
1.39 + m_RetPassInfo.pAssignOperator = NULL;
1.40 +
1.41 +
1.42 + m_ParamsPassInfo.resize(pProto->numOfParams);
1.43 +
1.44 + for (int i = 1; i <= pProto->numOfParams; ++i)
1.45 + {
1.46 + m_ParamsPassInfo[i-1].size = pProto->paramsPassInfo[i].size;
1.47 + m_ParamsPassInfo[i-1].type = pProto->paramsPassInfo[i].type;
1.48 + m_ParamsPassInfo[i-1].flags = GetRealFlags(pProto->paramsPassInfo[i]);
1.49
1.50 - PassInfo *newParamsPassInfo = new PassInfo[src->numOfParams + 1];
1.51 - for (int i = 0; i <= src->numOfParams; ++i)
1.52 - newParamsPassInfo[i] = src->paramsPassInfo[i];
1.53 + m_ParamsPassInfo[i-1].pNormalCtor = NULL;
1.54 + m_ParamsPassInfo[i-1].pCopyCtor = NULL;
1.55 + m_ParamsPassInfo[i-1].pDtor = NULL;
1.56 + m_ParamsPassInfo[i-1].pAssignOperator = NULL;
1.57 + }
1.58 + }
1.59 + else if (pProto->paramsPassInfo[0].size == 1)
1.60 + {
1.61 + // Version 2
1.62 + m_Version = 1;
1.63 + m_Convention = pProto->convention;
1.64 + m_NumOfParams = pProto->numOfParams;
1.65 +
1.66 + m_RetPassInfo.size = pProto->retPassInfo.size;
1.67 + m_RetPassInfo.type = pProto->retPassInfo.type;
1.68 + m_RetPassInfo.flags = pProto->retPassInfo.flags;
1.69
1.70 - ProtoInfo *newProto = new ProtoInfo;
1.71 - newProto->retPassInfo = src->retPassInfo;
1.72 - newProto->paramsPassInfo = newParamsPassInfo;
1.73 - newProto->numOfParams = src->numOfParams;
1.74 - newProto->convention = src->convention;
1.75 + m_RetPassInfo.pNormalCtor = pProto->retPassInfo2.pNormalCtor;
1.76 + m_RetPassInfo.pCopyCtor = pProto->retPassInfo2.pCopyCtor;
1.77 + m_RetPassInfo.pDtor = pProto->retPassInfo2.pDtor;
1.78 + m_RetPassInfo.pAssignOperator = pProto->retPassInfo2.pAssignOperator;
1.79 +
1.80 + m_ParamsPassInfo.resize(pProto->numOfParams);
1.81
1.82 - return newProto;
1.83 + for (int i = 1; i <= pProto->numOfParams; ++i)
1.84 + {
1.85 + m_ParamsPassInfo[i-1].size = pProto->paramsPassInfo[i].size;
1.86 + m_ParamsPassInfo[i-1].type = pProto->paramsPassInfo[i].type;
1.87 + m_ParamsPassInfo[i-1].flags = pProto->paramsPassInfo[i].flags;
1.88 +
1.89 + m_ParamsPassInfo[i-1].pNormalCtor = pProto->paramsPassInfo2[i].pNormalCtor;
1.90 + m_ParamsPassInfo[i-1].pCopyCtor = pProto->paramsPassInfo2[i].pCopyCtor;
1.91 + m_ParamsPassInfo[i-1].pDtor = pProto->paramsPassInfo2[i].pDtor;
1.92 + m_ParamsPassInfo[i-1].pAssignOperator = pProto->paramsPassInfo2[i].pAssignOperator;
1.93 + }
1.94 + }
1.95 + else
1.96 + {
1.97 + // Unknown
1.98 + m_Version = -1;
1.99 + }
1.100 }
1.101
1.102 - void CProto::FreeProto(ProtoInfo *prot)
1.103 + // Basic compat test
1.104 + // Other than this, we assume that the plugins know what they're doing
1.105 + bool CProto::operator == (const CProto &other) const
1.106 {
1.107 - if (!prot)
1.108 - return;
1.109 -
1.110 - delete [] prot->paramsPassInfo;
1.111 - delete prot;
1.112 - }
1.113 -
1.114 - bool CProto::Equal(const ProtoInfo *p1, const ProtoInfo *p2)
1.115 - {
1.116 - if (!p1 || !p2)
1.117 + if (m_Version < 0 || other.GetVersion() < 0)
1.118 return false;
1.119
1.120 - if (p1->numOfParams != p2->numOfParams)
1.121 + if (m_NumOfParams != other.GetNumOfParams())
1.122 return false;
1.123
1.124 - if (p1->convention != ProtoInfo::CallConv_Unknown && p2->convention != ProtoInfo::CallConv_Unknown &&
1.125 - p1->convention != p2->convention)
1.126 + if (m_Convention != ProtoInfo::CallConv_Unknown && other.GetConvention() != ProtoInfo::CallConv_Unknown &&
1.127 + m_Convention != other.GetConvention())
1.128 return false;
1.129
1.130 - if (p1->retPassInfo.size != p2->retPassInfo.size)
1.131 + if (GetRealSize(GetRet()) != GetRealSize(other.GetRet()))
1.132 return false;
1.133
1.134 - // Skip params[0] : dummy
1.135 - for (int i = 1; i <= p1->numOfParams; ++i)
1.136 + for (int i = 0; i < m_NumOfParams; ++i)
1.137 {
1.138 - if (GetRealSize(p1->paramsPassInfo[i]) != GetRealSize(p2->paramsPassInfo[i]))
1.139 + if (GetRealSize(GetParam(i)) != GetRealSize(other.GetParam(i)))
1.140 return false;
1.141 - if (p1->paramsPassInfo[i].type != PassInfo::PassType_Unknown && p2->paramsPassInfo[i].type != PassInfo::PassType_Unknown)
1.142 + if (GetParam(i).type != PassInfo::PassType_Unknown && other.GetParam(i).type != PassInfo::PassType_Unknown)
1.143 {
1.144 - if (p1->paramsPassInfo[i].type != p2->paramsPassInfo[i].type)
1.145 + if (GetParam(i).type != other.GetParam(i).type)
1.146 return false;
1.147 - if (GetRealFlags(p1->paramsPassInfo[i]) != GetRealFlags(p2->paramsPassInfo[i]))
1.148 + if (GetParam(i).flags != other.GetParam(i).flags)
1.149 return false;
1.150 }
1.151 }
1.152
1.153 return true;
1.154 }
1.155 -
1.156 -
1.157 - //////////////////////////////////////////////////////////////////////////
1.158 - // CHookManContainerList
1.159 - //////////////////////////////////////////////////////////////////////////
1.160 - CHookManagerContainer &CHookManContainerList::GetContainer(int vtbloffs, int vtblidx, const CProto &proto)
1.161 - {
1.162 - iterator iter = find(CHookManagerContainer::Descriptor(vtbloffs, vtblidx, proto));
1.163 - if (iter == end())
1.164 - {
1.165 - CHookManagerContainer newCont(vtbloffs, vtblidx, proto);
1.166 - push_back(newCont);
1.167 - return back();
1.168 - }
1.169 - else
1.170 - {
1.171 - return *iter;
1.172 - }
1.173 - }
1.174 -
1.175 - void CHookManContainerList::RemoveHookMans(Plugin plug)
1.176 - {
1.177 - for (iterator iter = begin(); iter != end(); ++iter)
1.178 - iter->RemoveHookMans(plug);
1.179 - }
1.180 -
1.181 - void CHookManContainerList::RemoveHookMans(Plugin plug, HookManagerPubFunc pubFunc)
1.182 - {
1.183 - for (iterator iter = begin(); iter != end(); ++iter)
1.184 - iter->RemoveHookMans(plug, pubFunc);
1.185 - }
1.186 -
1.187 - //////////////////////////////////////////////////////////////////////////
1.188 - // CHookManagerContainer
1.189 - //////////////////////////////////////////////////////////////////////////
1.190 -
1.191 - bool CHookManagerContainer::AddHookManager(const CHookManager &hookMan)
1.192 - {
1.193 - iterator iter;
1.194 -
1.195 - // Don't accept invalid hook managers
1.196 - if (!hookMan)
1.197 - return false;
1.198 -
1.199 - // Check whether such a hook manager already exists; if yes, ignore.
1.200 - iter = find(hookMan);
1.201 - if (iter != end())
1.202 - {
1.203 - // but make sure that it's activated (ms_HI stored)
1.204 - iter->Register();
1.205 - return true;
1.206 - }
1.207 -
1.208 - // It doesn't -> add it. Add it to the end of its version group.
1.209 - for (iter = begin(); iter != end(); ++iter)
1.210 - {
1.211 - if (iter->GetVersion() < hookMan.GetVersion())
1.212 - break;
1.213 - }
1.214 -
1.215 - bool isBeginning = iter == begin();
1.216 -
1.217 - insert(iter, hookMan);
1.218 -
1.219 - if (isBeginning)
1.220 - {
1.221 - iter = begin();
1.222 -
1.223 - if (size() > 1)
1.224 - {
1.225 - // If the next hookman isn't empty (there were hooks before, but this hookman is better)
1.226 - // transfer its vfnptrs and deactivate it
1.227 -
1.228 - iterator second = iter;
1.229 - ++second;
1.230 -
1.231 - if (!second->GetVfnPtrList().empty())
1.232 - {
1.233 - // Move the vfnptrs from the old hook manager to the new one
1.234 - iter->GetVfnPtrList() = second->GetVfnPtrList();
1.235 - second->GetVfnPtrList().clear();
1.236 -
1.237 - // Unregister the old one, register the new one
1.238 - second->Unregister();
1.239 -
1.240 - // Go through all vfnptrs in this hookman and patch them to point to the new manager's handler!
1.241 - // :TODO: change this to something like for_all :X
1.242 - for (List<CVfnPtr>::iterator vfnptr_iter = iter->GetVfnPtrList().begin();
1.243 - vfnptr_iter != iter->GetVfnPtrList().end(); ++vfnptr_iter)
1.244 - {
1.245 - vfnptr_iter->Patch(iter->GetHookFunc());
1.246 - }
1.247 - }
1.248 - }
1.249 -
1.250 - // activate the new active hookman
1.251 - iter->Register();
1.252 - }
1.253 -
1.254 - return true;
1.255 - }
1.256 -
1.257 - void CHookManagerContainer::RemoveHookMans(Plugin plug, HookManagerPubFunc pubFunc)
1.258 - {
1.259 - bool erasethis = false;
1.260 -
1.261 - for (iterator iter = begin(); iter != end(); erasethis ? (iter = erase(iter)) : ++iter)
1.262 - {
1.263 - erasethis = false;
1.264 - if (iter->GetOwnerPlugin() == plug
1.265 - && (pubFunc == NULL ? true : (iter->GetPubFunc() == pubFunc)))
1.266 - {
1.267 - if (iter == begin() && !iter->GetVfnPtrList().empty())
1.268 - {
1.269 - // The hook manager is active
1.270 - // -> Transfer its hooks to the next hook manager (which _has_ to exist)
1.271 - SH_ASSERT(size() > 1, ("No more hook managers!?"));
1.272 -
1.273 - iterator second = iter;
1.274 - ++second;
1.275 -
1.276 - SH_ASSERT(second->GetOwnerPlugin() != plug, ("New hook manager from same plugin!"));
1.277 -
1.278 - second->GetVfnPtrList() = iter->GetVfnPtrList();
1.279 - iter->GetVfnPtrList().clear();
1.280 -
1.281 - second->Register();
1.282 - iter->Unregister();
1.283 -
1.284 - // Go through all vfnptrs in this hookman and patch them to point to the new manager's handler!
1.285 - // :TODO: change this to something like for_all :X
1.286 - for (List<CVfnPtr>::iterator vfnptr_iter = second->GetVfnPtrList().begin();
1.287 - vfnptr_iter != second->GetVfnPtrList().end(); ++vfnptr_iter)
1.288 - {
1.289 - vfnptr_iter->Patch(second->GetHookFunc());
1.290 - }
1.291 - }
1.292 - erasethis = true;
1.293 - }
1.294 - }
1.295 - }
1.296
1.297 //////////////////////////////////////////////////////////////////////////
1.298 // CHookManager
1.299 @@ -258,28 +165,103 @@
1.300 m_HookfuncVfnptr = hookfunc_vfnptr;
1.301 }
1.302
1.303 - CVfnPtr &CHookManager::GetVfnPtr(void *vfnptr)
1.304 + //////////////////////////////////////////////////////////////////////////
1.305 + // CVfnPtrList
1.306 + //////////////////////////////////////////////////////////////////////////
1.307 +
1.308 + CVfnPtr &CVfnPtrList::GetVfnPtr(void *vfnptr)
1.309 {
1.310 - List<CVfnPtr>::iterator iter = m_VfnPtrList.find(vfnptr);
1.311 - if (iter == m_VfnPtrList.end())
1.312 + iterator iter = find(vfnptr);
1.313 + if (iter == end())
1.314 {
1.315 CVfnPtr newVfnPtr(vfnptr);
1.316 - m_VfnPtrList.push_back(newVfnPtr);
1.317 + push_back(newVfnPtr);
1.318
1.319 - newVfnPtr.Patch(GetHookFunc());
1.320 -
1.321 - return m_VfnPtrList.back();
1.322 + return back();
1.323 }
1.324 else
1.325 {
1.326 return *iter;
1.327 }
1.328 }
1.329 -
1.330 //////////////////////////////////////////////////////////////////////////
1.331 // CVfnPtr
1.332 //////////////////////////////////////////////////////////////////////////
1.333
1.334 + void CVfnPtr::AddHookMan(CHookManager *pHookMan)
1.335 + {
1.336 + List<CHookManager*>::iterator iter;
1.337 +
1.338 + // Don't accept invalid hook managers
1.339 + if (!*pHookMan)
1.340 + return;
1.341 +
1.342 + // Check whether this hook manager already exists; if yes, ignore.
1.343 + iter = m_HookMans.find(pHookMan);
1.344 + if (iter != m_HookMans.end())
1.345 + return;
1.346 +
1.347 + // It doesn't -> add it. Add it to the end of its version group.
1.348 + for (iter = m_HookMans.begin(); iter != m_HookMans.end(); ++iter)
1.349 + {
1.350 + if ((*iter)->GetVersion() < pHookMan->GetVersion())
1.351 + break;
1.352 + }
1.353 +
1.354 + bool isBeginning = iter == m_HookMans.begin();
1.355 +
1.356 + m_HookMans.insert(iter, pHookMan);
1.357 +
1.358 + if (isBeginning)
1.359 + {
1.360 + pHookMan->IncrRef(this);
1.361 + if (m_HookMans.size() > 1)
1.362 + {
1.363 + // If another hookman was used until now but this one is better
1.364 + // (which it is because it's the first -> it has a higher version)
1.365 + // -> switch!
1.366 +
1.367 + List<CHookManager*>::iterator second = m_HookMans.begin();
1.368 + ++second;
1.369 +
1.370 + (*second)->DecrRef(this);
1.371 + }
1.372 +
1.373 + // Make sure that this vfnptr points at it
1.374 + Patch(pHookMan->GetHookFunc());
1.375 + }
1.376 + }
1.377 +
1.378 + bool CVfnPtr::HookManRemoved(CHookManager *pHookMan)
1.379 + {
1.380 + // Don't accept invalid hook managers
1.381 + if (!*pHookMan)
1.382 + return true;
1.383 +
1.384 + List<CHookManager*>::iterator iter = m_HookMans.find(pHookMan);
1.385 + if (iter == m_HookMans.end())
1.386 + return true; // Didn't exist here anyway
1.387 +
1.388 + if (iter == m_HookMans.begin())
1.389 + {
1.390 + // It is the first one!
1.391 + pHookMan->DecrRef(this);
1.392 + m_HookMans.erase(iter);
1.393 +
1.394 + if (m_HookMans.empty())
1.395 + return false; // No more hookmans -> let SH delete us
1.396 +
1.397 + // Activate second -> now first hookman
1.398 + m_HookMans.front()->IncrRef(this);
1.399 + Patch(m_HookMans.front()->GetHookFunc());
1.400 + }
1.401 + else
1.402 + {
1.403 + m_HookMans.erase(iter);
1.404 + }
1.405 + return true;
1.406 + }
1.407 +
1.408 bool CVfnPtr::Patch(void *newValue)
1.409 {
1.410 if (!SetMemAccess(m_Ptr, sizeof(void*), SH_MEM_READ | SH_MEM_WRITE))
1.411 @@ -400,6 +382,16 @@
1.412 }
1.413
1.414
1.415 + void CHookIDManager::RemoveAll(void *vfnptr)
1.416 + {
1.417 + size_t cursize = m_Entries.size();
1.418 + for (size_t i = 0; i < cursize; ++i)
1.419 + {
1.420 + if (!m_Entries[i].isfree && m_Entries[i].vfnptr == vfnptr)
1.421 + m_Entries[i].isfree = true;
1.422 + }
1.423 + }
1.424 +
1.425 //////////////////////////////////////////////////////////////////////////
1.426 // CSourceHookImpl
1.427 //////////////////////////////////////////////////////////////////////////
1.428 @@ -410,6 +402,7 @@
1.429 }
1.430 CSourceHookImpl::~CSourceHookImpl()
1.431 {
1.432 + CompleteShutdown();
1.433 }
1.434
1.435 int CSourceHookImpl::GetIfaceVersion()
1.436 @@ -433,21 +426,6 @@
1.437 if (!hookManager)
1.438 return 0;
1.439
1.440 - // Make sure that the container exists
1.441 - CHookManagerContainer &container = m_HookManContainers.GetContainer(hookManager.GetVtblOffs(),
1.442 - hookManager.GetVtblIdx(), hookManager.GetProto());
1.443 -
1.444 - container.AddHookManager(hookManager);
1.445 -
1.446 - if (!container)
1.447 - {
1.448 - // For some weird reason, the container is empty!
1.449 - SH_ASSERT(0, ("No hookman found - but we've just added one!"));
1.450 - return 0;
1.451 - }
1.452 -
1.453 - CHookManager &hookMan = container.GetActiveHookManager();
1.454 -
1.455 void *adjustediface = NULL;
1.456 void **cur_vtptr = NULL;
1.457 void *cur_vfnptr = NULL;
1.458 @@ -459,8 +437,8 @@
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) + hookMan.GetVtblOffs());
1.463 - cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + hookMan.GetVtblIdx());
1.464 + reinterpret_cast<char*>(adjustediface) + hookManager.GetVtblOffs());
1.465 + cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + hookManager.GetVtblIdx());
1.466 break;
1.467
1.468 case Hook_VP:
1.469 @@ -468,8 +446,8 @@
1.470 adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
1.471
1.472 cur_vtptr = *reinterpret_cast<void***>(
1.473 - reinterpret_cast<char*>(adjustediface) + hookMan.GetVtblOffs());
1.474 - cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + hookMan.GetVtblIdx());
1.475 + reinterpret_cast<char*>(adjustediface) + hookManager.GetVtblOffs());
1.476 + cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + hookManager.GetVtblIdx());
1.477
1.478 adjustediface = NULL;
1.479 break;
1.480 @@ -478,18 +456,19 @@
1.481 adjustediface = NULL;
1.482
1.483 cur_vtptr = reinterpret_cast<void**>(iface);
1.484 - cur_vfnptr = cur_vtptr + hookMan.GetVtblIdx();
1.485 + cur_vfnptr = cur_vtptr + hookManager.GetVtblIdx();
1.486
1.487 break;
1.488 }
1.489
1.490 - CVfnPtr &vfnPtr = hookMan.GetVfnPtr(cur_vfnptr);
1.491 + CVfnPtr &vfnPtr = m_VfnPtrs.GetVfnPtr(cur_vfnptr);
1.492 + vfnPtr.AddHookMan(m_HookManList.GetHookMan(hookManager));
1.493 CIface &ifaceinst = vfnPtr.GetIface(adjustediface);
1.494
1.495 // Add the hook
1.496 CHook hook(plug, thisptr_offs, handler,
1.497
1.498 - m_HookIDMan.New(hookMan.GetProto(), hookMan.GetVtblOffs(), hookMan.GetVtblIdx(),
1.499 + m_HookIDMan.New(hookManager.GetProto(), hookManager.GetVtblOffs(), hookManager.GetVtblIdx(),
1.500 cur_vfnptr, adjustediface, plug, thisptr_offs, handler, post)
1.501
1.502 );
1.503 @@ -540,18 +519,9 @@
1.504 return false;
1.505 }
1.506
1.507 - // find hookman
1.508 - CHookManContainerList::iterator hmcl_iter = m_HookManContainers.find(
1.509 - CHookManagerContainer::Descriptor(hentry->vtbl_offs, hentry->vtbl_idx, hentry->proto));
1.510 -
1.511 - if (hmcl_iter == m_HookManContainers.end() || !(*hmcl_iter))
1.512 - return false;
1.513 -
1.514 - CHookManager &hookman = hmcl_iter->GetActiveHookManager();
1.515 -
1.516 // find vfnptr
1.517 - List<CVfnPtr>::iterator vfnptr_iter = hookman.GetVfnPtrList().find(hentry->vfnptr);
1.518 - if (vfnptr_iter == hookman.GetVfnPtrList().end())
1.519 + List<CVfnPtr>::iterator vfnptr_iter = m_VfnPtrs.find(hentry->vfnptr);
1.520 + if (vfnptr_iter == m_VfnPtrs.end())
1.521 return false;
1.522
1.523 // find iface
1.524 @@ -599,22 +569,9 @@
1.525 ctx_iter->VfnPtrRemoved(&(*vfnptr_iter));
1.526 }
1.527
1.528 - // Only patch the vfnptr back if the module is still in memory
1.529 - // If it's not, do not remove stuff like we did before
1.530 - // First off we did it wrong (shutdown the whole hookman, uh..) and secondly applications may be
1.531 - // confused by RemoveHook returning false then (yeah, I know, I made this one up, no one checks for RemoveHook error)
1.532 - if (ModuleInMemory(reinterpret_cast<char*>(vfnptr_iter->GetPtr()), SH_PTRSIZE))
1.533 - {
1.534 - vfnptr_iter->Revert();
1.535 - }
1.536 + vfnptr_iter->Revert();
1.537
1.538 - hookman.GetVfnPtrList().erase(vfnptr_iter);
1.539 -
1.540 - if (hookman.GetVfnPtrList().empty())
1.541 - {
1.542 - // Unregister the hook manager
1.543 - hookman.Unregister();
1.544 - }
1.545 + m_VfnPtrs.erase(vfnptr_iter);
1.546 }
1.547 }
1.548
1.549 @@ -679,50 +636,49 @@
1.550 RemoveHookByID(*iter);
1.551
1.552 // 2) Remove all hook managers
1.553 - m_HookManContainers.RemoveHookMans(plug);
1.554 + for (CHookManList::iterator iter = m_HookManList.begin(); iter != m_HookManList.end(); )
1.555 + {
1.556 + if (iter->GetOwnerPlugin() == plug)
1.557 + iter = RemoveHookManager(iter);
1.558 + else
1.559 + ++iter;
1.560 + }
1.561 + }
1.562 +
1.563 + CHookManList::iterator CSourceHookImpl::RemoveHookManager(CHookManList::iterator hookman_iter)
1.564 + {
1.565 + // 2) Remove it
1.566 + for (CVfnPtrList::iterator vfnptr_iter = m_VfnPtrs.begin();
1.567 + vfnptr_iter != m_VfnPtrs.end();)
1.568 + {
1.569 + if (!vfnptr_iter->HookManRemoved(&(*hookman_iter)))
1.570 + {
1.571 + // This vfnptr has no more hook managers
1.572 + // and asks to be removed.
1.573 +
1.574 + vfnptr_iter->Revert();
1.575 +
1.576 + m_HookIDMan.RemoveAll(vfnptr_iter->GetPtr());
1.577 + vfnptr_iter = m_VfnPtrs.erase(vfnptr_iter);
1.578 + }
1.579 + else
1.580 + {
1.581 + ++vfnptr_iter;
1.582 + }
1.583 + }
1.584 +
1.585 + return m_HookManList.erase(hookman_iter);
1.586 }
1.587
1.588 void CSourceHookImpl::RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc)
1.589 {
1.590 - // 1) Remove all its hooks
1.591 - CVector<int> removehooks;
1.592 -
1.593 - // Get info about hook manager
1.594 - CHookManager hookManager(plug, pubFunc);
1.595 - if (!hookManager)
1.596 - return;
1.597 + // Find the hook manager
1.598 + CHookManList::iterator hookman_iter = m_HookManList.find(CHookManager::Descriptor(plug, pubFunc));
1.599
1.600 - // Make sure that the container exists
1.601 - CHookManagerContainer &container = m_HookManContainers.GetContainer(hookManager.GetVtblOffs(),
1.602 - hookManager.GetVtblIdx(), hookManager.GetProto());
1.603 -
1.604 - CHookManagerContainer::iterator hookman_iter = container.find(CHookManager::Descriptor(plug, pubFunc));
1.605 -
1.606 - if (hookman_iter != container.end())
1.607 + if (hookman_iter != m_HookManList.end())
1.608 {
1.609 - for (List<CVfnPtr>::iterator vfnptr_iter = hookman_iter->GetVfnPtrList().begin();
1.610 - vfnptr_iter != hookman_iter->GetVfnPtrList().end(); ++vfnptr_iter)
1.611 - {
1.612 - for (List<CIface>::iterator iface_iter = vfnptr_iter->GetIfaceList().begin();
1.613 - iface_iter != vfnptr_iter->GetIfaceList().end(); ++iface_iter)
1.614 - {
1.615 - List<CHook>::iterator hook_iter;
1.616 - for (hook_iter = iface_iter->GetPreHookList().begin();
1.617 - hook_iter != iface_iter->GetPreHookList().end(); ++hook_iter)
1.618 - removehooks.push_back(hook_iter->GetID());
1.619 -
1.620 - for (hook_iter = iface_iter->GetPostHookList().begin();
1.621 - hook_iter != iface_iter->GetPostHookList().end(); ++hook_iter)
1.622 - removehooks.push_back(hook_iter->GetID());
1.623 - }
1.624 - }
1.625 + RemoveHookManager(hookman_iter);
1.626 }
1.627 -
1.628 - for (CVector<int>::iterator iter = removehooks.begin(); iter != removehooks.end(); ++iter)
1.629 - RemoveHookByID(*iter);
1.630 -
1.631 - // 2) Remove it
1.632 - container.RemoveHookMans(plug, pubFunc);
1.633 }
1.634
1.635 void CSourceHookImpl::SetIgnoreHooks(void *vfnptr)
1.636 @@ -740,19 +696,10 @@
1.637
1.638 void *CSourceHookImpl::GetOrigVfnPtrEntry(void *vfnptr)
1.639 {
1.640 - for (CHookManContainerList::iterator hmcl_iter = m_HookManContainers.begin();
1.641 - hmcl_iter != m_HookManContainers.end(); ++hmcl_iter)
1.642 + for (CVfnPtrList::iterator vfnptr_iter = m_VfnPtrs.begin(); vfnptr_iter != m_VfnPtrs.end(); ++vfnptr_iter)
1.643 {
1.644 - for (CHookManagerContainer::iterator hookmaniter = hmcl_iter->begin();
1.645 - hookmaniter != hmcl_iter->end(); ++hookmaniter)
1.646 - {
1.647 - for (List<CVfnPtr>::iterator vfnptr_iter = hookmaniter->GetVfnPtrList().begin();
1.648 - vfnptr_iter != hookmaniter->GetVfnPtrList().end(); ++vfnptr_iter)
1.649 - {
1.650 - if (vfnptr_iter->GetPtr() == vfnptr)
1.651 - return vfnptr_iter->GetOrigEntry();
1.652 - }
1.653 - }
1.654 + if (vfnptr_iter->GetPtr() == vfnptr)
1.655 + return vfnptr_iter->GetOrigEntry();
1.656 }
1.657 return NULL;
1.658 }
1.659 @@ -801,15 +748,20 @@
1.660 oldctx->m_CallOrig = true;
1.661 oldctx->m_State = CHookContext::State_Dead;
1.662
1.663 - List<CVfnPtr> &vfnptrList = static_cast<CHookManager*>(hi)->GetVfnPtrList();
1.664 - List<CVfnPtr>::iterator vfnptr_iter = vfnptrList.find(vfnptr);
1.665 - if (vfnptr_iter == vfnptrList.end())
1.666 + List<CVfnPtr*> &vfnptr_list = static_cast<CHookManager*>(hi)->GetVfnPtrList();
1.667 + for (List<CVfnPtr*>::iterator vfnptr_iter = vfnptr_list.begin();
1.668 + vfnptr_iter != vfnptr_list.end(); ++vfnptr_iter)
1.669 {
1.670 - // :TODO: what??
1.671 + if (**vfnptr_iter == vfnptr)
1.672 + break;
1.673 + }
1.674 + if (vfnptr_iter == vfnptr_list.end())
1.675 + {
1.676 + SH_ASSERT(false, ("How can a hook exist on a vfnptr which we don't have in our db?!"));
1.677 }
1.678 else
1.679 {
1.680 - *origentry = vfnptr_iter->GetOrigEntry();
1.681 + *origentry = (*vfnptr_iter)->GetOrigEntry();
1.682 }
1.683
1.684 oldctx->pOrigRet = origRetPtr;
1.685 @@ -852,17 +804,22 @@
1.686
1.687 pCtx->pIface = NULL;
1.688
1.689 - List<CVfnPtr> &vfnptrList = static_cast<CHookManager*>(hi)->GetVfnPtrList();
1.690 - List<CVfnPtr>::iterator vfnptr_iter = vfnptrList.find(vfnptr);
1.691 - if (vfnptr_iter == vfnptrList.end())
1.692 + List<CVfnPtr*> &vfnptr_list = static_cast<CHookManager*>(hi)->GetVfnPtrList();
1.693 + for (List<CVfnPtr*>::iterator vfnptr_iter = vfnptr_list.begin();
1.694 + vfnptr_iter != vfnptr_list.end(); ++vfnptr_iter)
1.695 + {
1.696 + if (**vfnptr_iter == vfnptr)
1.697 + break;
1.698 + }
1.699 + if (vfnptr_iter == vfnptr_list.end())
1.700 {
1.701 pCtx->m_State = CHookContext::State_Dead;
1.702 }
1.703 else
1.704 {
1.705 - pCtx->pVfnPtr = &(*vfnptr_iter);
1.706 - *origentry = vfnptr_iter->GetOrigEntry();
1.707 - pCtx->pIface = vfnptr_iter->FindIface(thisptr);
1.708 + pCtx->pVfnPtr = *vfnptr_iter;
1.709 + *origentry = pCtx->pVfnPtr->GetOrigEntry();
1.710 + pCtx->pIface = pCtx->pVfnPtr->FindIface(thisptr);
1.711 }
1.712
1.713 pCtx->pStatus = statusPtr;
1.714 @@ -928,18 +885,9 @@
1.715 return false;
1.716 }
1.717
1.718 - // find hookman
1.719 - CHookManContainerList::iterator hmcl_iter = m_HookManContainers.find(
1.720 - CHookManagerContainer::Descriptor(hentry->vtbl_offs, hentry->vtbl_idx, hentry->proto));
1.721 -
1.722 - if (hmcl_iter == m_HookManContainers.end() || !(*hmcl_iter))
1.723 - return false;
1.724 -
1.725 - CHookManager &hookman = hmcl_iter->GetActiveHookManager();
1.726 -
1.727 // find vfnptr
1.728 - List<CVfnPtr>::iterator vfnptr_iter = hookman.GetVfnPtrList().find(hentry->vfnptr);
1.729 - if (vfnptr_iter == hookman.GetVfnPtrList().end())
1.730 + List<CVfnPtr>::iterator vfnptr_iter = m_VfnPtrs.find(hentry->vfnptr);
1.731 + if (vfnptr_iter == m_VfnPtrs.end())
1.732 return false;
1.733
1.734 // find iface