Initial commit of vsp bridge for new core. Not tested, barely compiles.
4 * ======================================================
6 * Copyright (C) 2004-2008 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.
30 #include "../metamod_oslink.h"
31 #include <sourcehook.h>
34 #include <tier0/icommandline.h>
35 #include <tier1/utldict.h>
36 #include <sh_vector.h>
37 #include <sh_string.h>
38 #include "../metamod_util.h"
39 #include "provider_ep2.h"
41 #include "metamod_console.h"
42 #include "vsp_listener.h"
43 #include <filesystem.h>
46 typedef void (*CONPRINTF_FUNC)(const char *, ...);
52 UsrMsgInfo(int s, const char *t) : size(s), name(t)
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 IFileSystem *baseFs = NULL;
78 ConCommand meta_local_cmd("meta", LocalCommand_Meta, "Metamod:Source control options");
80 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
82 bool AssumeUserMessages()
89 while (server->GetUserMessageInfo(q, buffer, sizeof(buffer), size))
91 usermsgs_list.push_back(UsrMsgInfo(size, buffer));
98 void BaseProvider::ConsolePrint(const char *str)
103 void BaseProvider::Notify_DLLInit_Pre(CreateInterfaceFn engineFactory,
104 CreateInterfaceFn serverFactory)
106 engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
109 DisplayError("Could not find IVEngineServer! Metamod cannot load.");
112 icvar = (ICvar *)((engineFactory)(CVAR_INTERFACE_VERSION, NULL));
115 DisplayError("Could not find ICvar! Metamod cannot load.");
120 if ((gameclients = (IServerGameClients *)(serverFactory("ServerGameClients003", NULL)))
123 gameclients = (IServerGameClients *)(serverFactory("ServerGameClients004", NULL));
126 baseFs = (IFileSystem *)((engineFactory)(FILESYSTEM_INTERFACE_VERSION, NULL));
129 ::LogMessage("Unable to find \"%s\": .vdf files will not be parsed", FILESYSTEM_INTERFACE_VERSION);
133 RegisterConCommandBase(&meta_local_cmd);
134 conbases_unreg.push_back(&meta_local_cmd);
136 if ((usermsgs_extracted = CacheUserMessages()) == false)
138 usermsgs_extracted = AssumeUserMessages();
143 SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, ClientCommand, false);
147 void BaseProvider::Notify_DLLShutdown_Pre()
149 List<ConCommandBase *>::iterator iter;
151 for (iter = conbases_unreg.begin();
152 iter != conbases_unreg.end();
155 UnregisterConCommandBase((*iter));
159 bool BaseProvider::IsRemotePrintingAvailable()
164 void BaseProvider::ClientConsolePrint(edict_t *client, const char *message)
166 engine->ClientPrintf(client, message);
169 void BaseProvider::ServerCommand(const char *cmd)
171 engine->ServerCommand(cmd);
174 const char *BaseProvider::GetConVarString(ConVar *convar)
181 return convar->GetString();
184 void BaseProvider::SetConVarString(ConVar *convar, const char *str)
186 convar->SetValue(str);
189 bool BaseProvider::IsConCommandBaseACommand(ConCommandBase *pCommand)
191 return pCommand->IsCommand();
195 bool BaseProvider::IsSourceEngineBuildCompatible(int build)
197 return (build == SOURCE_ENGINE_ORIGINAL
198 || build == SOURCE_ENGINE_EPISODEONE);
201 const char *BaseProvider::GetCommandLineValue(const char *key, const char *defval)
203 if (key[0] == '-' || key[0] == '+')
205 return CommandLine_Tier0()->ParmValue(key, defval);
210 if ((val = icvar->GetCommandLineValue(key)) == NULL)
221 int BaseProvider::TryServerGameDLL(const char *iface)
223 if (strncmp(iface, "ServerGameDLL", 13) != 0)
228 return atoi(&iface[13]);
231 bool BaseProvider::LogMessage(const char *buffer)
238 engine->LogPrint(buffer);
243 bool BaseProvider::GetHookInfo(ProvidedHooks hook, SourceHook::MemFuncInfo *pInfo)
245 SourceHook::MemFuncInfo mfi = {true, -1, 0, 0};
247 if (hook == ProvidedHook_LevelInit)
249 SourceHook::GetFuncInfo(&IServerGameDLL::LevelInit, mfi);
251 else if (hook == ProvidedHook_LevelShutdown)
253 SourceHook::GetFuncInfo(&IServerGameDLL::LevelShutdown, mfi);
255 else if (hook == ProvidedHook_GameInit)
257 SourceHook::GetFuncInfo(&IServerGameDLL::GameInit, mfi);
259 else if (hook == ProvidedHook_DLLShutdown)
261 SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfi);
263 else if (hook == ProvidedHook_DLLInit)
265 SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfi);
270 return (mfi.thisptroffs >= 0);
273 void BaseProvider::DisplayError(const char *fmt, ...)
279 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
285 void BaseProvider::DisplayWarning(const char *fmt, ...)
291 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
297 IConCommandBaseAccessor *BaseProvider::GetConCommandBaseAccessor()
299 return &g_SMConVarAccessor;
302 bool BaseProvider::RegisterConCommandBase(ConCommandBase *pCommand)
304 return g_SMConVarAccessor.Register(pCommand);
307 void BaseProvider::UnregisterConCommandBase(ConCommandBase *pCommand)
309 return g_SMConVarAccessor.Unregister(pCommand);
312 int BaseProvider::GetUserMessageCount()
314 if (!usermsgs_extracted)
319 return (int)usermsgs_list.size();
322 int BaseProvider::FindUserMessage(const char *name, int *size)
324 for (size_t i = 0; i < usermsgs_list.size(); i++)
326 if (usermsgs_list[i].name.compare(name) == 0)
330 *size = usermsgs_list[i].size;
339 const char *BaseProvider::GetUserMessage(int index, int *size)
341 if (!usermsgs_extracted || index < 0 || index >= (int)usermsgs_list.size())
348 *size = usermsgs_list[index].size;
351 return usermsgs_list[index].name.c_str();
354 const char *BaseProvider::GetGameDescription()
356 return server->GetGameDescription();
359 int BaseProvider::DetermineSourceEngine(const char *game)
361 return SOURCE_ENGINE_ORANGEBOX;
364 ConVar *BaseProvider::CreateConVar(const char *name,
370 if (flags & ConVarFlag_Notify)
372 newflags |= FCVAR_NOTIFY;
374 if (flags & ConVarFlag_SpOnly)
376 newflags |= FCVAR_SPONLY;
379 ConVar *pVar = new ConVar(name, defval, newflags, help);
381 RegisterConCommandBase(pVar);
382 conbases_unreg.push_back(pVar);
387 IServerPluginCallbacks *BaseProvider::GetVSPCallbacks(int version)
394 g_VspListener.SetLoadable(true);
395 return &g_VspListener;
398 bool BaseProvider::ProcessVDF(const char *file, char path[], size_t path_len, char alias[], size_t alias_len)
406 const char *plugin_file, *p_alias;
408 pValues = new KeyValues("Metamod Plugin");
410 if (!pValues->LoadFromFile(baseFs, file))
412 pValues->deleteThis();
416 if ((plugin_file = pValues->GetString("file", NULL)) == NULL)
418 pValues->deleteThis();
422 if ((p_alias = pValues->GetString("alias", NULL)) != NULL)
424 UTIL_Format(alias, alias_len, "%s", p_alias);
428 UTIL_Format(alias, alias_len, "");
431 /* Attempt to find a file extension */
432 if (UTIL_GetExtension(plugin_file) == NULL)
434 g_pMetamod->PathFormat(path,
437 g_pMetamod->GetBaseDir(),
439 #if defined WIN32 || defined _WIN32
448 g_pMetamod->PathFormat(path,
451 g_pMetamod->GetBaseDir(),
455 pValues->deleteThis();
460 class GlobCommand : public IMetamodSourceCommandInfo
463 GlobCommand(const CCommand *cmd) : m_cmd(cmd)
467 unsigned int GetArgCount()
469 return m_cmd->ArgC() - 1;
472 const char *GetArg(unsigned int num)
474 return m_cmd->Arg(num);
477 const char *GetArgString()
479 return m_cmd->ArgS();
482 const CCommand *m_cmd;
485 void LocalCommand_Meta(const CCommand &args)
487 GlobCommand cmd(&args);
491 void ClientCommand(edict_t *pEdict, const CCommand &_cmd)
493 GlobCommand cmd(&_cmd);
495 if (strcmp(cmd.GetArg(0), "meta") == 0)
497 Command_ClientMeta(pEdict, &cmd);
498 RETURN_META(MRES_SUPERCEDE);
501 RETURN_META(MRES_IGNORED);
504 bool vcmp(const void *_addr1, const void *_addr2, size_t len)
506 unsigned char *addr1 = (unsigned char *)_addr1;
507 unsigned char *addr2 = (unsigned char *)_addr2;
509 for (size_t i=0; i<len; i++)
515 if (addr1[i] != addr2[i])
524 //////////////////////////////////////////////////////////////////////
525 // EVEN MORE HACKS HERE! YOU HAVE BEEN WARNED! //
526 // Signatures necessary in finding the pointer to the CUtlDict that //
527 // stores user message information. //
528 // IServerGameDLL::GetUserMessageInfo() normally crashes with bad //
529 // message indices. This is our answer to it. Yuck! <:-( //
530 //////////////////////////////////////////////////////////////////////
532 /* General Windows sig */
533 #define MSGCLASS_SIGLEN 7
534 #define MSGCLASS_SIG "\x8B\x0D\x2A\x2A\x2A\x2A\x56"
535 #define MSGCLASS_OFFS 2
537 /* Dystopia Wimdows hack */
538 #define MSGCLASS2_SIGLEN 16
539 #define MSGCLASS2_SIG "\x56\x8B\x74\x24\x2A\x85\xF6\x7C\x2A\x3B\x35\x2A\x2A\x2A\x2A\x7D"
540 #define MSGCLASS2_OFFS 11
542 /* Windows frame pointer sig */
543 #define MSGCLASS3_SIGLEN 18
544 #define MSGCLASS3_SIG "\x55\x8B\xEC\x51\x89\x2A\x2A\x8B\x2A\x2A\x50\x8B\x0D\x2A\x2A\x2A\x2A\xE8"
545 #define MSGCLASS3_OFFS 13
546 #elif defined OS_LINUX
547 /* No frame pointer sig */
548 #define MSGCLASS_SIGLEN 14
549 #define MSGCLASS_SIG "\x53\x83\xEC\x2A\x8B\x2A\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
550 #define MSGCLASS_OFFS 9
552 /* Frame pointer sig */
553 #define MSGCLASS2_SIGLEN 16
554 #define MSGCLASS2_SIG "\x55\x89\xE5\x53\x83\xEC\x2A\x8B\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
555 #define MSGCLASS2_OFFS 11
564 typedef CUtlDict<UserMessage *, int> UserMsgDict;
566 /* This is the ugliest function in all of SourceMM */
567 bool CacheUserMessages()
569 /* Get address of original GetUserMessageInfo() */
570 char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(server, &IServerGameDLL::GetUserMessageInfo);
572 /* Oh dear, we have a relative jump on our hands
573 * PVK II on Windows made me do this, but I suppose it doesn't hurt to check this on Linux too...
575 if (*vfunc == '\xE9')
577 /* Get address from displacement...
579 * Add 5 because it's relative to next instruction:
580 * Opcode <1 byte> + 32-bit displacement <4 bytes>
582 vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5;
585 CUtlDict<UserMessage *, int> *dict = NULL;
587 if (vcmp(vfunc, MSGCLASS_SIG, MSGCLASS_SIGLEN))
589 /* Get address of CUserMessages instance */
590 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS_OFFS);
592 /* Get address of CUserMessages::m_UserMessages */
593 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
595 else if (vcmp(vfunc, MSGCLASS2_SIG, MSGCLASS2_SIGLEN))
598 /* If we get here, the code is possibly inlined like in Dystopia */
600 /* Get the address of the CUtlRBTree */
601 char *rbtree = *reinterpret_cast<char **>(vfunc + MSGCLASS2_OFFS);
603 /* CUtlDict should be 8 bytes before the CUtlRBTree (hacktacular!) */
604 dict = reinterpret_cast<UserMsgDict *>(rbtree - 8);
605 #elif defined OS_LINUX
606 /* Get address of CUserMessages instance */
607 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS2_OFFS);
609 /* Get address of CUserMessages::m_UserMessages */
610 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
614 else if (vcmp(vfunc, MSGCLASS3_SIG, MSGCLASS3_SIGLEN))
616 /* Get address of CUserMessages instance */
617 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS3_OFFS);
619 /* Get address of CUserMessages::m_UserMessages */
620 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
624 #if !defined OS_WIN32
628 if (GetFileOfAddress(vfunc, path, sizeof(path)))
630 void *handle = dlopen(path, RTLD_NOW);
633 void *addr = dlsym(handle, "usermessages");
638 dict = (UserMsgDict *)*(void **)addr;
647 int msg_count = dict->Count();
649 /* Ensure that count is within bounds of an unsigned byte, because that's what engine supports */
650 if (msg_count < 0 || msg_count > 255)
658 /* Cache messages in our CUtlDict */
659 for (int i = 0; i < msg_count; i++)
661 msg = dict->Element(i);
662 u_msg.name = msg->name;
663 u_msg.size = msg->size;
664 usermsgs_list.push_back(u_msg);