core/provider/console.cpp
author Scott Ehlert <ds@alliedmods.net>
Wed Mar 21 06:09:12 2012 -0500 (2012-03-21)
changeset 770 ff8eb91fd60a
parent 725 9eec529c3caa
child 780 35f2a207badf
permissions -rw-r--r--
Triggering a 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 #if SOURCE_ENGINE != SE_ALIENSWARM
    45 	pCommand->SetNext(NULL);
    46 #endif
    47 	icvar->RegisterConCommand(pCommand);
    48 
    49 	return true;
    50 }
    51 
    52 bool SMConVarAccessor::Register(ConCommandBase *pCommand)
    53 {
    54 #if SOURCE_ENGINE != SE_ALIENSWARM
    55 	pCommand->SetNext(NULL);
    56 #endif
    57 	icvar->RegisterConCommand(pCommand);
    58 
    59 	return true;
    60 }
    61 
    62 void SMConVarAccessor::RemoveMetamodCommands()
    63 {
    64 	List<ConCommandBase *>::iterator iter;
    65 
    66 	for (iter = m_RegisteredCommands.begin(); iter != m_RegisteredCommands.end(); iter++)
    67 	{
    68 		Unregister(*iter);
    69 	}
    70 }
    71 
    72 #if SOURCE_ENGINE == SE_DARKMESSIAH
    73 /* Signature for ICvar::GetCommands() in vstdlib for Win32 and Linux.
    74  *
    75  * 20226EE0 A1 50 5C 5A 20   mov         eax,dword ptr ds:[205A5C50h] <-- What we want
    76  * 20226EE5 C3               ret              
    77  */
    78 #define CMDLIST_SIG "\xA1\x2A\x2A\x2A\x2A\xC3"
    79 #define CMDLIST_SIGLEN 6
    80 
    81 /* Linux symbol name of ConCommandBase list in vstdlib */
    82 #define CMDLIST_SYMBOL "_ZN14ConCommandBase18s_pConCommandBasesE"
    83 
    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.
    86  *
    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.
    89  */
    90 bool SMConVarAccessor::InitConCommandBaseList()
    91 {
    92 	char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(icvar, &ICvar::GetCommands);
    93 
    94 	if (*vfunc == '\xE9')
    95 	{
    96 		/* Get address from displacement...
    97 		*
    98 		* Add 5 because it's relative to next instruction:
    99 		* Opcode <1 byte> + 32-bit displacement <4 bytes> 
   100 		*/
   101 		vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5;
   102 	}
   103 
   104 	if (!vfunc)
   105 	{
   106 		return false;
   107 	}
   108 
   109 #ifdef OS_WIN32
   110 	if (UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
   111 	{
   112 		/* Skip past 0xA1 and get addr of ConCommandBase list var */
   113 		m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
   114 		return true;
   115 	}
   116 #elif defined OS_LINUX
   117 	/* Try dlsym first */
   118 	char path[PATH_SIZE];
   119 	if (GetFileOfAddress((void *)icvar, path, sizeof(path)))
   120 	{
   121 		void *handle = dlopen(path, RTLD_NOW);
   122 		if (handle)
   123 		{
   124 			m_TopConCommandBase = reinterpret_cast<ConCommandBase **>(dlsym(handle, CMDLIST_SYMBOL));
   125 			dlclose(handle);
   126 			return true;
   127 		}
   128 	}
   129 
   130 	/* If dlsym failed, then verify signature of function */
   131 	if (!m_TopConCommandBase && UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
   132 	{
   133 		/* Skip past 0xA1 and get addr of ConCommandBase list var */
   134 		m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
   135 		return true;
   136 	}
   137 #endif
   138 
   139 	return false;
   140 }
   141 #endif
   142 
   143 void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
   144 {
   145 #if SOURCE_ENGINE >= SE_ORANGEBOX
   146 	icvar->UnregisterConCommand(pCommand);
   147 #else
   148 	ConCommandBase *pCur = NULL;
   149 	ConCommandBase *pPrev = NULL;
   150 
   151 	if (!pCommand)
   152 	{
   153 		return;
   154 	}
   155 
   156 	pCur = icvar->GetCommands();
   157 
   158 	if (!m_TopConCommandBase || !pCur)
   159 	{
   160 		return;
   161 	}
   162 
   163 	if (pCur == pCommand)
   164 	{
   165 		*m_TopConCommandBase = const_cast<ConCommandBase *>(pCommand->GetNext());
   166 		pCommand->SetNext(NULL);
   167 		return;
   168 	}
   169 	
   170 	pPrev = pCur;
   171 	pCur = const_cast<ConCommandBase *>(pCur->GetNext());
   172 
   173 	while (pCur)
   174 	{
   175 		if (pCur == pCommand)
   176 		{
   177 			pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
   178 			pCommand->SetNext(NULL);
   179 		}
   180 
   181 		pPrev = pCur;
   182 		pCur = const_cast<ConCommandBase *>(pCur->GetNext());
   183 	}
   184 #endif
   185 }
   186