Fixed line endings on a few files.
3 * ======================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC and authors.
7 * ======================================================
9 * This software is provided 'as-is', without any express or implied warranty.
10 * In no event will the authors be held liable for any damages arising from
11 * the use of this software.
13 * Permission is granted to anyone to use this software for any purpose,
14 * including commercial applications, and to alter it and redistribute it
15 * freely, subject to the following restrictions:
17 * 1. The origin of this software must not be misrepresented; you must not
18 * claim that you wrote the original software. If you use this software in a
19 * product, an acknowledgment in the product documentation would be
20 * appreciated but is not required.
21 * 2. Altered source versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software.
23 * 3. This notice may not be removed or altered from any source distribution.
29 #include "../metamod_oslink.h"
30 #include <sourcehook.h>
33 #include <tier0/icommandline.h>
34 #include <tier1/utldict.h>
35 #include <sh_vector.h>
36 #include <sh_string.h>
37 #include "../metamod_util.h"
38 #include "provider_ep2.h"
40 #include "metamod_console.h"
41 #include "vsp_listener.h"
42 #include <filesystem.h>
46 typedef void (*CONPRINTF_FUNC)(const char *, ...);
52 UsrMsgInfo(int s, const char *t) : size(s), name(t)
60 #if SOURCE_ENGINE == SE_DARKMESSIAH
62 DLL_IMPORT ICommandLine *CommandLine();
66 bool CacheUserMessages();
67 #if SOURCE_ENGINE >= SE_ORANGEBOX
68 void ClientCommand(edict_t *pEdict, const CCommand &args);
69 void LocalCommand_Meta(const CCommand &args);
71 void ClientCommand(edict_t *pEdict);
72 void LocalCommand_Meta();
75 void _ServerCommand();
77 static bool usermsgs_extracted = false;
78 static CVector<UsrMsgInfo> usermsgs_list;
79 static VSPListener g_VspListener;
80 static BaseProvider g_Ep1Provider;
81 static List<ConCommandBase *> conbases_unreg;
84 IFileSystem *baseFs = NULL;
85 IServerGameDLL *server = NULL;
86 IVEngineServer *engine = NULL;
87 IServerGameClients *gameclients = NULL;
88 IMetamodSourceProvider *provider = &g_Ep1Provider;
89 ConCommand meta_local_cmd("meta", LocalCommand_Meta, "Metamod:Source control options");
91 #if SOURCE_ENGINE >= SE_ORANGEBOX
92 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
94 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
97 bool AssumeUserMessages()
104 while (server->GetUserMessageInfo(q, buffer, sizeof(buffer), size))
106 usermsgs_list.push_back(UsrMsgInfo(size, buffer));
113 void BaseProvider::ConsolePrint(const char *str)
115 #if SOURCE_ENGINE >= SE_ORANGEBOX
122 void BaseProvider::Notify_DLLInit_Pre(CreateInterfaceFn engineFactory,
123 CreateInterfaceFn serverFactory)
125 engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
128 DisplayError("Could not find IVEngineServer! Metamod cannot load.");
131 #if SOURCE_ENGINE >= SE_ORANGEBOX
132 icvar = (ICvar *)((engineFactory)(CVAR_INTERFACE_VERSION, NULL));
134 icvar = (ICvar *)((engineFactory)(VENGINE_CVAR_INTERFACE_VERSION, NULL));
138 DisplayError("Could not find ICvar! Metamod cannot load.");
143 if ((gameclients = (IServerGameClients *)(serverFactory("ServerGameClients003", NULL)))
146 gameclients = (IServerGameClients *)(serverFactory("ServerGameClients004", NULL));
149 baseFs = (IFileSystem *)((engineFactory)(FILESYSTEM_INTERFACE_VERSION, NULL));
152 mm_LogMessage("Unable to find \"%s\": .vdf files will not be parsed", FILESYSTEM_INTERFACE_VERSION);
156 #if SOURCE_ENGINE >= SE_ORANGEBOX
160 g_SMConVarAccessor.RegisterConCommandBase(&meta_local_cmd);
162 if ((usermsgs_extracted = CacheUserMessages()) == false)
164 usermsgs_extracted = AssumeUserMessages();
167 #if SOURCE_ENGINE == SE_DARKMESSIAH
168 if (!g_SMConVarAccessor.InitConCommandBaseList())
170 /* This is very unlikely considering it's old engine */
171 mm_LogMessage("[META] Warning: Failed to find ConCommandBase list!");
172 mm_LogMessage("[META] Warning: ConVars and ConCommands cannot be unregistered properly! Please file a bug report.");
178 SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, ClientCommand, false);
182 void BaseProvider::Notify_DLLShutdown_Pre()
184 g_SMConVarAccessor.RemoveMetamodCommands();
186 #if SOURCE_ENGINE == SE_DARKMESSIAH
187 if (g_Metamod.IsLoadedAsGameDLL())
189 icvar->UnlinkVariables(FCVAR_GAMEDLL);
194 bool BaseProvider::IsRemotePrintingAvailable()
199 void BaseProvider::ClientConsolePrint(edict_t *client, const char *message)
201 engine->ClientPrintf(client, message);
204 void BaseProvider::ServerCommand(const char *cmd)
206 engine->ServerCommand(cmd);
209 const char *BaseProvider::GetConVarString(ConVar *convar)
216 return convar->GetString();
219 void BaseProvider::SetConVarString(ConVar *convar, const char *str)
221 convar->SetValue(str);
224 bool BaseProvider::IsConCommandBaseACommand(ConCommandBase *pCommand)
226 return pCommand->IsCommand();
230 bool BaseProvider::IsSourceEngineBuildCompatible(int build)
232 return (build == SOURCE_ENGINE_ORIGINAL
233 || build == SOURCE_ENGINE_EPISODEONE);
236 const char *BaseProvider::GetCommandLineValue(const char *key, const char *defval)
238 if (key[0] == '-' || key[0] == '+')
240 return CommandLine()->ParmValue(key, defval);
245 if ((val = icvar->GetCommandLineValue(key)) == NULL)
256 int BaseProvider::TryServerGameDLL(const char *iface)
258 if (strncmp(iface, "ServerGameDLL", 13) != 0)
263 return atoi(&iface[13]);
266 bool BaseProvider::LogMessage(const char *buffer)
273 engine->LogPrint(buffer);
278 bool BaseProvider::GetHookInfo(ProvidedHooks hook, SourceHook::MemFuncInfo *pInfo)
280 SourceHook::MemFuncInfo mfi = {true, -1, 0, 0};
282 if (hook == ProvidedHook_LevelInit)
284 SourceHook::GetFuncInfo(&IServerGameDLL::LevelInit, mfi);
286 else if (hook == ProvidedHook_LevelShutdown)
288 SourceHook::GetFuncInfo(&IServerGameDLL::LevelShutdown, mfi);
290 else if (hook == ProvidedHook_GameInit)
292 SourceHook::GetFuncInfo(&IServerGameDLL::GameInit, mfi);
297 return (mfi.thisptroffs >= 0);
300 void BaseProvider::DisplayError(const char *fmt, ...)
306 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
312 void BaseProvider::DisplayWarning(const char *fmt, ...)
318 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
324 IConCommandBaseAccessor *BaseProvider::GetConCommandBaseAccessor()
326 return &g_SMConVarAccessor;
329 bool BaseProvider::RegisterConCommandBase(ConCommandBase *pCommand)
331 return g_SMConVarAccessor.Register(pCommand);
334 void BaseProvider::UnregisterConCommandBase(ConCommandBase *pCommand)
336 return g_SMConVarAccessor.Unregister(pCommand);
339 int BaseProvider::GetUserMessageCount()
341 if (!usermsgs_extracted)
346 return (int)usermsgs_list.size();
349 int BaseProvider::FindUserMessage(const char *name, int *size)
351 for (size_t i = 0; i < usermsgs_list.size(); i++)
353 if (usermsgs_list[i].name.compare(name) == 0)
357 *size = usermsgs_list[i].size;
366 const char *BaseProvider::GetUserMessage(int index, int *size)
368 if (!usermsgs_extracted || index < 0 || index >= (int)usermsgs_list.size())
375 *size = usermsgs_list[index].size;
378 return usermsgs_list[index].name.c_str();
381 const char *BaseProvider::GetGameDescription()
383 return server->GetGameDescription();
386 int BaseProvider::DetermineSourceEngine(const char *game)
388 #if SOURCE_ENGINE == SE_LEFT4DEAD
389 return SOURCE_ENGINE_LEFT4DEAD;
390 #elif SOURCE_ENGINE == SE_ORANGEBOX
391 return SOURCE_ENGINE_ORANGEBOX;
393 return SOURCE_ENGINE_DARKMESSIAH;
397 ConVar *BaseProvider::CreateConVar(const char *name,
403 if (flags & ConVarFlag_Notify)
405 newflags |= FCVAR_NOTIFY;
407 if (flags & ConVarFlag_SpOnly)
409 newflags |= FCVAR_SPONLY;
412 ConVar *pVar = new ConVar(name, defval, newflags, help);
414 g_SMConVarAccessor.RegisterConCommandBase(pVar);
419 IServerPluginCallbacks *BaseProvider::GetVSPCallbacks(int version)
426 g_VspListener.SetLoadable(true);
427 return &g_VspListener;
430 bool BaseProvider::ProcessVDF(const char *file, char path[], size_t path_len, char alias[], size_t alias_len)
438 const char *plugin_file, *p_alias;
440 pValues = new KeyValues("Metamod Plugin");
442 if (!pValues->LoadFromFile(baseFs, file))
444 pValues->deleteThis();
448 if ((plugin_file = pValues->GetString("file", NULL)) == NULL)
450 pValues->deleteThis();
454 UTIL_Format(path, path_len, "%s", plugin_file);
456 if ((p_alias = pValues->GetString("alias", NULL)) != NULL)
458 UTIL_Format(alias, alias_len, "%s", p_alias);
462 UTIL_Format(alias, alias_len, "");
465 pValues->deleteThis();
470 #if SOURCE_ENGINE >= SE_ORANGEBOX
471 class GlobCommand : public IMetamodSourceCommandInfo
474 GlobCommand(const CCommand *cmd) : m_cmd(cmd)
478 unsigned int GetArgCount()
480 return m_cmd->ArgC() - 1;
483 const char *GetArg(unsigned int num)
485 return m_cmd->Arg(num);
488 const char *GetArgString()
490 return m_cmd->ArgS();
493 const CCommand *m_cmd;
496 class GlobCommand : public IMetamodSourceCommandInfo
499 unsigned int GetArgCount()
501 return engine->Cmd_Argc() - 1;
504 const char *GetArg(unsigned int num)
506 return engine->Cmd_Argv(num);
509 const char *GetArgString()
511 return engine->Cmd_Args();
516 #if SOURCE_ENGINE >= SE_ORANGEBOX
517 void LocalCommand_Meta(const CCommand &args)
519 GlobCommand cmd(&args);
521 void LocalCommand_Meta()
528 #if SOURCE_ENGINE >= SE_ORANGEBOX
529 void ClientCommand(edict_t *pEdict, const CCommand &_cmd)
531 GlobCommand cmd(&_cmd);
533 void ClientCommand(edict_t *pEdict)
537 if (strcmp(cmd.GetArg(0), "meta") == 0)
539 Command_ClientMeta(pEdict, &cmd);
540 RETURN_META(MRES_SUPERCEDE);
543 RETURN_META(MRES_IGNORED);
546 //////////////////////////////////////////////////////////////////////
547 // EVEN MORE HACKS HERE! YOU HAVE BEEN WARNED! //
548 // Signatures necessary in finding the pointer to the CUtlDict that //
549 // stores user message information. //
550 // IServerGameDLL::GetUserMessageInfo() normally crashes with bad //
551 // message indices. This is our answer to it. Yuck! <:-( //
552 //////////////////////////////////////////////////////////////////////
554 /* General Windows sig */
555 #define MSGCLASS_SIGLEN 7
556 #define MSGCLASS_SIG "\x8B\x0D\x2A\x2A\x2A\x2A\x56"
557 #define MSGCLASS_OFFS 2
559 /* Dystopia Windows hack */
560 #define MSGCLASS2_SIGLEN 16
561 #define MSGCLASS2_SIG "\x56\x8B\x74\x24\x2A\x85\xF6\x7C\x2A\x3B\x35\x2A\x2A\x2A\x2A\x7D"
562 #define MSGCLASS2_OFFS 11
564 /* Windows frame pointer sig */
565 #define MSGCLASS3_SIGLEN 18
566 #define MSGCLASS3_SIG "\x55\x8B\xEC\x51\x89\x2A\x2A\x8B\x2A\x2A\x50\x8B\x0D\x2A\x2A\x2A\x2A\xE8"
567 #define MSGCLASS3_OFFS 13
568 #elif defined OS_LINUX
569 /* No frame pointer sig */
570 #define MSGCLASS_SIGLEN 14
571 #define MSGCLASS_SIG "\x53\x83\xEC\x2A\x8B\x2A\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
572 #define MSGCLASS_OFFS 9
574 /* Frame pointer sig */
575 #define MSGCLASS2_SIGLEN 16
576 #define MSGCLASS2_SIG "\x55\x89\xE5\x53\x83\xEC\x2A\x8B\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
577 #define MSGCLASS2_OFFS 11
586 typedef CUtlDict<UserMessage *, int> UserMsgDict;
588 /* This is the ugliest function in all of MM:S */
589 bool CacheUserMessages()
591 UserMsgDict *dict = NULL;
593 /* Get address of original GetUserMessageInfo() */
594 char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(server, &IServerGameDLL::GetUserMessageInfo);
596 /* Oh dear, we have a relative jump on our hands
597 * PVK II on Windows made me do this, but I suppose it doesn't hurt to check this on Linux too...
599 if (*vfunc == '\xE9')
601 /* Get address from displacement...
603 * Add 5 because it's relative to next instruction:
604 * Opcode <1 byte> + 32-bit displacement <4 bytes>
606 vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5;
609 if (UTIL_VerifySignature(vfunc, MSGCLASS_SIG, MSGCLASS_SIGLEN))
611 /* Get address of CUserMessages instance */
612 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS_OFFS);
614 /* Get address of CUserMessages::m_UserMessages */
615 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
617 else if (UTIL_VerifySignature(vfunc, MSGCLASS2_SIG, MSGCLASS2_SIGLEN))
620 /* If we get here, the code is possibly inlined like in Dystopia */
622 /* Get the address of the CUtlRBTree */
623 char *rbtree = *reinterpret_cast<char **>(vfunc + MSGCLASS2_OFFS);
625 /* CUtlDict should be 8 bytes before the CUtlRBTree (hacktacular!) */
626 dict = reinterpret_cast<UserMsgDict *>(rbtree - 8);
627 #elif defined OS_LINUX
628 /* Get address of CUserMessages instance */
629 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS2_OFFS);
631 /* Get address of CUserMessages::m_UserMessages */
632 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
636 else if (UTIL_VerifySignature(vfunc, MSGCLASS3_SIG, MSGCLASS3_SIGLEN))
638 /* Get address of CUserMessages instance */
639 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS3_OFFS);
641 /* Get address of CUserMessages::m_UserMessages */
642 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
646 #if !defined OS_WIN32
650 if (GetFileOfAddress(vfunc, path, sizeof(path)))
652 void *handle = dlopen(path, RTLD_NOW);
655 void *addr = dlsym(handle, "usermessages");
660 dict = (UserMsgDict *)*(void **)addr;
669 int msg_count = dict->Count();
671 /* Ensure that count is within bounds of an unsigned byte, because that's what engine supports */
672 if (msg_count < 0 || msg_count > 255)
680 /* Cache messages in our CUtlDict */
681 for (int i = 0; i < msg_count; i++)
683 msg = dict->Element(i);
684 u_msg.name = msg->name;
685 u_msg.size = msg->size;
686 usermsgs_list.push_back(u_msg);