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