/
sp_hooks.cpp
214 lines (179 loc) · 6.18 KB
/
sp_hooks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* =============================================================================
* Source Python
* Copyright (C) 2012-2018 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/
//---------------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------------
// Boost.Python
#include "boost/python.hpp"
using namespace boost::python;
// SDK
#include "edict.h"
#include "game/shared/shareddefs.h"
#include "game/shared/usercmd.h"
// Source.Python
#include "sp_hooks.h"
#include "utilities/conversions.h"
#include "utilities/call_python.h"
#include "modules/entities/entities_entity.h"
#include "modules/listeners/listeners_manager.h"
//---------------------------------------------------------------------------------
// GLOBAL VARIABLES
//---------------------------------------------------------------------------------
std::vector<IEntityHook*> g_EntityHooks;
//---------------------------------------------------------------------------------
// ISimpleEntityHook
//---------------------------------------------------------------------------------
ISimpleEntityHook::ISimpleEntityHook(const char* func_name, HookHandlerFn* hook_handler, HookType_t hook_type)
{
this->func_name = func_name;
this->hook_handler = hook_handler;
this->hook_type = hook_type;
}
bool ISimpleEntityHook::Initialize(CBaseEntity* pEntity)
{
if (!Test(pEntity))
{
return false;
}
PythonLog(4, "Initializing core hook (%s)...", this->func_name);
unsigned int index;
if (!IndexFromBaseEntity(pEntity, index))
{
PythonLog(0, "Failed to convert the entity pointer to an index (%s)", this->func_name);
return true;
}
CFunction* func = NULL;
try
{
static object Entity = import("entities.entity").attr("Entity");
object entity = Entity(index);
func = extract<CFunction*>(entity.attr(this->func_name));
}
catch (...)
{
PyErr_Print();
PyErr_Clear();
PythonLog(0, "Failed to import entities.entity.Entity or to retrieve %s.", this->func_name);
return true;
}
CHook* pHook = GetHookManager()->FindHook((void*) func->m_ulAddr);
if (!pHook)
{
pHook = GetHookManager()->HookFunction(
(void*) func->m_ulAddr,
func->m_pCallingConvention);
if (!pHook)
{
PythonLog(0, "Could not create a hook for %s.", this->func_name);
return true;
}
}
pHook->AddCallback(this->hook_type, this->hook_handler);
PythonLog(3, "Core hook (%s) has been initialized.", this->func_name);
return true;
}
//---------------------------------------------------------------------------------
// PlayerHook
//---------------------------------------------------------------------------------
PlayerHook::PlayerHook(const char* func_name, HookHandlerFn* hook_handler, HookType_t hook_type)
:ISimpleEntityHook(func_name, hook_handler, hook_type)
{
}
bool PlayerHook::Test(CBaseEntity* pEntity)
{
CBaseEntityWrapper* pWrapper = (CBaseEntityWrapper*) pEntity;
return pWrapper->IsPlayer();
}
//---------------------------------------------------------------------------------
// FUNCTIONS
//---------------------------------------------------------------------------------
void InitHooks()
{
CBaseEntity* pEntity = (CBaseEntity *) servertools->FirstEntity();
while (pEntity)
{
InitHooks(pEntity);
pEntity = (CBaseEntity *) servertools->NextEntity(pEntity);
}
}
void InitHooks(CBaseEntity* pEntity)
{
if (!pEntity)
return;
std::vector<IEntityHook*>::iterator it = g_EntityHooks.begin();
while (it != g_EntityHooks.end())
{
IEntityHook* pHook = *it;
if (pHook->Initialize(pEntity))
{
it = g_EntityHooks.erase(it);
delete pHook;
}
else
{
++it;
}
}
}
//---------------------------------------------------------------------------------
// HOOKS
//---------------------------------------------------------------------------------
bool PrePlayerRunCommand(HookType_t hook_type, CHook* pHook)
{
GET_LISTENER_MANAGER(OnPlayerRunCommand, run_command_manager);
GET_LISTENER_MANAGER(OnButtonStateChanged, button_state_manager);
if (!run_command_manager->GetCount() && !button_state_manager->GetCount())
return false;
static object Player = import("players.entity").attr("Player");
CBaseEntity* pEntity = pHook->GetArgument<CBaseEntity*>(0);
unsigned int index;
if (!IndexFromBaseEntity(pEntity, index))
return false;
// https://github.com/Source-Python-Dev-Team/Source.Python/issues/149
#if defined(ENGINE_BRANCH_TF2)
CUserCmd cmd = *pHook->GetArgument<CUserCmd*>(1);
CUserCmd* pCmd = &cmd;
#else
CUserCmd* pCmd = pHook->GetArgument<CUserCmd*>(1);
#endif
object player = Player(index);
CALL_LISTENERS(OnPlayerRunCommand, player, ptr(pCmd));
if (button_state_manager->GetCount())
{
CBaseEntityWrapper* pWrapper = (CBaseEntityWrapper*) pEntity;
static int offset = pWrapper->FindDatamapPropertyOffset("m_nButtons");
int buttons = pWrapper->GetDatamapPropertyByOffset<int>(offset);
if (buttons != pCmd->buttons)
{
CALL_LISTENERS(OnButtonStateChanged, player, buttons, pCmd->buttons);
}
}
#if defined(ENGINE_BRANCH_TF2)
CUserCmd* pRealCmd = pHook->GetArgument<CUserCmd*>(1);
memcpy(pRealCmd, pCmd, sizeof(CUserCmd));
#endif
return false;
}