Fixed small new[] / delete mismatch in HookManGen (CGenContext::BuildProtoInfo)
4 * ======================================================
6 * Copyright (C) 2004-2007 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.
33 #include "../metamod_oslink.h"
34 #include <sourcehook.h>
37 #include <tier0/icommandline.h>
38 #include <tier1/utldict.h>
39 #include <sh_vector.h>
40 #include <sh_string.h>
41 #include "../metamod_util.h"
42 #include "provider_ep2.h"
44 #include "metamod_console.h"
45 #include "vsp_listener.h"
46 #include <filesystem.h>
53 typedef void (*CONPRINTF_FUNC)(const char *, ...);
61 DLL_IMPORT ICommandLine *CommandLine();
63 bool CacheUserMessages();
64 void ClientCommand(edict_t *pEdict, const CCommand &args);
65 void LocalCommand_Meta(const CCommand &args);
66 void _ServerCommand();
68 bool usermsgs_extracted = false;
69 CVector<UsrMsgInfo> usermsgs_list;
71 ISmmAPI *metamod_api = NULL;
72 IVEngineServer *engine = NULL;
73 IServerGameClients *gameclients = NULL;
74 VSPListener g_VspListener;
75 BaseProvider g_Ep1Provider;
76 IMetamodSourceProvider *provider = &g_Ep1Provider;
77 List<ConCommandBase *> conbases_unreg;
78 IFileSystem *baseFs = NULL;
79 ConCommand meta_local_cmd("meta", LocalCommand_Meta, "Metamod:Source control options");
81 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
83 void BaseProvider::ConsolePrint(const char *str)
88 void BaseProvider::Notify_DLLInit_Pre(CreateInterfaceFn engineFactory,
89 CreateInterfaceFn serverFactory)
91 engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
94 DisplayError("Could not find IVEngineServer! Metamod cannot load.");
97 icvar = (ICvar *)((engineFactory)(CVAR_INTERFACE_VERSION, NULL));
100 DisplayError("Could not find ICvar! Metamod cannot load.");
105 if ((gameclients = (IServerGameClients *)(serverFactory("ServerGameClients003", NULL)))
108 gameclients = (IServerGameClients *)(serverFactory("ServerGameClients004", NULL));
111 baseFs = (IFileSystem *)((engineFactory)(FILESYSTEM_INTERFACE_VERSION, NULL));
114 ::LogMessage("Unable to find \"%s\": .vdf files will not be parsed", FILESYSTEM_INTERFACE_VERSION);
118 RegisterConCommandBase(&meta_local_cmd);
119 conbases_unreg.push_back(&meta_local_cmd);
121 usermsgs_extracted = CacheUserMessages();
125 SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, ClientCommand, false);
129 void BaseProvider::Notify_DLLShutdown_Pre()
131 List<ConCommandBase *>::iterator iter;
133 for (iter = conbases_unreg.begin();
134 iter != conbases_unreg.end();
137 UnregisterConCommandBase((*iter));
141 bool BaseProvider::IsRemotePrintingAvailable()
146 void BaseProvider::ClientConsolePrint(edict_t *client, const char *message)
148 engine->ClientPrintf(client, message);
151 void BaseProvider::ServerCommand(const char *cmd)
153 engine->ServerCommand(cmd);
156 const char *BaseProvider::GetConVarString(ConVar *convar)
163 return convar->GetString();
166 void BaseProvider::SetConVarString(ConVar *convar, const char *str)
168 convar->SetValue(str);
171 bool BaseProvider::IsConCommandBaseACommand(ConCommandBase *pCommand)
173 return pCommand->IsCommand();
177 bool BaseProvider::IsSourceEngineBuildCompatible(int build)
179 return (build == SOURCE_ENGINE_ORIGINAL
180 || build == SOURCE_ENGINE_EPISODEONE);
183 const char *BaseProvider::GetCommandLineValue(const char *key, const char *defval)
185 if (key[0] == '-' || key[0] == '+')
187 return CommandLine_Tier0()->ParmValue(key, defval);
192 if ((val = icvar->GetCommandLineValue(key)) == NULL)
203 int BaseProvider::TryServerGameDLL(const char *iface)
205 if (strncmp(iface, "ServerGameDLL", 13) != 0)
210 return atoi(&iface[13]);
213 bool BaseProvider::LogMessage(const char *buffer)
220 engine->LogPrint(buffer);
225 bool BaseProvider::GetHookInfo(ProvidedHooks hook, SourceHook::MemFuncInfo *pInfo)
227 SourceHook::MemFuncInfo mfi = {true, -1, 0, 0};
229 if (hook == ProvidedHook_LevelInit)
231 SourceHook::GetFuncInfo(&IServerGameDLL::LevelInit, mfi);
233 else if (hook == ProvidedHook_LevelShutdown)
235 SourceHook::GetFuncInfo(&IServerGameDLL::LevelShutdown, mfi);
237 else if (hook == ProvidedHook_GameInit)
239 SourceHook::GetFuncInfo(&IServerGameDLL::GameInit, mfi);
241 else if (hook == ProvidedHook_DLLShutdown)
243 SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfi);
245 else if (hook == ProvidedHook_DLLInit)
247 SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfi);
252 return (mfi.thisptroffs >= 0);
255 void BaseProvider::DisplayError(const char *fmt, ...)
261 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
267 void BaseProvider::DisplayWarning(const char *fmt, ...)
273 UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
279 IConCommandBaseAccessor *BaseProvider::GetConCommandBaseAccessor()
281 return &g_SMConVarAccessor;
284 bool BaseProvider::RegisterConCommandBase(ConCommandBase *pCommand)
286 return g_SMConVarAccessor.Register(pCommand);
289 void BaseProvider::UnregisterConCommandBase(ConCommandBase *pCommand)
291 return g_SMConVarAccessor.Unregister(pCommand);
294 int BaseProvider::GetUserMessageCount()
296 if (!usermsgs_extracted)
301 return (int)usermsgs_list.size();
304 int BaseProvider::FindUserMessage(const char *name, int *size)
306 for (size_t i = 0; i < usermsgs_list.size(); i++)
308 if (usermsgs_list[i].name.compare(name) == 0)
312 *size = usermsgs_list[i].size;
321 const char *BaseProvider::GetUserMessage(int index, int *size)
323 if (!usermsgs_extracted || index < 0 || index >= (int)usermsgs_list.size())
330 *size = usermsgs_list[index].size;
333 return usermsgs_list[index].name.c_str();
336 const char *BaseProvider::GetGameDescription()
338 return server->GetGameDescription();
341 int BaseProvider::DetermineSourceEngine(const char *game)
343 return SOURCE_ENGINE_ORANGEBOX;
346 ConVar *BaseProvider::CreateConVar(const char *name,
352 if (flags & ConVarFlag_Notify)
354 newflags |= FCVAR_NOTIFY;
356 if (flags & ConVarFlag_Replicated)
358 newflags |= FCVAR_REPLICATED;
360 if (flags & ConVarFlag_SpOnly)
362 newflags |= FCVAR_SPONLY;
365 ConVar *pVar = new ConVar(name, defval, newflags, help);
367 RegisterConCommandBase(pVar);
368 conbases_unreg.push_back(pVar);
373 IServerPluginCallbacks *BaseProvider::GetVSPCallbacks(const char *iface)
375 g_VspListener.SetLoadable(true);
376 return &g_VspListener;
379 bool BaseProvider::IsAlternatelyLoaded()
381 return g_VspListener.IsRootLoadMethod();
384 bool BaseProvider::ProcessVDF(const char *file, char path[], size_t path_len, char alias[], size_t alias_len)
392 const char *plugin_file, *p_alias;
394 pValues = new KeyValues("Metamod Plugin");
396 if (!pValues->LoadFromFile(baseFs, file))
398 pValues->deleteThis();
402 if ((plugin_file = pValues->GetString("file", NULL)) == NULL)
404 pValues->deleteThis();
408 if ((p_alias = pValues->GetString("alias", NULL)) != NULL)
410 UTIL_Format(alias, alias_len, "%s", p_alias);
414 UTIL_Format(alias, alias_len, "");
417 /* Attempt to find a file extension */
418 if (UTIL_GetExtension(plugin_file) == NULL)
420 g_pMetamod->PathFormat(path,
423 g_pMetamod->GetBaseDir(),
425 #if defined WIN32 || defined _WIN32
434 g_pMetamod->PathFormat(path,
437 g_pMetamod->GetBaseDir(),
441 pValues->deleteThis();
446 class GlobCommand : public IMetamodSourceCommandInfo
449 GlobCommand(const CCommand *cmd) : m_cmd(cmd)
453 unsigned int GetArgCount()
455 return m_cmd->ArgC() - 1;
458 const char *GetArg(unsigned int num)
460 return m_cmd->Arg(num);
463 const char *GetArgString()
465 return m_cmd->ArgS();
468 const CCommand *m_cmd;
471 void LocalCommand_Meta(const CCommand &args)
473 GlobCommand cmd(&args);
477 void ClientCommand(edict_t *pEdict, const CCommand &_cmd)
479 GlobCommand cmd(&_cmd);
481 if (strcmp(cmd.GetArg(0), "meta") == 0)
483 Command_ClientMeta(pEdict, &cmd);
484 RETURN_META(MRES_SUPERCEDE);
487 RETURN_META(MRES_IGNORED);
490 bool vcmp(const void *_addr1, const void *_addr2, size_t len)
492 unsigned char *addr1 = (unsigned char *)_addr1;
493 unsigned char *addr2 = (unsigned char *)_addr2;
495 for (size_t i=0; i<len; i++)
501 if (addr1[i] != addr2[i])
510 //////////////////////////////////////////////////////////////////////
511 // EVEN MORE HACKS HERE! YOU HAVE BEEN WARNED! //
512 // Signatures necessary in finding the pointer to the CUtlDict that //
513 // stores user message information. //
514 // IServerGameDLL::GetUserMessageInfo() normally crashes with bad //
515 // message indices. This is our answer to it. Yuck! <:-( //
516 //////////////////////////////////////////////////////////////////////
518 /* General Windows sig */
519 #define MSGCLASS_SIGLEN 7
520 #define MSGCLASS_SIG "\x8B\x0D\x2A\x2A\x2A\x2A\x56"
521 #define MSGCLASS_OFFS 2
523 /* Dystopia Wimdows hack */
524 #define MSGCLASS2_SIGLEN 16
525 #define MSGCLASS2_SIG "\x56\x8B\x74\x24\x2A\x85\xF6\x7C\x2A\x3B\x35\x2A\x2A\x2A\x2A\x7D"
526 #define MSGCLASS2_OFFS 11
528 /* Windows frame pointer sig */
529 #define MSGCLASS3_SIGLEN 18
530 #define MSGCLASS3_SIG "\x55\x8B\xEC\x51\x89\x2A\x2A\x8B\x2A\x2A\x50\x8B\x0D\x2A\x2A\x2A\x2A\xE8"
531 #define MSGCLASS3_OFFS 13
532 #elif defined OS_LINUX
533 /* No frame pointer sig */
534 #define MSGCLASS_SIGLEN 14
535 #define MSGCLASS_SIG "\x53\x83\xEC\x2A\x8B\x2A\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
536 #define MSGCLASS_OFFS 9
538 /* Frame pointer sig */
539 #define MSGCLASS2_SIGLEN 16
540 #define MSGCLASS2_SIG "\x55\x89\xE5\x53\x83\xEC\x2A\x8B\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
541 #define MSGCLASS2_OFFS 11
550 typedef CUtlDict<UserMessage *, int> UserMsgDict;
552 /* This is the ugliest function in all of SourceMM */
553 bool CacheUserMessages()
555 /* Get address of original GetUserMessageInfo() */
556 char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(server, &IServerGameDLL::GetUserMessageInfo);
558 /* Oh dear, we have a relative jump on our hands
559 * PVK II on Windows made me do this, but I suppose it doesn't hurt to check this on Linux too...
561 if (*vfunc == '\xE9')
563 /* Get address from displacement...
565 * Add 5 because it's relative to next instruction:
566 * Opcode <1 byte> + 32-bit displacement <4 bytes>
568 vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5;
571 CUtlDict<UserMessage *, int> *dict = NULL;
573 if (vcmp(vfunc, MSGCLASS_SIG, MSGCLASS_SIGLEN))
575 /* Get address of CUserMessages instance */
576 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS_OFFS);
578 /* Get address of CUserMessages::m_UserMessages */
579 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
581 else if (vcmp(vfunc, MSGCLASS2_SIG, MSGCLASS2_SIGLEN))
584 /* If we get here, the code is possibly inlined like in Dystopia */
586 /* Get the address of the CUtlRBTree */
587 char *rbtree = *reinterpret_cast<char **>(vfunc + MSGCLASS2_OFFS);
589 /* CUtlDict should be 8 bytes before the CUtlRBTree (hacktacular!) */
590 dict = reinterpret_cast<UserMsgDict *>(rbtree - 8);
591 #elif defined OS_LINUX
592 /* Get address of CUserMessages instance */
593 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS2_OFFS);
595 /* Get address of CUserMessages::m_UserMessages */
596 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
600 else if (vcmp(vfunc, MSGCLASS3_SIG, MSGCLASS3_SIGLEN))
602 /* Get address of CUserMessages instance */
603 char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS3_OFFS);
605 /* Get address of CUserMessages::m_UserMessages */
606 dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
610 #if !defined OS_WIN32
614 if (GetFileOfAddress(vfunc, path, sizeof(path)))
616 void *handle = dlopen(path, RTLD_NOW);
619 void *addr = dlsym(handle, "usermessages");
620 dict = (UserMsgDict *)*(void **)addr;
629 int msg_count = dict->Count();
631 /* Ensure that count is within bounds of an unsigned byte, because that's what engine supports */
632 if (msg_count < 0 || msg_count > 255)
640 /* Cache messages in our CUtlDict */
641 for (int i = 0; i < msg_count; i++)
643 msg = dict->Element(i);
644 u_msg.name = msg->name;
645 u_msg.size = msg->size;
646 usermsgs_list.push_back(u_msg);