core/provider/console.cpp
changeset 637 b66489d8aedc
parent 539 db914129ae01
child 725 9eec529c3caa
     1.1 --- a/core/provider/console.cpp	Mon Feb 16 18:13:31 2009 -0500
     1.2 +++ b/core/provider/console.cpp	Wed Feb 18 01:38:23 2009 -0600
     1.3 @@ -27,13 +27,20 @@
     1.4  
     1.5  #include "console.h"
     1.6  #include "provider_ep2.h"
     1.7 +#include "metamod_util.h"
     1.8  
     1.9  using namespace SourceHook;
    1.10  
    1.11  SMConVarAccessor g_SMConVarAccessor;
    1.12  
    1.13 +#if SOURCE_ENGINE >= SE_ORANGEBOX
    1.14 +#else
    1.15 +#define RegisterConCommand RegisterConCommandBase
    1.16 +#endif
    1.17 +
    1.18  bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
    1.19  {
    1.20 +	m_RegisteredCommands.push_back(pCommand);
    1.21  	pCommand->SetNext(NULL);
    1.22  	icvar->RegisterConCommand(pCommand);
    1.23  
    1.24 @@ -48,8 +55,128 @@
    1.25  	return true;
    1.26  }
    1.27  
    1.28 +void SMConVarAccessor::RemoveMetamodCommands()
    1.29 +{
    1.30 +	List<ConCommandBase *>::iterator iter;
    1.31 +
    1.32 +	for (iter = m_RegisteredCommands.begin(); iter != m_RegisteredCommands.end(); iter++)
    1.33 +	{
    1.34 +		Unregister(*iter);
    1.35 +	}
    1.36 +}
    1.37 +
    1.38 +#if SOURCE_ENGINE == SE_DARKMESSIAH
    1.39 +/* Signature for ICvar::GetCommands() in vstdlib for Win32 and Linux.
    1.40 + *
    1.41 + * 20226EE0 A1 50 5C 5A 20   mov         eax,dword ptr ds:[205A5C50h] <-- What we want
    1.42 + * 20226EE5 C3               ret              
    1.43 + */
    1.44 +#define CMDLIST_SIG "\xA1\x2A\x2A\x2A\x2A\xC3"
    1.45 +#define CMDLIST_SIGLEN 6
    1.46 +
    1.47 +/* Linux symbol name of ConCommandBase list in vstdlib */
    1.48 +#define CMDLIST_SYMBOL "_ZN14ConCommandBase18s_pConCommandBasesE"
    1.49 +
    1.50 +/* This function retrieves the address of the var that holds the top of the ConCommandBase list.
    1.51 + * Having this allows us to change the beginning of this list with ease.
    1.52 + *
    1.53 + * This craziness eliminates the need for the eternal command/cvar used previously which
    1.54 + * could have caused a crash as a result of registering commands/cvars more than once.
    1.55 + */
    1.56 +bool SMConVarAccessor::InitConCommandBaseList()
    1.57 +{
    1.58 +	char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(icvar, &ICvar::GetCommands);
    1.59 +
    1.60 +	if (*vfunc == '\xE9')
    1.61 +	{
    1.62 +		/* Get address from displacement...
    1.63 +		*
    1.64 +		* Add 5 because it's relative to next instruction:
    1.65 +		* Opcode <1 byte> + 32-bit displacement <4 bytes> 
    1.66 +		*/
    1.67 +		vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5;
    1.68 +	}
    1.69 +
    1.70 +	if (!vfunc)
    1.71 +	{
    1.72 +		return false;
    1.73 +	}
    1.74 +
    1.75 +#ifdef OS_WIN32
    1.76 +	if (UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
    1.77 +	{
    1.78 +		/* Skip past 0xA1 and get addr of ConCommandBase list var */
    1.79 +		m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
    1.80 +		return true;
    1.81 +	}
    1.82 +#elif defined OS_LINUX
    1.83 +	/* Try dlsym first */
    1.84 +	char path[PATH_SIZE];
    1.85 +	if (GetFileOfAddress((void *)icvar, path, sizeof(path)))
    1.86 +	{
    1.87 +		void *handle = dlopen(path, RTLD_NOW);
    1.88 +		if (handle)
    1.89 +		{
    1.90 +			m_TopConCommandBase = reinterpret_cast<ConCommandBase **>(dlsym(handle, CMDLIST_SYMBOL));
    1.91 +			dlclose(handle);
    1.92 +			return true;
    1.93 +		}
    1.94 +	}
    1.95 +
    1.96 +	/* If dlsym failed, then verify signature of function */
    1.97 +	if (!m_TopConCommandBase && UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
    1.98 +	{
    1.99 +		/* Skip past 0xA1 and get addr of ConCommandBase list var */
   1.100 +		m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
   1.101 +		return true;
   1.102 +	}
   1.103 +#endif
   1.104 +
   1.105 +	return false;
   1.106 +}
   1.107 +#endif
   1.108 +
   1.109  void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
   1.110  {
   1.111 +#if SOURCE_ENGINE >= SE_ORANGEBOX
   1.112  	icvar->UnregisterConCommand(pCommand);
   1.113 +#else
   1.114 +	ConCommandBase *pCur = NULL;
   1.115 +	ConCommandBase *pPrev = NULL;
   1.116 +
   1.117 +	if (!pCommand)
   1.118 +	{
   1.119 +		return;
   1.120 +	}
   1.121 +
   1.122 +	pCur = icvar->GetCommands();
   1.123 +
   1.124 +	if (!m_TopConCommandBase || !pCur)
   1.125 +	{
   1.126 +		return;
   1.127 +	}
   1.128 +
   1.129 +	if (pCur == pCommand)
   1.130 +	{
   1.131 +		*m_TopConCommandBase = const_cast<ConCommandBase *>(pCommand->GetNext());
   1.132 +		pCommand->SetNext(NULL);
   1.133 +		return;
   1.134 +	}
   1.135 +	
   1.136 +	pPrev = pCur;
   1.137 +	pCur = const_cast<ConCommandBase *>(pCur->GetNext());
   1.138 +
   1.139 +	while (pCur)
   1.140 +	{
   1.141 +		if (pCur == pCommand)
   1.142 +		{
   1.143 +			pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
   1.144 +			pCommand->SetNext(NULL);
   1.145 +		}
   1.146 +
   1.147 +		pPrev = pCur;
   1.148 +		pCur = const_cast<ConCommandBase *>(pCur->GetNext());
   1.149 +	}
   1.150 +#endif
   1.151  }
   1.152