|
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); |
|
dvander@371
|
44 |
pCommand->SetNext(NULL); |
|
dvander@371
|
45 |
icvar->RegisterConCommand(pCommand); |
|
dvander@371
|
46 |
|
|
dvander@371
|
47 |
return true; |
|
dvander@371
|
48 |
} |
|
dvander@371
|
49 |
|
|
dvander@371
|
50 |
bool SMConVarAccessor::Register(ConCommandBase *pCommand) |
|
dvander@371
|
51 |
{ |
|
dvander@371
|
52 |
pCommand->SetNext(NULL); |
|
dvander@371
|
53 |
icvar->RegisterConCommand(pCommand); |
|
dvander@371
|
54 |
|
|
dvander@371
|
55 |
return true; |
|
dvander@371
|
56 |
} |
|
dvander@371
|
57 |
|
|
ds@637
|
58 |
void SMConVarAccessor::RemoveMetamodCommands() |
|
ds@637
|
59 |
{ |
|
ds@637
|
60 |
List<ConCommandBase *>::iterator iter; |
|
ds@637
|
61 |
|
|
ds@637
|
62 |
for (iter = m_RegisteredCommands.begin(); iter != m_RegisteredCommands.end(); iter++) |
|
ds@637
|
63 |
{ |
|
ds@637
|
64 |
Unregister(*iter); |
|
ds@637
|
65 |
} |
|
ds@637
|
66 |
} |
|
ds@637
|
67 |
|
|
ds@637
|
68 |
#if SOURCE_ENGINE == SE_DARKMESSIAH |
|
ds@637
|
69 |
/* Signature for ICvar::GetCommands() in vstdlib for Win32 and Linux. |
|
ds@637
|
70 |
* |
|
ds@637
|
71 |
* 20226EE0 A1 50 5C 5A 20 mov eax,dword ptr ds:[205A5C50h] <-- What we want |
|
ds@637
|
72 |
* 20226EE5 C3 ret |
|
ds@637
|
73 |
*/ |
|
ds@637
|
74 |
#define CMDLIST_SIG "\xA1\x2A\x2A\x2A\x2A\xC3" |
|
ds@637
|
75 |
#define CMDLIST_SIGLEN 6 |
|
ds@637
|
76 |
|
|
ds@637
|
77 |
/* Linux symbol name of ConCommandBase list in vstdlib */ |
|
ds@637
|
78 |
#define CMDLIST_SYMBOL "_ZN14ConCommandBase18s_pConCommandBasesE" |
|
ds@637
|
79 |
|
|
ds@637
|
80 |
/* This function retrieves the address of the var that holds the top of the ConCommandBase list. |
|
ds@637
|
81 |
* Having this allows us to change the beginning of this list with ease. |
|
ds@637
|
82 |
* |
|
ds@637
|
83 |
* This craziness eliminates the need for the eternal command/cvar used previously which |
|
ds@637
|
84 |
* could have caused a crash as a result of registering commands/cvars more than once. |
|
ds@637
|
85 |
*/ |
|
ds@637
|
86 |
bool SMConVarAccessor::InitConCommandBaseList() |
|
ds@637
|
87 |
{ |
|
ds@637
|
88 |
char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(icvar, &ICvar::GetCommands); |
|
ds@637
|
89 |
|
|
ds@637
|
90 |
if (*vfunc == '\xE9') |
|
ds@637
|
91 |
{ |
|
ds@637
|
92 |
/* Get address from displacement... |
|
ds@637
|
93 |
* |
|
ds@637
|
94 |
* Add 5 because it's relative to next instruction: |
|
ds@637
|
95 |
* Opcode <1 byte> + 32-bit displacement <4 bytes> |
|
ds@637
|
96 |
*/ |
|
ds@637
|
97 |
vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5; |
|
ds@637
|
98 |
} |
|
ds@637
|
99 |
|
|
ds@637
|
100 |
if (!vfunc) |
|
ds@637
|
101 |
{ |
|
ds@637
|
102 |
return false; |
|
ds@637
|
103 |
} |
|
ds@637
|
104 |
|
|
ds@637
|
105 |
#ifdef OS_WIN32 |
|
ds@637
|
106 |
if (UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN)) |
|
ds@637
|
107 |
{ |
|
ds@637
|
108 |
/* Skip past 0xA1 and get addr of ConCommandBase list var */ |
|
ds@637
|
109 |
m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1); |
|
ds@637
|
110 |
return true; |
|
ds@637
|
111 |
} |
|
ds@637
|
112 |
#elif defined OS_LINUX |
|
ds@637
|
113 |
/* Try dlsym first */ |
|
ds@637
|
114 |
char path[PATH_SIZE]; |
|
ds@637
|
115 |
if (GetFileOfAddress((void *)icvar, path, sizeof(path))) |
|
ds@637
|
116 |
{ |
|
ds@637
|
117 |
void *handle = dlopen(path, RTLD_NOW); |
|
ds@637
|
118 |
if (handle) |
|
ds@637
|
119 |
{ |
|
ds@637
|
120 |
m_TopConCommandBase = reinterpret_cast<ConCommandBase **>(dlsym(handle, CMDLIST_SYMBOL)); |
|
ds@637
|
121 |
dlclose(handle); |
|
ds@637
|
122 |
return true; |
|
ds@637
|
123 |
} |
|
ds@637
|
124 |
} |
|
ds@637
|
125 |
|
|
ds@637
|
126 |
/* If dlsym failed, then verify signature of function */ |
|
ds@637
|
127 |
if (!m_TopConCommandBase && UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN)) |
|
ds@637
|
128 |
{ |
|
ds@637
|
129 |
/* Skip past 0xA1 and get addr of ConCommandBase list var */ |
|
ds@637
|
130 |
m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1); |
|
ds@637
|
131 |
return true; |
|
ds@637
|
132 |
} |
|
ds@637
|
133 |
#endif |
|
ds@637
|
134 |
|
|
ds@637
|
135 |
return false; |
|
ds@637
|
136 |
} |
|
ds@637
|
137 |
#endif |
|
ds@637
|
138 |
|
|
dvander@371
|
139 |
void SMConVarAccessor::Unregister(ConCommandBase *pCommand) |
|
dvander@371
|
140 |
{ |
|
ds@637
|
141 |
#if SOURCE_ENGINE >= SE_ORANGEBOX |
|
dvander@371
|
142 |
icvar->UnregisterConCommand(pCommand); |
|
ds@637
|
143 |
#else |
|
ds@637
|
144 |
ConCommandBase *pCur = NULL; |
|
ds@637
|
145 |
ConCommandBase *pPrev = NULL; |
|
ds@637
|
146 |
|
|
ds@637
|
147 |
if (!pCommand) |
|
ds@637
|
148 |
{ |
|
ds@637
|
149 |
return; |
|
ds@637
|
150 |
} |
|
ds@637
|
151 |
|
|
ds@637
|
152 |
pCur = icvar->GetCommands(); |
|
ds@637
|
153 |
|
|
ds@637
|
154 |
if (!m_TopConCommandBase || !pCur) |
|
ds@637
|
155 |
{ |
|
ds@637
|
156 |
return; |
|
ds@637
|
157 |
} |
|
ds@637
|
158 |
|
|
ds@637
|
159 |
if (pCur == pCommand) |
|
ds@637
|
160 |
{ |
|
ds@637
|
161 |
*m_TopConCommandBase = const_cast<ConCommandBase *>(pCommand->GetNext()); |
|
ds@637
|
162 |
pCommand->SetNext(NULL); |
|
ds@637
|
163 |
return; |
|
ds@637
|
164 |
} |
|
ds@637
|
165 |
|
|
ds@637
|
166 |
pPrev = pCur; |
|
ds@637
|
167 |
pCur = const_cast<ConCommandBase *>(pCur->GetNext()); |
|
ds@637
|
168 |
|
|
ds@637
|
169 |
while (pCur) |
|
ds@637
|
170 |
{ |
|
ds@637
|
171 |
if (pCur == pCommand) |
|
ds@637
|
172 |
{ |
|
ds@637
|
173 |
pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext())); |
|
ds@637
|
174 |
pCommand->SetNext(NULL); |
|
ds@637
|
175 |
} |
|
ds@637
|
176 |
|
|
ds@637
|
177 |
pPrev = pCur; |
|
ds@637
|
178 |
pCur = const_cast<ConCommandBase *>(pCur->GetNext()); |
|
ds@637
|
179 |
} |
|
ds@637
|
180 |
#endif |
|
dvander@371
|
181 |
} |
|
dvander@371
|
182 |
|