Fixed MetamodVersionInfo::GetGameDir always returning NULL or garbage.
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_NUCLEARDAWN
381 return SOURCE_ENGINE_NUCLEARDAWN;
382 #elif SOURCE_ENGINE == SE_LEFT4DEAD
383 return SOURCE_ENGINE_LEFT4DEAD;
384 #elif SOURCE_ENGINE == SE_ORANGEBOX
385 return SOURCE_ENGINE_ORANGEBOX;
386 #elif SOURCE_ENGINE == SE_CSS
387 return SOURCE_ENGINE_CSS;
388 #elif SOURCE_ENGINE == SE_HL2DM
389 return SOURCE_ENGINE_HL2DM;
390 #elif SOURCE_ENGINE == SE_DODS
391 return SOURCE_ENGINE_DODS;
392 #elif SOURCE_ENGINE == SE_TF2
393 return SOURCE_ENGINE_TF2;
394 #elif SOURCE_ENGINE == SE_DARKMESSIAH
395 return SOURCE_ENGINE_DARKMESSIAH;
396 #elif SOURCE_ENGINE == SE_EYE
397 return SOURCE_ENGINE_EYE;
398 #elif SOURCE_ENGINE == SE_PORTAL2
399 return SOURCE_ENGINE_PORTAL2;
400 #elif SOURCE_ENGINE == SE_CSGO
401 return SOURCE_ENGINE_CSGO;
402 #elif SOURCE_ENGINE == SE_DOTA
403 return SOURCE_ENGINE_DOTA;
405 #error "SOURCE_ENGINE not defined to a known value"
409 ConVar *BaseProvider::CreateConVar(const char *name,
415 if (flags & ConVarFlag_Notify)
417 newflags |= FCVAR_NOTIFY;
419 if (flags & ConVarFlag_SpOnly)
421 newflags |= FCVAR_SPONLY;
424 ConVar *pVar = new ConVar(name, defval, newflags, help);
426 g_SMConVarAccessor.RegisterConCommandBase(pVar);
431 bool BaseProvider::ProcessVDF(const char *file, char path[], size_t path_len, char alias[], size_t alias_len)
439 const char *plugin_file, *p_alias;
441 pValues = new KeyValues("Metamod Plugin");
443 if (!pValues->LoadFromFile(baseFs, file))
445 pValues->deleteThis();
449 if ((plugin_file = pValues->GetString("file", NULL)) == NULL)
451 pValues->deleteThis();
455 UTIL_Format(path, path_len, "%s", plugin_file);
457 if ((p_alias = pValues->GetString("alias", NULL)) != NULL)
459 UTIL_Format(alias, alias_len, "%s", p_alias);
463 UTIL_Format(alias, alias_len, "");
466 pValues->deleteThis();
471 #if SOURCE_ENGINE >= SE_ORANGEBOX
472 class GlobCommand : public IMetamodSourceCommandInfo
475 GlobCommand(const CCommand *cmd) : m_cmd(cmd)
479 unsigned int GetArgCount()
481 return m_cmd->ArgC() - 1;
484 const char *GetArg(unsigned int num)
486 return m_cmd->Arg(num);
489 const char *GetArgString()
491 return m_cmd->ArgS();
494 const CCommand *m_cmd;
497 class GlobCommand : public IMetamodSourceCommandInfo
500 unsigned int GetArgCount()
502 return engine->Cmd_Argc() - 1;
505 const char *GetArg(unsigned int num)
507 return engine->Cmd_Argv(num);
510 const char *GetArgString()
512 return engine->Cmd_Args();
517 #if SOURCE_ENGINE == SE_DOTA
518 void LocalCommand_Meta(const CCommandContext &context, const CCommand &args)
520 GlobCommand cmd(&args);
521 #elif SOURCE_ENGINE >= SE_ORANGEBOX
522 void LocalCommand_Meta(const CCommand &args)
524 GlobCommand cmd(&args);
526 void LocalCommand_Meta()
533 #if SOURCE_ENGINE == SE_DOTA
534 void ClientCommand(CEntityIndex index, const CCommand &_cmd)
536 int client = index.Get();
537 GlobCommand cmd(&_cmd);
538 #elif SOURCE_ENGINE >= SE_ORANGEBOX
539 void ClientCommand(edict_t *client, const CCommand &_cmd)
541 GlobCommand cmd(&_cmd);
543 void ClientCommand(edict_t *client)
547 if (strcmp(cmd.GetArg(0), "meta") == 0)
549 Command_ClientMeta(client, &cmd);
550 RETURN_META(MRES_SUPERCEDE);
553 RETURN_META(MRES_IGNORED);
556 #if SOURCE_ENGINE == SE_CSGO || SOURCE_ENGINE == SE_DOTA
558 void CacheUserMessages()
564 /* This only gets called if IServerGameDLL::GetUserMessageInfo() triggers it */
565 void Detour_Error(const tchar *pMsg, ...)
567 /* Jump back to setjmp() in CacheUserMessages() */
568 longjmp(usermsg_end, 1);
571 #define IA32_JMP_IMM32 0xE9
573 /* IServerGameDLL::GetUserMessageInfo() crashes on games based on the old engine and
574 * early Orange Box. This is because Error() from tier0 gets called when a bad index is
575 * passed. This is all due to a bug in CUtlRBTree::IsValidIndex().
577 * So we detour Error() to fix this. Our detour then jumps back into CacheUserMessages()
578 * to a point before GetUserMessageInfo() is called. The detour is then removed and we
581 void CacheUserMessages()
585 unsigned char *target, *detour;
586 unsigned char orig_bytes[5];
588 target = (unsigned char *)&Error;
589 detour = (unsigned char *)&Detour_Error;
591 /* Save bytes from target function */
592 memcpy(orig_bytes, target, sizeof(orig_bytes));
594 /* Patch in relative jump to our Error() detour */
595 SetMemAccess(target, sizeof(orig_bytes), SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
596 target[0] = IA32_JMP_IMM32;
597 *(int32_t *)&target[1] = (int32_t)(detour - (target + 5));
599 /* This is where longjmp() will end up */
600 if (setjmp(usermsg_end))
602 /* Restore bytes and memory protection */
603 memcpy(target, orig_bytes, sizeof(orig_bytes));
604 SetMemAccess(target, sizeof(orig_bytes), SH_MEM_READ|SH_MEM_EXEC);
610 /* If GetUserMessageInfo() calls Error(), we should end up in our detour */
611 while (server->GetUserMessageInfo(q, buffer, sizeof(buffer), size))
613 usermsgs_list.push_back(UsrMsgInfo(size, buffer));
617 /* Jump back to setjmp() */
618 longjmp(usermsg_end, 1);