1.1 --- a/sourcehook/sourcehook.cpp Tue Dec 06 09:37:57 2005 +0000
1.2 +++ b/sourcehook/sourcehook.cpp Sat Dec 10 22:37:53 2005 +0000
1.3 @@ -182,6 +182,93 @@
1.4 }
1.5 }
1.6
1.7 + void CSourceHookImpl::RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc)
1.8 + {
1.9 + // Moo!
1.10 +
1.11 + CHookManagerInfo tmp;
1.12 + if (pubFunc(HA_GetInfo, &tmp) != 0)
1.13 + return;
1.14 +
1.15 + HookManInfoList::iterator hmil_iter = FindHookMan(m_HookMans.begin(), m_HookMans.end(),
1.16 + tmp.m_Proto, tmp.m_VtblOffs, tmp.m_VtblIdx);
1.17 +
1.18 + if (hmil_iter == m_HookMans.end())
1.19 + {
1.20 + // Moo ?
1.21 + return;
1.22 + }
1.23 +
1.24 + bool stillInUse = false;
1.25 + List<RemoveHookInfo> hookstoremove;
1.26 +
1.27 + // Iterate through all of the hook manager's hooks. Remove all hooks from the hookman's plugin.
1.28 +#define TMP_CHECK_LIST(name, ispost) \
1.29 + for (hook_iter = iface_iter->name.m_List.begin(); hook_iter != iface_iter->name.m_List.end(); ++hook_iter) \
1.30 + if (hook_iter->plug == plug) \
1.31 + hookstoremove.push_back(RemoveHookInfo(hook_iter->plug, iface_iter->m_Ptr, \
1.32 + hook_iter->thisptr_offs, hmil_iter->m_Func, hook_iter->handler, ispost)); \
1.33 + else \
1.34 + stillInUse = true; \
1.35 +
1.36 + for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = hmil_iter->m_VfnPtrs.begin();
1.37 + vfnptr_iter != hmil_iter->m_VfnPtrs.end(); ++vfnptr_iter)
1.38 + {
1.39 + for (CVfnPtr::IfaceListIter iface_iter = vfnptr_iter->m_Ifaces.begin();
1.40 + iface_iter != vfnptr_iter->m_Ifaces.end(); ++iface_iter)
1.41 + {
1.42 + List<HookInfo>::iterator hook_iter;
1.43 + TMP_CHECK_LIST(m_PreHooks, false);
1.44 + TMP_CHECK_LIST(m_PostHooks, true);
1.45 + }
1.46 + }
1.47 +#undef TMP_CHECK_LIST
1.48 +
1.49 + for (List<RemoveHookInfo>::iterator rmiter = hookstoremove.begin(); rmiter != hookstoremove.end(); ++rmiter)
1.50 + RemoveHook(*rmiter);
1.51 +
1.52 + CHookManagerInfo info = *hmil_iter;
1.53 +
1.54 + // Unlink the hook manager from the list
1.55 + m_HookMans.erase(hmil_iter);
1.56 +
1.57 + // If there were any hooks from other plugins, find a new suitable hook manager.
1.58 + if (stillInUse)
1.59 + {
1.60 + // Find a suitable hook manager in an other plugin
1.61 + HookManInfoList::iterator newHookMan = FindHookMan(m_HookMans.begin(), m_HookMans.end(),
1.62 + info.m_Proto, info.m_VtblOffs, info.m_VtblIdx);
1.63 +
1.64 + // This should _never_ happen.
1.65 + // If there is a hook from an other plugin, the plugin must have provided a hook manager as well.
1.66 + SH_ASSERT(newHookMan != m_HookMans.end(),
1.67 + ("Could not find a suitable hook manager in an other plugin!"));
1.68 +
1.69 + // AddHook should make sure that every plugin only has _one_ hook manager for _one_ proto/vi/vo
1.70 + SH_ASSERT(newHookMan->m_Plug != plug, ("New hook manager from same plugin!"));
1.71 +
1.72 + // The first hook manager should be always used - so the new hook manager has to be empty
1.73 + SH_ASSERT(newHookMan->m_VfnPtrs.empty(), ("New hook manager not empty!"));
1.74 +
1.75 + // Move the vfnptrs from the old hook manager to the new one
1.76 + newHookMan->m_VfnPtrs = info.m_VfnPtrs;
1.77 +
1.78 + // Unregister the old one, register the new one
1.79 + info.m_Func(HA_Unregister, NULL);
1.80 + newHookMan->m_Func(HA_Register, &(*newHookMan));
1.81 +
1.82 + // Go through all vfnptrs in this hookman and patch them to point to the new manager's handler!
1.83 + // or whatever
1.84 + for (CHookManagerInfo::VfnPtrListIter vfnptr_iter = newHookMan->m_VfnPtrs.begin();
1.85 + vfnptr_iter != newHookMan->m_VfnPtrs.end(); ++vfnptr_iter)
1.86 + {
1.87 + // And DEREFERENCE newHookMan->m_HookfuncVfnptr!
1.88 + // otherwise it will be executing the vtable... had to find out the hard way
1.89 + *reinterpret_cast<void**>(vfnptr_iter->m_Ptr) = *reinterpret_cast<void**>(newHookMan->m_HookfuncVfnptr);
1.90 + }
1.91 + }
1.92 + }
1.93 +
1.94 void CSourceHookImpl::CompleteShutdown()
1.95 {
1.96 List<RemoveHookInfo> hookstoremove;
1.97 @@ -214,17 +301,22 @@
1.98 bool CSourceHookImpl::AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post)
1.99 {
1.100 void *adjustediface = reinterpret_cast<void*>(reinterpret_cast<char*>(iface) + thisptr_offs);
1.101 +
1.102 // 1) Get info about the hook manager
1.103 CHookManagerInfo tmp;
1.104 if (myHookMan(HA_GetInfo, &tmp) != 0)
1.105 return false;
1.106
1.107 + void **cur_vtptr = *reinterpret_cast<void***>(
1.108 + reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs);
1.109 + void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.m_VtblIdx);
1.110 +
1.111 // Add the proposed hook manager to the _end_ of the list if the plugin doesn't have a hook manager
1.112 // with this proto/vo/vi registered
1.113 HookManInfoList::iterator hkmi_iter;
1.114 for (hkmi_iter = m_HookMans.begin(); hkmi_iter != m_HookMans.end(); ++hkmi_iter)
1.115 {
1.116 - if (hkmi_iter->m_Plug == plug && strcmp(hkmi_iter->m_Proto, tmp.m_Proto) == 0 &&
1.117 + if (hkmi_iter->m_Plug == plug && ProtosEquiv(hkmi_iter->m_Proto, tmp.m_Proto) &&
1.118 hkmi_iter->m_VtblOffs == tmp.m_VtblOffs && hkmi_iter->m_VtblIdx == tmp.m_VtblIdx)
1.119 break;
1.120 }
1.121 @@ -241,14 +333,21 @@
1.122 tmp.m_VtblOffs, tmp.m_VtblIdx);
1.123 SH_ASSERT(hookman != m_HookMans.end(), ("No hookman found - but if there was none, we've just added one!"));
1.124
1.125 + // Check whether there is already an other hook manager for the same vfnptr but with other values before
1.126 + // If yes, it means that the two are incompatible, so return false.
1.127 + for (hkmi_iter = m_HookMans.begin(); hkmi_iter != m_HookMans.end(); ++hkmi_iter)
1.128 + {
1.129 + if (hkmi_iter != hookman)
1.130 + {
1.131 + if (hkmi_iter->m_VfnPtrs.find(cur_vfnptr) != hkmi_iter->m_VfnPtrs.end())
1.132 + return false;
1.133 + }
1.134 + }
1.135 +
1.136 // Tell it to store the pointer if it's not already active
1.137 if (hookman->m_VfnPtrs.empty())
1.138 hookman->m_Func(HA_Register, &(*hookman));
1.139
1.140 - void **cur_vtptr = *reinterpret_cast<void***>(
1.141 - reinterpret_cast<char*>(adjustediface) + tmp.m_VtblOffs);
1.142 - void *cur_vfnptr = reinterpret_cast<void*>(cur_vtptr + tmp.m_VtblIdx);
1.143 -
1.144 CHookManagerInfo::VfnPtrListIter vfnptr_iter = hookman->m_VfnPtrs.find(cur_vfnptr);
1.145
1.146 if (vfnptr_iter == hookman->m_VfnPtrs.end())
1.147 @@ -486,7 +585,7 @@
1.148 HookManInfoList::iterator hookmaniter;
1.149 for (hookmaniter = m_HookMans.begin(); hookmaniter != m_HookMans.end(); ++hookmaniter)
1.150 {
1.151 - if (strcmp(hookmaniter->m_Proto, proto) == 0 && hookmaniter->m_VtblOffs == vtblofs &&
1.152 + if (ProtosEquiv(hookmaniter->m_Proto, proto) && hookmaniter->m_VtblOffs == vtblofs &&
1.153 hookmaniter->m_VtblIdx == vtblidx)
1.154 break;
1.155 }
1.156 @@ -658,6 +757,65 @@
1.157 }
1.158 }
1.159
1.160 + bool CSourceHookImpl::ProtosEquiv(const char *p1, const char *p2)
1.161 + {
1.162 + /*
1.163 + Old protos look like this:
1.164 +
1.165 + 0_void:
1.166 + "attrib"
1.167 + 1_void:
1.168 + "attrib|param1_type"
1.169 + 2_void:
1.170 + "attrib|param1_type|param2_type
1.171 + 0:
1.172 + "attrib|ret_type"
1.173 + 1:
1.174 + "attrib|ret_type|param1_type"
1.175 + 2:
1.176 + "attrib|ret_type|param2_type"
1.177 +
1.178 + New protos are in fact pointers to the ProtoInfo sturcture (see sourcehook.h for details)
1.179 +
1.180 + Old protos _never_ begin with a null character
1.181 + New protos _always_ begin with a null character
1.182 + */
1.183 +
1.184 + if (*p1 && *p2) // Case1: Both old
1.185 + {
1.186 + // As in old versions
1.187 + return strcmp(p1, p2) == 0;
1.188 + }
1.189 + else if (!*p1 && !*p2) // Case2: Both new
1.190 + {
1.191 + const ProtoInfo *pi1 = reinterpret_cast<const ProtoInfo*>(p1);
1.192 + const ProtoInfo *pi2 = reinterpret_cast<const ProtoInfo*>(p2);
1.193 +
1.194 + if (pi1->retTypeSize == pi2->retTypeSize &&
1.195 + pi1->numOfParams == pi2->numOfParams)
1.196 + {
1.197 + // params[0] is 0 for "normal" functions and -1 for vararg functions
1.198 + // params[1] is size of first parameter
1.199 + // params[2] is size of second parameter ...
1.200 + for (int i = 0; i <= pi1->numOfParams; ++i)
1.201 + {
1.202 + if (pi1->params[i] != pi2->params[i])
1.203 + return false;
1.204 + }
1.205 + return true;
1.206 + }
1.207 + else
1.208 + {
1.209 + return false;
1.210 + }
1.211 + }
1.212 + else // Case3: Mixed old/new
1.213 + {
1.214 + // Trust the user
1.215 + return true;
1.216 + }
1.217 + }
1.218 +
1.219 ////////////////////////////
1.220 // CHookManagerInfo
1.221 ////////////////////////////
1.222 @@ -686,6 +844,8 @@
1.223 ////////////////////////////
1.224
1.225
1.226 + // If you get a crash here, the ptr passed is invalid
1.227 + // This usually means a SH_DECL_MANUALHOOK* with wrong thisptroffs/vtbloffs/vtblidx
1.228 CSourceHookImpl::CVfnPtr::CVfnPtr(void *ptr) : m_Ptr(ptr), m_OrigEntry(*reinterpret_cast<void**>(ptr))
1.229 {
1.230 }