core/provider/console.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri May 14 01:15:28 2010 -0500 (2010-05-14)
changeset 707 93a43cced910
parent 637 b66489d8aedc
child 725 9eec529c3caa
permissions -rw-r--r--
Triggering build.
     1 /**
     2  * vim: set ts=4 :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2008 AlliedModders LLC and authors.
     6  * All rights reserved.
     7  * ======================================================
     8  *
     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.
    12  * 
    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:
    16  *
    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.
    24  *
    25  * Version: $Id$
    26  */
    27 
    28 #include "console.h"
    29 #include "provider_ep2.h"
    30 #include "metamod_util.h"
    31 
    32 using namespace SourceHook;
    33 
    34 SMConVarAccessor g_SMConVarAccessor;
    35 
    36 #if SOURCE_ENGINE >= SE_ORANGEBOX
    37 #else
    38 #define RegisterConCommand RegisterConCommandBase
    39 #endif
    40 
    41 bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
    42 {
    43 	m_RegisteredCommands.push_back(pCommand);
    44 	pCommand->SetNext(NULL);
    45 	icvar->RegisterConCommand(pCommand);
    46 
    47 	return true;
    48 }
    49 
    50 bool SMConVarAccessor::Register(ConCommandBase *pCommand)
    51 {
    52 	pCommand->SetNext(NULL);
    53 	icvar->RegisterConCommand(pCommand);
    54 
    55 	return true;
    56 }
    57 
    58 void SMConVarAccessor::RemoveMetamodCommands()
    59 {
    60 	List<ConCommandBase *>::iterator iter;
    61 
    62 	for (iter = m_RegisteredCommands.begin(); iter != m_RegisteredCommands.end(); iter++)
    63 	{
    64 		Unregister(*iter);
    65 	}
    66 }
    67 
    68 #if SOURCE_ENGINE == SE_DARKMESSIAH
    69 /* Signature for ICvar::GetCommands() in vstdlib for Win32 and Linux.
    70  *
    71  * 20226EE0 A1 50 5C 5A 20   mov         eax,dword ptr ds:[205A5C50h] <-- What we want
    72  * 20226EE5 C3               ret              
    73  */
    74 #define CMDLIST_SIG "\xA1\x2A\x2A\x2A\x2A\xC3"
    75 #define CMDLIST_SIGLEN 6
    76 
    77 /* Linux symbol name of ConCommandBase list in vstdlib */
    78 #define CMDLIST_SYMBOL "_ZN14ConCommandBase18s_pConCommandBasesE"
    79 
    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.
    82  *
    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.
    85  */
    86 bool SMConVarAccessor::InitConCommandBaseList()
    87 {
    88 	char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(icvar, &ICvar::GetCommands);
    89 
    90 	if (*vfunc == '\xE9')
    91 	{
    92 		/* Get address from displacement...
    93 		*
    94 		* Add 5 because it's relative to next instruction:
    95 		* Opcode <1 byte> + 32-bit displacement <4 bytes> 
    96 		*/
    97 		vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5;
    98 	}
    99 
   100 	if (!vfunc)
   101 	{
   102 		return false;
   103 	}
   104 
   105 #ifdef OS_WIN32
   106 	if (UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
   107 	{
   108 		/* Skip past 0xA1 and get addr of ConCommandBase list var */
   109 		m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
   110 		return true;
   111 	}
   112 #elif defined OS_LINUX
   113 	/* Try dlsym first */
   114 	char path[PATH_SIZE];
   115 	if (GetFileOfAddress((void *)icvar, path, sizeof(path)))
   116 	{
   117 		void *handle = dlopen(path, RTLD_NOW);
   118 		if (handle)
   119 		{
   120 			m_TopConCommandBase = reinterpret_cast<ConCommandBase **>(dlsym(handle, CMDLIST_SYMBOL));
   121 			dlclose(handle);
   122 			return true;
   123 		}
   124 	}
   125 
   126 	/* If dlsym failed, then verify signature of function */
   127 	if (!m_TopConCommandBase && UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
   128 	{
   129 		/* Skip past 0xA1 and get addr of ConCommandBase list var */
   130 		m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
   131 		return true;
   132 	}
   133 #endif
   134 
   135 	return false;
   136 }
   137 #endif
   138 
   139 void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
   140 {
   141 #if SOURCE_ENGINE >= SE_ORANGEBOX
   142 	icvar->UnregisterConCommand(pCommand);
   143 #else
   144 	ConCommandBase *pCur = NULL;
   145 	ConCommandBase *pPrev = NULL;
   146 
   147 	if (!pCommand)
   148 	{
   149 		return;
   150 	}
   151 
   152 	pCur = icvar->GetCommands();
   153 
   154 	if (!m_TopConCommandBase || !pCur)
   155 	{
   156 		return;
   157 	}
   158 
   159 	if (pCur == pCommand)
   160 	{
   161 		*m_TopConCommandBase = const_cast<ConCommandBase *>(pCommand->GetNext());
   162 		pCommand->SetNext(NULL);
   163 		return;
   164 	}
   165 	
   166 	pPrev = pCur;
   167 	pCur = const_cast<ConCommandBase *>(pCur->GetNext());
   168 
   169 	while (pCur)
   170 	{
   171 		if (pCur == pCommand)
   172 		{
   173 			pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
   174 			pCommand->SetNext(NULL);
   175 		}
   176 
   177 		pPrev = pCur;
   178 		pCur = const_cast<ConCommandBase *>(pCur->GetNext());
   179 	}
   180 #endif
   181 }
   182