dvander@371: /** dvander@371: * vim: set ts=4 : dvander@371: * ====================================================== dvander@371: * Metamod:Source ds@523: * Copyright (C) 2004-2008 AlliedModders LLC and authors. dvander@371: * All rights reserved. dvander@371: * ====================================================== dvander@371: * dvander@371: * This software is provided 'as-is', without any express or implied warranty. dvander@371: * In no event will the authors be held liable for any damages arising from dvander@371: * the use of this software. dvander@371: * dvander@371: * Permission is granted to anyone to use this software for any purpose, dvander@371: * including commercial applications, and to alter it and redistribute it dvander@371: * freely, subject to the following restrictions: dvander@371: * dvander@371: * 1. The origin of this software must not be misrepresented; you must not dvander@371: * claim that you wrote the original software. If you use this software in a dvander@371: * product, an acknowledgment in the product documentation would be dvander@371: * appreciated but is not required. dvander@371: * 2. Altered source versions must be plainly marked as such, and must not be dvander@371: * misrepresented as being the original software. dvander@371: * 3. This notice may not be removed or altered from any source distribution. dvander@371: * dvander@371: * Version: $Id$ dvander@371: */ dvander@371: dvander@371: #include "console.h" dvander@371: #include "provider_ep2.h" ds@637: #include "metamod_util.h" dvander@371: dvander@371: using namespace SourceHook; dvander@371: dvander@371: SMConVarAccessor g_SMConVarAccessor; dvander@371: ds@637: #if SOURCE_ENGINE >= SE_ORANGEBOX ds@637: #else ds@637: #define RegisterConCommand RegisterConCommandBase ds@637: #endif ds@637: dvander@371: bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand) dvander@371: { ds@637: m_RegisteredCommands.push_back(pCommand); ds@725: #if SOURCE_ENGINE != SE_ALIENSWARM dvander@371: pCommand->SetNext(NULL); ds@725: #endif dvander@371: icvar->RegisterConCommand(pCommand); dvander@371: dvander@371: return true; dvander@371: } dvander@371: dvander@371: bool SMConVarAccessor::Register(ConCommandBase *pCommand) dvander@371: { ds@725: #if SOURCE_ENGINE != SE_ALIENSWARM dvander@371: pCommand->SetNext(NULL); ds@725: #endif dvander@371: icvar->RegisterConCommand(pCommand); dvander@371: dvander@371: return true; dvander@371: } dvander@371: ds@637: void SMConVarAccessor::RemoveMetamodCommands() ds@637: { ds@637: List::iterator iter; ds@637: ds@637: for (iter = m_RegisteredCommands.begin(); iter != m_RegisteredCommands.end(); iter++) ds@637: { ds@637: Unregister(*iter); ds@637: } ds@637: } ds@637: ds@637: #if SOURCE_ENGINE == SE_DARKMESSIAH ds@637: /* Signature for ICvar::GetCommands() in vstdlib for Win32 and Linux. ds@637: * ds@637: * 20226EE0 A1 50 5C 5A 20 mov eax,dword ptr ds:[205A5C50h] <-- What we want ds@637: * 20226EE5 C3 ret ds@637: */ ds@637: #define CMDLIST_SIG "\xA1\x2A\x2A\x2A\x2A\xC3" ds@637: #define CMDLIST_SIGLEN 6 ds@637: ds@637: /* Linux symbol name of ConCommandBase list in vstdlib */ ds@637: #define CMDLIST_SYMBOL "_ZN14ConCommandBase18s_pConCommandBasesE" ds@637: ds@637: /* This function retrieves the address of the var that holds the top of the ConCommandBase list. ds@637: * Having this allows us to change the beginning of this list with ease. ds@637: * ds@637: * This craziness eliminates the need for the eternal command/cvar used previously which ds@637: * could have caused a crash as a result of registering commands/cvars more than once. ds@637: */ ds@637: bool SMConVarAccessor::InitConCommandBaseList() ds@637: { ds@637: char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(icvar, &ICvar::GetCommands); ds@637: ds@637: if (*vfunc == '\xE9') ds@637: { ds@637: /* Get address from displacement... ds@637: * ds@637: * Add 5 because it's relative to next instruction: ds@637: * Opcode <1 byte> + 32-bit displacement <4 bytes> ds@637: */ ds@637: vfunc += *reinterpret_cast(vfunc + 1) + 5; ds@637: } ds@637: ds@637: if (!vfunc) ds@637: { ds@637: return false; ds@637: } ds@637: ds@637: #ifdef OS_WIN32 ds@637: if (UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN)) ds@637: { ds@637: /* Skip past 0xA1 and get addr of ConCommandBase list var */ ds@637: m_TopConCommandBase = *reinterpret_cast(vfunc + 1); ds@637: return true; ds@637: } ds@637: #elif defined OS_LINUX ds@637: /* Try dlsym first */ ds@637: char path[PATH_SIZE]; ds@637: if (GetFileOfAddress((void *)icvar, path, sizeof(path))) ds@637: { ds@637: void *handle = dlopen(path, RTLD_NOW); ds@637: if (handle) ds@637: { ds@637: m_TopConCommandBase = reinterpret_cast(dlsym(handle, CMDLIST_SYMBOL)); ds@637: dlclose(handle); ds@637: return true; ds@637: } ds@637: } ds@637: ds@637: /* If dlsym failed, then verify signature of function */ ds@637: if (!m_TopConCommandBase && UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN)) ds@637: { ds@637: /* Skip past 0xA1 and get addr of ConCommandBase list var */ ds@637: m_TopConCommandBase = *reinterpret_cast(vfunc + 1); ds@637: return true; ds@637: } ds@637: #endif ds@637: ds@637: return false; ds@637: } ds@637: #endif ds@637: dvander@371: void SMConVarAccessor::Unregister(ConCommandBase *pCommand) dvander@371: { ds@637: #if SOURCE_ENGINE >= SE_ORANGEBOX dvander@371: icvar->UnregisterConCommand(pCommand); ds@637: #else ds@637: ConCommandBase *pCur = NULL; ds@637: ConCommandBase *pPrev = NULL; ds@637: ds@637: if (!pCommand) ds@637: { ds@637: return; ds@637: } ds@637: ds@637: pCur = icvar->GetCommands(); ds@637: ds@637: if (!m_TopConCommandBase || !pCur) ds@637: { ds@637: return; ds@637: } ds@637: ds@637: if (pCur == pCommand) ds@637: { ds@637: *m_TopConCommandBase = const_cast(pCommand->GetNext()); ds@637: pCommand->SetNext(NULL); ds@637: return; ds@637: } ds@637: ds@637: pPrev = pCur; ds@637: pCur = const_cast(pCur->GetNext()); ds@637: ds@637: while (pCur) ds@637: { ds@637: if (pCur == pCommand) ds@637: { ds@637: pPrev->SetNext(const_cast(pCommand->GetNext())); ds@637: pCommand->SetNext(NULL); ds@637: } ds@637: ds@637: pPrev = pCur; ds@637: pCur = const_cast(pCur->GetNext()); ds@637: } ds@637: #endif dvander@371: } dvander@371: