This repository was archived by the owner on Jul 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathextension.cpp
More file actions
420 lines (346 loc) · 10.4 KB
/
extension.cpp
File metadata and controls
420 lines (346 loc) · 10.4 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#pragma comment(lib, "legacy_stdio_definitions.lib")
#include "extension.h"
#include <map>
#include <cstdio>
#include "triggers_shared.h"
TriggerPushFix g_Fixer;
SMEXT_LINK(&g_Fixer);
SH_DECL_MANUALHOOK1_void(Touch, 0, 0, 0, CBaseEntity *);
SH_DECL_MANUALHOOK1_void(EndTouch, 0, 0, 0, CBaseEntity *);
SH_DECL_MANUALHOOK2_void(PlayerRunCmdHook, 0, 0, 0, CUserCmd *, IMoveHelper *);
IServerTools *servertools = NULL;
IForward* g_pOnTouch = NULL;
IForward* g_pOnTouched = NULL;
IBinTools* bintools = NULL;
ISDKHooks* sdkhooks = NULL;
ICallWrapper* passesfiltercall = NULL;
struct ExecPlayer
{
ExecPlayer(int id) : hookid(id) {
mutex = threader->MakeMutex();
}
~ExecPlayer() {
if (hookid) {
SH_REMOVE_HOOK_ID(hookid);
hookid = 0;
}
mutex->DestroyThis();
}
bool CanPush(CBaseEntity* ent) {
if (count.find(ent) == count.end()) {
count[ent] = 0;
}
return count[ent] < 1;
}
void IncCount(CBaseEntity* ent) {
if (count.find(ent) == count.end()) {
count[ent] = 0;
}
count[ent]++;
}
void ResetCounts() {
count.clear();
}
public:
IMutex *mutex;
std::map<CBaseEntity*, unsigned int> count;
int hookid;
};
struct TriggerHooker
{
TriggerHooker(int touchid) : touchhook(touchid) {}
~TriggerHooker() {
if (touchhook) {
SH_REMOVE_HOOK_ID(touchhook);
touchhook = 0;
}
}
public:
int touchhook;
};
ExecPlayer *g_ExecList[SM_MAXPLAYERS + 1];
std::map<CBaseEntity*, TriggerHooker*> g_HookList;
static CBaseEntity* FindEntityByClassname(CBaseEntity *pEntity, const char *searchname);
bool TriggerPushFix::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlength, bool late)
{
// Macro fix
size_t maxlen = maxlength;
GET_V_IFACE_ANY(GetServerFactory, servertools, IServerTools, VSERVERTOOLS_INTERFACE_VERSION);
return true;
}
bool TriggerPushFix::SDK_OnLoad(char *error, size_t maxlength, bool late)
{
// Get offsets from gamedata.
IGameConfig* pGameConf;
if (!gameconfs->LoadGameConfigFile("triggerpushfix.games", &pGameConf, error, maxlength)) {
snprintf(error, maxlength, "Failed to load gamedata file");
gameconfs->CloseGameConfigFile(pGameConf);
return false;
}
int index;
if (pGameConf->GetOffset("Touch", &index)) {
SH_MANUALHOOK_RECONFIGURE(Touch, index, 0, 0);
}
else {
snprintf(error, maxlength, "Failed to obtain 'Touch' offset from gamedata");
gameconfs->CloseGameConfigFile(pGameConf);
return false;
}
if (pGameConf->GetOffset("EndTouch", &index)) {
SH_MANUALHOOK_RECONFIGURE(EndTouch, index, 0, 0);
}
else {
snprintf(error, maxlength, "Failed to obtain 'EndTouch' offset from gamedata");
gameconfs->CloseGameConfigFile(pGameConf);
return false;
}
if (pGameConf->GetOffset("PlayerRunCmd", &index)) {
SH_MANUALHOOK_RECONFIGURE(PlayerRunCmdHook, index, 0, 0);
}
else {
snprintf(error, maxlength, "Failed to obtain 'PlayerRunCmd' offset from gamedata");
gameconfs->CloseGameConfigFile(pGameConf);
return false;
}
sharesys->AddDependency(myself, "bintools.ext", true, true);
sharesys->AddDependency(myself, "sdkhooks.ext", true, true);
if (!sharesys->RequestInterface(SMINTERFACE_BINTOOLS_NAME, SMINTERFACE_BINTOOLS_VERSION, myself, reinterpret_cast<SMInterface**>(&bintools))) {
snprintf(error, maxlength, "Cannot get IBintools Interface");
return false;
}
if (!sharesys->RequestInterface(SMINTERFACE_SDKHOOKS_NAME, SMINTERFACE_SDKHOOKS_VERSION, myself, reinterpret_cast<SMInterface**>(&sdkhooks))) {
snprintf(error, maxlength, "Cannot get SDKHooks Interface");
return false;
}
PassInfo retBuf;
retBuf.type = PassType_Basic;
retBuf.flags = PASSFLAG_BYVAL;
retBuf.size = sizeof(bool);
PassInfo paramInfo[1];
paramInfo[0].type = PassType_Basic;
paramInfo[0].flags = PASSFLAG_BYVAL;
paramInfo[0].size = sizeof(CBaseEntity*);
if (pGameConf->GetOffset("PassesTriggerFilters", &index)) {
passesfiltercall = bintools->CreateVCall(index, 0, 0, &retBuf, paramInfo, 1);
}
else {
snprintf(error, maxlength, "Failed to obtain 'PassesTriggerFilters' offset from gamedata");
gameconfs->CloseGameConfigFile(pGameConf);
return false;
}
gameconfs->CloseGameConfigFile(pGameConf);
// Late Load.
g_ExecList[0] = NULL;
for (int client = 1; client < SM_MAXPLAYERS + 1; client++) {
g_ExecList[client] = NULL;
}
for (int client = 1; client < SM_MAXPLAYERS + 1; client++) {
edict_t *edict = gamehelpers->EdictOfIndex(client);
if (!edict || edict->IsFree()) {
continue;
}
IGamePlayer *player = playerhelpers->GetGamePlayer(edict);
if (!player || !player->IsInGame()) {
continue;
}
IServerUnknown *unk = edict->GetUnknown();
if (!unk) {
continue;
}
CBaseEntity *pEntity = unk->GetBaseEntity();
g_ExecList[client] = new ExecPlayer(SH_ADD_MANUALHOOK(PlayerRunCmdHook, pEntity, SH_MEMBER(&g_Fixer, &TriggerPushFix::Hook_PlayerRunCmd), false));
}
CBaseEntity *pEntity = NULL;
while ((pEntity = FindEntityByClassname(pEntity, "trigger_push")) != NULL) {
g_HookList[pEntity] = new TriggerHooker(SH_ADD_MANUALHOOK(Touch, pEntity, SH_MEMBER(&g_Fixer, &TriggerPushFix::Hook_Touch), false));
}
sdkhooks->AddEntityListener(this);
playerhelpers->AddClientListener(this);
return true;
}
void TriggerPushFix::SDK_OnAllLoaded()
{
g_pOnTouch = forwards->CreateForward("TriggerPushFix_OnTouch", ET_Event, 2, NULL, Param_Cell, Param_Cell);
g_pOnTouched = forwards->CreateForward("TriggerPushFix_OnTouched", ET_Ignore, 2, NULL, Param_Cell, Param_Cell);
}
void TriggerPushFix::SDK_OnUnload()
{
sdkhooks->RemoveEntityListener(this);
playerhelpers->RemoveClientListener(this);
for (int client = 1; client < SM_MAXPLAYERS + 1; client++) {
if (g_ExecList[client]) {
delete g_ExecList[client];
g_ExecList[client] = NULL;
}
}
for (auto it = g_HookList.begin(); it != g_HookList.end(); it++) {
delete it->second;
}
g_HookList.clear();
passesfiltercall->Destroy();
forwards->ReleaseForward(g_pOnTouch);
forwards->ReleaseForward(g_pOnTouched);
}
void TriggerPushFix::OnEntityCreated(CBaseEntity *pEntity, const char *classname)
{
if (!strcmp(classname, "trigger_push")) {
g_HookList[pEntity] = new TriggerHooker(SH_ADD_MANUALHOOK(Touch, pEntity, SH_MEMBER(&g_Fixer, &TriggerPushFix::Hook_Touch), false));
}
}
void TriggerPushFix::OnEntityDestroyed(CBaseEntity *pEntity)
{
if (g_HookList.find(pEntity) != g_HookList.end()) {
delete g_HookList[pEntity];
g_HookList.erase(pEntity);
}
}
void TriggerPushFix::OnClientPutInServer(int client)
{
edict_t *edict = gamehelpers->EdictOfIndex(client);
if (!edict || edict->IsFree()) {
return;
}
IGamePlayer *player = playerhelpers->GetGamePlayer(edict);
if (!player) {
return;
}
IServerUnknown *unk = edict->GetUnknown();
if (!unk) {
return;
}
CBaseEntity *pEntity = unk->GetBaseEntity();
g_ExecList[client] = new ExecPlayer(SH_ADD_MANUALHOOK(PlayerRunCmdHook, pEntity, SH_MEMBER(&g_Fixer, &TriggerPushFix::Hook_PlayerRunCmd), false));
}
void TriggerPushFix::OnClientDisconnecting(int client)
{
if (!g_ExecList[client]) {
return;
}
delete g_ExecList[client];
g_ExecList[client] = NULL;
}
void TriggerPushFix::Hook_Touch(CBaseEntity *pOther)
{
int other = gamehelpers->EntityToBCompatRef(pOther);
if (other < 1 || other > playerhelpers->GetMaxClients()) {
RETURN_META(MRES_IGNORED);
}
// Call Pre-Forward.
if (!g_pOnTouch) {
RETURN_META(MRES_IGNORED);
}
CBaseEntity* pEntity = META_IFACEPTR(CBaseEntity);
int entity = gamehelpers->EntityToBCompatRef(pEntity);
cell_t result = Pl_Continue;
g_pOnTouch->PushCell(entity);
g_pOnTouch->PushCell(other);
g_pOnTouch->Execute(&result);
if (result == Pl_Handled) {
RETURN_META(MRES_IGNORED);
}
CBaseEntity **params = reinterpret_cast<CBaseEntity**>(new char[sizeof(CBaseEntity*)*2]);
params[0] = pEntity;
params[1] = pOther;
bool *passes_ptr = new bool;
*passes_ptr = false;
passesfiltercall->Execute(params, passes_ptr);
bool passes = *passes_ptr;
delete params;
delete passes_ptr;
if (!passes) {
RETURN_META(MRES_IGNORED);
}
sm_datatable_info_t info;
if (!gamehelpers->FindDataMapInfo(gamehelpers->GetDataMap(pOther), "m_spawnflags", &info)) {
RETURN_META(MRES_IGNORED);
}
int spawnflags = *(int*)((char*)pEntity + info.actual_offset);
if (spawnflags & SF_TRIG_PUSH_ONCE) {
RETURN_META(MRES_IGNORED);
}
if (!gamehelpers->FindDataMapInfo(gamehelpers->GetDataMap(pOther), "m_MoveType", &info)) {
RETURN_META(MRES_IGNORED);
}
MoveType_t movetype = (MoveType_t)*(char*)((char *)pOther + info.actual_offset);
switch (movetype) {
case MOVETYPE_NONE:
case MOVETYPE_PUSH:
case MOVETYPE_NOCLIP:
case MOVETYPE_VPHYSICS:
{
RETURN_META(MRES_IGNORED);
}
}
if (g_ExecList[other] == NULL) {
RETURN_META(MRES_IGNORED);
}
// Call Post-Forward.
g_pOnTouched->PushCell(entity);
g_pOnTouched->PushCell(other);
g_pOnTouched->Execute();
g_ExecList[other]->mutex->Lock();
if (g_ExecList[other]->CanPush(pEntity)) {
g_ExecList[other]->IncCount(pEntity);
g_ExecList[other]->mutex->Unlock();
RETURN_META(MRES_IGNORED);
}
g_ExecList[other]->mutex->Unlock();
RETURN_META(MRES_SUPERCEDE);
}
void TriggerPushFix::Hook_PlayerRunCmd(CUserCmd *ucmd, IMoveHelper *moveHelper)
{
CBaseEntity* pEntity = META_IFACEPTR(CBaseEntity);
int client = gamehelpers->EntityToBCompatRef(pEntity);
if (client < 1 || client > playerhelpers->GetMaxClients()) {
RETURN_META(MRES_IGNORED);
}
if (g_ExecList[client] == NULL) {
return;
}
g_ExecList[client]->mutex->Lock();
g_ExecList[client]->ResetCounts();
g_ExecList[client]->mutex->Unlock();
RETURN_META(MRES_IGNORED);
}
static CBaseEntity* FindEntityByClassname(CBaseEntity *pEntity, const char *searchname)
{
if (!pEntity) {
pEntity = (CBaseEntity *)servertools->FirstEntity();
}
else {
pEntity = (CBaseEntity *)servertools->NextEntity(pEntity);
}
// it's tough to find a good ent these days
if (!pEntity) {
return NULL;
}
const char *classname;
int lastletterpos;
static int offset = -1;
if (offset == -1) {
sm_datatable_info_t info;
if (!gamehelpers->FindDataMapInfo(gamehelpers->GetDataMap(pEntity), "m_iClassname", &info)) {
return NULL;
}
offset = info.actual_offset;
}
string_t s;
while (pEntity) {
if ((s = *(string_t *)((uint8_t *)pEntity + offset)) == NULL_STRING) {
pEntity = (CBaseEntity *)servertools->NextEntity(pEntity);
continue;
}
classname = STRING(s);
lastletterpos = strlen(searchname) - 1;
if (searchname[lastletterpos] == '*') {
if (V_strncasecmp(searchname, classname, lastletterpos) == 0) {
return pEntity;
}
}
else if (V_strcasecmp(searchname, classname) == 0) {
return pEntity;
}
pEntity = (CBaseEntity *)servertools->NextEntity(pEntity);
}
return NULL;
}