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.");
103 if ((gameclients = (IServerGameClients *)(serverFactory("ServerGameClients003", NULL)))
106 gameclients = (IServerGameClients *)(serverFactory("ServerGameClients004", NULL));
109 RegisterConCommandBase(&meta_local_cmd);
110 conbases_unreg.push_back(&meta_local_cmd);
112 usermsgs_extracted = CacheUserMessages();
116 SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, ClientCommand, false);
120 void BaseProvider::Notify_DLLShutdown_Pre()
122 List<ConCommandBase *>::iterator iter;
124 for (iter = conbases_unreg.begin();
125 iter != conbases_unreg.end();
128 UnregisterConCommandBase((*iter));
132 bool BaseProvider::IsRemotePrintingAvailable()
137 void BaseProvider::ClientConsolePrint(edict_t *client, const char *message)
139 engine->ClientPrintf(client, message);
142 void BaseProvider::ServerCommand(const char *cmd)
144 engine->ServerCommand(cmd);
147 const char *BaseProvider::GetConVarString(ConVar *convar)
154 return convar->GetString();
157 bool BaseProvider::IsConCommandBaseACommand(ConCommandBase *pCommand)
159 return pCommand->IsCommand();
163 bool BaseProvider::IsSourceEngineBuildCompatible(int build)
165 return (build == SOURCE_ENGINE_ORIGINAL
166 || build == SOURCE_ENGINE_EPISODEONE);
169 const char *BaseProvider::GetCommandLineValue(const char *key, const char *defval)
171 if (key[0] == '-' || key[0] == '+')
173 return CommandLine_Tier0()->ParmValue(key, defval);
178 if ((val = icvar->GetCommandLineValue(key)) == NULL)
189 int BaseProvider::TryServerGameDLL(const char *iface)
191 if (strncmp(iface, "ServerGameDLL", 13) != 0)
196 return atoi(&iface[13]);
199 bool BaseProvider::LogMessage(const char *buffer)
206 engine->LogPrint(buffer);
211 bool BaseProvider::GetHookInfo(ProvidedHooks hook, SourceHook::MemFuncInfo *pInfo)
213 SourceHook::MemFuncInfo mfi = {true, -1, 0, 0};
215 if (hook == ProvidedHook_LevelInit)
217 SourceHook::GetFuncInfo(&IServerGameDLL::LevelInit, mfi);
219 else if (hook == ProvidedHook_LevelShutdown)
221 SourceHook::GetFuncInfo(&IServerGameDLL::LevelShutdown, mfi);
223 else if (hook == ProvidedHook_GameInit)
225 SourceHook::GetFuncInfo(&IServerGameDLL::GameInit, mfi);
227 else if (hook == ProvidedHook_DLLShutdown)
229 SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfi);
231 else if (hook == ProvidedHook_DLLInit)
233 SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfi);
238 return (mfi.thisptroffs >= 0);
241 void BaseProvider::DisplayError(const char *fmt, ...)
247 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
253 void BaseProvider::DisplayWarning(const char *fmt, ...)
259 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
265 IConCommandBaseAccessor *BaseProvider::GetConCommandBaseAccessor()
267 return &g_SMConVarAccessor;
270 bool BaseProvider::RegisterConCommandBase(ConCommandBase *pCommand)
272 return g_SMConVarAccessor.Register(pCommand);
275 void BaseProvider::UnregisterConCommandBase(ConCommandBase *pCommand)
277 return g_SMConVarAccessor.Unregister(pCommand);
280 int BaseProvider::GetUserMessageCount()
282 if (!usermsgs_extracted)
287 return (int)usermsgs_list.size();
290 int BaseProvider::FindUserMessage(const char *name, int *size)
292 for (size_t i = 0; i < usermsgs_list.size(); i++)
294 if (usermsgs_list[i].name.compare(name) == 0)
298 *size = usermsgs_list[i].size;
307 const char *BaseProvider::GetUserMessage(int index, int *size)
309 if (!usermsgs_extracted || index < 0 || index >= (int)usermsgs_list.size())
316 *size = usermsgs_list[index].size;
319 return usermsgs_list[index].name.c_str();
322 const char *BaseProvider::GetGameDescription()
324 return server->GetGameDescription();
327 ConVar *BaseProvider::CreateConVar(const char *name,
333 if (flags & ConVarFlag_Notify)
335 newflags |= FCVAR_NOTIFY;
337 if (flags & ConVarFlag_Replicated)
339 newflags |= FCVAR_REPLICATED;
341 if (flags & ConVarFlag_SpOnly)
343 newflags |= FCVAR_SPONLY;
346 ConVar *pVar = new ConVar(name, defval, newflags, help);
348 RegisterConCommandBase(pVar);
349 conbases_unreg.push_back(pVar);
354 IServerPluginCallbacks *BaseProvider::GetVSPCallbacks(const char *iface)
356 g_VspListener.SetLoadable(true);
357 return &g_VspListener;
360 class GlobCommand : public IMetamodSourceCommandInfo
363 GlobCommand(const CCommand *cmd) : m_cmd(cmd)
367 unsigned int GetArgCount()
369 return m_cmd->ArgC();
372 const char *GetArg(unsigned int num)
374 return m_cmd->Arg(num);
377 const char *GetArgString()
379 return m_cmd->ArgS();
382 const CCommand *m_cmd;
385 void LocalCommand_Meta(const CCommand &args)
387 GlobCommand cmd(&args);
391 void ClientCommand(edict_t *pEdict, const CCommand &_cmd)
393 GlobCommand cmd(&_cmd);
395 if (strcmp(cmd.GetArg(0), "meta") == 0)
397 Command_ClientMeta(pEdict, &cmd);
401 bool vcmp(const void *_addr1, const void *_addr2, size_t len)
403 unsigned char *addr1 = (unsigned char *)_addr1;
404 unsigned char *addr2 = (unsigned char *)_addr2;
406 for (size_t i=0; i<len; i++)
412 if (addr1[i] != addr2[i])
421 //////////////////////////////////////////////////////////////////////
422 // EVEN MORE HACKS HERE! YOU HAVE BEEN WARNED! //
423 // Signatures necessary in finding the pointer to the CUtlDict that //
424 // stores user message information. //
425 // IServerGameDLL::GetUserMessageInfo() normally crashes with bad //
426 // message indices. This is our answer to it. Yuck! <:-( //
427 //////////////////////////////////////////////////////////////////////
429 /* General Windows sig */
430 #define MSGCLASS_SIGLEN 7
431 #define MSGCLASS_SIG "\x8B\x0D\x2A\x2A\x2A\x2A\x56"
432 #define MSGCLASS_OFFS 2
434 /* Dystopia Wimdows hack */
435 #define MSGCLASS2_SIGLEN 16
436 #define MSGCLASS2_SIG "\x56\x8B\x74\x24\x2A\x85\xF6\x7C\x2A\x3B\x35\x2A\x2A\x2A\x2A\x7D"
437 #define MSGCLASS2_OFFS 11
439 /* Windows frame pointer sig */
440 #define MSGCLASS3_SIGLEN 18
441 #define MSGCLASS3_SIG "\x55\x8B\xEC\x51\x89\x2A\x2A\x8B\x2A\x2A\x50\x8B\x0D\x2A\x2A\x2A\x2A\xE8"
442 #define MSGCLASS3_OFFS 13
443 #elif defined OS_LINUX
444 /* No frame pointer sig */
445 #define MSGCLASS_SIGLEN 14
446 #define MSGCLASS_SIG "\x53\x83\xEC\x2A\x8B\x2A\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
447 #define MSGCLASS_OFFS 9
449 /* Frame pointer sig */
450 #define MSGCLASS2_SIGLEN 16
451 #define MSGCLASS2_SIG "\x55\x89\xE5\x53\x83\xEC\x2A\x8B\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
452 #define MSGCLASS2_OFFS 11
461 typedef CUtlDict<UserMessage *, int> UserMsgDict;
463 /* This is the ugliest function in all of SourceMM */
464 bool CacheUserMessages()
466 /* Get address of original GetUserMessageInfo() */
467 char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(server, &IServerGameDLL::GetUserMessageInfo);
469 /* Oh dear, we have a relative jump on our hands
470 * PVK II on Windows made me do this, but I suppose it doesn't hurt to check this on Linux too...
472 if (*vfunc == '\xE9')
474 /* Get address from displacement...
476 * Add 5 because it's relative to next instruction:
477 * Opcode <1 byte> + 32-bit displacement <4 bytes>
479 vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5;
482 CUtlDict<UserMessage *, int> *dict = NULL;
484 if (vcmp(vfunc, MSGCLASS_SIG, MSGCLASS_SIGLEN))
486 /* Get address of CUserMessages instance */
487 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS_OFFS);
489 /* Get address of CUserMessages::m_UserMessages */
490 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
492 else if (vcmp(vfunc, MSGCLASS2_SIG, MSGCLASS2_SIGLEN))
495 /* If we get here, the code is possibly inlined like in Dystopia */
497 /* Get the address of the CUtlRBTree */
498 char *rbtree = *reinterpret_cast<char **>(vfunc + MSGCLASS2_OFFS);
500 /* CUtlDict should be 8 bytes before the CUtlRBTree (hacktacular!) */
501 dict = reinterpret_cast<UserMsgDict *>(rbtree - 8);
502 #elif defined OS_LINUX
503 /* Get address of CUserMessages instance */
504 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS2_OFFS);
506 /* Get address of CUserMessages::m_UserMessages */
507 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
511 else if (vcmp(vfunc, MSGCLASS3_SIG, MSGCLASS3_SIGLEN))
513 /* Get address of CUserMessages instance */
514 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS3_OFFS);
516 /* Get address of CUserMessages::m_UserMessages */
517 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
523 int msg_count = dict->Count();
525 /* Ensure that count is within bounds of an unsigned byte, because that's what engine supports */
526 if (msg_count < 0 || msg_count > 255)
534 /* Cache messages in our CUtlDict */
535 for (int i = 0; i < msg_count; i++)
537 msg = dict->Element(i);
538 u_msg.name = msg->name;
539 u_msg.size = msg->size;
540 usermsgs_list.push_back(u_msg);