/
extension.cpp
1816 lines (1521 loc) · 55.9 KB
/
extension.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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* vim: set ts=4 :
* =============================================================================
* Source SDK Hooks Extension
* Copyright (C) 2010-2012 Nicholas Hastings
* Copyright (C) 2009-2010 Erik Minekus
* =============================================================================
*
* 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, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," 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, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include <sourcemod_version.h>
#include "extension.h"
#include "compat_wrappers.h"
#include "macros.h"
#include "natives.h"
#include <sm_platform.h>
#include <const.h>
#include <IBinTools.h>
//#define SDKHOOKSDEBUG
/**
* Globals
*/
// Order MUST match SDKHookType enum
HookTypeData g_HookTypes[SDKHook_MAXHOOKS] =
{
// Hook name DT required Supported (always false til later)
{"EndTouch", "", false},
{"FireBulletsPost", "", false},
{"OnTakeDamage", "", false},
{"OnTakeDamagePost", "", false},
{"PreThink", "DT_BasePlayer", false},
{"PostThink", "DT_BasePlayer", false},
{"SetTransmit", "", false},
{"Spawn", "", false},
{"StartTouch", "", false},
{"Think", "", false},
{"Touch", "", false},
{"TraceAttack", "", false},
{"TraceAttackPost", "", false},
{"WeaponCanSwitchTo", "DT_BaseCombatCharacter", false},
{"WeaponCanUse", "DT_BaseCombatCharacter", false},
{"WeaponDrop", "DT_BaseCombatCharacter", false},
{"WeaponEquip", "DT_BaseCombatCharacter", false},
{"WeaponSwitch", "DT_BaseCombatCharacter", false},
{"ShouldCollide", "", false},
{"PreThinkPost", "DT_BasePlayer", false},
{"PostThinkPost", "DT_BasePlayer", false},
{"ThinkPost", "", false},
{"EndTouchPost", "", false},
{"GroundEntChangedPost", "", false},
{"SpawnPost", "", false},
{"StartTouchPost", "", false},
{"TouchPost", "", false},
{"VPhysicsUpdate", "", false},
{"VPhysicsUpdatePost", "", false},
{"WeaponCanSwitchToPost", "DT_BaseCombatCharacter", false},
{"WeaponCanUsePost", "DT_BaseCombatCharacter", false},
{"WeaponDropPost", "DT_BaseCombatCharacter", false},
{"WeaponEquipPost", "DT_BaseCombatCharacter", false},
{"WeaponSwitchPost", "DT_BaseCombatCharacter", false},
{"Use", "", false},
{"UsePost", "", false},
{"Reload", "DT_BaseCombatWeapon", false},
{"ReloadPost", "DT_BaseCombatWeapon", false},
{"GetMaxHealth", "", false},
{"Blocked", "", false},
{"BlockedPost", "", false},
{"OnTakeDamageAlive", "DT_BaseCombatCharacter", false},
{"OnTakeDamageAlivePost", "DT_BaseCombatCharacter", false},
// There is no DT for CBaseMultiplayerPlayer. Going up a level
{"CanBeAutobalanced", "DT_BasePlayer", false},
};
SDKHooks g_Interface;
SMEXT_LINK(&g_Interface);
CGlobalVars *gpGlobals;
std::vector<CVTableList *> g_HookList[SDKHook_MAXHOOKS];
IBinTools *g_pBinTools = NULL;
ICvar *icvar = NULL;
#if SOURCE_ENGINE >= SE_ORANGEBOX
IServerTools *servertools = NULL;
#endif
// global hooks and forwards
IForward *g_pOnEntityCreated = NULL;
IForward *g_pOnEntityDestroyed = NULL;
#ifdef GAMEDESC_CAN_CHANGE
int g_hookOnGetGameDescription = 0;
IForward *g_pOnGetGameNameDescription = NULL;
#endif
int g_hookOnLevelInit = 0;
IForward *g_pOnLevelInit = NULL;
IGameConfig *g_pGameConf = NULL;
CUtlVector<IEntityListener *> *EntListeners()
{
void *gEntList = gamehelpers->GetGlobalEntityList();
if (gEntList)
{
int offset = -1; /* 65572 */
if (g_pGameConf->GetOffset("EntityListeners", &offset))
{
return (CUtlVector<IEntityListener *> *)((intptr_t) gEntList + offset);
}
}
else
{
void *entListeners;
if (g_pGameConf->GetAddress("EntityListenersPtr", &entListeners))
{
return (CUtlVector<IEntityListener *> *)entListeners;
}
}
return NULL;
}
/**
* IServerGameDLL & IVEngineServer Hooks
*/
SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, const char *, const char *, const char *, const char *, bool, bool);
#ifdef GAMEDESC_CAN_CHANGE
SH_DECL_HOOK0(IServerGameDLL, GetGameDescription, SH_NOATTRIB, 0, const char *);
#endif
/**
* CBaseEntity Hooks
*/
SH_DECL_MANUALHOOK1_void(EndTouch, 0, 0, 0, CBaseEntity *);
SH_DECL_MANUALHOOK1_void(FireBullets, 0, 0, 0, FireBulletsInfo_t const&);
#ifdef GETMAXHEALTH_IS_VIRTUAL
SH_DECL_MANUALHOOK0(GetMaxHealth, 0, 0, 0, int);
#endif
SH_DECL_MANUALHOOK1_void(GroundEntChanged, 0, 0, 0, void *);
SH_DECL_MANUALHOOK1(OnTakeDamage, 0, 0, 0, int, CTakeDamageInfoHack &);
SH_DECL_MANUALHOOK1(OnTakeDamage_Alive, 0, 0, 0, int, CTakeDamageInfoHack &);
SH_DECL_MANUALHOOK0_void(PreThink, 0, 0, 0);
SH_DECL_MANUALHOOK0_void(PostThink, 0, 0, 0);
SH_DECL_MANUALHOOK0(Reload, 0, 0, 0, bool);
SH_DECL_MANUALHOOK2_void(SetTransmit, 0, 0, 0, CCheckTransmitInfo *, bool);
SH_DECL_MANUALHOOK2(ShouldCollide, 0, 0, 0, bool, int, int);
SH_DECL_MANUALHOOK0_void(Spawn, 0, 0, 0);
SH_DECL_MANUALHOOK1_void(StartTouch, 0, 0, 0, CBaseEntity *);
SH_DECL_MANUALHOOK0_void(Think, 0, 0, 0);
SH_DECL_MANUALHOOK1_void(Touch, 0, 0, 0, CBaseEntity *);
#if SOURCE_ENGINE == SE_HL2DM || SOURCE_ENGINE == SE_DODS || SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_TF2 \
|| SOURCE_ENGINE == SE_BMS || SOURCE_ENGINE == SE_SDK2013
SH_DECL_MANUALHOOK4_void(TraceAttack, 0, 0, 0, CTakeDamageInfoHack &, const Vector &, CGameTrace *, CDmgAccumulator *);
#else
SH_DECL_MANUALHOOK3_void(TraceAttack, 0, 0, 0, CTakeDamageInfoHack &, const Vector &, CGameTrace *);
#endif
SH_DECL_MANUALHOOK4_void(Use, 0, 0, 0, CBaseEntity *, CBaseEntity *, USE_TYPE, float);
SH_DECL_MANUALHOOK1_void(VPhysicsUpdate, 0, 0, 0, IPhysicsObject *);
SH_DECL_MANUALHOOK1(Weapon_CanSwitchTo, 0, 0, 0, bool, CBaseCombatWeapon *);
SH_DECL_MANUALHOOK1(Weapon_CanUse, 0, 0, 0, bool, CBaseCombatWeapon *);
SH_DECL_MANUALHOOK3_void(Weapon_Drop, 0, 0, 0, CBaseCombatWeapon *, const Vector *, const Vector *);
SH_DECL_MANUALHOOK1_void(Weapon_Equip, 0, 0, 0, CBaseCombatWeapon *);
SH_DECL_MANUALHOOK2(Weapon_Switch, 0, 0, 0, bool, CBaseCombatWeapon *, int);
SH_DECL_MANUALHOOK1_void(Blocked, 0, 0, 0, CBaseEntity *);
SH_DECL_MANUALHOOK0(CanBeAutobalanced, 0, 0, 0, bool);
/**
* Forwards
*/
bool SDKHooks::SDK_OnLoad(char *error, size_t maxlength, bool late)
{
char buffer[256];
g_pSM->BuildPath(Path_SM, buffer, sizeof(buffer)-1, "/extensions/sdkhooks.ext." PLATFORM_LIB_EXT);
if (libsys->PathExists(buffer) && libsys->IsPathFile(buffer))
{
g_pSM->Format(error, maxlength-1, "SDKHooks 2.x cannot load while old version (sdkhooks.ext." PLATFORM_LIB_EXT ") is still in extensions dir");
return false;
}
g_pSM->BuildPath(Path_SM, buffer, sizeof(buffer)-1, "/gamedata/sdkhooks.games.txt");
if (libsys->PathExists(buffer) && libsys->IsPathFile(buffer))
{
g_pSM->Format(error, maxlength-1, "SDKHooks 2.x cannot load while old gamedata file (sdkhooks.games.txt) is still in gamedata dir");
return false;
}
buffer[0] = '\0';
if (!gameconfs->LoadGameConfigFile("sdkhooks.games", &g_pGameConf, buffer, sizeof(buffer)))
{
if (buffer[0])
{
g_pSM->Format(error, maxlength, "Could not read sdkhooks.games gamedata: %s", buffer);
}
return false;
}
memset(m_EntityCache, INVALID_EHANDLE_INDEX, sizeof(m_EntityCache));
CUtlVector<IEntityListener *> *entListeners = EntListeners();
if (!entListeners)
{
g_pSM->Format(error, maxlength, "Failed to setup entity listeners");
return false;
}
entListeners->AddToTail(this);
sharesys->AddDependency(myself, "bintools.ext", true, true);
sharesys->AddNatives(myself, g_Natives);
sharesys->RegisterLibrary(myself, "sdkhooks");
sharesys->AddInterface(myself, &g_Interface);
sharesys->AddCapabilityProvider(myself, this, "SDKHook_DmgCustomInOTD");
sharesys->AddCapabilityProvider(myself, this, "SDKHook_LogicalEntSupport");
playerhelpers->AddClientListener(&g_Interface);
plsys->AddPluginsListener(&g_Interface);
g_pOnEntityCreated = forwards->CreateForward("OnEntityCreated", ET_Ignore, 2, NULL, Param_Cell, Param_String);
g_pOnEntityDestroyed = forwards->CreateForward("OnEntityDestroyed", ET_Ignore, 1, NULL, Param_Cell);
#ifdef GAMEDESC_CAN_CHANGE
g_pOnGetGameNameDescription = forwards->CreateForward("OnGetGameDescription", ET_Hook, 2, NULL, Param_String);
#endif
g_pOnLevelInit = forwards->CreateForward("OnLevelInit", ET_Ignore, 2, NULL, Param_String, Param_String);
SetupHooks();
#if SOURCE_ENGINE >= SE_ORANGEBOX
int index;
CBaseHandle hndl;
for (IHandleEntity *pEnt = (IHandleEntity *)servertools->FirstEntity(); pEnt; pEnt = (IHandleEntity *)servertools->NextEntity((CBaseEntity *)pEnt))
{
hndl = pEnt->GetRefEHandle();
if (!hndl.IsValid())
continue;
index = hndl.GetEntryIndex();
if (IsEntityIndexInRange(index))
{
m_EntityCache[index] = gamehelpers->IndexToReference(index);
}
else
{
g_pSM->LogError(myself, "SDKHooks::HandleEntityCreated - Got entity index out of range (%d)", index);
}
}
#else
for (int i = 0; i < NUM_ENT_ENTRIES; i++)
{
if (gamehelpers->ReferenceToEntity(i) != NULL)
m_EntityCache[i] = gamehelpers->IndexToReference(i);
}
#endif
return true;
}
inline void HookLevelInit()
{
assert(g_hookOnLevelInit == 0);
g_hookOnLevelInit = SH_ADD_HOOK(IServerGameDLL, LevelInit, gamedll, SH_MEMBER(&g_Interface, &SDKHooks::Hook_LevelInit), false);
}
#ifdef GAMEDESC_CAN_CHANGE
inline void HookGetGameDescription()
{
assert(g_hookOnGetGameDescription == 0);
g_hookOnGetGameDescription = SH_ADD_HOOK(IServerGameDLL, GetGameDescription, gamedll, SH_MEMBER(&g_Interface, &SDKHooks::Hook_GetGameDescription), false);
}
#endif
void SDKHooks::SDK_OnAllLoaded()
{
SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
if (!g_pBinTools)
{
g_pSM->LogError(myself, "Could not find interface: " SMINTERFACE_BINTOOLS_NAME);
return;
}
if (g_pOnLevelInit->GetFunctionCount() > 0)
HookLevelInit();
#ifdef GAMEDESC_CAN_CHANGE
if (g_pOnGetGameNameDescription->GetFunctionCount() > 0)
HookGetGameDescription();
#endif
}
bool SDKHooks::QueryRunning(char* error, size_t maxlength)
{
SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
return true;
}
bool SDKHooks::QueryInterfaceDrop(SMInterface* pInterface)
{
if (pInterface == g_pBinTools)
{
return false;
}
return IExtensionInterface::QueryInterfaceDrop(pInterface);
}
#define KILL_HOOK_IF_ACTIVE(hook) \
if (hook != 0) \
{ \
SH_REMOVE_HOOK_ID(hook); \
hook = 0; \
}
void SDKHooks::SDK_OnUnload()
{
// Remove left over hooks
Unhook(reinterpret_cast<SourcePawn::IPluginContext *>(NULL));
KILL_HOOK_IF_ACTIVE(g_hookOnLevelInit);
#ifdef GAMEDESC_CAN_CHANGE
KILL_HOOK_IF_ACTIVE(g_hookOnGetGameDescription);
#endif
forwards->ReleaseForward(g_pOnEntityCreated);
forwards->ReleaseForward(g_pOnEntityDestroyed);
#ifdef GAMEDESC_CAN_CHANGE
forwards->ReleaseForward(g_pOnGetGameNameDescription);
#endif
forwards->ReleaseForward(g_pOnLevelInit);
plsys->RemovePluginsListener(&g_Interface);
playerhelpers->RemoveClientListener(&g_Interface);
sharesys->DropCapabilityProvider(myself, this, "SDKHook_DmgCustomInOTD");
sharesys->DropCapabilityProvider(myself, this, "SDKHook_LogicalEntSupport");
CUtlVector<IEntityListener *> *entListeners = EntListeners();
entListeners->FindAndRemove(this);
gameconfs->CloseGameConfigFile(g_pGameConf);
}
bool SDKHooks::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
{
GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
#if SOURCE_ENGINE >= SE_ORANGEBOX
GET_V_IFACE_ANY(GetServerFactory, servertools, IServerTools, VSERVERTOOLS_INTERFACE_VERSION);
g_pCVar = icvar;
#endif
CONVAR_REGISTER(this);
gpGlobals = ismm->GetCGlobals();
return true;
}
const char *SDKHooks::GetExtensionVerString()
{
return SOURCEMOD_VERSION;
}
const char *SDKHooks::GetExtensionDateString()
{
return SOURCEMOD_BUILD_TIME;
}
void SDKHooks::OnPluginLoaded(IPlugin *plugin)
{
if (g_pOnLevelInit->GetFunctionCount() > 0 && g_hookOnLevelInit == 0)
HookLevelInit();
#ifdef GAMEDESC_CAN_CHANGE
if (g_pOnGetGameNameDescription->GetFunctionCount() > 0 && g_hookOnGetGameDescription == 0)
HookGetGameDescription();
#endif
}
void SDKHooks::OnPluginUnloaded(IPlugin *plugin)
{
Unhook(plugin->GetBaseContext());
if (g_pOnLevelInit->GetFunctionCount() == 0)
{
KILL_HOOK_IF_ACTIVE(g_hookOnLevelInit);
}
#ifdef GAMEDESC_CAN_CHANGE
if (g_pOnGetGameNameDescription->GetFunctionCount() == 0)
KILL_HOOK_IF_ACTIVE(g_hookOnGetGameDescription);
#endif
}
void SDKHooks::OnClientPutInServer(int client)
{
CBaseEntity *pPlayer = gamehelpers->ReferenceToEntity(client);
HandleEntityCreated(pPlayer, client, gamehelpers->EntityToReference(pPlayer));
}
void SDKHooks::OnClientDisconnecting(int client)
{
CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(client);
HandleEntityDeleted(pEntity);
}
void SDKHooks::AddEntityListener(ISMEntityListener *listener)
{
m_EntListeners.push_back(listener);
}
void SDKHooks::RemoveEntityListener(ISMEntityListener *listener)
{
m_EntListeners.remove(listener);
}
bool SDKHooks::RegisterConCommandBase(ConCommandBase *pVar)
{
/* Always call META_REGCVAR instead of going through the engine. */
return META_REGCVAR(pVar);
}
FeatureStatus SDKHooks::GetFeatureStatus(FeatureType type, const char *name)
{
return FeatureStatus_Available;
}
/**
* Functions
*/
static void PopulateCallbackList(const std::vector<HookList> &source, std::vector<IPluginFunction *> &destination, int entity)
{
destination.reserve(8);
for (size_t iter = 0; iter < source.size(); ++iter)
{
if (source[iter].entity != entity)
{
continue;
}
destination.push_back(source[iter].callback);
}
}
cell_t SDKHooks::Call(int entity, SDKHookType type, int other)
{
return Call(gamehelpers->ReferenceToEntity(entity), type, gamehelpers->ReferenceToEntity(other));
}
cell_t SDKHooks::Call(CBaseEntity *pEnt, SDKHookType type, int other)
{
return Call(pEnt, type, gamehelpers->ReferenceToEntity(other));
}
cell_t SDKHooks::Call(CBaseEntity *pEnt, SDKHookType type, CBaseEntity *pOther)
{
cell_t ret = Pl_Continue;
CVTableHook vhook(pEnt);
std::vector<CVTableList *> &vtablehooklist = g_HookList[type];
for (size_t entry = 0; entry < vtablehooklist.size(); ++entry)
{
if (vhook != vtablehooklist[entry]->vtablehook)
{
continue;
}
int entity = gamehelpers->EntityToBCompatRef(pEnt);
int other = gamehelpers->EntityToBCompatRef(pOther);
std::vector<IPluginFunction *> callbackList;
PopulateCallbackList(vtablehooklist[entry]->hooks, callbackList, entity);
for (entry = 0; entry < callbackList.size(); ++entry)
{
IPluginFunction *callback = callbackList[entry];
callback->PushCell(entity);
callback->PushCell(other);
cell_t res;
callback->Execute(&res);
if(res > ret)
{
ret = res;
}
}
break;
}
return ret;
}
void SDKHooks::SetupHooks()
{
int offset;
// gamedata pre post
// (pre is not necessarily a prehook, just named without "Post" appeneded)
CHECKOFFSET(EndTouch, true, true);
CHECKOFFSET(FireBullets, false, true);
CHECKOFFSET(GroundEntChanged, false, true);
CHECKOFFSET(OnTakeDamage, true, true);
CHECKOFFSET(OnTakeDamage_Alive,true, true);
CHECKOFFSET(PreThink, true, true);
CHECKOFFSET(PostThink, true, true);
CHECKOFFSET(Reload, true, true);
CHECKOFFSET(SetTransmit, true, false);
CHECKOFFSET(ShouldCollide, true, false);
CHECKOFFSET(Spawn, true, true);
CHECKOFFSET(StartTouch, true, true);
CHECKOFFSET(Think, true, true);
CHECKOFFSET(Touch, true, true);
CHECKOFFSET(TraceAttack, true, true);
CHECKOFFSET(Use, true, true);
CHECKOFFSET_W(CanSwitchTo, true, true);
CHECKOFFSET_W(CanUse, true, true);
CHECKOFFSET_W(Drop, true, true);
CHECKOFFSET_W(Equip, true, true);
CHECKOFFSET_W(Switch, true, true);
CHECKOFFSET(VPhysicsUpdate, true, true);
CHECKOFFSET(Blocked, true, true);
CHECKOFFSET(CanBeAutobalanced, true, false);
// this one is in a class all its own -_-
offset = 0;
g_pGameConf->GetOffset("GroundEntChanged", &offset);
if (offset > 0)
{
SH_MANUALHOOK_RECONFIGURE(GroundEntChanged, offset, 0, 0);
g_HookTypes[SDKHook_GroundEntChangedPost].supported = true;
}
#ifdef GETMAXHEALTH_IS_VIRTUAL
CHECKOFFSET(GetMaxHealth, true, false);
#endif
}
HookReturn SDKHooks::Hook(int entity, SDKHookType type, IPluginFunction *callback)
{
if(!g_HookTypes[type].supported)
return HookRet_NotSupported;
CBaseEntity *pEnt = gamehelpers->ReferenceToEntity(entity);
if(!pEnt)
return HookRet_InvalidEntity;
if(type < 0 || type >= SDKHook_MAXHOOKS)
return HookRet_InvalidHookType;
if (!!strcmp(g_HookTypes[type].dtReq, ""))
{
IServerUnknown *pUnk = (IServerUnknown *)pEnt;
IServerNetworkable *pNet = pUnk->GetNetworkable();
if (pNet && !UTIL_ContainsDataTable(pNet->GetServerClass()->m_pTable, g_HookTypes[type].dtReq))
return HookRet_BadEntForHookType;
}
size_t entry;
CVTableHook vhook(pEnt);
std::vector<CVTableList *> &vtablehooklist = g_HookList[type];
for (entry = 0; entry < vtablehooklist.size(); ++entry)
{
if (vhook == vtablehooklist[entry]->vtablehook)
{
break;
}
}
if (entry == vtablehooklist.size())
{
int hookid = 0;
switch(type)
{
case SDKHook_EndTouch:
hookid = SH_ADD_MANUALVPHOOK(EndTouch, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_EndTouch), false);
break;
case SDKHook_EndTouchPost:
hookid = SH_ADD_MANUALVPHOOK(EndTouch, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_EndTouchPost), true);
break;
case SDKHook_FireBulletsPost:
hookid = SH_ADD_MANUALVPHOOK(FireBullets, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_FireBulletsPost), true);
break;
#ifdef GETMAXHEALTH_IS_VIRTUAL
case SDKHook_GetMaxHealth:
hookid = SH_ADD_MANUALVPHOOK(GetMaxHealth, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_GetMaxHealth), false);
break;
#endif
case SDKHook_GroundEntChangedPost:
hookid = SH_ADD_MANUALVPHOOK(GroundEntChanged, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_GroundEntChangedPost), true);
break;
case SDKHook_OnTakeDamage:
hookid = SH_ADD_MANUALVPHOOK(OnTakeDamage, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_OnTakeDamage), false);
break;
case SDKHook_OnTakeDamagePost:
hookid = SH_ADD_MANUALVPHOOK(OnTakeDamage, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_OnTakeDamagePost), true);
break;
case SDKHook_OnTakeDamage_Alive:
hookid = SH_ADD_MANUALVPHOOK(OnTakeDamage_Alive, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_OnTakeDamage_Alive), false);
break;
case SDKHook_OnTakeDamage_AlivePost:
hookid = SH_ADD_MANUALVPHOOK(OnTakeDamage_Alive, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_OnTakeDamage_AlivePost), true);
break;
case SDKHook_PreThink:
hookid = SH_ADD_MANUALVPHOOK(PreThink, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_PreThink), false);
break;
case SDKHook_PreThinkPost:
hookid = SH_ADD_MANUALVPHOOK(PreThink, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_PreThinkPost), true);
break;
case SDKHook_PostThink:
hookid = SH_ADD_MANUALVPHOOK(PostThink, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_PostThink), false);
break;
case SDKHook_PostThinkPost:
hookid = SH_ADD_MANUALVPHOOK(PostThink, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_PostThinkPost), true);
break;
case SDKHook_Reload:
hookid = SH_ADD_MANUALVPHOOK(Reload, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_Reload), false);
break;
case SDKHook_ReloadPost:
hookid = SH_ADD_MANUALVPHOOK(Reload, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_ReloadPost), true);
break;
case SDKHook_SetTransmit:
hookid = SH_ADD_MANUALVPHOOK(SetTransmit, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_SetTransmit), false);
break;
case SDKHook_Spawn:
hookid = SH_ADD_MANUALVPHOOK(Spawn, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_Spawn), false);
break;
case SDKHook_SpawnPost:
hookid = SH_ADD_MANUALVPHOOK(Spawn, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_SpawnPost), true);
break;
case SDKHook_StartTouch:
hookid = SH_ADD_MANUALVPHOOK(StartTouch, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_StartTouch), false);
break;
case SDKHook_StartTouchPost:
hookid = SH_ADD_MANUALVPHOOK(StartTouch, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_StartTouchPost), true);
break;
case SDKHook_Think:
hookid = SH_ADD_MANUALVPHOOK(Think, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_Think), false);
break;
case SDKHook_ThinkPost:
hookid = SH_ADD_MANUALVPHOOK(Think, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_ThinkPost), true);
break;
case SDKHook_Touch:
hookid = SH_ADD_MANUALVPHOOK(Touch, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_Touch), false);
break;
case SDKHook_TouchPost:
hookid = SH_ADD_MANUALVPHOOK(Touch, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_TouchPost), true);
break;
case SDKHook_TraceAttack:
hookid = SH_ADD_MANUALVPHOOK(TraceAttack, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_TraceAttack), false);
break;
case SDKHook_TraceAttackPost:
hookid = SH_ADD_MANUALVPHOOK(TraceAttack, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_TraceAttackPost), true);
break;
case SDKHook_Use:
hookid = SH_ADD_MANUALVPHOOK(Use, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_Use), false);
break;
case SDKHook_UsePost:
hookid = SH_ADD_MANUALVPHOOK(Use, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_UsePost), true);
break;
case SDKHook_VPhysicsUpdate:
hookid = SH_ADD_MANUALVPHOOK(VPhysicsUpdate, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_VPhysicsUpdate), false);
break;
case SDKHook_VPhysicsUpdatePost:
hookid = SH_ADD_MANUALVPHOOK(VPhysicsUpdate, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_VPhysicsUpdatePost), true);
break;
case SDKHook_WeaponCanSwitchTo:
hookid = SH_ADD_MANUALVPHOOK(Weapon_CanSwitchTo, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_WeaponCanSwitchTo), false);
break;
case SDKHook_WeaponCanSwitchToPost:
hookid = SH_ADD_MANUALVPHOOK(Weapon_CanSwitchTo, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_WeaponCanSwitchToPost), true);
break;
case SDKHook_WeaponCanUse:
hookid = SH_ADD_MANUALVPHOOK(Weapon_CanUse, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_WeaponCanUse), false);
break;
case SDKHook_WeaponCanUsePost:
hookid = SH_ADD_MANUALVPHOOK(Weapon_CanUse, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_WeaponCanUsePost), true);
break;
case SDKHook_WeaponDrop:
hookid = SH_ADD_MANUALVPHOOK(Weapon_Drop, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_WeaponDrop), false);
break;
case SDKHook_WeaponDropPost:
hookid = SH_ADD_MANUALVPHOOK(Weapon_Drop, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_WeaponDropPost), true);
break;
case SDKHook_WeaponEquip:
hookid = SH_ADD_MANUALVPHOOK(Weapon_Equip, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_WeaponEquip), false);
break;
case SDKHook_WeaponEquipPost:
hookid = SH_ADD_MANUALVPHOOK(Weapon_Equip, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_WeaponEquipPost), true);
break;
case SDKHook_WeaponSwitch:
hookid = SH_ADD_MANUALVPHOOK(Weapon_Switch, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_WeaponSwitch), false);
break;
case SDKHook_WeaponSwitchPost:
hookid = SH_ADD_MANUALVPHOOK(Weapon_Switch, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_WeaponSwitchPost), true);
break;
case SDKHook_ShouldCollide:
hookid = SH_ADD_MANUALVPHOOK(ShouldCollide, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_ShouldCollide), true);
break;
case SDKHook_Blocked:
hookid = SH_ADD_MANUALVPHOOK(Blocked, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_Blocked), false);
break;
case SDKHook_BlockedPost:
hookid = SH_ADD_MANUALVPHOOK(Blocked, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_BlockedPost), true);
break;
case SDKHook_CanBeAutobalanced:
hookid = SH_ADD_MANUALVPHOOK(CanBeAutobalanced, pEnt, SH_MEMBER(&g_Interface, &SDKHooks::Hook_CanBeAutobalanced), false);
break;
}
vhook.SetHookID(hookid);
CVTableList *vtablelist = new CVTableList;
vtablelist->vtablehook = new CVTableHook(vhook);
vtablehooklist.push_back(vtablelist);
}
// Add hook to hook list
HookList hook;
hook.entity = gamehelpers->EntityToBCompatRef(pEnt);
hook.callback = callback;
vtablehooklist[entry]->hooks.push_back(hook);
return HookRet_Successful;
}
void SDKHooks::Unhook(CBaseEntity *pEntity)
{
if (pEntity == NULL)
{
return;
}
int entity = gamehelpers->EntityToBCompatRef(pEntity);
for (size_t type = 0; type < SDKHook_MAXHOOKS; ++type)
{
std::vector<CVTableList *> &vtablehooklist = g_HookList[type];
for (size_t listentry = 0; listentry < vtablehooklist.size(); ++listentry)
{
std::vector<HookList> &pawnhooks = vtablehooklist[listentry]->hooks;
for (size_t entry = 0; entry < pawnhooks.size(); ++entry)
{
if (entity != pawnhooks[entry].entity)
{
continue;
}
pawnhooks.erase(pawnhooks.begin() + entry);
entry--;
}
if (pawnhooks.size() == 0)
{
delete vtablehooklist[listentry];
vtablehooklist.erase(vtablehooklist.begin() + listentry);
listentry--;
}
}
}
}
void SDKHooks::Unhook(IPluginContext *pContext)
{
for (size_t type = 0; type < SDKHook_MAXHOOKS; ++type)
{
std::vector<CVTableList *> &vtablehooklist = g_HookList[type];
for (size_t listentry = 0; listentry < vtablehooklist.size(); ++listentry)
{
std::vector<HookList> &pawnhooks = vtablehooklist[listentry]->hooks;
for (size_t entry = 0; entry < pawnhooks.size(); ++entry)
{
if (pContext != NULL && pContext != pawnhooks[entry].callback->GetParentRuntime()->GetDefaultContext())
{
continue;
}
pawnhooks.erase(pawnhooks.begin() + entry);
entry--;
}
if (pawnhooks.size() == 0)
{
delete vtablehooklist[listentry];
vtablehooklist.erase(vtablehooklist.begin() + listentry);
listentry--;
}
}
}
}
void SDKHooks::Unhook(int entity, SDKHookType type, IPluginFunction *pCallback)
{
CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(entity);
if (pEntity == NULL)
{
return;
}
CVTableHook vhook(pEntity);
std::vector<CVTableList *> &vtablehooklist = g_HookList[type];
for (size_t listentry = 0; listentry < vtablehooklist.size(); ++listentry)
{
if (vhook != vtablehooklist[listentry]->vtablehook)
{
continue;
}
entity = gamehelpers->EntityToBCompatRef(pEntity);
std::vector<HookList> &pawnhooks = vtablehooklist[listentry]->hooks;
for (size_t entry = 0; entry < pawnhooks.size(); ++entry)
{
HookList &hookentry = pawnhooks[entry];
if (entity != hookentry.entity || pCallback != hookentry.callback)
{
continue;
}
pawnhooks.erase(pawnhooks.begin() + entry);
entry--;
}
if (pawnhooks.size() == 0)
{
delete vtablehooklist[listentry];
vtablehooklist.erase(vtablehooklist.begin() + listentry);
listentry--;
}
break;
}
}
/**
* IEntityFactoryDictionary, IServerGameDLL & IVEngineServer Hook Handlers
*/
void SDKHooks::OnEntityCreated(CBaseEntity *pEntity)
{
// Call OnEntityCreated forward
int ref = gamehelpers->EntityToReference(pEntity);
int index = gamehelpers->ReferenceToIndex(ref);
// This can be -1 for player ents before any players have connected
if ((unsigned)index == INVALID_EHANDLE_INDEX || (index > 0 && index <= playerhelpers->GetMaxClients()))
{
return;
}
if (!IsEntityIndexInRange(index))
{
g_pSM->LogError(myself, "SDKHooks::OnEntityCreated - Got entity index out of range (%d)", index);
return;
}
// The entity could already exist. The creation notifier fires twice for some paths
if (m_EntityCache[index] != ref)
{
HandleEntityCreated(pEntity, index, ref);
}
}
#ifdef GAMEDESC_CAN_CHANGE
const char *SDKHooks::Hook_GetGameDescription()
{
static char szGameDesc[64];
cell_t result = Pl_Continue;
g_pSM->Format(szGameDesc, sizeof(szGameDesc), "%s",
SH_CALL(gamedll, &IServerGameDLL::GetGameDescription)());
// Call OnGetGameDescription forward
g_pOnGetGameNameDescription->PushStringEx(szGameDesc, sizeof(szGameDesc), SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
g_pOnGetGameNameDescription->Execute(&result);
if(result == Pl_Changed)
RETURN_META_VALUE(MRES_SUPERCEDE, szGameDesc);
RETURN_META_VALUE(MRES_IGNORED, NULL);
}
#endif
bool SDKHooks::Hook_LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
{
// Call OnLevelInit forward
g_pOnLevelInit->PushString(pMapName);
g_pOnLevelInit->PushString("");
g_pOnLevelInit->Execute();
RETURN_META_VALUE(MRES_IGNORED, true);
}
/**
* CBaseEntity Hook Handlers
*/
bool SDKHooks::Hook_CanBeAutobalanced()
{
CBaseEntity *pPlayer = META_IFACEPTR(CBaseEntity);
CVTableHook vhook(pPlayer);
std::vector<CVTableList *> &vtablehooklist = g_HookList[SDKHook_CanBeAutobalanced];
for (size_t entry = 0; entry < vtablehooklist.size(); ++entry)
{
if (vhook != vtablehooklist[entry]->vtablehook)
{
continue;
}
int entity = gamehelpers->EntityToBCompatRef(pPlayer);
bool origRet = SH_MCALL(pPlayer, CanBeAutobalanced)();
bool newRet = origRet;
std::vector<IPluginFunction *> callbackList;
PopulateCallbackList(vtablehooklist[entry]->hooks, callbackList, entity);
for (entry = 0; entry < callbackList.size(); ++entry)
{
cell_t res = origRet;
IPluginFunction *callback = callbackList[entry];
callback->PushCell(entity);
callback->PushCell(origRet);
callback->Execute(&res);
// Only update our new ret if different from original
// (so if multiple plugins returning different answers,
// the one(s) that changed it win)
if ((bool)res != origRet)
newRet = !origRet;
}
if (newRet != origRet)
RETURN_META_VALUE(MRES_SUPERCEDE, newRet);
break;
}
RETURN_META_VALUE(MRES_IGNORED, false);
}
void SDKHooks::Hook_EndTouch(CBaseEntity *pOther)
{
cell_t result = Call(META_IFACEPTR(CBaseEntity), SDKHook_EndTouch, pOther);
if(result >= Pl_Handled)
RETURN_META(MRES_SUPERCEDE);
RETURN_META(MRES_IGNORED);
}
void SDKHooks::Hook_EndTouchPost(CBaseEntity *pOther)
{
Call(META_IFACEPTR(CBaseEntity), SDKHook_EndTouchPost, pOther);
RETURN_META(MRES_IGNORED);
}
void SDKHooks::Hook_FireBulletsPost(const FireBulletsInfo_t &info)
{
CBaseEntity *pEntity = META_IFACEPTR(CBaseEntity);
int entity = gamehelpers->EntityToBCompatRef(pEntity);
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(entity);
if(!pPlayer)
RETURN_META(MRES_IGNORED);
IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
if(!pInfo)
RETURN_META(MRES_IGNORED);