1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcemm/episode1/provider_ep1.cpp Fri Sep 28 21:31:02 2007 +0000
1.3 @@ -0,0 +1,553 @@
1.4 +#include <sourcehook.h>
1.5 +#include "convar_smm.h"
1.6 +#include <eiface.h>
1.7 +#include <tier0/icommandline.h>
1.8 +#include <tier1/utldict.h>
1.9 +#include <sh_vector.h>
1.10 +#include <sh_string.h>
1.11 +#include "../metamod_util.h"
1.12 +#include "provider_ep1.h"
1.13 +#include "console.h"
1.14 +#include "metamod_console.h"
1.15 +
1.16 +/* Types */
1.17 +typedef void (*CONPRINTF_FUNC)(const char *, ...);
1.18 +struct UsrMsgInfo
1.19 +{
1.20 + int size;
1.21 + String name;
1.22 +};
1.23 +/* Imports */
1.24 +#undef CommandLine
1.25 +DLL_IMPORT ICommandLine *CommandLine();
1.26 +/* Functions */
1.27 +CONPRINTF_FUNC ExtractRemotePrinter();
1.28 +bool CacheUserMessages();
1.29 +void ClientCommand(edict_t *pEdict);
1.30 +void _ServerCommand();
1.31 +/* Variables */
1.32 +bool usermsgs_extracted = false;
1.33 +CVector<UsrMsgInfo> usermsgs_list;
1.34 +CONPRINTF_FUNC echo_msg_func = NULL;
1.35 +ICvar *icvar = NULL;
1.36 +ISmmAPI *metamod_api = NULL;
1.37 +IVEngineServer *engine = NULL;
1.38 +IServerGameClients *gameclients = NULL;
1.39 +BaseProvider g_Ep1Provider;
1.40 +IMetamodSourceProvider *provider = &g_Ep1Provider;
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 + if (echo_msg_func != NULL)
1.47 + {
1.48 + echo_msg_func("%s", str);
1.49 + }
1.50 + else
1.51 + {
1.52 + Msg("%s", str);
1.53 + }
1.54 +}
1.55 +
1.56 +void BaseProvider::Notify_DLLInit_Pre(void *gamedll,
1.57 + CreateInterfaceFn engineFactory,
1.58 + CreateInterfaceFn serverFactory)
1.59 +{
1.60 + server = (IServerGameDLL *)gamedll;
1.61 + engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
1.62 + if (!engine)
1.63 + {
1.64 + Error("Could not find IVEngineServer! Metamod cannot load.");
1.65 + return;
1.66 + }
1.67 + icvar = (ICvar *)((engineFactory)(VENGINE_CVAR_INTERFACE_VERSION, NULL));
1.68 + if (!icvar)
1.69 + {
1.70 + Error("Could not find ICvar! Metamod cannot load.");
1.71 + return;
1.72 + }
1.73 +
1.74 + if ((gameclients = (IServerGameClients *)(serverFactory("ServerGameClients003", NULL)))
1.75 + == NULL)
1.76 + {
1.77 + gameclients = (IServerGameClients *)(serverFactory("ServerGameClients004", NULL));
1.78 + }
1.79 +
1.80 + echo_msg_func = ExtractRemotePrinter();
1.81 + usermsgs_extracted = CacheUserMessages();
1.82 +
1.83 + if (gameclients)
1.84 + {
1.85 + SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, ClientCommand, false);
1.86 + }
1.87 +
1.88 + ConCommandBaseMgr::OneTimeInit(&g_SMConVarAccessor);
1.89 +}
1.90 +
1.91 +bool BaseProvider::IsRemotePrintingAvailable()
1.92 +{
1.93 + return (echo_msg_func != NULL);
1.94 +}
1.95 +
1.96 +void BaseProvider::ClientConsolePrint(edict_t *client, const char *message)
1.97 +{
1.98 + engine->ClientPrintf(client, message);
1.99 +}
1.100 +
1.101 +void BaseProvider::ServerCommand(const char *cmd)
1.102 +{
1.103 + engine->ServerCommand(cmd);
1.104 +}
1.105 +
1.106 +const char *BaseProvider::GetConVarString(ConVar *convar)
1.107 +{
1.108 + return convar->GetString();
1.109 +}
1.110 +
1.111 +bool BaseProvider::IsConCommandBaseACommand(ConCommandBase *pCommand)
1.112 +{
1.113 + return pCommand->IsCommand();
1.114 +}
1.115 +
1.116 +
1.117 +bool BaseProvider::IsSourceEngineBuildCompatible(int build)
1.118 +{
1.119 + return (build == SOURCE_ENGINE_ORIGINAL
1.120 + || build == SOURCE_ENGINE_EPISODEONE);
1.121 +}
1.122 +
1.123 +const char *BaseProvider::GetCommandLineValue(const char *key, const char *defval)
1.124 +{
1.125 + if (key[0] == '-' || key[0] == '+')
1.126 + {
1.127 + return CommandLine()->ParmValue(key, defval);
1.128 + }
1.129 + else
1.130 + {
1.131 + const char *val;
1.132 + if ((val = icvar->GetCommandLineValue(key)) == NULL)
1.133 + {
1.134 + return defval;
1.135 + }
1.136 +
1.137 + return val;
1.138 + }
1.139 +}
1.140 +
1.141 +int BaseProvider::TryServerGameDLL(const char *iface)
1.142 +{
1.143 + if (strncmp(iface, "ServerGameDLL", 13) != 0)
1.144 + {
1.145 + return 0;
1.146 + }
1.147 +
1.148 + return atoi(&iface[13]);
1.149 +}
1.150 +
1.151 +bool BaseProvider::LogMessage(const char *buffer)
1.152 +{
1.153 + if (!engine)
1.154 + {
1.155 + return false;
1.156 + }
1.157 +
1.158 + engine->LogPrint(buffer);
1.159 +
1.160 + return true;
1.161 +}
1.162 +
1.163 +bool BaseProvider::GetHookInfo(ProvidedHooks hook, SourceHook::MemFuncInfo *pInfo)
1.164 +{
1.165 + SourceHook::MemFuncInfo mfi = {true, -1, 0, 0};
1.166 +
1.167 + if (hook == ProvidedHook_LevelInit)
1.168 + {
1.169 + SourceHook::GetFuncInfo(&IServerGameDLL::LevelInit, mfi);
1.170 + }
1.171 + else if (hook == ProvidedHook_LevelShutdown)
1.172 + {
1.173 + SourceHook::GetFuncInfo(&IServerGameDLL::LevelShutdown, mfi);
1.174 + }
1.175 + else if (hook == ProvidedHook_GameInit)
1.176 + {
1.177 + SourceHook::GetFuncInfo(&IServerGameDLL::GameInit, mfi);
1.178 + }
1.179 + else if (hook == ProvidedHook_DLLShutdown)
1.180 + {
1.181 + SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfi);
1.182 + }
1.183 + else if (hook == ProvidedHook_DLLInit)
1.184 + {
1.185 + SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfi);
1.186 + }
1.187 +
1.188 + *pInfo = mfi;
1.189 +
1.190 + return (mfi.thisptroffs >= 0);
1.191 +}
1.192 +
1.193 +void BaseProvider::DisplayError(const char *fmt, ...)
1.194 +{
1.195 + va_list ap;
1.196 + char buffer[2048];
1.197 +
1.198 + va_start(ap, fmt);
1.199 + UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
1.200 + va_end(ap);
1.201 +
1.202 + Error(buffer);
1.203 +}
1.204 +
1.205 +void BaseProvider::DisplayWarning(const char *fmt, ...)
1.206 +{
1.207 + va_list ap;
1.208 + char buffer[2048];
1.209 +
1.210 + va_start(ap, fmt);
1.211 + UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
1.212 + va_end(ap);
1.213 +
1.214 + Warning(buffer);
1.215 +}
1.216 +
1.217 +IConCommandBaseAccessor *BaseProvider::GetConCommandBaseAccessor()
1.218 +{
1.219 + return &g_SMConVarAccessor;
1.220 +}
1.221 +
1.222 +bool BaseProvider::RegisterConCommandBase(ConCommandBase *pCommand)
1.223 +{
1.224 + return g_SMConVarAccessor.Register(pCommand);
1.225 +}
1.226 +
1.227 +void BaseProvider::UnregisterConCommandBase(ConCommandBase *pCommand)
1.228 +{
1.229 + return g_SMConVarAccessor.Unregister(pCommand);
1.230 +}
1.231 +
1.232 +int BaseProvider::GetUserMessageCount()
1.233 +{
1.234 + if (!usermsgs_extracted)
1.235 + {
1.236 + return -1;
1.237 + }
1.238 +
1.239 + return (int)usermsgs_list.size();
1.240 +}
1.241 +
1.242 +int BaseProvider::FindUserMessage(const char *name, int *size)
1.243 +{
1.244 + for (size_t i = 0; i < usermsgs_list.size(); i++)
1.245 + {
1.246 + if (usermsgs_list[i].name.compare(name) == 0)
1.247 + {
1.248 + if (size)
1.249 + {
1.250 + *size = usermsgs_list[i].size;
1.251 + }
1.252 + return (int)i;
1.253 + }
1.254 + }
1.255 +
1.256 + return -1;
1.257 +}
1.258 +
1.259 +const char *BaseProvider::GetUserMessage(int index, int *size)
1.260 +{
1.261 + if (!usermsgs_extracted || index < 0 || index >= (int)usermsgs_list.size())
1.262 + {
1.263 + return NULL;
1.264 + }
1.265 +
1.266 + if (size)
1.267 + {
1.268 + *size = usermsgs_list[index].size;
1.269 + }
1.270 +
1.271 + return usermsgs_list[index].name.c_str();
1.272 +}
1.273 +
1.274 +const char *BaseProvider::GetGameDescription()
1.275 +{
1.276 + return server->GetGameDescription();
1.277 +}
1.278 +
1.279 +ConVar *BaseProvider::CreateConVar(const char *name,
1.280 + const char *defval,
1.281 + const char *help,
1.282 + int flags)
1.283 +{
1.284 + int newflags = 0;
1.285 + if (flags & ConVarFlag_Notify)
1.286 + {
1.287 + newflags |= FCVAR_NOTIFY;
1.288 + }
1.289 + if (flags & ConVarFlag_Replicated)
1.290 + {
1.291 + newflags |= FCVAR_REPLICATED;
1.292 + }
1.293 + if (flags & ConVarFlag_SpOnly)
1.294 + {
1.295 + newflags |= FCVAR_SPONLY;
1.296 + }
1.297 +
1.298 + return new ConVar(name, defval, newflags, help);
1.299 +}
1.300 +
1.301 +IServerPluginCallbacks *BaseProvider::GetVSPCallbacks(const char *iface)
1.302 +{
1.303 + return NULL;
1.304 +}
1.305 +
1.306 +class GlobCommand : public IMetamodSourceCommandInfo
1.307 +{
1.308 +public:
1.309 + unsigned int GetArgCount()
1.310 + {
1.311 + return engine->Cmd_Argc() - 1;
1.312 + }
1.313 +
1.314 + const char *GetArg(unsigned int num)
1.315 + {
1.316 + return engine->Cmd_Argv(num);
1.317 + }
1.318 +
1.319 + const char *GetArgString()
1.320 + {
1.321 + return engine->Cmd_Args();
1.322 + }
1.323 +};
1.324 +
1.325 +CON_COMMAND(meta, "Metamod:Source control commands")
1.326 +{
1.327 + GlobCommand cmd;
1.328 + Command_Meta(&cmd);
1.329 +}
1.330 +
1.331 +void ClientCommand(edict_t *pEdict)
1.332 +{
1.333 + GlobCommand cmd;
1.334 +
1.335 + if (strcmp(cmd.GetArg(0), "meta") == 0)
1.336 + {
1.337 + Command_ClientMeta(pEdict, &cmd);
1.338 + }
1.339 +}
1.340 +
1.341 +//////////////////////////////////////////////////////////////////////////
1.342 +//THERE BE HAX HERE!!!! DON'T TELL ALFRED, BUT GABE WANTED IT THAT WAY. //
1.343 +// (note: you can find the offset by looking for the text //
1.344 +// "Echo text to console", you'll find the callback cmd pushed on the //
1.345 +// stack.) //
1.346 +//////////////////////////////////////////////////////////////////////////
1.347 +
1.348 +#define SIGLEN 8
1.349 +#define ENGINE486_SIG "\x55\x89\xE5\x53\x83\xEC\x14\xBB"
1.350 +#define ENGINE486_OFFS 40
1.351 +#define ENGINE686_SIG "\x53\x83\xEC\x08\xBB\x01\x00\x00"
1.352 +#define ENGINE686_OFFS 50
1.353 +#define ENGINEAMD_SIG "\x53\x51\xBB\x01\x00\x00\x00\x51"
1.354 +#define ENGINEAMD_OFFS 47
1.355 +#define ENGINEW32_SIG "\xA1\x2A\x2A\x2A\x2A\x56\xBE\x01"
1.356 +#define ENGINEW32_OFFS 38
1.357 +#define IA32_CALL 0xE8
1.358 +
1.359 +bool vcmp(const void *_addr1, const void *_addr2, size_t len)
1.360 +{
1.361 + unsigned char *addr1 = (unsigned char *)_addr1;
1.362 + unsigned char *addr2 = (unsigned char *)_addr2;
1.363 +
1.364 + for (size_t i=0; i<len; i++)
1.365 + {
1.366 + if (addr2[i] == '*')
1.367 + {
1.368 + continue;
1.369 + }
1.370 + if (addr1[i] != addr2[i])
1.371 + {
1.372 + return false;
1.373 + }
1.374 + }
1.375 +
1.376 + return true;
1.377 +}
1.378 +
1.379 +//Thanks to fysh for the idea of extracting info from "echo" and for
1.380 +// having the original offsets at hand!
1.381 +CONPRINTF_FUNC ExtractRemotePrinter()
1.382 +{
1.383 + ConCommandBase *pBase = icvar->GetCommands();
1.384 + unsigned char *ptr = NULL;
1.385 + FnCommandCallback callback = NULL;
1.386 + int offs = 0;
1.387 +
1.388 + while (pBase)
1.389 + {
1.390 + if (strcmp(pBase->GetName(), "echo") == 0)
1.391 + {
1.392 + callback = ((ConCommand *)pBase)->GetCallback();
1.393 + ptr = (unsigned char *)callback;
1.394 +#ifdef OS_LINUX
1.395 + if (vcmp(ptr, ENGINE486_SIG, SIGLEN))
1.396 + {
1.397 + offs = ENGINE486_OFFS;
1.398 + }
1.399 + else if (vcmp(ptr, ENGINE686_SIG, SIGLEN))
1.400 + {
1.401 + offs = ENGINE686_OFFS;
1.402 + }
1.403 + else if (vcmp(ptr, ENGINEAMD_SIG, SIGLEN))
1.404 + {
1.405 + offs = ENGINEAMD_OFFS;
1.406 + }
1.407 +#elif defined OS_WIN32 // Only one Windows engine binary so far...
1.408 + if (vcmp(ptr, ENGINEW32_SIG, SIGLEN))
1.409 + {
1.410 + offs = ENGINEW32_OFFS;
1.411 + }
1.412 +#endif
1.413 +
1.414 + if (!offs || ptr[offs - 1] != IA32_CALL)
1.415 + {
1.416 + return NULL;
1.417 + }
1.418 + //get the relative offset
1.419 + void *addr = *((void **)(ptr + offs));
1.420 + //add the base offset, to the ip (which is the address+offset + 4 bytes for next instruction)
1.421 + return (CONPRINTF_FUNC)((unsigned long)addr + (unsigned long)(ptr + offs) + 4);
1.422 + }
1.423 + pBase = const_cast<ConCommandBase *>(pBase->GetNext());
1.424 + }
1.425 +
1.426 + return NULL;
1.427 +}
1.428 +
1.429 +//////////////////////////////////////////////////////////////////////
1.430 +// EVEN MORE HACKS HERE! YOU HAVE BEEN WARNED! //
1.431 +// Signatures necessary in finding the pointer to the CUtlDict that //
1.432 +// stores user message information. //
1.433 +// IServerGameDLL::GetUserMessageInfo() normally crashes with bad //
1.434 +// message indices. This is our answer to it. Yuck! <:-( //
1.435 +//////////////////////////////////////////////////////////////////////
1.436 +#ifdef OS_WIN32
1.437 + /* General Windows sig */
1.438 + #define MSGCLASS_SIGLEN 7
1.439 + #define MSGCLASS_SIG "\x8B\x0D\x2A\x2A\x2A\x2A\x56"
1.440 + #define MSGCLASS_OFFS 2
1.441 +
1.442 + /* Dystopia Wimdows hack */
1.443 + #define MSGCLASS2_SIGLEN 16
1.444 + #define MSGCLASS2_SIG "\x56\x8B\x74\x24\x2A\x85\xF6\x7C\x2A\x3B\x35\x2A\x2A\x2A\x2A\x7D"
1.445 + #define MSGCLASS2_OFFS 11
1.446 +
1.447 + /* Windows frame pointer sig */
1.448 + #define MSGCLASS3_SIGLEN 18
1.449 + #define MSGCLASS3_SIG "\x55\x8B\xEC\x51\x89\x2A\x2A\x8B\x2A\x2A\x50\x8B\x0D\x2A\x2A\x2A\x2A\xE8"
1.450 + #define MSGCLASS3_OFFS 13
1.451 +#elif defined OS_LINUX
1.452 + /* No frame pointer sig */
1.453 + #define MSGCLASS_SIGLEN 14
1.454 + #define MSGCLASS_SIG "\x53\x83\xEC\x2A\x8B\x2A\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
1.455 + #define MSGCLASS_OFFS 9
1.456 +
1.457 + /* Frame pointer sig */
1.458 + #define MSGCLASS2_SIGLEN 16
1.459 + #define MSGCLASS2_SIG "\x55\x89\xE5\x53\x83\xEC\x2A\x8B\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
1.460 + #define MSGCLASS2_OFFS 11
1.461 +#endif
1.462 +
1.463 +struct UserMessage
1.464 +{
1.465 + int size;
1.466 + const char *name;
1.467 +};
1.468 +
1.469 +typedef CUtlDict<UserMessage *, int> UserMsgDict;
1.470 +
1.471 +/* This is the ugliest function in all of SourceMM */
1.472 +bool CacheUserMessages()
1.473 +{
1.474 + /* Get address of original GetUserMessageInfo() */
1.475 + char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(server, &IServerGameDLL::GetUserMessageInfo);
1.476 +
1.477 + /* Oh dear, we have a relative jump on our hands
1.478 + * PVK II on Windows made me do this, but I suppose it doesn't hurt to check this on Linux too...
1.479 + */
1.480 + if (*vfunc == '\xE9')
1.481 + {
1.482 + /* Get address from displacement...
1.483 + *
1.484 + * Add 5 because it's relative to next instruction:
1.485 + * Opcode <1 byte> + 32-bit displacement <4 bytes>
1.486 + */
1.487 + vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5;
1.488 + }
1.489 +
1.490 + CUtlDict<UserMessage *, int> *dict = NULL;
1.491 +
1.492 + if (vcmp(vfunc, MSGCLASS_SIG, MSGCLASS_SIGLEN))
1.493 + {
1.494 + /* Get address of CUserMessages instance */
1.495 + char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS_OFFS);
1.496 +
1.497 + /* Get address of CUserMessages::m_UserMessages */
1.498 + dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
1.499 + }
1.500 + else if (vcmp(vfunc, MSGCLASS2_SIG, MSGCLASS2_SIGLEN))
1.501 + {
1.502 + #ifdef OS_WIN32
1.503 + /* If we get here, the code is possibly inlined like in Dystopia */
1.504 +
1.505 + /* Get the address of the CUtlRBTree */
1.506 + char *rbtree = *reinterpret_cast<char **>(vfunc + MSGCLASS2_OFFS);
1.507 +
1.508 + /* CUtlDict should be 8 bytes before the CUtlRBTree (hacktacular!) */
1.509 + dict = reinterpret_cast<UserMsgDict *>(rbtree - 8);
1.510 + #elif defined OS_LINUX
1.511 + /* Get address of CUserMessages instance */
1.512 + char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS2_OFFS);
1.513 +
1.514 + /* Get address of CUserMessages::m_UserMessages */
1.515 + dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
1.516 + #endif
1.517 + #ifdef OS_WIN32
1.518 + }
1.519 + else if (vcmp(vfunc, MSGCLASS3_SIG, MSGCLASS3_SIGLEN))
1.520 + {
1.521 + /* Get address of CUserMessages instance */
1.522 + char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS3_OFFS);
1.523 +
1.524 + /* Get address of CUserMessages::m_UserMessages */
1.525 + dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
1.526 + #endif
1.527 + }
1.528 +
1.529 + if (dict)
1.530 + {
1.531 + int msg_count = dict->Count();
1.532 +
1.533 + /* Ensure that count is within bounds of an unsigned byte, because that's what engine supports */
1.534 + if (msg_count < 0 || msg_count > 255)
1.535 + {
1.536 + return false;
1.537 + }
1.538 +
1.539 + UserMessage *msg;
1.540 + UsrMsgInfo u_msg;
1.541 +
1.542 + /* Cache messages in our CUtlDict */
1.543 + for (int i = 0; i < msg_count; i++)
1.544 + {
1.545 + msg = dict->Element(i);
1.546 + u_msg.name = msg->name;
1.547 + u_msg.size = msg->size;
1.548 + usermsgs_list.push_back(u_msg);
1.549 + }
1.550 +
1.551 + return true;
1.552 + }
1.553 +
1.554 + return false;
1.555 +}
1.556 +