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 "../metamod_oslink.h"
34 #include <sourcehook.h>
37 #include <tier0/icommandline.h>
38 #include <tier1/utldict.h>
39 #include <sh_vector.h>
40 #include <sh_string.h>
41 #include "../metamod_util.h"
42 #include "provider_ep2.h"
44 #include "metamod_console.h"
45 #include "vsp_listener.h"
52 typedef void (*CONPRINTF_FUNC)(const char *, ...);
60 DLL_IMPORT ICommandLine *CommandLine();
62 bool CacheUserMessages();
63 void ClientCommand(edict_t *pEdict, const CCommand &args);
64 void LocalCommand_Meta(const CCommand &args);
65 void _ServerCommand();
67 bool usermsgs_extracted = false;
68 CVector<UsrMsgInfo> usermsgs_list;
70 ISmmAPI *metamod_api = NULL;
71 IVEngineServer *engine = NULL;
72 IServerGameClients *gameclients = NULL;
73 VSPListener g_VspListener;
74 BaseProvider g_Ep1Provider;
75 IMetamodSourceProvider *provider = &g_Ep1Provider;
76 List<ConCommandBase *> conbases_unreg;
77 ConCommand meta_local_cmd("meta", LocalCommand_Meta, "Metamod:Source control options");
79 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
81 void BaseProvider::ConsolePrint(const char *str)
86 void BaseProvider::Notify_DLLInit_Pre(CreateInterfaceFn engineFactory,
87 CreateInterfaceFn serverFactory)
89 engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
92 DisplayError("Could not find IVEngineServer! Metamod cannot load.");
95 icvar = (ICvar *)((engineFactory)(CVAR_INTERFACE_VERSION, NULL));
98 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 int BaseProvider::DetermineSourceEngine(const char *game)
329 return SOURCE_ENGINE_ORANGEBOX;
332 ConVar *BaseProvider::CreateConVar(const char *name,
338 if (flags & ConVarFlag_Notify)
340 newflags |= FCVAR_NOTIFY;
342 if (flags & ConVarFlag_Replicated)
344 newflags |= FCVAR_REPLICATED;
346 if (flags & ConVarFlag_SpOnly)
348 newflags |= FCVAR_SPONLY;
351 ConVar *pVar = new ConVar(name, defval, newflags, help);
353 RegisterConCommandBase(pVar);
354 conbases_unreg.push_back(pVar);
359 IServerPluginCallbacks *BaseProvider::GetVSPCallbacks(const char *iface)
361 g_VspListener.SetLoadable(true);
362 return &g_VspListener;
365 bool BaseProvider::IsAlternatelyLoaded()
367 return g_VspListener.IsRootLoadMethod();
370 class GlobCommand : public IMetamodSourceCommandInfo
373 GlobCommand(const CCommand *cmd) : m_cmd(cmd)
377 unsigned int GetArgCount()
379 return m_cmd->ArgC() - 1;
382 const char *GetArg(unsigned int num)
384 return m_cmd->Arg(num);
387 const char *GetArgString()
389 return m_cmd->ArgS();
392 const CCommand *m_cmd;
395 void LocalCommand_Meta(const CCommand &args)
397 GlobCommand cmd(&args);
401 void ClientCommand(edict_t *pEdict, const CCommand &_cmd)
403 GlobCommand cmd(&_cmd);
405 if (strcmp(cmd.GetArg(0), "meta") == 0)
407 Command_ClientMeta(pEdict, &cmd);
408 RETURN_META(MRES_SUPERCEDE);
411 RETURN_META(MRES_IGNORED);
414 bool vcmp(const void *_addr1, const void *_addr2, size_t len)
416 unsigned char *addr1 = (unsigned char *)_addr1;
417 unsigned char *addr2 = (unsigned char *)_addr2;
419 for (size_t i=0; i<len; i++)
425 if (addr1[i] != addr2[i])
434 //////////////////////////////////////////////////////////////////////
435 // EVEN MORE HACKS HERE! YOU HAVE BEEN WARNED! //
436 // Signatures necessary in finding the pointer to the CUtlDict that //
437 // stores user message information. //
438 // IServerGameDLL::GetUserMessageInfo() normally crashes with bad //
439 // message indices. This is our answer to it. Yuck! <:-( //
440 //////////////////////////////////////////////////////////////////////
442 /* General Windows sig */
443 #define MSGCLASS_SIGLEN 7
444 #define MSGCLASS_SIG "\x8B\x0D\x2A\x2A\x2A\x2A\x56"
445 #define MSGCLASS_OFFS 2
447 /* Dystopia Wimdows hack */
448 #define MSGCLASS2_SIGLEN 16
449 #define MSGCLASS2_SIG "\x56\x8B\x74\x24\x2A\x85\xF6\x7C\x2A\x3B\x35\x2A\x2A\x2A\x2A\x7D"
450 #define MSGCLASS2_OFFS 11
452 /* Windows frame pointer sig */
453 #define MSGCLASS3_SIGLEN 18
454 #define MSGCLASS3_SIG "\x55\x8B\xEC\x51\x89\x2A\x2A\x8B\x2A\x2A\x50\x8B\x0D\x2A\x2A\x2A\x2A\xE8"
455 #define MSGCLASS3_OFFS 13
456 #elif defined OS_LINUX
457 /* No frame pointer sig */
458 #define MSGCLASS_SIGLEN 14
459 #define MSGCLASS_SIG "\x53\x83\xEC\x2A\x8B\x2A\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
460 #define MSGCLASS_OFFS 9
462 /* Frame pointer sig */
463 #define MSGCLASS2_SIGLEN 16
464 #define MSGCLASS2_SIG "\x55\x89\xE5\x53\x83\xEC\x2A\x8B\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
465 #define MSGCLASS2_OFFS 11
474 typedef CUtlDict<UserMessage *, int> UserMsgDict;
476 /* This is the ugliest function in all of SourceMM */
477 bool CacheUserMessages()
479 /* Get address of original GetUserMessageInfo() */
480 char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(server, &IServerGameDLL::GetUserMessageInfo);
482 /* Oh dear, we have a relative jump on our hands
483 * PVK II on Windows made me do this, but I suppose it doesn't hurt to check this on Linux too...
485 if (*vfunc == '\xE9')
487 /* Get address from displacement...
489 * Add 5 because it's relative to next instruction:
490 * Opcode <1 byte> + 32-bit displacement <4 bytes>
492 vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5;
495 CUtlDict<UserMessage *, int> *dict = NULL;
497 if (vcmp(vfunc, MSGCLASS_SIG, MSGCLASS_SIGLEN))
499 /* Get address of CUserMessages instance */
500 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS_OFFS);
502 /* Get address of CUserMessages::m_UserMessages */
503 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
505 else if (vcmp(vfunc, MSGCLASS2_SIG, MSGCLASS2_SIGLEN))
508 /* If we get here, the code is possibly inlined like in Dystopia */
510 /* Get the address of the CUtlRBTree */
511 char *rbtree = *reinterpret_cast<char **>(vfunc + MSGCLASS2_OFFS);
513 /* CUtlDict should be 8 bytes before the CUtlRBTree (hacktacular!) */
514 dict = reinterpret_cast<UserMsgDict *>(rbtree - 8);
515 #elif defined OS_LINUX
516 /* Get address of CUserMessages instance */
517 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS2_OFFS);
519 /* Get address of CUserMessages::m_UserMessages */
520 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
524 else if (vcmp(vfunc, MSGCLASS3_SIG, MSGCLASS3_SIGLEN))
526 /* Get address of CUserMessages instance */
527 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS3_OFFS);
529 /* Get address of CUserMessages::m_UserMessages */
530 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
534 #if !defined OS_WIN32
538 if (GetFileOfAddress(vfunc, path, sizeof(path)))
540 void *handle = dlopen(path, RTLD_NOW);
543 void *addr = dlsym(handle, "usermessages");
544 dict = (UserMsgDict *)*(void **)addr;
553 int msg_count = dict->Count();
555 /* Ensure that count is within bounds of an unsigned byte, because that's what engine supports */
556 if (msg_count < 0 || msg_count > 255)
564 /* Cache messages in our CUtlDict */
565 for (int i = 0; i < msg_count; i++)
567 msg = dict->Element(i);
568 u_msg.name = msg->name;
569 u_msg.size = msg->size;
570 usermsgs_list.push_back(u_msg);