Rewrote user message caching code to always use IServerGameDLL::GetUserMessageInfo (
bug 4117, r=dvander).
Games where the above function can crash are handled by detouring tier0's Error() function and jumping back to our own code. So in theory this should work on all games now.
3 * ======================================================
5 * Copyright (C) 2004-2008 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.
29 #include "provider_ep2.h"
30 #include "metamod_util.h"
32 using namespace SourceHook;
34 SMConVarAccessor g_SMConVarAccessor;
36 #if SOURCE_ENGINE >= SE_ORANGEBOX
38 #define RegisterConCommand RegisterConCommandBase
41 bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
43 m_RegisteredCommands.push_back(pCommand);
44 pCommand->SetNext(NULL);
45 icvar->RegisterConCommand(pCommand);
50 bool SMConVarAccessor::Register(ConCommandBase *pCommand)
52 pCommand->SetNext(NULL);
53 icvar->RegisterConCommand(pCommand);
58 void SMConVarAccessor::RemoveMetamodCommands()
60 List<ConCommandBase *>::iterator iter;
62 for (iter = m_RegisteredCommands.begin(); iter != m_RegisteredCommands.end(); iter++)
68 #if SOURCE_ENGINE == SE_DARKMESSIAH
69 /* Signature for ICvar::GetCommands() in vstdlib for Win32 and Linux.
71 * 20226EE0 A1 50 5C 5A 20 mov eax,dword ptr ds:[205A5C50h] <-- What we want
74 #define CMDLIST_SIG "\xA1\x2A\x2A\x2A\x2A\xC3"
75 #define CMDLIST_SIGLEN 6
77 /* Linux symbol name of ConCommandBase list in vstdlib */
78 #define CMDLIST_SYMBOL "_ZN14ConCommandBase18s_pConCommandBasesE"
80 /* This function retrieves the address of the var that holds the top of the ConCommandBase list.
81 * Having this allows us to change the beginning of this list with ease.
83 * This craziness eliminates the need for the eternal command/cvar used previously which
84 * could have caused a crash as a result of registering commands/cvars more than once.
86 bool SMConVarAccessor::InitConCommandBaseList()
88 char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(icvar, &ICvar::GetCommands);
92 /* Get address from displacement...
94 * Add 5 because it's relative to next instruction:
95 * Opcode <1 byte> + 32-bit displacement <4 bytes>
97 vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5;
106 if (UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
108 /* Skip past 0xA1 and get addr of ConCommandBase list var */
109 m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
112 #elif defined OS_LINUX
113 /* Try dlsym first */
114 char path[PATH_SIZE];
115 if (GetFileOfAddress((void *)icvar, path, sizeof(path)))
117 void *handle = dlopen(path, RTLD_NOW);
120 m_TopConCommandBase = reinterpret_cast<ConCommandBase **>(dlsym(handle, CMDLIST_SYMBOL));
126 /* If dlsym failed, then verify signature of function */
127 if (!m_TopConCommandBase && UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
129 /* Skip past 0xA1 and get addr of ConCommandBase list var */
130 m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
139 void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
141 #if SOURCE_ENGINE >= SE_ORANGEBOX
142 icvar->UnregisterConCommand(pCommand);
144 ConCommandBase *pCur = NULL;
145 ConCommandBase *pPrev = NULL;
152 pCur = icvar->GetCommands();
154 if (!m_TopConCommandBase || !pCur)
159 if (pCur == pCommand)
161 *m_TopConCommandBase = const_cast<ConCommandBase *>(pCommand->GetNext());
162 pCommand->SetNext(NULL);
167 pCur = const_cast<ConCommandBase *>(pCur->GetNext());
171 if (pCur == pCommand)
173 pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
174 pCommand->SetNext(NULL);
178 pCur = const_cast<ConCommandBase *>(pCur->GetNext());