4 * ======================================================
6 * Copyright (C) 2004-2007 AlliedModders LLC and authors.
8 * ======================================================
10 * This software is provided 'as-is', without any express or implied warranty.
11 * In no event will the authors be held liable for any damages arising from
12 * the use of this software.
14 * Permission is granted to anyone to use this software for any purpose,
15 * including commercial applications, and to alter it and redistribute it
16 * freely, subject to the following restrictions:
18 * 1. The origin of this software must not be misrepresented; you must not
19 * claim that you wrote the original software. If you use this software in a
20 * product, an acknowledgment in the product documentation would be
21 * appreciated but is not required.
22 * 2. Altered source versions must be plainly marked as such, and must not be
23 * misrepresented as being the original software.
24 * 3. This notice may not be removed or altered from any source distribution.
33 #include <sourcehook.h>
36 #include <tier0/icommandline.h>
37 #include <tier1/utldict.h>
38 #include <sh_vector.h>
39 #include <sh_string.h>
40 #include "../metamod_util.h"
41 #include "provider_ep2.h"
43 #include "metamod_console.h"
44 #include "vsp_listener.h"
51 typedef void (*CONPRINTF_FUNC)(const char *, ...);
59 DLL_IMPORT ICommandLine *CommandLine();
61 bool CacheUserMessages();
62 void ClientCommand(edict_t *pEdict, const CCommand &args);
63 void LocalCommand_Meta(const CCommand &args);
64 void _ServerCommand();
66 bool usermsgs_extracted = false;
67 CVector<UsrMsgInfo> usermsgs_list;
69 ISmmAPI *metamod_api = NULL;
70 IVEngineServer *engine = NULL;
71 IServerGameClients *gameclients = NULL;
72 VSPListener g_VspListener;
73 BaseProvider g_Ep1Provider;
74 IMetamodSourceProvider *provider = &g_Ep1Provider;
75 List<ConCommandBase *> conbases_unreg;
76 ConCommand meta_local_cmd("meta", LocalCommand_Meta, "Metamod:Source control options");
78 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
80 void BaseProvider::ConsolePrint(const char *str)
85 void BaseProvider::Notify_DLLInit_Pre(void *gamedll,
86 CreateInterfaceFn engineFactory,
87 CreateInterfaceFn serverFactory)
89 server = (IServerGameDLL *)gamedll;
90 engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
93 DisplayError("Could not find IVEngineServer! Metamod cannot load.");
96 icvar = (ICvar *)((engineFactory)(CVAR_INTERFACE_VERSION, NULL));
99 DisplayError("Could not find ICvar! Metamod cannot load.");
104 if ((gameclients = (IServerGameClients *)(serverFactory("ServerGameClients003", NULL)))
107 gameclients = (IServerGameClients *)(serverFactory("ServerGameClients004", NULL));
110 RegisterConCommandBase(&meta_local_cmd);
111 conbases_unreg.push_back(&meta_local_cmd);
113 usermsgs_extracted = CacheUserMessages();
117 SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, ClientCommand, false);
121 void BaseProvider::Notify_DLLShutdown_Pre()
123 List<ConCommandBase *>::iterator iter;
125 for (iter = conbases_unreg.begin();
126 iter != conbases_unreg.end();
129 UnregisterConCommandBase((*iter));
133 bool BaseProvider::IsRemotePrintingAvailable()
138 void BaseProvider::ClientConsolePrint(edict_t *client, const char *message)
140 engine->ClientPrintf(client, message);
143 void BaseProvider::ServerCommand(const char *cmd)
145 engine->ServerCommand(cmd);
148 const char *BaseProvider::GetConVarString(ConVar *convar)
155 return convar->GetString();
158 bool BaseProvider::IsConCommandBaseACommand(ConCommandBase *pCommand)
160 return pCommand->IsCommand();
164 bool BaseProvider::IsSourceEngineBuildCompatible(int build)
166 return (build == SOURCE_ENGINE_ORIGINAL
167 || build == SOURCE_ENGINE_EPISODEONE);
170 const char *BaseProvider::GetCommandLineValue(const char *key, const char *defval)
172 if (key[0] == '-' || key[0] == '+')
174 return CommandLine_Tier0()->ParmValue(key, defval);
179 if ((val = icvar->GetCommandLineValue(key)) == NULL)
190 int BaseProvider::TryServerGameDLL(const char *iface)
192 if (strncmp(iface, "ServerGameDLL", 13) != 0)
197 return atoi(&iface[13]);
200 bool BaseProvider::LogMessage(const char *buffer)
207 engine->LogPrint(buffer);
212 bool BaseProvider::GetHookInfo(ProvidedHooks hook, SourceHook::MemFuncInfo *pInfo)
214 SourceHook::MemFuncInfo mfi = {true, -1, 0, 0};
216 if (hook == ProvidedHook_LevelInit)
218 SourceHook::GetFuncInfo(&IServerGameDLL::LevelInit, mfi);
220 else if (hook == ProvidedHook_LevelShutdown)
222 SourceHook::GetFuncInfo(&IServerGameDLL::LevelShutdown, mfi);
224 else if (hook == ProvidedHook_GameInit)
226 SourceHook::GetFuncInfo(&IServerGameDLL::GameInit, mfi);
228 else if (hook == ProvidedHook_DLLShutdown)
230 SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfi);
232 else if (hook == ProvidedHook_DLLInit)
234 SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfi);
239 return (mfi.thisptroffs >= 0);
242 void BaseProvider::DisplayError(const char *fmt, ...)
248 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
254 void BaseProvider::DisplayWarning(const char *fmt, ...)
260 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
266 IConCommandBaseAccessor *BaseProvider::GetConCommandBaseAccessor()
268 return &g_SMConVarAccessor;
271 bool BaseProvider::RegisterConCommandBase(ConCommandBase *pCommand)
273 return g_SMConVarAccessor.Register(pCommand);
276 void BaseProvider::UnregisterConCommandBase(ConCommandBase *pCommand)
278 return g_SMConVarAccessor.Unregister(pCommand);
281 int BaseProvider::GetUserMessageCount()
283 if (!usermsgs_extracted)
288 return (int)usermsgs_list.size();
291 int BaseProvider::FindUserMessage(const char *name, int *size)
293 for (size_t i = 0; i < usermsgs_list.size(); i++)
295 if (usermsgs_list[i].name.compare(name) == 0)
299 *size = usermsgs_list[i].size;
308 const char *BaseProvider::GetUserMessage(int index, int *size)
310 if (!usermsgs_extracted || index < 0 || index >= (int)usermsgs_list.size())
317 *size = usermsgs_list[index].size;
320 return usermsgs_list[index].name.c_str();
323 const char *BaseProvider::GetGameDescription()
325 return server->GetGameDescription();
328 int BaseProvider::DetermineSourceEngine(const char *game)
330 return SOURCE_ENGINE_ORANGEBOX;
333 ConVar *BaseProvider::CreateConVar(const char *name,
339 if (flags & ConVarFlag_Notify)
341 newflags |= FCVAR_NOTIFY;
343 if (flags & ConVarFlag_Replicated)
345 newflags |= FCVAR_REPLICATED;
347 if (flags & ConVarFlag_SpOnly)
349 newflags |= FCVAR_SPONLY;
352 ConVar *pVar = new ConVar(name, defval, newflags, help);
354 RegisterConCommandBase(pVar);
355 conbases_unreg.push_back(pVar);
360 IServerPluginCallbacks *BaseProvider::GetVSPCallbacks(const char *iface)
362 g_VspListener.SetLoadable(true);
363 return &g_VspListener;
366 class GlobCommand : public IMetamodSourceCommandInfo
369 GlobCommand(const CCommand *cmd) : m_cmd(cmd)
373 unsigned int GetArgCount()
375 return m_cmd->ArgC();
378 const char *GetArg(unsigned int num)
380 return m_cmd->Arg(num);
383 const char *GetArgString()
385 return m_cmd->ArgS();
388 const CCommand *m_cmd;
391 void LocalCommand_Meta(const CCommand &args)
393 GlobCommand cmd(&args);
397 void ClientCommand(edict_t *pEdict, const CCommand &_cmd)
399 GlobCommand cmd(&_cmd);
401 if (strcmp(cmd.GetArg(0), "meta") == 0)
403 Command_ClientMeta(pEdict, &cmd);
407 bool vcmp(const void *_addr1, const void *_addr2, size_t len)
409 unsigned char *addr1 = (unsigned char *)_addr1;
410 unsigned char *addr2 = (unsigned char *)_addr2;
412 for (size_t i=0; i<len; i++)
418 if (addr1[i] != addr2[i])
427 //////////////////////////////////////////////////////////////////////
428 // EVEN MORE HACKS HERE! YOU HAVE BEEN WARNED! //
429 // Signatures necessary in finding the pointer to the CUtlDict that //
430 // stores user message information. //
431 // IServerGameDLL::GetUserMessageInfo() normally crashes with bad //
432 // message indices. This is our answer to it. Yuck! <:-( //
433 //////////////////////////////////////////////////////////////////////
435 /* General Windows sig */
436 #define MSGCLASS_SIGLEN 7
437 #define MSGCLASS_SIG "\x8B\x0D\x2A\x2A\x2A\x2A\x56"
438 #define MSGCLASS_OFFS 2
440 /* Dystopia Wimdows hack */
441 #define MSGCLASS2_SIGLEN 16
442 #define MSGCLASS2_SIG "\x56\x8B\x74\x24\x2A\x85\xF6\x7C\x2A\x3B\x35\x2A\x2A\x2A\x2A\x7D"
443 #define MSGCLASS2_OFFS 11
445 /* Windows frame pointer sig */
446 #define MSGCLASS3_SIGLEN 18
447 #define MSGCLASS3_SIG "\x55\x8B\xEC\x51\x89\x2A\x2A\x8B\x2A\x2A\x50\x8B\x0D\x2A\x2A\x2A\x2A\xE8"
448 #define MSGCLASS3_OFFS 13
449 #elif defined OS_LINUX
450 /* No frame pointer sig */
451 #define MSGCLASS_SIGLEN 14
452 #define MSGCLASS_SIG "\x53\x83\xEC\x2A\x8B\x2A\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
453 #define MSGCLASS_OFFS 9
455 /* Frame pointer sig */
456 #define MSGCLASS2_SIGLEN 16
457 #define MSGCLASS2_SIG "\x55\x89\xE5\x53\x83\xEC\x2A\x8B\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
458 #define MSGCLASS2_OFFS 11
467 typedef CUtlDict<UserMessage *, int> UserMsgDict;
469 /* This is the ugliest function in all of SourceMM */
470 bool CacheUserMessages()
472 /* Get address of original GetUserMessageInfo() */
473 char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(server, &IServerGameDLL::GetUserMessageInfo);
475 /* Oh dear, we have a relative jump on our hands
476 * PVK II on Windows made me do this, but I suppose it doesn't hurt to check this on Linux too...
478 if (*vfunc == '\xE9')
480 /* Get address from displacement...
482 * Add 5 because it's relative to next instruction:
483 * Opcode <1 byte> + 32-bit displacement <4 bytes>
485 vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5;
488 CUtlDict<UserMessage *, int> *dict = NULL;
490 if (vcmp(vfunc, MSGCLASS_SIG, MSGCLASS_SIGLEN))
492 /* Get address of CUserMessages instance */
493 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS_OFFS);
495 /* Get address of CUserMessages::m_UserMessages */
496 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
498 else if (vcmp(vfunc, MSGCLASS2_SIG, MSGCLASS2_SIGLEN))
501 /* If we get here, the code is possibly inlined like in Dystopia */
503 /* Get the address of the CUtlRBTree */
504 char *rbtree = *reinterpret_cast<char **>(vfunc + MSGCLASS2_OFFS);
506 /* CUtlDict should be 8 bytes before the CUtlRBTree (hacktacular!) */
507 dict = reinterpret_cast<UserMsgDict *>(rbtree - 8);
508 #elif defined OS_LINUX
509 /* Get address of CUserMessages instance */
510 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS2_OFFS);
512 /* Get address of CUserMessages::m_UserMessages */
513 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
517 else if (vcmp(vfunc, MSGCLASS3_SIG, MSGCLASS3_SIGLEN))
519 /* Get address of CUserMessages instance */
520 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS3_OFFS);
522 /* Get address of CUserMessages::m_UserMessages */
523 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
527 #if !defined OS_WIN32
531 if (GetFileOfAddress(vfunc, path, sizeof(path)))
533 void *handle = dlopen(path, RTLD_NOW);
536 void *addr = dlsym(handle, "usermessages");
537 dict = (UserMsgDict *)*(void **)addr;
546 int msg_count = dict->Count();
548 /* Ensure that count is within bounds of an unsigned byte, because that's what engine supports */
549 if (msg_count < 0 || msg_count > 255)
557 /* Cache messages in our CUtlDict */
558 for (int i = 0; i < msg_count; i++)
560 msg = dict->Element(i);
561 u_msg.name = msg->name;
562 u_msg.size = msg->size;
563 usermsgs_list.push_back(u_msg);