-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpluginstatemanager.inc
1250 lines (1061 loc) · 30.2 KB
/
pluginstatemanager.inc
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
#pragma semicolon 1
#pragma newdecls required
#include <dhooks>
#include <sdkhooks>
#tryinclude <sourcescramble>
#define PSM_VERSION 1.1.3
typedef PluginStateChangedCallback = function void(bool enabled);
enum struct EventData
{
char name[32];
EventHook callback;
EventHookMode mode;
void Toggle(bool enable)
{
if (enable)
HookEvent(this.name, this.callback, this.mode);
else
UnhookEvent(this.name, this.callback, this.mode);
}
}
enum struct DynamicDetourData
{
char name[64];
DynamicDetour detour;
DHookCallback callbackPre;
DHookCallback callbackPost;
void Toggle(bool enable)
{
if (this.callbackPre != INVALID_FUNCTION)
{
if (enable)
this.detour.Enable(Hook_Pre, this.callbackPre);
else
this.detour.Disable(Hook_Pre, this.callbackPre);
}
if (this.callbackPost != INVALID_FUNCTION)
{
if (enable)
this.detour.Enable(Hook_Post, this.callbackPost);
else
this.detour.Disable(Hook_Post, this.callbackPost);
}
}
}
enum struct DynamicHookData
{
char name[64];
DynamicHook hook;
}
enum struct DHookIdData
{
int hookid;
DynamicHook hook;
}
enum struct SDKHookData
{
int ref;
SDKHookType type;
SDKHookCB callback;
}
enum struct ConVarData
{
char name[64];
char value[64];
char initialValue[64];
ConVar sourceConVar;
}
enum struct ConVarChangeHookData
{
ConVar convar;
ConVarChanged callback;
void Toggle(bool enable)
{
if (enable)
this.convar.AddChangeHook(this.callback);
else
this.convar.RemoveChangeHook(this.callback);
}
}
enum struct EntityOutputHookData
{
char classname[64];
char output[64];
EntityOutput callback;
void Toggle(bool enable)
{
if (enable)
HookEntityOutput(this.classname, this.output, this.callback);
else
UnhookEntityOutput(this.classname, this.output, this.callback);
}
}
enum struct UserMessageHookData
{
UserMsg msg_id;
MsgHook hook;
bool intercept;
MsgPostHook post;
void Toggle(bool enable)
{
if (enable)
HookUserMessage(this.msg_id, this.hook, this.intercept, this.post);
else
UnhookUserMessage(this.msg_id, this.hook, this.intercept);
}
}
enum struct CommandListenerData
{
char command[64];
CommandListener callback;
void Toggle(bool enable)
{
if (enable)
AddCommandListener(this.callback, this.command);
else
RemoveCommandListener(this.callback, this.command);
}
}
enum struct MultiTargetFilterData
{
char pattern[64];
MultiTargetFilter filter;
char phrase[64];
bool phraseIsML;
void Toggle(bool enable)
{
if (enable)
AddMultiTargetFilter(this.pattern, this.filter, this.phrase, this.phraseIsML);
else
RemoveMultiTargetFilter(this.pattern, this.filter);
}
}
enum struct TempEntHookData
{
char name[64];
TEHook hook;
void Toggle(bool enable)
{
if (enable)
AddTempEntHook(this.name, this.hook);
else
RemoveTempEntHook(this.name, this.hook);
}
}
enum struct NormalSHookData
{
NormalSHook hook;
void Toggle(bool enable)
{
if (enable)
AddNormalSoundHook(this.hook);
else
RemoveNormalSoundHook(this.hook);
}
}
#if defined __sourcescramble_ext_included
enum struct MemoryPatchData
{
char name[64];
MemoryPatch patch;
void Toggle(bool enable)
{
if (enable)
this.patch.Enable();
else
this.patch.Disable();
}
}
#endif
static bool s_isInitialized;
static bool s_isEnabled;
static char s_conVarName[64];
static ConVar s_stateConVar;
static GameData s_gamedata;
static PrivateForward s_stateChangedFwd;
static ArrayList s_events; // ArrayList<EventData>
static ArrayList s_dynamicDetours; // ArrayList <DynamicDetourData>
static ArrayList s_dynamicHooks; // ArrayList<DynamicHookData>
static ArrayList s_dynamicHookIds; // ArrayList<DHookIdData>
static ArrayList s_sdkHooks; // ArrayList<SDKHookData>
static ArrayList s_conVars; // ArrayList<ConVarData>
static ArrayList s_conVarChangeHooks; // ArrayList<ConVarChangeHookData>
static ArrayList s_entityOutputHooks; // ArrayList<EntityOutputHookData>
static ArrayList s_userMessageHooks; // ArrayList<UserMessageHookData>
static ArrayList s_commandListeners; // ArrayList<CommandListenerData>
static ArrayList s_multiTargetFilters; // ArrayList<MultiTargetFilterData>
static ArrayList s_tempEntHooks; // ArrayList<TempEntHookData>
static ArrayList s_normalSHooks; // ArrayList<NormalSHookData>
#if defined __sourcescramble_ext_included
static ArrayList s_memoryPatches; // ArrayList<MemoryPatchData>
#endif
//=============================================================================
// Core Features
//=============================================================================
/**
* Initializes the plugin state manager.
*
* This function should be called in the 'OnPluginStart' forward.
*
* @param convarName The name of the convar used to determine whether the plugin should be enabled.
* If the calling plugin does not create this convar, one will be automatically created.
* @param gamedata Optional gamedata handle.
*/
stock void PSM_Init(const char[] convarName, GameData gamedata = null)
{
if (s_isInitialized)
return;
strcopy(s_conVarName, sizeof(s_conVarName), convarName);
RequestFrame(InitializeStateConVar);
if (gamedata)
s_gamedata = view_as<GameData>(CloneHandle(gamedata));
s_stateChangedFwd = new PrivateForward(ET_Ignore, Param_Cell);
s_events = new ArrayList(sizeof(EventData));
s_dynamicDetours = new ArrayList(sizeof(DynamicDetourData));
s_dynamicHooks = new ArrayList(sizeof(DynamicHookData));
s_dynamicHookIds = new ArrayList(sizeof(DHookIdData));
s_sdkHooks = new ArrayList(sizeof(SDKHookData));
s_conVars = new ArrayList(sizeof(ConVarData));
s_conVarChangeHooks = new ArrayList(sizeof(ConVarChangeHookData));
s_entityOutputHooks = new ArrayList(sizeof(EntityOutputHookData));
s_userMessageHooks = new ArrayList(sizeof(UserMessageHookData));
s_commandListeners = new ArrayList(sizeof(CommandListenerData));
s_multiTargetFilters = new ArrayList(sizeof(MultiTargetFilterData));
s_tempEntHooks = new ArrayList(sizeof(TempEntHookData));
s_normalSHooks = new ArrayList(sizeof(NormalSHookData));
#if defined __sourcescramble_ext_included
s_memoryPatches = new ArrayList(sizeof(MemoryPatchData));
#endif
s_isInitialized = true;
}
/**
* Convenience function that toggles the enabled state of the plugin if it isn't in that state already.
*
* This function should be called in the 'OnConfigsExecuted' forward.
*
* @error Plugin state manager not initialized.
*/
stock void PSM_TogglePluginState()
{
ThrowIfNotInitialized();
InitializeStateConVar();
if (PSM_IsEnabled() != s_stateConVar.BoolValue)
{
PSM_SetPluginState(s_stateConVar.BoolValue);
}
}
/**
* Sets the enabled state of the plugin.
*
* Most plugins want to use 'PSM_TogglePluginState' to respect the convar.
*
* @param enable If true, the plugin will be enabled. If false, the plugin will be disabled.
* @error Plugin state manager not initialized.
*/
stock void PSM_SetPluginState(bool enable)
{
ThrowIfNotInitialized();
if (PSM_IsEnabled() == enable)
return;
s_isEnabled = enable;
// Events
for (int i = 0; i < s_events.Length; i++)
{
EventData data;
if (s_events.GetArray(i, data))
{
data.Toggle(enable);
}
}
// Detours
for (int i = 0; i < s_dynamicDetours.Length; i++)
{
DynamicDetourData data;
if (s_dynamicDetours.GetArray(i, data))
{
data.Toggle(enable);
}
}
if (!enable)
{
// Dynamic hooks
for (int i = s_dynamicHookIds.Length - 1; i >= 0; i--)
{
int hookid = s_dynamicHookIds.Get(i, DHookIdData::hookid);
DynamicHook.RemoveHook(hookid);
}
// SDKHooks
int entity = -1;
while ((entity = FindEntityByClassname(entity, "*")) != -1)
{
PSM_SDKUnhook(entity);
}
}
// Entity output hooks
for (int i = 0; i < s_entityOutputHooks.Length; i++)
{
EntityOutputHookData data;
if (s_entityOutputHooks.GetArray(i, data))
{
data.Toggle(enable);
}
}
// User message hooks
for (int i = 0; i < s_userMessageHooks.Length; i++)
{
UserMessageHookData data;
if (s_userMessageHooks.GetArray(i, data))
{
data.Toggle(enable);
}
}
// Command listeners
for (int i = 0; i < s_commandListeners.Length; i++)
{
CommandListenerData data;
if (s_commandListeners.GetArray(i, data))
{
data.Toggle(enable);
}
}
// Multi-target filters
for (int i = 0; i < s_multiTargetFilters.Length; i++)
{
MultiTargetFilterData data;
if (s_multiTargetFilters.GetArray(i, data))
{
data.Toggle(enable);
}
}
// ConVars
for (int i = 0; i < s_conVars.Length; i++)
{
ToggleConVar(i, enable);
}
// ConVar change hooks
for (int i = 0; i < s_conVarChangeHooks.Length; i++)
{
ConVarChangeHookData data;
if (s_conVarChangeHooks.GetArray(i, data))
{
data.Toggle(enable);
}
}
// TE hooks
for (int i = 0; i < s_tempEntHooks.Length; i++)
{
TempEntHookData data;
if (s_tempEntHooks.GetArray(i, data))
{
data.Toggle(enable);
}
}
// Normal sound hooks
for (int i = 0; i < s_normalSHooks.Length; i++)
{
NormalSHookData data;
if (s_normalSHooks.GetArray(i, data))
{
data.Toggle(enable);
}
}
#if defined __sourcescramble_ext_included
// Memory patches
for (int i = 0; i < s_memoryPatches.Length; i++)
{
MemoryPatchData data;
if (s_memoryPatches.GetArray(i, data))
{
data.Toggle(enable);
}
}
#endif
if (s_stateChangedFwd.FunctionCount > 0)
{
Call_StartForward(s_stateChangedFwd);
Call_PushCell(enable);
Call_Finish();
}
}
/**
* Registers a callback function that is called when the state of the plugin changes.
*
* @param func A PluginStateChangedCallback function pointer.
* @return True on success, false otherwise.
* @error Plugin state manager not initialized.
*/
stock bool PSM_AddPluginStateChangedHook(PluginStateChangedCallback func)
{
ThrowIfNotInitialized();
return s_stateChangedFwd.AddFunction(null, func);
}
/**
* Returns whether the Plugin Manager is initialized.
*
* @return True if initialized, false otherwise.
*/
stock bool PSM_IsInitialized()
{
return s_isInitialized;
}
/**
* Returns whether the plugin is currently enabled.
*
* @return True if enabled, false otherwise.
*/
stock bool PSM_IsEnabled()
{
return PSM_IsInitialized() && s_isEnabled;
}
/**
* Returns the GameData handle set by 'PSM_Init'.
*
* @return GameData handle, or null.
*/
stock GameData PSM_GetGameData()
{
return s_gamedata;
}
//=============================================================================
// Events
//=============================================================================
/**
* Registers an event hook.
*
* @param name Name of event.
* @param callback An EventHook function pointer.
* @param mode Optional EventHookMode determining the type of hook.
* @error Invalid event name or plugin state manager not initialized.
*/
stock void PSM_AddEventHook(const char[] name, EventHook callback, EventHookMode mode = EventHookMode_Post)
{
ThrowIfNotInitialized();
Event event = CreateEvent(name, true);
if (event)
{
event.Cancel();
EventData data;
strcopy(data.name, sizeof(data.name), name);
data.callback = callback;
data.mode = mode;
s_events.PushArray(data);
if (PSM_IsEnabled())
data.Toggle(true);
}
else
{
ThrowError("Failed to find event '%s'", name);
}
}
/**
* Unregisters an event hook.
*
* @param name Name of event.
* @param callback An EventHook function pointer.
* @param mode Optional EventHookMode determining the type of hook.
* @error Plugin state manager not initialized.
*/
stock void PSM_RemoveEventHook(const char[] name, EventHook callback, EventHookMode mode = EventHookMode_Post)
{
ThrowIfNotInitialized();
for (int i = 0; i < s_events.Length; i++)
{
EventData data;
if (s_events.GetArray(i, data) && StrEqual(name, data.name) && callback == data.callback && mode == data.mode)
{
if (PSM_IsEnabled())
data.Toggle(false);
s_events.Erase(i);
}
}
}
//=============================================================================
// Detours
//=============================================================================
/**
* Registers a dynamic detour by its name in the "Functions" section of the gamedata file.
*
* @param name Name of the function in a "Functions" section to load.
* @param callbackPre Optional pre callback function.
* @param callbackPost Optional post callback function.
* @error Invalid gamedata, plugin state manager not initialized or GameData handle not initialized.
*/
stock DynamicDetour PSM_AddDynamicDetourFromConf(const char[] name, DHookCallback callbackPre = INVALID_FUNCTION, DHookCallback callbackPost = INVALID_FUNCTION)
{
ThrowIfNotInitialized(true);
DynamicDetour detour = DynamicDetour.FromConf(s_gamedata, name);
if (detour)
{
PSM_AddDynamicDetour(name, detour, callbackPre, callbackPost);
return detour;
}
else
{
ThrowError("Failed to create detour setup handle '%s'", name);
return null;
}
}
/**
* Registers a dynamic detour.
*
* @param name Name to register the dynamic detour as.
* @param detour The DynamicDetour object.
* @param callbackPre Optional pre callback function.
* @param callbackPost Optional post callback function.
* @error Plugin state manager or GameData handle not initialized.
*/
stock void PSM_AddDynamicDetour(const char[] name, DynamicDetour detour, DHookCallback callbackPre = INVALID_FUNCTION, DHookCallback callbackPost = INVALID_FUNCTION)
{
ThrowIfNotInitialized();
DynamicDetourData data;
strcopy(data.name, sizeof(data.name), name);
data.detour = detour;
data.callbackPre = callbackPre;
data.callbackPost = callbackPost;
s_dynamicDetours.PushArray(data);
if (PSM_IsEnabled())
data.Toggle(true);
}
/**
* Unregisters a dynamic detour.
*
* @param name The name of the registered dynamic detour.
* @param callbackPre Optional pre callback function.
* @param callbackPost Optional post callback function.
* @error Plugin state manager not initialized.
*/
stock void PSM_RemoveDynamicDetour(const char[] name, DHookCallback callbackPre = INVALID_FUNCTION, DHookCallback callbackPost = INVALID_FUNCTION)
{
ThrowIfNotInitialized();
for (int i = s_dynamicDetours.Length - 1; i >= 0; i--)
{
DynamicDetourData data;
if (s_dynamicDetours.GetArray(i, data) && StrEqual(name, data.name) && callbackPre == data.callbackPre && callbackPost == data.callbackPost)
{
if (PSM_IsEnabled())
data.Toggle(false);
s_dynamicDetours.Erase(i);
}
}
}
//=============================================================================
// Dynamic Hooks
//=============================================================================
/**
* Registers a dynamic hook by its name in the "Functions" section of the gamedata file.
*
* @param name Name of the function in a "Functions" section to load.
* @return The DynamicHook on success, null otherwise.
* @error Invalid gamedata or plugin state manager not initialized.
*/
stock DynamicHook PSM_AddDynamicHookFromConf(const char[] name)
{
ThrowIfNotInitialized(true);
DynamicHook hook = DynamicHook.FromConf(s_gamedata, name);
if (hook)
{
PSM_AddDynamicHook(name, hook);
return hook;
}
else
{
ThrowError("Failed to create hook setup handle '%s'", name);
return null;
}
}
/**
* Registers a dynamic hook.
*
* @param name Name to register the dynamic hook as.
* @param hook The DynamicHook object.
* @error Plugin state manager not initialized.
*/
stock void PSM_AddDynamicHook(const char[] name, DynamicHook hook)
{
ThrowIfNotInitialized();
DynamicHookData data;
strcopy(data.name, sizeof(data.name), name);
data.hook = hook;
s_dynamicHooks.PushArray(data);
}
/**
* Unregisters a dynamic hook.
*
* @param name The name of the registered dynamic hook.
* @error Plugin state manager not initialized.
*/
stock void PSM_RemoveDynamicHook(const char[] name)
{
ThrowIfNotInitialized();
for (int i = s_dynamicHooks.Length - 1; i >= 0; i--)
{
DynamicHookData data;
if (s_dynamicHooks.GetArray(i, data) && StrEqual(name, data.name))
{
// Unhook any entities that belong to this hook
for (int j = s_dynamicHookIds.Length - 1; j >= 0; j--)
{
if (s_dynamicHookIds.Get(j, DHookIdData::hook) == data.hook)
DynamicHook.RemoveHook(s_dynamicHookIds.Get(j, DHookIdData::hookid));
}
s_dynamicHooks.Erase(i);
}
}
}
/**
* Finds a dynamic hook by name and hooks an entity.
*
* This function does nothing if the plugin is not currently enabled.
*
* @param name The name of the registered dynamic hook.
* @param mode The desired hook mode - pre or post.
* @param entity Entity index to hook on.
* @param callback Callback function.
* @return A hookid on success, INVALID_HOOK_ID otherwise.
* @error Plugin state manager not initialized.
*/
stock int PSM_DHookEntityByName(const char[] name, HookMode mode, int entity, DHookCallback callback)
{
ThrowIfNotInitialized();
if (!PSM_IsEnabled())
return INVALID_HOOK_ID;
int index = s_dynamicHooks.FindString(name);
if (index == -1)
ThrowError("Failed to find dynamic hook '%s'", name);
DynamicHook hook = s_dynamicHooks.Get(index, DynamicHookData::hook);
return PSM_DHookEntity(hook, mode, entity, callback);
}
/**
* Hooks an entity.
*
* This function does nothing if the plugin is not currently enabled.
*
* @param hook The DynamicHook handle.
* @param mode The desired hook mode - pre or post.
* @param entity Entity index to hook on.
* @param callback Callback function.
* @return A hookid on success, INVALID_HOOK_ID otherwise.
* @error Plugin state manager not initialized.
*/
stock int PSM_DHookEntity(DynamicHook hook, HookMode mode, int entity, DHookCallback callback)
{
ThrowIfNotInitialized();
if (!PSM_IsEnabled())
return INVALID_HOOK_ID;
int hookid = hook.HookEntity(mode, entity, callback, OnDynamicHookRemoved);
if (hookid != INVALID_HOOK_ID)
{
DHookIdData data;
data.hookid = hookid;
data.hook = hook;
s_dynamicHookIds.PushArray(data);
}
return hookid;
}
/**
* Finds a dynamic hook by name and hooks the gamerules object.
*
* This function does nothing if the plugin is not currently enabled.
*
* @param name The name of the registered dynamic hook.
* @param mode The desired hook mode - pre or post.
* @param callback Callback function.
* @return A hookid on success, INVALID_HOOK_ID otherwise.
* @error Plugin state manager not initialized.
*/
stock int PSM_DHookGameRulesByName(const char[] name, HookMode mode, DHookCallback callback)
{
ThrowIfNotInitialized();
if (!PSM_IsEnabled())
return INVALID_HOOK_ID;
int index = s_dynamicHooks.FindString(name);
if (index == -1)
ThrowError("Failed to find dynamic hook '%s'", name);
DynamicHook hook = s_dynamicHooks.Get(index, DynamicHookData::hook);
return PSM_DHookGameRules(hook, mode, callback);
}
/**
* Hooks the gamerules object.
*
* This function does nothing if the plugin is not currently enabled.
*
* @param hook The DynamicHook handle.
* @param mode The desired hook mode - pre or post.
* @param callback Callback function.
* @return A hookid on success, INVALID_HOOK_ID otherwise.
* @error Plugin state manager not initialized.
*/
stock int PSM_DHookGameRules(DynamicHook hook, HookMode mode, DHookCallback callback)
{
ThrowIfNotInitialized();
if (!PSM_IsEnabled())
return INVALID_HOOK_ID;
int hookid = hook.HookGamerules(mode, callback, OnDynamicHookRemoved);
if (hookid != INVALID_HOOK_ID)
{
DHookIdData data;
data.hookid = hookid;
data.hook = hook;
s_dynamicHookIds.PushArray(data);
}
return hookid;
}
//=============================================================================
// SDKHooks
//=============================================================================
/**
* Hooks an entity.
*
* This function does nothing if the plugin is not currently enabled.
*
* @param entity Entity index.
* @param type Type of function to hook.
* @param callback Function to call when hook is called.
* @return True on success, false otherwise.
* @error Plugin state manager not initialized.
*/
stock bool PSM_SDKHook(int entity, SDKHookType type, SDKHookCB callback)
{
ThrowIfNotInitialized();
if (!PSM_IsEnabled())
return false;
SDKHookData data;
data.ref = EntIndexToEntRefSafe(entity);
data.type = type;
data.callback = callback;
s_sdkHooks.PushArray(data);
SDKHook(entity, type, callback);
return true;
}
/**
* Unhooks an entity.
*
* @param entity Entity index.
* @error Plugin state manager not initialized.
*/
stock void PSM_SDKUnhook(int entity)
{
ThrowIfNotInitialized();
int ref = EntIndexToEntRefSafe(entity);
for (int i = s_sdkHooks.Length - 1; i >= 0; i--)
{
SDKHookData data;
if (s_sdkHooks.GetArray(i, data) && ref == data.ref)
{
SDKUnhook(data.ref, data.type, data.callback);
s_sdkHooks.Erase(i);
}
}
}
//=============================================================================
// ConVars
//=============================================================================
/**
* Registers an "enforced" convar, meaning that its value will be enforced even if it is changed by configs or other plugins.
*
* @param name The name of the convar.
* @param value The value to enforce.
* @error Invalid convar name or plugin state manager not initialized.
*/
stock void PSM_AddEnforcedConVar(const char[] name, const char[] value)
{
ThrowIfNotInitialized();
ConVar convar = FindConVar(name);
if (!convar)
ThrowError("Failed to find convar '%s'", name);
ConVarData data;
strcopy(data.name, sizeof(data.name), name);
strcopy(data.value, sizeof(data.value), value);
int index = s_conVars.PushArray(data);
if (PSM_IsEnabled())
ToggleConVar(index, true);
}
/**
* Registers a "synced" convar, meaning that its value will always be kept in sync with another convar.
*
* @param targetName The name of the convar to keep in sync.
* @param relatedName The name of the source convar.
* @error Invalid convar name or plugin state manager not initialized.
*/
stock void PSM_AddSyncedConVar(const char[] targetName, const char[] sourceName)
{
ThrowIfNotInitialized();
ConVar convar = FindConVar(targetName);
if (!convar)
ThrowError("Failed to find convar '%s'", targetName);
ConVar sourceConVar = FindConVar(sourceName);
if (!sourceConVar)
ThrowError("Failed to find convar '%s'", sourceName);
ConVarData data;
strcopy(data.name, sizeof(data.name), targetName);
data.sourceConVar = sourceConVar;
int index = s_conVars.PushArray(data);
if (PSM_IsEnabled())
ToggleConVar(index, true);
}
/**
* Registers a hook for when a console variable's value is changed.
*
* @param convar The convar.
* @param callback An OnConVarChanged function pointer.
* @error Plugin state manager not initialized.
*/
stock void PSM_AddConVarChangeHook(ConVar convar, ConVarChanged callback)
{
ThrowIfNotInitialized();
ConVarChangeHookData data;
data.convar = convar;
data.callback = callback;
s_conVarChangeHooks.PushArray(data);
if (PSM_IsEnabled())
data.Toggle(true);
}
//=============================================================================
// Miscellaneous
//=============================================================================
/**
* Registers an entity output hook.
*
* @param classname The classname to hook.
* @param output The output name to hook.
* @param callback An EntityOutput function pointer.
* @error Plugin state manager not initialized.
*/
stock void PSM_AddEntityOutputHook(const char[] classname, const char[] output, EntityOutput callback)
{
ThrowIfNotInitialized();
EntityOutputHookData data;
strcopy(data.classname, sizeof(data.classname), classname);
strcopy(data.output, sizeof(data.output), output);
data.callback = callback;
s_entityOutputHooks.PushArray(data);
if (PSM_IsEnabled())
data.Toggle(true);
}
/**
* Registers a user message hook.
*
* @param msg_id Message index.
* @param hook Function to use as a hook.
* @param intercept If intercept is true, message will be fully intercepted,
* allowing the user to block the message. Otherwise,
* the hook is normal and ignores the return value.
* @param post Notification function.
* @error Plugin state manager not initialized or invalid message index.
*/
stock void PSM_AddUserMessageHook(UserMsg msg_id, MsgHook hook, bool intercept = false, MsgPostHook post = INVALID_FUNCTION)
{
ThrowIfNotInitialized();
UserMessageHookData data;
data.msg_id = msg_id;
data.hook = hook;
data.intercept = intercept;
data.post = post;
s_userMessageHooks.PushArray(data);
if (PSM_IsEnabled())
data.Toggle(true);
}