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