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