1.1 --- a/providers/episode1/provider_ep1.cpp Thu Sep 27 19:28:30 2007 +0000
1.2 +++ b/providers/episode1/provider_ep1.cpp Thu Sep 27 20:43:03 2007 +0000
1.3 @@ -1,23 +1,41 @@
1.4 #include "convar_smm.h"
1.5 #include <eiface.h>
1.6 #include <tier0/icommandline.h>
1.7 +#include <tier1/utldict.h>
1.8 #include <sourcehook/sourcehook.h>
1.9 +#include <sh_vector.h>
1.10 +#include <sh_string.h>
1.11 #include "provider_util.h"
1.12 #include "provider_ep1.h"
1.13 #include "console.h"
1.14
1.15 /* Types */
1.16 typedef void (*CONPRINTF_FUNC)(const char *, ...);
1.17 +struct UsrMsgInfo
1.18 +{
1.19 + int size;
1.20 + String name;
1.21 +};
1.22 /* Imports */
1.23 #undef CommandLine
1.24 DLL_IMPORT ICommandLine *CommandLine();
1.25 /* Functions */
1.26 CONPRINTF_FUNC ExtractRemotePrinter();
1.27 +bool CacheUserMessages();
1.28 +void ClientCommand(edict_t *pEdict);
1.29 /* Variables */
1.30 +bool usermsgs_extracted = false;
1.31 +CVector<UsrMsgInfo> usermsgs_list;
1.32 CONPRINTF_FUNC echo_msg_func = NULL;
1.33 +METAMOD_COMMAND client_cmd_handler = NULL;
1.34 +ICvar *icvar = NULL;
1.35 +ISourceHook *g_SHPtr = NULL;
1.36 IVEngineServer *engine = NULL;
1.37 IServerGameDLL *server = NULL;
1.38 -ICvar *icvar = NULL;
1.39 +IServerGameClients *gameclients = NULL;
1.40 +unsigned int g_PLID;
1.41 +
1.42 +SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
1.43
1.44 void BaseProvider::ConsolePrint(const char *str)
1.45 {
1.46 @@ -34,6 +52,9 @@
1.47 void BaseProvider::Notify_DLLInit_Pre()
1.48 {
1.49 echo_msg_func = ExtractRemotePrinter();
1.50 + usermsgs_extracted = CacheUserMessages();
1.51 +
1.52 + SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, ClientCommand, false);
1.53 }
1.54
1.55 bool BaseProvider::IsRemotePrintingAvailable()
1.56 @@ -177,6 +198,91 @@
1.57 return g_SMConVarAccessor.Unregister(pCommand);
1.58 }
1.59
1.60 +int BaseProvider::GetUserMessageCount()
1.61 +{
1.62 + if (!usermsgs_extracted)
1.63 + {
1.64 + return -1;
1.65 + }
1.66 +
1.67 + return (int)usermsgs_list.size();
1.68 +}
1.69 +
1.70 +int BaseProvider::FindUserMessage(const char *name, int *size)
1.71 +{
1.72 + for (size_t i = 0; i < usermsgs_list.size(); i++)
1.73 + {
1.74 + if (usermsgs_list[i].name.compare(name) == 0)
1.75 + {
1.76 + if (size)
1.77 + {
1.78 + *size = usermsgs_list[i].size;
1.79 + }
1.80 + return (int)i;
1.81 + }
1.82 + }
1.83 +
1.84 + return -1;
1.85 +}
1.86 +
1.87 +const char *BaseProvider::GetUserMessage(int index, int *size)
1.88 +{
1.89 + if (!usermsgs_extracted || index < 0 || index >= (int)usermsgs_list.size())
1.90 + {
1.91 + return NULL;
1.92 + }
1.93 +
1.94 + if (size)
1.95 + {
1.96 + *size = usermsgs_list[index].size;
1.97 + }
1.98 +
1.99 + return usermsgs_list[index].name.c_str();
1.100 +}
1.101 +
1.102 +const char *BaseProvider::GetGameDescription()
1.103 +{
1.104 + return server->GetGameDescription();
1.105 +}
1.106 +
1.107 +void BaseProvider::SetClientCommandHandler(METAMOD_COMMAND callback)
1.108 +{
1.109 + client_cmd_handler = callback;
1.110 +}
1.111 +
1.112 +class GlobCommand : public IMetamodSourceCommandInfo
1.113 +{
1.114 +public:
1.115 + unsigned int GetArgCount()
1.116 + {
1.117 + return engine->Cmd_Argc() - 1;
1.118 + }
1.119 +
1.120 + const char *GetArg(unsigned int num)
1.121 + {
1.122 + return engine->Cmd_Argv(num);
1.123 + }
1.124 +
1.125 + const char *GetArgString()
1.126 + {
1.127 + return engine->Cmd_Args();
1.128 + }
1.129 +};
1.130 +
1.131 +void ClientCommand(edict_t *pEdict)
1.132 +{
1.133 + if (client_cmd_handler)
1.134 + {
1.135 + GlobCommand cmd;
1.136 + bool result;
1.137 +
1.138 + if ((result = client_cmd_handler(pEdict, &cmd)) == true)
1.139 + {
1.140 + RETURN_META(MRES_SUPERCEDE);
1.141 + }
1.142 + }
1.143 +}
1.144 +
1.145 //////////////////////////////////////////////////////////////////////////
1.146 //THERE BE HAX HERE!!!! DON'T TELL ALFRED, BUT GABE WANTED IT THAT WAY. //
1.147 // (note: you can find the offset by looking for the text //
1.148 @@ -203,9 +309,13 @@
1.149 for (size_t i=0; i<len; i++)
1.150 {
1.151 if (addr2[i] == '*')
1.152 + {
1.153 continue;
1.154 + }
1.155 if (addr1[i] != addr2[i])
1.156 + {
1.157 return false;
1.158 + }
1.159 }
1.160
1.161 return true;
1.162 @@ -222,7 +332,7 @@
1.163
1.164 while (pBase)
1.165 {
1.166 - if ( strcmp(pBase->GetName(), "echo") == 0 )
1.167 + if (strcmp(pBase->GetName(), "echo") == 0)
1.168 {
1.169 callback = ((ConCommand *)pBase)->GetCallback();
1.170 ptr = (unsigned char *)callback;
1.171 @@ -261,4 +371,132 @@
1.172 return NULL;
1.173 }
1.174
1.175 +//////////////////////////////////////////////////////////////////////
1.176 +// EVEN MORE HACKS HERE! YOU HAVE BEEN WARNED! //
1.177 +// Signatures necessary in finding the pointer to the CUtlDict that //
1.178 +// stores user message information. //
1.179 +// IServerGameDLL::GetUserMessageInfo() normally crashes with bad //
1.180 +// message indices. This is our answer to it. Yuck! <:-( //
1.181 +//////////////////////////////////////////////////////////////////////
1.182 +#ifdef OS_WIN32
1.183 + /* General Windows sig */
1.184 + #define MSGCLASS_SIGLEN 7
1.185 + #define MSGCLASS_SIG "\x8B\x0D\x2A\x2A\x2A\x2A\x56"
1.186 + #define MSGCLASS_OFFS 2
1.187
1.188 + /* Dystopia Wimdows hack */
1.189 + #define MSGCLASS2_SIGLEN 16
1.190 + #define MSGCLASS2_SIG "\x56\x8B\x74\x24\x2A\x85\xF6\x7C\x2A\x3B\x35\x2A\x2A\x2A\x2A\x7D"
1.191 + #define MSGCLASS2_OFFS 11
1.192 +
1.193 + /* Windows frame pointer sig */
1.194 + #define MSGCLASS3_SIGLEN 18
1.195 + #define MSGCLASS3_SIG "\x55\x8B\xEC\x51\x89\x2A\x2A\x8B\x2A\x2A\x50\x8B\x0D\x2A\x2A\x2A\x2A\xE8"
1.196 + #define MSGCLASS3_OFFS 13
1.197 +#elif defined OS_LINUX
1.198 + /* No frame pointer sig */
1.199 + #define MSGCLASS_SIGLEN 14
1.200 + #define MSGCLASS_SIG "\x53\x83\xEC\x2A\x8B\x2A\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
1.201 + #define MSGCLASS_OFFS 9
1.202 +
1.203 + /* Frame pointer sig */
1.204 + #define MSGCLASS2_SIGLEN 16
1.205 + #define MSGCLASS2_SIG "\x55\x89\xE5\x53\x83\xEC\x2A\x8B\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
1.206 + #define MSGCLASS2_OFFS 11
1.207 +#endif
1.208 +
1.209 +struct UserMessage
1.210 +{
1.211 + int size;
1.212 + const char *name;
1.213 +};
1.214 +
1.215 +typedef CUtlDict<UserMessage *, int> UserMsgDict;
1.216 +
1.217 +/* This is the ugliest function in all of SourceMM */
1.218 +bool CacheUserMessages()
1.219 +{
1.220 + /* Get address of original GetUserMessageInfo() */
1.221 + char *vfunc = NULL;
1.222 + /* :TODO: fix this for v5 (char *)SH_GET_ORIG_VFNPTR_ENTRY(server, &IServerGameDLL::GetUserMessageInfo); */
1.223 +
1.224 + /* Oh dear, we have a relative jump on our hands
1.225 + * PVK II on Windows made me do this, but I suppose it doesn't hurt to check this on Linux too...
1.226 + */
1.227 + if (*vfunc == '\xE9')
1.228 + {
1.229 + /* Get address from displacement...
1.230 + *
1.231 + * Add 5 because it's relative to next instruction:
1.232 + * Opcode <1 byte> + 32-bit displacement <4 bytes>
1.233 + */
1.234 + vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5;
1.235 + }
1.236 +
1.237 + CUtlDict<UserMessage *, int> *dict = NULL;
1.238 +
1.239 + if (vcmp(vfunc, MSGCLASS_SIG, MSGCLASS_SIGLEN))
1.240 + {
1.241 + /* Get address of CUserMessages instance */
1.242 + char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS_OFFS);
1.243 +
1.244 + /* Get address of CUserMessages::m_UserMessages */
1.245 + dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
1.246 + }
1.247 + else if (vcmp(vfunc, MSGCLASS2_SIG, MSGCLASS2_SIGLEN))
1.248 + {
1.249 + #ifdef OS_WIN32
1.250 + /* If we get here, the code is possibly inlined like in Dystopia */
1.251 +
1.252 + /* Get the address of the CUtlRBTree */
1.253 + char *rbtree = *reinterpret_cast<char **>(vfunc + MSGCLASS2_OFFS);
1.254 +
1.255 + /* CUtlDict should be 8 bytes before the CUtlRBTree (hacktacular!) */
1.256 + dict = reinterpret_cast<UserMsgDict *>(rbtree - 8);
1.257 + #elif defined OS_LINUX
1.258 + /* Get address of CUserMessages instance */
1.259 + char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS2_OFFS);
1.260 +
1.261 + /* Get address of CUserMessages::m_UserMessages */
1.262 + dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
1.263 + #endif
1.264 + #ifdef OS_WIN32
1.265 + }
1.266 + else if (vcmp(vfunc, MSGCLASS3_SIG, MSGCLASS3_SIGLEN))
1.267 + {
1.268 + /* Get address of CUserMessages instance */
1.269 + char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS3_OFFS);
1.270 +
1.271 + /* Get address of CUserMessages::m_UserMessages */
1.272 + dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
1.273 + #endif
1.274 + }
1.275 +
1.276 + if (dict)
1.277 + {
1.278 + int msg_count = dict->Count();
1.279 +
1.280 + /* Ensure that count is within bounds of an unsigned byte, because that's what engine supports */
1.281 + if (msg_count < 0 || msg_count > 255)
1.282 + {
1.283 + return false;
1.284 + }
1.285 +
1.286 + UserMessage *msg;
1.287 + UsrMsgInfo u_msg;
1.288 +
1.289 + /* Cache messages in our CUtlDict */
1.290 + for (int i = 0; i < msg_count; i++)
1.291 + {
1.292 + msg = dict->Element(i);
1.293 + u_msg.name = msg->name;
1.294 + u_msg.size = msg->size;
1.295 + usermsgs_list.push_back(u_msg);
1.296 + }
1.297 +
1.298 + return true;
1.299 + }
1.300 +
1.301 + return false;
1.302 +}
1.303 +