Fixed passage of non-literals as error and warning format strings (r=DS).
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_ORANGEBOX
66 void ClientCommand(edict_t *pEdict, const CCommand &args);
67 void LocalCommand_Meta(const CCommand &args);
69 void ClientCommand(edict_t *pEdict);
70 void LocalCommand_Meta();
73 void _ServerCommand();
75 static BaseProvider g_Ep1Provider;
76 static List<ConCommandBase *> conbases_unreg;
77 static CVector<UsrMsgInfo> usermsgs_list;
78 static jmp_buf usermsg_end;
81 IFileSystem *baseFs = NULL;
82 IServerGameDLL *server = NULL;
83 IVEngineServer *engine = NULL;
84 IServerGameClients *gameclients = NULL;
85 IMetamodSourceProvider *provider = &g_Ep1Provider;
86 ConCommand meta_local_cmd("meta", LocalCommand_Meta, "Metamod:Source control options");
88 #if SOURCE_ENGINE >= SE_ORANGEBOX
89 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
91 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
94 void BaseProvider::ConsolePrint(const char *str)
96 #if SOURCE_ENGINE >= SE_ORANGEBOX
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 #if SOURCE_ENGINE >= SE_ORANGEBOX
113 icvar = (ICvar *)((engineFactory)(CVAR_INTERFACE_VERSION, NULL));
115 icvar = (ICvar *)((engineFactory)(VENGINE_CVAR_INTERFACE_VERSION, NULL));
119 DisplayError("Could not find ICvar! Metamod cannot load.");
124 if ((gameclients = (IServerGameClients *)(serverFactory("ServerGameClients003", NULL)))
127 gameclients = (IServerGameClients *)(serverFactory("ServerGameClients004", NULL));
130 baseFs = (IFileSystem *)((engineFactory)(FILESYSTEM_INTERFACE_VERSION, NULL));
133 mm_LogMessage("Unable to find \"%s\": .vdf files will not be parsed", FILESYSTEM_INTERFACE_VERSION);
136 #if SOURCE_ENGINE >= SE_ORANGEBOX
140 g_SMConVarAccessor.RegisterConCommandBase(&meta_local_cmd);
144 #if SOURCE_ENGINE == SE_DARKMESSIAH
145 if (!g_SMConVarAccessor.InitConCommandBaseList())
147 /* This is very unlikely considering it's old engine */
148 mm_LogMessage("[META] Warning: Failed to find ConCommandBase list!");
149 mm_LogMessage("[META] Warning: ConVars and ConCommands cannot be unregistered properly! Please file a bug report.");
155 SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, ClientCommand, false);
159 void BaseProvider::Notify_DLLShutdown_Pre()
161 g_SMConVarAccessor.RemoveMetamodCommands();
163 #if SOURCE_ENGINE == SE_DARKMESSIAH
164 if (g_Metamod.IsLoadedAsGameDLL())
166 icvar->UnlinkVariables(FCVAR_GAMEDLL);
171 bool BaseProvider::IsRemotePrintingAvailable()
176 void BaseProvider::ClientConsolePrint(edict_t *client, const char *message)
178 engine->ClientPrintf(client, message);
181 void BaseProvider::ServerCommand(const char *cmd)
183 engine->ServerCommand(cmd);
186 const char *BaseProvider::GetConVarString(ConVar *convar)
193 return convar->GetString();
196 void BaseProvider::SetConVarString(ConVar *convar, const char *str)
198 convar->SetValue(str);
201 bool BaseProvider::IsConCommandBaseACommand(ConCommandBase *pCommand)
203 return pCommand->IsCommand();
207 bool BaseProvider::IsSourceEngineBuildCompatible(int build)
209 return (build == SOURCE_ENGINE_ORIGINAL
210 || build == SOURCE_ENGINE_EPISODEONE);
213 const char *BaseProvider::GetCommandLineValue(const char *key, const char *defval)
215 if (key[0] == '-' || key[0] == '+')
217 return CommandLine()->ParmValue(key, defval);
222 if ((val = icvar->GetCommandLineValue(key)) == NULL)
233 int BaseProvider::TryServerGameDLL(const char *iface)
235 if (strncmp(iface, "ServerGameDLL", 13) != 0)
240 return atoi(&iface[13]);
243 bool BaseProvider::LogMessage(const char *buffer)
250 engine->LogPrint(buffer);
255 bool BaseProvider::GetHookInfo(ProvidedHooks hook, SourceHook::MemFuncInfo *pInfo)
257 SourceHook::MemFuncInfo mfi = {true, -1, 0, 0};
259 if (hook == ProvidedHook_LevelInit)
261 SourceHook::GetFuncInfo(&IServerGameDLL::LevelInit, mfi);
263 else if (hook == ProvidedHook_LevelShutdown)
265 SourceHook::GetFuncInfo(&IServerGameDLL::LevelShutdown, mfi);
267 else if (hook == ProvidedHook_GameInit)
269 SourceHook::GetFuncInfo(&IServerGameDLL::GameInit, mfi);
274 return (mfi.thisptroffs >= 0);
277 void BaseProvider::DisplayError(const char *fmt, ...)
283 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
289 void BaseProvider::DisplayWarning(const char *fmt, ...)
295 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
298 Warning("%s", buffer);
301 IConCommandBaseAccessor *BaseProvider::GetConCommandBaseAccessor()
303 return &g_SMConVarAccessor;
306 bool BaseProvider::RegisterConCommandBase(ConCommandBase *pCommand)
308 return g_SMConVarAccessor.Register(pCommand);
311 void BaseProvider::UnregisterConCommandBase(ConCommandBase *pCommand)
313 return g_SMConVarAccessor.Unregister(pCommand);
316 int BaseProvider::GetUserMessageCount()
318 return (int)usermsgs_list.size();
321 int BaseProvider::FindUserMessage(const char *name, int *size)
323 for (size_t i = 0; i < usermsgs_list.size(); i++)
325 if (usermsgs_list[i].name.compare(name) == 0)
329 *size = usermsgs_list[i].size;
338 const char *BaseProvider::GetUserMessage(int index, int *size)
340 if (index < 0 || index >= (int)usermsgs_list.size())
347 *size = usermsgs_list[index].size;
350 return usermsgs_list[index].name.c_str();
353 const char *BaseProvider::GetGameDescription()
355 return server->GetGameDescription();
358 int BaseProvider::DetermineSourceEngine(const char *game)
360 #if SOURCE_ENGINE == SE_BLOODYGOODTIME
361 return SOURCE_ENGINE_BLOODYGOODTIME;
362 #elif SOURCE_ENGINE == SE_ALIENSWARM
363 return SOURCE_ENGINE_ALIENSWARM;
364 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
365 return SOURCE_ENGINE_LEFT4DEAD2;
366 #elif SOURCE_ENGINE == SE_LEFT4DEAD
367 return SOURCE_ENGINE_LEFT4DEAD;
368 #elif SOURCE_ENGINE == SE_ORANGEBOX
369 return SOURCE_ENGINE_ORANGEBOX;
370 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
371 return SOURCE_ENGINE_ORANGEBOXVALVE;
372 #elif SOURCE_ENGINE == SE_DARKMESSIAH
373 return SOURCE_ENGINE_DARKMESSIAH;
374 #elif SOURCE_ENGINE == SE_EYE
375 return SOURCE_ENGINE_EYE;
376 #elif SOURCE_ENGINE == SE_PORTAL2
377 return SOURCE_ENGINE_PORTAL2;
378 #elif SOURCE_ENGINE == SE_CSGO
379 return SOURCE_ENGINE_CSGO;
381 #error "SOURCE_ENGINE not defined to a known value"
385 ConVar *BaseProvider::CreateConVar(const char *name,
391 if (flags & ConVarFlag_Notify)
393 newflags |= FCVAR_NOTIFY;
395 if (flags & ConVarFlag_SpOnly)
397 newflags |= FCVAR_SPONLY;
400 ConVar *pVar = new ConVar(name, defval, newflags, help);
402 g_SMConVarAccessor.RegisterConCommandBase(pVar);
407 bool BaseProvider::ProcessVDF(const char *file, char path[], size_t path_len, char alias[], size_t alias_len)
415 const char *plugin_file, *p_alias;
417 pValues = new KeyValues("Metamod Plugin");
419 if (!pValues->LoadFromFile(baseFs, file))
421 pValues->deleteThis();
425 if ((plugin_file = pValues->GetString("file", NULL)) == NULL)
427 pValues->deleteThis();
431 UTIL_Format(path, path_len, "%s", plugin_file);
433 if ((p_alias = pValues->GetString("alias", NULL)) != NULL)
435 UTIL_Format(alias, alias_len, "%s", p_alias);
439 UTIL_Format(alias, alias_len, "");
442 pValues->deleteThis();
447 #if SOURCE_ENGINE >= SE_ORANGEBOX
448 class GlobCommand : public IMetamodSourceCommandInfo
451 GlobCommand(const CCommand *cmd) : m_cmd(cmd)
455 unsigned int GetArgCount()
457 return m_cmd->ArgC() - 1;
460 const char *GetArg(unsigned int num)
462 return m_cmd->Arg(num);
465 const char *GetArgString()
467 return m_cmd->ArgS();
470 const CCommand *m_cmd;
473 class GlobCommand : public IMetamodSourceCommandInfo
476 unsigned int GetArgCount()
478 return engine->Cmd_Argc() - 1;
481 const char *GetArg(unsigned int num)
483 return engine->Cmd_Argv(num);
486 const char *GetArgString()
488 return engine->Cmd_Args();
493 #if SOURCE_ENGINE >= SE_ORANGEBOX
494 void LocalCommand_Meta(const CCommand &args)
496 GlobCommand cmd(&args);
498 void LocalCommand_Meta()
505 #if SOURCE_ENGINE >= SE_ORANGEBOX
506 void ClientCommand(edict_t *pEdict, const CCommand &_cmd)
508 GlobCommand cmd(&_cmd);
510 void ClientCommand(edict_t *pEdict)
514 if (strcmp(cmd.GetArg(0), "meta") == 0)
516 Command_ClientMeta(pEdict, &cmd);
517 RETURN_META(MRES_SUPERCEDE);
520 RETURN_META(MRES_IGNORED);
523 /* This only gets called if IServerGameDLL::GetUserMessageInfo() triggers it */
524 void Detour_Error(const tchar *pMsg, ...)
526 /* Jump back to setjmp() in CacheUserMessages() */
527 longjmp(usermsg_end, 1);
530 #define IA32_JMP_IMM32 0xE9
532 /* IServerGameDLL::GetUserMessageInfo() crashes on games based on the old engine and
533 * early Orange Box. This is because Error() from tier0 gets called when a bad index is
534 * passed. This is all due to a bug in CUtlRBTree::IsValidIndex().
536 * So we detour Error() to fix this. Our detour then jumps back into CacheUserMessages()
537 * to a point before GetUserMessageInfo() is called. The detour is then removed and we
540 void CacheUserMessages()
544 unsigned char *target, *detour;
545 unsigned char orig_bytes[5];
547 target = (unsigned char *)&Error;
548 detour = (unsigned char *)&Detour_Error;
550 /* Save bytes from target function */
551 memcpy(orig_bytes, target, sizeof(orig_bytes));
553 /* Patch in relative jump to our Error() detour */
554 SetMemAccess(target, sizeof(orig_bytes), SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
555 target[0] = IA32_JMP_IMM32;
556 *(int32_t *)&target[1] = (int32_t)(detour - (target + 5));
558 /* This is where longjmp() will end up */
559 if (setjmp(usermsg_end))
561 /* Restore bytes and memory protection */
562 memcpy(target, orig_bytes, sizeof(orig_bytes));
563 SetMemAccess(target, sizeof(orig_bytes), SH_MEM_READ|SH_MEM_EXEC);
569 /* If GetUserMessageInfo() calls Error(), we should end up in our detour */
570 while (server->GetUserMessageInfo(q, buffer, sizeof(buffer), size))
572 usermsgs_list.push_back(UsrMsgInfo(size, buffer));
576 /* Jump back to setjmp() */
577 longjmp(usermsg_end, 1);