1.1 --- a/core-legacy/CSmmAPI.cpp Tue Nov 24 08:06:16 2009 -0600
1.2 +++ b/core-legacy/CSmmAPI.cpp Thu Dec 10 00:59:50 2009 -0600
1.3 @@ -14,6 +14,8 @@
1.4 #include "concommands.h"
1.5 #include "CPlugin.h"
1.6 #include "util.h"
1.7 +#include "sh_memory.h"
1.8 +#include <setjmp.h>
1.9
1.10 /**
1.11 * @brief Implementation of main API interface
1.12 @@ -21,22 +23,32 @@
1.13 */
1.14
1.15 using namespace SourceMM;
1.16 +using namespace SourceHook;
1.17 +
1.18 +struct UsrMsgInfo
1.19 +{
1.20 + UsrMsgInfo()
1.21 + {
1.22 + }
1.23 + UsrMsgInfo(int s, const char *t) : size(s), name(t)
1.24 + {
1.25 + }
1.26 + int size;
1.27 + String name;
1.28 +};
1.29
1.30 CSmmAPI g_SmmAPI;
1.31
1.32 +static CVector<UsrMsgInfo> usermsgs_list;
1.33 +static jmp_buf usermsg_end;
1.34 +
1.35 CSmmAPI::CSmmAPI()
1.36 {
1.37 m_ConPrintf = NULL;
1.38 m_CmdCache = false;
1.39 - m_MsgCount = -1;
1.40 m_VSP = false;
1.41 }
1.42
1.43 -CSmmAPI::~CSmmAPI()
1.44 -{
1.45 - m_UserMessages.RemoveAll();
1.46 -}
1.47 -
1.48 void CSmmAPI::LogMsg(ISmmPlugin *pl, const char *msg, ...)
1.49 {
1.50 va_list ap;
1.51 @@ -491,149 +503,96 @@
1.52 return g_GameDllVersion;
1.53 }
1.54
1.55 -//////////////////////////////////////////////////////////////////////
1.56 -// EVEN MORE HACKS HERE! YOU HAVE BEEN WARNED! //
1.57 -// Signatures necessary in finding the pointer to the CUtlDict that //
1.58 -// stores user message information. //
1.59 -// IServerGameDLL::GetUserMessageInfo() normally crashes with bad //
1.60 -// message indices. This is our answer to it. Yuck! <:-( //
1.61 -//////////////////////////////////////////////////////////////////////
1.62 -#ifdef OS_WIN32
1.63 - /* General Windows sig */
1.64 - #define MSGCLASS_SIGLEN 7
1.65 - #define MSGCLASS_SIG "\x8B\x0D\x2A\x2A\x2A\x2A\x56"
1.66 - #define MSGCLASS_OFFS 2
1.67 -
1.68 - /* Dystopia Wimdows hack */
1.69 - #define MSGCLASS2_SIGLEN 16
1.70 - #define MSGCLASS2_SIG "\x56\x8B\x74\x24\x2A\x85\xF6\x7C\x2A\x3B\x35\x2A\x2A\x2A\x2A\x7D"
1.71 - #define MSGCLASS2_OFFS 11
1.72 +/* This only gets called if IServerGameDLL::GetUserMessageInfo() triggers it */
1.73 +void Detour_Error(const tchar *pMsg, ...)
1.74 +{
1.75 + /* Jump back to setjmp() in CacheUserMessages() */
1.76 + longjmp(usermsg_end, 1);
1.77 +}
1.78
1.79 - /* Windows frame pointer sig */
1.80 - #define MSGCLASS3_SIGLEN 18
1.81 - #define MSGCLASS3_SIG "\x55\x8B\xEC\x51\x89\x2A\x2A\x8B\x2A\x2A\x50\x8B\x0D\x2A\x2A\x2A\x2A\xE8"
1.82 - #define MSGCLASS3_OFFS 13
1.83 -#elif defined OS_LINUX
1.84 - /* No frame pointer sig */
1.85 - #define MSGCLASS_SIGLEN 14
1.86 - #define MSGCLASS_SIG "\x53\x83\xEC\x2A\x8B\x2A\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
1.87 - #define MSGCLASS_OFFS 9
1.88 +/* IServerGameDLL::GetUserMessageInfo() crashes on games based on the old engine and
1.89 + * early Orange Box. This is because Error() from tier0 gets called when a bad index is
1.90 + * passed. This is all due to a bug in CUtlRBTree::IsValidIndex().
1.91 + *
1.92 + * So we detour Error() to fix this. Our detour then jumps back into CacheUserMessages()
1.93 + * to a point before GetUserMessageInfo() is called. The detour is then removed and we
1.94 + * exit.
1.95 + */
1.96 +void CSmmAPI::CacheUserMessages()
1.97 +{
1.98 + int q, size;
1.99 + char buffer[256];
1.100 + unsigned char *target, *detour;
1.101 + unsigned char orig_bytes[5];
1.102
1.103 - /* Frame pointer sig */
1.104 - #define MSGCLASS2_SIGLEN 16
1.105 - #define MSGCLASS2_SIG "\x55\x89\xE5\x53\x83\xEC\x2A\x8B\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
1.106 - #define MSGCLASS2_OFFS 11
1.107 -#endif
1.108 + target = (unsigned char *)&Error;
1.109 + detour = (unsigned char *)&Detour_Error;
1.110 +
1.111 + /* Save bytes from target function */
1.112 + memcpy(orig_bytes, target, sizeof(orig_bytes));
1.113
1.114 -/* This is the ugliest function in all of SourceMM */
1.115 -/* :TODO: Make this prettier */
1.116 -bool CSmmAPI::CacheUserMessages()
1.117 -{
1.118 - UserMsgDict *dict = NULL;
1.119 - char *vfunc = UTIL_GetOrigFunction(&IServerGameDLL::GetUserMessageInfo, g_GameDll.pGameDLL);
1.120 + /* Patch in relative jump to our Error() detour */
1.121 + SetMemAccess(target, sizeof(orig_bytes), SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
1.122 + target[0] = IA32_JMP_IMM32;
1.123 + *(int32_t *)&target[1] = (int32_t)(detour - (target + 5));
1.124
1.125 - if (!vfunc)
1.126 + /* This is where longjmp() will end up */
1.127 + if (setjmp(usermsg_end))
1.128 {
1.129 - return false;
1.130 + /* Restore bytes and memory protection */
1.131 + memcpy(target, orig_bytes, sizeof(orig_bytes));
1.132 + SetMemAccess(target, sizeof(orig_bytes), SH_MEM_READ|SH_MEM_EXEC);
1.133 + return;
1.134 }
1.135
1.136 - if (UTIL_VerifySignature(vfunc, MSGCLASS_SIG, MSGCLASS_SIGLEN))
1.137 - {
1.138 - /* Get address of CUserMessages instance */
1.139 - char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS_OFFS);
1.140 -
1.141 - /* Get address of CUserMessages::m_UserMessages */
1.142 - dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
1.143 - } else if (UTIL_VerifySignature(vfunc, MSGCLASS2_SIG, MSGCLASS2_SIGLEN)) {
1.144 - #ifdef OS_WIN32
1.145 - /* If we get here, the code is possibly inlined like in Dystopia */
1.146 -
1.147 - /* Get the address of the CUtlRBTree */
1.148 - char *rbtree = *reinterpret_cast<char **>(vfunc + MSGCLASS2_OFFS);
1.149 + q = 0;
1.150
1.151 - /* CUtlDict should be 8 bytes before the CUtlRBTree (hacktacular!) */
1.152 - dict = reinterpret_cast<UserMsgDict *>(rbtree - 8);
1.153 - #elif defined OS_LINUX
1.154 - /* Get address of CUserMessages instance */
1.155 - char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS2_OFFS);
1.156 -
1.157 - /* Get address of CUserMessages::m_UserMessages */
1.158 - dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
1.159 - #endif
1.160 - #ifdef OS_WIN32
1.161 - } else if (UTIL_VerifySignature(vfunc, MSGCLASS3_SIG, MSGCLASS3_SIGLEN)) {
1.162 - /* Get address of CUserMessages instance */
1.163 - char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS3_OFFS);
1.164 -
1.165 - /* Get address of CUserMessages::m_UserMessages */
1.166 - dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
1.167 - #endif
1.168 + /* If GetUserMessageInfo() calls Error(), we should end up in our detour */
1.169 + while (g_GameDll.pGameDLL->GetUserMessageInfo(q, buffer, sizeof(buffer), size))
1.170 + {
1.171 + usermsgs_list.push_back(UsrMsgInfo(size, buffer));
1.172 + q++;
1.173 }
1.174
1.175 - if (dict)
1.176 - {
1.177 - m_MsgCount = dict->Count();
1.178 -
1.179 - /* Ensure that count is within bounds of an unsigned byte, because that's what engine supports */
1.180 - if (m_MsgCount < 0 || m_MsgCount > 255)
1.181 - {
1.182 - m_MsgCount = -1;
1.183 - return false;
1.184 - }
1.185 -
1.186 - UserMessage *msg;
1.187 -
1.188 - /* Cache messages in our CUtlDict */
1.189 - for (int i = 0; i < m_MsgCount; i++)
1.190 - {
1.191 - msg = dict->Element(i);
1.192 - m_UserMessages.Insert(msg->name, msg);
1.193 - }
1.194 -
1.195 - return true;
1.196 - }
1.197 -
1.198 - return false;
1.199 -}
1.200 -
1.201 -bool CSmmAPI::MsgCacheSuccessful()
1.202 -{
1.203 - return m_MsgCount > -1;
1.204 + /* Jump back to setjmp() */
1.205 + longjmp(usermsg_end, 1);
1.206 }
1.207
1.208 int CSmmAPI::GetUserMessageCount()
1.209 {
1.210 - return m_MsgCount;
1.211 + return (int)usermsgs_list.size();
1.212 }
1.213
1.214 int CSmmAPI::FindUserMessage(const char *name, int *size)
1.215 {
1.216 - int index = m_UserMessages.Find(name);
1.217 -
1.218 - if (size && index > -1)
1.219 + for (size_t i = 0; i < usermsgs_list.size(); i++)
1.220 {
1.221 - UserMessage *msg = m_UserMessages.Element(index);
1.222 - *size = msg->size;
1.223 + if (usermsgs_list[i].name.compare(name) == 0)
1.224 + {
1.225 + if (size)
1.226 + {
1.227 + *size = usermsgs_list[i].size;
1.228 + }
1.229 + return (int)i;
1.230 + }
1.231 }
1.232
1.233 - return index;
1.234 + return -1;
1.235 }
1.236
1.237 const char *CSmmAPI::GetUserMessage(int index, int *size)
1.238 {
1.239 - if (m_MsgCount <= 0 || index < 0 || index >= m_MsgCount)
1.240 + if (index < 0 || index >= (int)usermsgs_list.size())
1.241 {
1.242 return NULL;
1.243 }
1.244
1.245 - UserMessage *msg = m_UserMessages.Element(index);
1.246 -
1.247 if (size)
1.248 {
1.249 - *size = msg->size;
1.250 + *size = usermsgs_list[index].size;
1.251 }
1.252
1.253 - return msg->name;
1.254 + return usermsgs_list[index].name.c_str();
1.255 }
1.256
1.257 IServerPluginCallbacks *CSmmAPI::GetVSPInfo(int *pVersion)