Updated for latest hl2sdk-dota changes.
2 * vim: set ts=4 sw=4 tw=99 noet :
3 * ======================================================
5 * Copyright (C) 2004-2009 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.
28 #include "../metamod_oslink.h"
29 #include <sourcehook.h>
32 #include <tier0/icommandline.h>
33 #include <sh_vector.h>
34 #include <sh_string.h>
35 #include "../metamod_util.h"
36 #include "provider_ep2.h"
38 #include "metamod_console.h"
39 #include <filesystem.h>
43 typedef void (*CONPRINTF_FUNC)(const char *, ...);
49 UsrMsgInfo(int s, const char *t) : size(s), name(t)
57 #if SOURCE_ENGINE == SE_DARKMESSIAH
59 DLL_IMPORT ICommandLine *CommandLine();
63 void CacheUserMessages();
64 void Detour_Error(const tchar *pMsg, ...);
65 #if SOURCE_ENGINE == SE_DOTA
66 void ClientCommand(CEntityIndex index, const CCommand &args);
67 void LocalCommand_Meta(const CCommandContext &context, const CCommand &args);
68 #elif SOURCE_ENGINE >= SE_ORANGEBOX
69 void ClientCommand(edict_t *pEdict, const CCommand &args);
70 void LocalCommand_Meta(const CCommand &args);
72 void ClientCommand(edict_t *pEdict);
73 void LocalCommand_Meta();
76 void _ServerCommand();
78 static BaseProvider g_Ep1Provider;
79 static List<ConCommandBase *> conbases_unreg;
80 static CVector<UsrMsgInfo> usermsgs_list;
81 static jmp_buf usermsg_end;
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_DOTA
92 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, CEntityIndex, const CCommand &);
93 #elif SOURCE_ENGINE >= SE_ORANGEBOX
94 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
96 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
99 void BaseProvider::ConsolePrint(const char *str)
101 #if SOURCE_ENGINE >= SE_ORANGEBOX
108 void BaseProvider::Notify_DLLInit_Pre(CreateInterfaceFn engineFactory,
109 CreateInterfaceFn serverFactory)
111 engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
114 DisplayError("Could not find IVEngineServer! Metamod cannot load.");
117 #if SOURCE_ENGINE >= SE_ORANGEBOX
118 icvar = (ICvar *)((engineFactory)(CVAR_INTERFACE_VERSION, NULL));
120 icvar = (ICvar *)((engineFactory)(VENGINE_CVAR_INTERFACE_VERSION, NULL));
124 DisplayError("Could not find ICvar! Metamod cannot load.");
129 if ((gameclients = (IServerGameClients *)(serverFactory("ServerGameClients003", NULL)))
132 gameclients = (IServerGameClients *)(serverFactory("ServerGameClients004", NULL));
135 baseFs = (IFileSystem *)((engineFactory)(FILESYSTEM_INTERFACE_VERSION, NULL));
138 mm_LogMessage("Unable to find \"%s\": .vdf files will not be parsed", FILESYSTEM_INTERFACE_VERSION);
141 #if SOURCE_ENGINE >= SE_ORANGEBOX
145 g_SMConVarAccessor.RegisterConCommandBase(&meta_local_cmd);
149 #if SOURCE_ENGINE == SE_DARKMESSIAH
150 if (!g_SMConVarAccessor.InitConCommandBaseList())
152 /* This is very unlikely considering it's old engine */
153 mm_LogMessage("[META] Warning: Failed to find ConCommandBase list!");
154 mm_LogMessage("[META] Warning: ConVars and ConCommands cannot be unregistered properly! Please file a bug report.");
160 SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, ClientCommand, false);
164 void BaseProvider::Notify_DLLShutdown_Pre()
166 g_SMConVarAccessor.RemoveMetamodCommands();
168 #if SOURCE_ENGINE == SE_DARKMESSIAH
169 if (g_Metamod.IsLoadedAsGameDLL())
171 icvar->UnlinkVariables(FCVAR_GAMEDLL);
176 bool BaseProvider::IsRemotePrintingAvailable()
181 void BaseProvider::ClientConsolePrint(edict_t *pEdict, const char *message)
183 #if SOURCE_ENGINE == SE_DOTA
184 int client = (int)(pEdict - g_Metamod.GetCGlobals()->pEdicts);
185 engine->ClientPrintf(client, message);
187 engine->ClientPrintf(pEdict, message);
191 void BaseProvider::ServerCommand(const char *cmd)
193 engine->ServerCommand(cmd);
196 const char *BaseProvider::GetConVarString(ConVar *convar)
203 return convar->GetString();
206 void BaseProvider::SetConVarString(ConVar *convar, const char *str)
208 convar->SetValue(str);
211 bool BaseProvider::IsConCommandBaseACommand(ConCommandBase *pCommand)
213 return pCommand->IsCommand();
217 bool BaseProvider::IsSourceEngineBuildCompatible(int build)
219 return (build == SOURCE_ENGINE_ORIGINAL
220 || build == SOURCE_ENGINE_EPISODEONE);
223 const char *BaseProvider::GetCommandLineValue(const char *key, const char *defval)
225 if (key[0] == '-' || key[0] == '+')
227 return CommandLine()->ParmValue(key, defval);
232 if ((val = icvar->GetCommandLineValue(key)) == NULL)
243 int BaseProvider::TryServerGameDLL(const char *iface)
245 if (strncmp(iface, "ServerGameDLL", 13) != 0)
250 return atoi(&iface[13]);
253 bool BaseProvider::LogMessage(const char *buffer)
260 engine->LogPrint(buffer);
265 bool BaseProvider::GetHookInfo(ProvidedHooks hook, SourceHook::MemFuncInfo *pInfo)
267 SourceHook::MemFuncInfo mfi = {true, -1, 0, 0};
269 if (hook == ProvidedHook_LevelInit)
271 SourceHook::GetFuncInfo(&IServerGameDLL::LevelInit, mfi);
273 else if (hook == ProvidedHook_LevelShutdown)
275 SourceHook::GetFuncInfo(&IServerGameDLL::LevelShutdown, mfi);
277 else if (hook == ProvidedHook_GameInit)
279 SourceHook::GetFuncInfo(&IServerGameDLL::GameInit, mfi);
284 return (mfi.thisptroffs >= 0);
287 void BaseProvider::DisplayError(const char *fmt, ...)
293 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
299 void BaseProvider::DisplayWarning(const char *fmt, ...)
305 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
308 Warning("%s", buffer);
311 IConCommandBaseAccessor *BaseProvider::GetConCommandBaseAccessor()
313 return &g_SMConVarAccessor;
316 bool BaseProvider::RegisterConCommandBase(ConCommandBase *pCommand)
318 return g_SMConVarAccessor.Register(pCommand);
321 void BaseProvider::UnregisterConCommandBase(ConCommandBase *pCommand)
323 return g_SMConVarAccessor.Unregister(pCommand);
326 int BaseProvider::GetUserMessageCount()
328 #if SOURCE_ENGINE == SE_CSGO || SOURCE_ENGINE == SE_DOTA
331 return (int)usermsgs_list.size();
335 int BaseProvider::FindUserMessage(const char *name, int *size)
337 for (size_t i = 0; i < usermsgs_list.size(); i++)
339 if (usermsgs_list[i].name.compare(name) == 0)
343 *size = usermsgs_list[i].size;
352 const char *BaseProvider::GetUserMessage(int index, int *size)
354 if (index < 0 || index >= (int)usermsgs_list.size())
361 *size = usermsgs_list[index].size;
364 return usermsgs_list[index].name.c_str();
367 const char *BaseProvider::GetGameDescription()
369 return server->GetGameDescription();
372 int BaseProvider::DetermineSourceEngine(const char *game)
374 #if SOURCE_ENGINE == SE_BLOODYGOODTIME
375 return SOURCE_ENGINE_BLOODYGOODTIME;
376 #elif SOURCE_ENGINE == SE_ALIENSWARM
377 return SOURCE_ENGINE_ALIENSWARM;
378 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
379 return SOURCE_ENGINE_LEFT4DEAD2;
380 #elif SOURCE_ENGINE == SE_LEFT4DEAD
381 return SOURCE_ENGINE_LEFT4DEAD;
382 #elif SOURCE_ENGINE == SE_ORANGEBOX
383 return SOURCE_ENGINE_ORANGEBOX;
384 #elif SOURCE_ENGINE == SE_CSS
385 return SOURCE_ENGINE_CSS;
386 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
387 return SOURCE_ENGINE_ORANGEBOXVALVE;
388 #elif SOURCE_ENGINE == SE_DARKMESSIAH
389 return SOURCE_ENGINE_DARKMESSIAH;
390 #elif SOURCE_ENGINE == SE_EYE
391 return SOURCE_ENGINE_EYE;
392 #elif SOURCE_ENGINE == SE_PORTAL2
393 return SOURCE_ENGINE_PORTAL2;
394 #elif SOURCE_ENGINE == SE_CSGO
395 return SOURCE_ENGINE_CSGO;
396 #elif SOURCE_ENGINE == SE_DOTA
397 return SOURCE_ENGINE_DOTA;
399 #error "SOURCE_ENGINE not defined to a known value"
403 ConVar *BaseProvider::CreateConVar(const char *name,
409 if (flags & ConVarFlag_Notify)
411 newflags |= FCVAR_NOTIFY;
413 if (flags & ConVarFlag_SpOnly)
415 newflags |= FCVAR_SPONLY;
418 ConVar *pVar = new ConVar(name, defval, newflags, help);
420 g_SMConVarAccessor.RegisterConCommandBase(pVar);
425 bool BaseProvider::ProcessVDF(const char *file, char path[], size_t path_len, char alias[], size_t alias_len)
433 const char *plugin_file, *p_alias;
435 pValues = new KeyValues("Metamod Plugin");
437 if (!pValues->LoadFromFile(baseFs, file))
439 pValues->deleteThis();
443 if ((plugin_file = pValues->GetString("file", NULL)) == NULL)
445 pValues->deleteThis();
449 UTIL_Format(path, path_len, "%s", plugin_file);
451 if ((p_alias = pValues->GetString("alias", NULL)) != NULL)
453 UTIL_Format(alias, alias_len, "%s", p_alias);
457 UTIL_Format(alias, alias_len, "");
460 pValues->deleteThis();
465 #if SOURCE_ENGINE >= SE_ORANGEBOX
466 class GlobCommand : public IMetamodSourceCommandInfo
469 GlobCommand(const CCommand *cmd) : m_cmd(cmd)
473 unsigned int GetArgCount()
475 return m_cmd->ArgC() - 1;
478 const char *GetArg(unsigned int num)
480 return m_cmd->Arg(num);
483 const char *GetArgString()
485 return m_cmd->ArgS();
488 const CCommand *m_cmd;
491 class GlobCommand : public IMetamodSourceCommandInfo
494 unsigned int GetArgCount()
496 return engine->Cmd_Argc() - 1;
499 const char *GetArg(unsigned int num)
501 return engine->Cmd_Argv(num);
504 const char *GetArgString()
506 return engine->Cmd_Args();
511 #if SOURCE_ENGINE == SE_DOTA
512 void LocalCommand_Meta(const CCommandContext &context, const CCommand &args)
514 GlobCommand cmd(&args);
515 #elif SOURCE_ENGINE >= SE_ORANGEBOX
516 void LocalCommand_Meta(const CCommand &args)
518 GlobCommand cmd(&args);
520 void LocalCommand_Meta()
527 #if SOURCE_ENGINE == SE_DOTA
528 void ClientCommand(CEntityIndex index, const CCommand &_cmd)
530 int client = index.Get();
531 GlobCommand cmd(&_cmd);
532 #elif SOURCE_ENGINE >= SE_ORANGEBOX
533 void ClientCommand(edict_t *client, const CCommand &_cmd)
535 GlobCommand cmd(&_cmd);
537 void ClientCommand(edict_t *client)
541 if (strcmp(cmd.GetArg(0), "meta") == 0)
543 Command_ClientMeta(client, &cmd);
544 RETURN_META(MRES_SUPERCEDE);
547 RETURN_META(MRES_IGNORED);
550 #if SOURCE_ENGINE == SE_CSGO || SOURCE_ENGINE == SE_DOTA
552 void CacheUserMessages()
558 /* This only gets called if IServerGameDLL::GetUserMessageInfo() triggers it */
559 void Detour_Error(const tchar *pMsg, ...)
561 /* Jump back to setjmp() in CacheUserMessages() */
562 longjmp(usermsg_end, 1);
565 #define IA32_JMP_IMM32 0xE9
567 /* IServerGameDLL::GetUserMessageInfo() crashes on games based on the old engine and
568 * early Orange Box. This is because Error() from tier0 gets called when a bad index is
569 * passed. This is all due to a bug in CUtlRBTree::IsValidIndex().
571 * So we detour Error() to fix this. Our detour then jumps back into CacheUserMessages()
572 * to a point before GetUserMessageInfo() is called. The detour is then removed and we
575 void CacheUserMessages()
579 unsigned char *target, *detour;
580 unsigned char orig_bytes[5];
582 target = (unsigned char *)&Error;
583 detour = (unsigned char *)&Detour_Error;
585 /* Save bytes from target function */
586 memcpy(orig_bytes, target, sizeof(orig_bytes));
588 /* Patch in relative jump to our Error() detour */
589 SetMemAccess(target, sizeof(orig_bytes), SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
590 target[0] = IA32_JMP_IMM32;
591 *(int32_t *)&target[1] = (int32_t)(detour - (target + 5));
593 /* This is where longjmp() will end up */
594 if (setjmp(usermsg_end))
596 /* Restore bytes and memory protection */
597 memcpy(target, orig_bytes, sizeof(orig_bytes));
598 SetMemAccess(target, sizeof(orig_bytes), SH_MEM_READ|SH_MEM_EXEC);
604 /* If GetUserMessageInfo() calls Error(), we should end up in our detour */
605 while (server->GetUserMessageInfo(q, buffer, sizeof(buffer), size))
607 usermsgs_list.push_back(UsrMsgInfo(size, buffer));
611 /* Jump back to setjmp() */
612 longjmp(usermsg_end, 1);