Added support for CS: Global Offensive (
bug 5228, r=DS).
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 #if SOURCE_ENGINE < SE_ALIENSWARM
45 pCommand->SetNext(NULL);
47 icvar->RegisterConCommand(pCommand);
52 bool SMConVarAccessor::Register(ConCommandBase *pCommand)
54 #if SOURCE_ENGINE < SE_ALIENSWARM
55 pCommand->SetNext(NULL);
57 icvar->RegisterConCommand(pCommand);
62 void SMConVarAccessor::RemoveMetamodCommands()
64 List<ConCommandBase *>::iterator iter;
66 for (iter = m_RegisteredCommands.begin(); iter != m_RegisteredCommands.end(); iter++)
72 #if SOURCE_ENGINE == SE_DARKMESSIAH
73 /* Signature for ICvar::GetCommands() in vstdlib for Win32 and Linux.
75 * 20226EE0 A1 50 5C 5A 20 mov eax,dword ptr ds:[205A5C50h] <-- What we want
78 #define CMDLIST_SIG "\xA1\x2A\x2A\x2A\x2A\xC3"
79 #define CMDLIST_SIGLEN 6
81 /* Linux symbol name of ConCommandBase list in vstdlib */
82 #define CMDLIST_SYMBOL "_ZN14ConCommandBase18s_pConCommandBasesE"
84 /* This function retrieves the address of the var that holds the top of the ConCommandBase list.
85 * Having this allows us to change the beginning of this list with ease.
87 * This craziness eliminates the need for the eternal command/cvar used previously which
88 * could have caused a crash as a result of registering commands/cvars more than once.
90 bool SMConVarAccessor::InitConCommandBaseList()
92 char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(icvar, &ICvar::GetCommands);
96 /* Get address from displacement...
98 * Add 5 because it's relative to next instruction:
99 * Opcode <1 byte> + 32-bit displacement <4 bytes>
101 vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5;
110 if (UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
112 /* Skip past 0xA1 and get addr of ConCommandBase list var */
113 m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
116 #elif defined OS_LINUX
117 /* Try dlsym first */
118 char path[PATH_SIZE];
119 if (GetFileOfAddress((void *)icvar, path, sizeof(path)))
121 void *handle = dlopen(path, RTLD_NOW);
124 m_TopConCommandBase = reinterpret_cast<ConCommandBase **>(dlsym(handle, CMDLIST_SYMBOL));
130 /* If dlsym failed, then verify signature of function */
131 if (!m_TopConCommandBase && UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
133 /* Skip past 0xA1 and get addr of ConCommandBase list var */
134 m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
143 void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
145 #if SOURCE_ENGINE >= SE_ORANGEBOX
146 icvar->UnregisterConCommand(pCommand);
148 ConCommandBase *pCur = NULL;
149 ConCommandBase *pPrev = NULL;
156 pCur = icvar->GetCommands();
158 if (!m_TopConCommandBase || !pCur)
163 if (pCur == pCommand)
165 *m_TopConCommandBase = const_cast<ConCommandBase *>(pCommand->GetNext());
166 pCommand->SetNext(NULL);
171 pCur = const_cast<ConCommandBase *>(pCur->GetNext());
175 if (pCur == pCommand)
177 pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
178 pCommand->SetNext(NULL);
182 pCur = const_cast<ConCommandBase *>(pCur->GetNext());