core/provider/console.cpp
author David Anderson <dvander@alliedmods.net>
Sat Oct 15 22:16:50 2011 -0700 (2011-10-15)
changeset 767 fd3ff8d792ce
parent 725 9eec529c3caa
child 780 35f2a207badf
permissions -rw-r--r--
Fix page protection bug causing a rare startup crash (bug 5133, r=fyren).
dvander@371
     1
/**
dvander@371
     2
 * vim: set ts=4 :
dvander@371
     3
 * ======================================================
dvander@371
     4
 * Metamod:Source
ds@523
     5
 * Copyright (C) 2004-2008 AlliedModders LLC and authors.
dvander@371
     6
 * All rights reserved.
dvander@371
     7
 * ======================================================
dvander@371
     8
 *
dvander@371
     9
 * This software is provided 'as-is', without any express or implied warranty.
dvander@371
    10
 * In no event will the authors be held liable for any damages arising from 
dvander@371
    11
 * the use of this software.
dvander@371
    12
 * 
dvander@371
    13
 * Permission is granted to anyone to use this software for any purpose, 
dvander@371
    14
 * including commercial applications, and to alter it and redistribute it 
dvander@371
    15
 * freely, subject to the following restrictions:
dvander@371
    16
 *
dvander@371
    17
 * 1. The origin of this software must not be misrepresented; you must not 
dvander@371
    18
 * claim that you wrote the original software. If you use this software in a 
dvander@371
    19
 * product, an acknowledgment in the product documentation would be 
dvander@371
    20
 * appreciated but is not required.
dvander@371
    21
 * 2. Altered source versions must be plainly marked as such, and must not be
dvander@371
    22
 * misrepresented as being the original software.
dvander@371
    23
 * 3. This notice may not be removed or altered from any source distribution.
dvander@371
    24
 *
dvander@371
    25
 * Version: $Id$
dvander@371
    26
 */
dvander@371
    27
dvander@371
    28
#include "console.h"
dvander@371
    29
#include "provider_ep2.h"
ds@637
    30
#include "metamod_util.h"
dvander@371
    31
dvander@371
    32
using namespace SourceHook;
dvander@371
    33
dvander@371
    34
SMConVarAccessor g_SMConVarAccessor;
dvander@371
    35
ds@637
    36
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@637
    37
#else
ds@637
    38
#define RegisterConCommand RegisterConCommandBase
ds@637
    39
#endif
ds@637
    40
dvander@371
    41
bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
dvander@371
    42
{
ds@637
    43
	m_RegisteredCommands.push_back(pCommand);
ds@725
    44
#if SOURCE_ENGINE != SE_ALIENSWARM
dvander@371
    45
	pCommand->SetNext(NULL);
ds@725
    46
#endif
dvander@371
    47
	icvar->RegisterConCommand(pCommand);
dvander@371
    48
dvander@371
    49
	return true;
dvander@371
    50
}
dvander@371
    51
dvander@371
    52
bool SMConVarAccessor::Register(ConCommandBase *pCommand)
dvander@371
    53
{
ds@725
    54
#if SOURCE_ENGINE != SE_ALIENSWARM
dvander@371
    55
	pCommand->SetNext(NULL);
ds@725
    56
#endif
dvander@371
    57
	icvar->RegisterConCommand(pCommand);
dvander@371
    58
dvander@371
    59
	return true;
dvander@371
    60
}
dvander@371
    61
ds@637
    62
void SMConVarAccessor::RemoveMetamodCommands()
ds@637
    63
{
ds@637
    64
	List<ConCommandBase *>::iterator iter;
ds@637
    65
ds@637
    66
	for (iter = m_RegisteredCommands.begin(); iter != m_RegisteredCommands.end(); iter++)
ds@637
    67
	{
ds@637
    68
		Unregister(*iter);
ds@637
    69
	}
ds@637
    70
}
ds@637
    71
ds@637
    72
#if SOURCE_ENGINE == SE_DARKMESSIAH
ds@637
    73
/* Signature for ICvar::GetCommands() in vstdlib for Win32 and Linux.
ds@637
    74
 *
ds@637
    75
 * 20226EE0 A1 50 5C 5A 20   mov         eax,dword ptr ds:[205A5C50h] <-- What we want
ds@637
    76
 * 20226EE5 C3               ret              
ds@637
    77
 */
ds@637
    78
#define CMDLIST_SIG "\xA1\x2A\x2A\x2A\x2A\xC3"
ds@637
    79
#define CMDLIST_SIGLEN 6
ds@637
    80
ds@637
    81
/* Linux symbol name of ConCommandBase list in vstdlib */
ds@637
    82
#define CMDLIST_SYMBOL "_ZN14ConCommandBase18s_pConCommandBasesE"
ds@637
    83
ds@637
    84
/* This function retrieves the address of the var that holds the top of the ConCommandBase list.
ds@637
    85
 * Having this allows us to change the beginning of this list with ease.
ds@637
    86
 *
ds@637
    87
 * This craziness eliminates the need for the eternal command/cvar used previously which
ds@637
    88
 * could have caused a crash as a result of registering commands/cvars more than once.
ds@637
    89
 */
ds@637
    90
bool SMConVarAccessor::InitConCommandBaseList()
ds@637
    91
{
ds@637
    92
	char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(icvar, &ICvar::GetCommands);
ds@637
    93
ds@637
    94
	if (*vfunc == '\xE9')
ds@637
    95
	{
ds@637
    96
		/* Get address from displacement...
ds@637
    97
		*
ds@637
    98
		* Add 5 because it's relative to next instruction:
ds@637
    99
		* Opcode <1 byte> + 32-bit displacement <4 bytes> 
ds@637
   100
		*/
ds@637
   101
		vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5;
ds@637
   102
	}
ds@637
   103
ds@637
   104
	if (!vfunc)
ds@637
   105
	{
ds@637
   106
		return false;
ds@637
   107
	}
ds@637
   108
ds@637
   109
#ifdef OS_WIN32
ds@637
   110
	if (UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
ds@637
   111
	{
ds@637
   112
		/* Skip past 0xA1 and get addr of ConCommandBase list var */
ds@637
   113
		m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
ds@637
   114
		return true;
ds@637
   115
	}
ds@637
   116
#elif defined OS_LINUX
ds@637
   117
	/* Try dlsym first */
ds@637
   118
	char path[PATH_SIZE];
ds@637
   119
	if (GetFileOfAddress((void *)icvar, path, sizeof(path)))
ds@637
   120
	{
ds@637
   121
		void *handle = dlopen(path, RTLD_NOW);
ds@637
   122
		if (handle)
ds@637
   123
		{
ds@637
   124
			m_TopConCommandBase = reinterpret_cast<ConCommandBase **>(dlsym(handle, CMDLIST_SYMBOL));
ds@637
   125
			dlclose(handle);
ds@637
   126
			return true;
ds@637
   127
		}
ds@637
   128
	}
ds@637
   129
ds@637
   130
	/* If dlsym failed, then verify signature of function */
ds@637
   131
	if (!m_TopConCommandBase && UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
ds@637
   132
	{
ds@637
   133
		/* Skip past 0xA1 and get addr of ConCommandBase list var */
ds@637
   134
		m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
ds@637
   135
		return true;
ds@637
   136
	}
ds@637
   137
#endif
ds@637
   138
ds@637
   139
	return false;
ds@637
   140
}
ds@637
   141
#endif
ds@637
   142
dvander@371
   143
void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
dvander@371
   144
{
ds@637
   145
#if SOURCE_ENGINE >= SE_ORANGEBOX
dvander@371
   146
	icvar->UnregisterConCommand(pCommand);
ds@637
   147
#else
ds@637
   148
	ConCommandBase *pCur = NULL;
ds@637
   149
	ConCommandBase *pPrev = NULL;
ds@637
   150
ds@637
   151
	if (!pCommand)
ds@637
   152
	{
ds@637
   153
		return;
ds@637
   154
	}
ds@637
   155
ds@637
   156
	pCur = icvar->GetCommands();
ds@637
   157
ds@637
   158
	if (!m_TopConCommandBase || !pCur)
ds@637
   159
	{
ds@637
   160
		return;
ds@637
   161
	}
ds@637
   162
ds@637
   163
	if (pCur == pCommand)
ds@637
   164
	{
ds@637
   165
		*m_TopConCommandBase = const_cast<ConCommandBase *>(pCommand->GetNext());
ds@637
   166
		pCommand->SetNext(NULL);
ds@637
   167
		return;
ds@637
   168
	}
ds@637
   169
	
ds@637
   170
	pPrev = pCur;
ds@637
   171
	pCur = const_cast<ConCommandBase *>(pCur->GetNext());
ds@637
   172
ds@637
   173
	while (pCur)
ds@637
   174
	{
ds@637
   175
		if (pCur == pCommand)
ds@637
   176
		{
ds@637
   177
			pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
ds@637
   178
			pCommand->SetNext(NULL);
ds@637
   179
		}
ds@637
   180
ds@637
   181
		pPrev = pCur;
ds@637
   182
		pCur = const_cast<ConCommandBase *>(pCur->GetNext());
ds@637
   183
	}
ds@637
   184
#endif
dvander@371
   185
}
dvander@371
   186