forked from Drifter321/DHooks2
-
Notifications
You must be signed in to change notification settings - Fork 11
/
natives.cpp
1513 lines (1264 loc) · 47.4 KB
/
natives.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
#include "natives.h"
#include "util.h"
#include "dynhooks_sourcepawn.h"
#include "signatures.h"
// Must match same enum in sdktools.inc
enum SDKFuncConfSource
{
SDKConf_Virtual,
SDKConf_Signature,
SDKConf_Address
};
bool GetHandleIfValidOrError(HandleType_t type, void **object, IPluginContext *pContext, cell_t param)
{
if(param == BAD_HANDLE)
{
return pContext->ThrowNativeError("Invalid Handle %i", BAD_HANDLE) != 0;
}
HandleError err;
HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
if((err = handlesys->ReadHandle(param, type, &sec, object)) != HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", param, err) != 0;
}
return true;
}
bool GetCallbackArgHandleIfValidOrError(HandleType_t type, HandleType_t otherType, void **object, IPluginContext *pContext, cell_t param)
{
if (param == BAD_HANDLE)
{
return pContext->ThrowNativeError("Invalid Handle %i", BAD_HANDLE) != 0;
}
HandleError err;
HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
if ((err = handlesys->ReadHandle(param, type, &sec, object)) != HandleError_None)
{
// Check if the user mixed up the callback signature and tried to call
// e.g. DHookGetParam on a hReturn handle. Print a nicer error message in that case.
void *dummy;
if (handlesys->ReadHandle(param, otherType, &sec, &dummy) == HandleError_None)
return pContext->ThrowNativeError("Invalid Handle %x (error %d). It looks like you've chosen the wrong hook callback signature for your setup and you're trying to access the wrong handle.", param, err) != 0;
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", param, err) != 0;
}
return true;
}
IPluginFunction *GetCallback(IPluginContext *pContext, HookSetup * setup, const cell_t *params, cell_t callback_index)
{
IPluginFunction *ret = NULL;
if (params[0] >= callback_index)
{
ret = pContext->GetFunctionById(params[callback_index]);
}
if (!ret && setup->callback)
{
ret = setup->callback;
}
return ret;
}
//native Handle:DHookCreate(offset, HookType:hooktype, ReturnType:returntype, ThisPointerType:thistype, DHookCallback:callback = INVALID_FUNCTION); // Callback is now optional here.
cell_t Native_CreateHook(IPluginContext *pContext, const cell_t *params)
{
IPluginFunction *callback = nullptr;
// The methodmap constructor doesn't have the callback parameter anymore.
if (params[0] >= 5)
callback = pContext->GetFunctionById(params[5]);
HookSetup *setup = new HookSetup((ReturnType)params[3], PASSFLAG_BYVAL, (HookType)params[2], (ThisPointerType)params[4], params[1], callback);
Handle_t hndl = handlesys->CreateHandle(g_HookSetupHandle, setup, pContext->GetIdentity(), myself->GetIdentity(), NULL);
if(!hndl)
{
delete setup;
return pContext->ThrowNativeError("Failed to create hook");
}
return hndl;
}
//native Handle:DHookCreateDetour(Address:funcaddr, CallingConvention:callConv, ReturnType:returntype, ThisPointerType:thistype);
cell_t Native_CreateDetour(IPluginContext *pContext, const cell_t *params)
{
HookSetup *setup = new HookSetup((ReturnType)params[3], PASSFLAG_BYVAL, (CallingConvention)params[2], (ThisPointerType)params[4], (void *)params[1]);
Handle_t hndl = handlesys->CreateHandle(g_HookSetupHandle, setup, pContext->GetIdentity(), myself->GetIdentity(), NULL);
if (!hndl)
{
delete setup;
return pContext->ThrowNativeError("Failed to create hook");
}
return hndl;
}
// native Handle:DHookCreateFromConf(Handle:gameconf, const String:function[]);
cell_t Native_DHookCreateFromConf(IPluginContext *pContext, const cell_t *params)
{
IGameConfig *conf;
HandleError err;
if ((conf = gameconfs->ReadHandle(params[1], pContext->GetIdentity(), &err)) == nullptr)
{
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
}
char *function;
pContext->LocalToString(params[2], &function);
SignatureWrapper *sig = g_pSignatures->GetFunctionSignature(function);
if (!sig)
{
return pContext->ThrowNativeError("Function signature \"%s\" was not found.", function);
}
HookSetup *setup = nullptr;
// This is a virtual hook.
if (sig->offset.length() > 0)
{
int offset;
if (!conf->GetOffset(sig->offset.c_str(), &offset))
{
return BAD_HANDLE;
}
setup = new HookSetup(sig->retType, PASSFLAG_BYVAL, sig->hookType, sig->thisType, offset, nullptr);
}
// This is a detour.
else
{
void *addr = nullptr;;
if (sig->signature.length() > 0)
{
if (!conf->GetMemSig(sig->signature.c_str(), &addr) || !addr)
{
return BAD_HANDLE;
}
}
else
{
if (!conf->GetAddress(sig->address.c_str(), &addr) || !addr)
{
return BAD_HANDLE;
}
}
setup = new HookSetup(sig->retType, PASSFLAG_BYVAL, sig->callConv, sig->thisType, addr);
}
// Push all the arguments.
for (ArgumentInfo &arg : sig->args)
{
ParamInfo info = arg.info;
setup->params.push_back(info);
}
// Create the handle to hold this setup.
Handle_t hndl = handlesys->CreateHandle(g_HookSetupHandle, setup, pContext->GetIdentity(), myself->GetIdentity(), NULL);
if (!hndl)
{
delete setup;
return pContext->ThrowNativeError("Failed to create hook");
}
return hndl;
}
//native bool:DHookSetFromConf(Handle:setup, Handle:gameconf, SDKFuncConfSource:source, const String:name[]);
cell_t Native_SetFromConf(IPluginContext *pContext, const cell_t *params)
{
HookSetup *setup;
if (!GetHandleIfValidOrError(g_HookSetupHandle, (void **)&setup, pContext, params[1]))
{
return 0;
}
IGameConfig *conf;
HandleError err;
if ((conf = gameconfs->ReadHandle(params[2], pContext->GetIdentity(), &err)) == nullptr)
{
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[2], err);
}
char *key;
pContext->LocalToString(params[4], &key);
int offset = -1;
void *addr = nullptr;;
switch (params[3])
{
case SDKConf_Virtual:
if (!conf->GetOffset(key, &offset))
{
return 0;
}
break;
case SDKConf_Signature:
if (!conf->GetMemSig(key, &addr) || !addr)
{
return 0;
}
break;
case SDKConf_Address:
if (!conf->GetAddress(key, &addr) || !addr)
{
return 0;
}
break;
default:
return pContext->ThrowNativeError("Unknown SDKFuncConfSource: %d", params[3]);
}
// Save the new info. This always invalidates the other option.
// Detour or vhook.
setup->funcAddr = addr;
setup->offset = offset;
return 1;
}
//native bool:DHookAddParam(Handle:setup, HookParamType:type, size=-1, DHookPassFlag:flag=DHookPass_ByVal, DHookRegister custom_register=DHookRegister_Default);
cell_t Native_AddParam(IPluginContext *pContext, const cell_t *params)
{
HookSetup *setup;
if(!GetHandleIfValidOrError(g_HookSetupHandle, (void **)&setup, pContext, params[1]))
{
return 0;
}
ParamInfo info;
info.type = (HookParamType)params[2];
if(params[0] >= 4)
{
info.flags = params[4];
}
else
{
info.flags = PASSFLAG_BYVAL;
}
if (params[0] >= 5)
{
PluginRegister custom_register = (PluginRegister)params[5];
info.custom_register = DynamicHooks_ConvertRegisterFrom(custom_register);
// Stay future proof.
if (info.custom_register == None && custom_register != DHookRegister_Default)
return pContext->ThrowNativeError("Unhandled DHookRegister %d", params[5]);
if (info.custom_register != None && info.type == HookParamType_Object)
return pContext->ThrowNativeError("Can't pass an object in a register.");
}
else
{
info.custom_register = None;
}
if(params[0] >= 3 && params[3] != -1)
{
info.size = params[3];
}
else if(info.type == HookParamType_Object)
{
return pContext->ThrowNativeError("Object param being set with no size");
}
else
{
info.size = GetParamTypeSize(info.type);
}
info.pass_type = GetParamTypePassType(info.type);
setup->params.push_back(info);
return 1;
}
// native bool:DHookEnableDetour(Handle:setup, bool:post, DHookCallback:callback);
cell_t Native_EnableDetour(IPluginContext *pContext, const cell_t *params)
{
HookSetup *setup;
if (!GetHandleIfValidOrError(g_HookSetupHandle, (void **)&setup, pContext, params[1]))
{
return 0;
}
if (setup->funcAddr == nullptr)
{
return pContext->ThrowNativeError("Hook not setup for a detour.");
}
IPluginFunction *callback = pContext->GetFunctionById(params[3]);
if (!callback)
{
return pContext->ThrowNativeError("Failed to retrieve function by id");
}
bool post = params[2] != 0;
HookType_t hookType = post ? HOOKTYPE_POST : HOOKTYPE_PRE;
// Check if we already detoured that function.
CHookManager *pDetourManager = GetHookManager();
CHook* pDetour = pDetourManager->FindHook(setup->funcAddr);
// If there is no detour on this function yet, create it.
if (!pDetour)
{
ICallingConvention *pCallConv = ConstructCallingConvention(setup);
pDetour = pDetourManager->HookFunction(setup->funcAddr, pCallConv);
if (!UpdateRegisterArgumentSizes(pDetour, setup))
return pContext->ThrowNativeError("A custom register for a parameter isn't supported.");
}
// Register our pre/post handler.
pDetour->AddCallback(hookType, (HookHandlerFn *)&HandleDetour);
// Add the plugin callback to the map.
return AddDetourPluginHook(hookType, pDetour, setup, callback);
}
// native bool:DHookDisableDetour(Handle:setup, bool:post, DHookCallback:callback);
cell_t Native_DisableDetour(IPluginContext *pContext, const cell_t *params)
{
HookSetup *setup;
if (!GetHandleIfValidOrError(g_HookSetupHandle, (void **)&setup, pContext, params[1]))
{
return 0;
}
if (setup->funcAddr == nullptr)
{
return pContext->ThrowNativeError("Hook not setup for a detour.");
}
IPluginFunction *callback = pContext->GetFunctionById(params[3]);
if (!callback)
{
return pContext->ThrowNativeError("Failed to retrieve function by id");
}
bool post = params[2] != 0;
HookType_t hookType = post ? HOOKTYPE_POST : HOOKTYPE_PRE;
// Check if we already detoured that function.
CHookManager *pDetourManager = GetHookManager();
CHook* pDetour = pDetourManager->FindHook(setup->funcAddr);
if (!pDetour || !pDetour->IsCallbackRegistered(hookType, (HookHandlerFn *)&HandleDetour))
{
return pContext->ThrowNativeError("Function not detoured.");
}
// Remove the callback from the hook.
return RemoveDetourPluginHook(hookType, pDetour, callback);
}
cell_t HookEntityImpl(IPluginContext *pContext, const cell_t *params, uint32_t callbackIndex, uint32_t removalcbIndex)
{
HookSetup *setup;
if(!GetHandleIfValidOrError(g_HookSetupHandle, (void **)&setup, pContext, params[1]))
{
return 0;
}
if (setup->offset == -1)
{
return pContext->ThrowNativeError("Hook not setup for a virtual hook.");
}
if(setup->hookType != HookType_Entity)
{
return pContext->ThrowNativeError("Hook is not an entity hook");
}
IPluginFunction *cb = GetCallback(pContext, setup, params, callbackIndex);
if (!cb)
{
return pContext->ThrowNativeError("Failed to hook entity %i, no callback provided", params[3]);
}
bool post = params[2] != 0;
IPluginFunction *removalcb = pContext->GetFunctionById(params[removalcbIndex]);
for(int i = g_pHooks.size() -1; i >= 0; i--)
{
DHooksManager *manager = g_pHooks.at(i);
if(manager->callback->hookType == HookType_Entity && manager->callback->entity == gamehelpers->ReferenceToBCompatRef(params[3]) && manager->callback->offset == setup->offset && manager->callback->post == post && manager->remove_callback == removalcb && manager->callback->plugin_callback == cb)
{
return manager->hookid;
}
}
CBaseEntity *pEnt = gamehelpers->ReferenceToEntity(params[3]);
if(!pEnt)
{
return pContext->ThrowNativeError("Invalid entity passed %i", params[3]);
}
DHooksManager *manager = new DHooksManager(setup, pEnt, removalcb, cb, post);
if(!manager->hookid)
{
delete manager;
return 0;
}
g_pHooks.push_back(manager);
return manager->hookid;
}
// native DHookEntity(Handle:setup, bool:post, entity, DHookRemovalCB:removalcb, DHookCallback:callback = INVALID_FUNCTION); // Both callbacks are optional
cell_t Native_HookEntity(IPluginContext *pContext, const cell_t *params)
{
return HookEntityImpl(pContext, params, 5, 4);
}
// public native int DynamicHook.HookEntity(HookMode mode, int entity, DHookCallback callback, DHookRemovalCB removalcb=INVALID_FUNCTION);
cell_t Native_HookEntity_Methodmap(IPluginContext *pContext, const cell_t *params)
{
return HookEntityImpl(pContext, params, 4, 5);
}
cell_t HookGamerulesImpl(IPluginContext *pContext, const cell_t *params, uint32_t callbackIndex, uint32_t removalcbIndex)
{
HookSetup *setup;
if(!GetHandleIfValidOrError(g_HookSetupHandle, (void **)&setup, pContext, params[1]))
{
return 0;
}
if (setup->offset == -1)
{
return pContext->ThrowNativeError("Hook not setup for a virtual hook.");
}
if(setup->hookType != HookType_GameRules)
{
return pContext->ThrowNativeError("Hook is not a gamerules hook");
}
IPluginFunction *cb = GetCallback(pContext, setup, params, callbackIndex);
if (!cb)
{
return pContext->ThrowNativeError("Failed to hook gamerules, no callback provided");
}
bool post = params[2] != 0;
IPluginFunction *removalcb = pContext->GetFunctionById(params[removalcbIndex]);
for(int i = g_pHooks.size() -1; i >= 0; i--)
{
DHooksManager *manager = g_pHooks.at(i);
if(manager->callback->hookType == HookType_GameRules && manager->callback->offset == setup->offset && manager->callback->post == post && manager->remove_callback == removalcb && manager->callback->plugin_callback == cb)
{
return manager->hookid;
}
}
void *rules = g_pSDKTools->GetGameRules();
if(!rules)
{
return pContext->ThrowNativeError("Could not get gamerules pointer");
}
DHooksManager *manager = new DHooksManager(setup, rules, removalcb, cb, post);
if(!manager->hookid)
{
delete manager;
return 0;
}
g_pHooks.push_back(manager);
return manager->hookid;
}
// native DHookGamerules(Handle:setup, bool:post, DHookRemovalCB:removalcb, DHookCallback:callback = INVALID_FUNCTION); // Both callbacks are optional
cell_t Native_HookGamerules(IPluginContext *pContext, const cell_t *params)
{
return HookGamerulesImpl(pContext, params, 4, 3);
}
// public native int DynamicHook.HookGamerules(HookMode mode, DHookCallback callback, DHookRemovalCB removalcb=INVALID_FUNCTION);
cell_t Native_HookGamerules_Methodmap(IPluginContext *pContext, const cell_t *params)
{
return HookGamerulesImpl(pContext, params, 3, 4);
}
cell_t HookRawImpl(IPluginContext *pContext, const cell_t *params, int callbackIndex, int removalcbIndex)
{
HookSetup *setup;
if(!GetHandleIfValidOrError(g_HookSetupHandle, (void **)&setup, pContext, params[1]))
{
return 0;
}
if (setup->offset == -1)
{
return pContext->ThrowNativeError("Hook not setup for a virtual hook.");
}
if(setup->hookType != HookType_Raw)
{
return pContext->ThrowNativeError("Hook is not a raw hook");
}
IPluginFunction *cb = GetCallback(pContext, setup, params, callbackIndex);
if (!cb)
{
return pContext->ThrowNativeError("Failed to hook address, no callback provided");
}
bool post = params[2] != 0;
IPluginFunction *removalcb = nullptr;
if (removalcbIndex > 0)
removalcb = pContext->GetFunctionById(params[removalcbIndex]);
void *iface = (void *)(params[3]);
for(int i = g_pHooks.size() -1; i >= 0; i--)
{
DHooksManager *manager = g_pHooks.at(i);
if(manager->callback->hookType == HookType_Raw && manager->addr == (intptr_t)iface && manager->callback->offset == setup->offset && manager->callback->post == post && manager->remove_callback == removalcb && manager->callback->plugin_callback == cb)
{
return manager->hookid;
}
}
if(!iface)
{
return pContext->ThrowNativeError("Invalid address passed");
}
DHooksManager *manager = new DHooksManager(setup, iface, removalcb, cb, post);
if(!manager->hookid)
{
delete manager;
return 0;
}
g_pHooks.push_back(manager);
return manager->hookid;
}
// DHookRaw(Handle:setup, bool:post, Address:addr, DHookRemovalCB:removalcb, DHookCallback:callback = INVALID_FUNCTION); // Both callbacks are optional
cell_t Native_HookRaw(IPluginContext *pContext, const cell_t *params)
{
return HookRawImpl(pContext, params, 5, 4);
}
// public native int DynamicHook.HookRaw(HookMode mode, Address addr, DHookCallback callback);
cell_t Native_HookRaw_Methodmap(IPluginContext *pContext, const cell_t *params)
{
return HookRawImpl(pContext, params, 4, 0);
}
// native bool:DHookRemoveHookID(hookid);
cell_t Native_RemoveHookID(IPluginContext *pContext, const cell_t *params)
{
for(int i = g_pHooks.size() -1; i >= 0; i--)
{
DHooksManager *manager = g_pHooks.at(i);
if(manager->hookid == params[1] && manager->callback->plugin_callback->GetParentRuntime()->GetDefaultContext() == pContext)
{
delete manager;
g_pHooks.erase(g_pHooks.begin() + i);
return 1;
}
}
return 0;
}
// native any:DHookGetParam(Handle:hParams, num);
cell_t Native_GetParam(IPluginContext *pContext, const cell_t *params)
{
HookParamsStruct *paramStruct;
if(!GetCallbackArgHandleIfValidOrError(g_HookParamsHandle, g_HookReturnHandle, (void **)¶mStruct, pContext, params[1]))
{
return 0;
}
if(params[2] < 0 || params[2] > (int)paramStruct->dg->params.size())
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
if(params[2] == 0)
{
return paramStruct->dg->params.size();
}
int index = params[2] - 1;
size_t offset = GetParamOffset(paramStruct, index);
void *addr = (void **)((intptr_t)paramStruct->orgParams + offset);
if(*(void **)addr == NULL && (paramStruct->dg->params.at(index).type == HookParamType_CBaseEntity || paramStruct->dg->params.at(index).type == HookParamType_Edict))
{
return pContext->ThrowNativeError("Trying to get value for null pointer.");
}
switch(paramStruct->dg->params.at(index).type)
{
case HookParamType_Int:
return *(int *)addr;
case HookParamType_Bool:
return *(cell_t *)addr != 0;
case HookParamType_CBaseEntity:
return gamehelpers->EntityToBCompatRef(*(CBaseEntity **)addr);
case HookParamType_Edict:
return gamehelpers->IndexOfEdict(*(edict_t **)addr);
case HookParamType_Float:
return sp_ftoc(*(float *)addr);
default:
return pContext->ThrowNativeError("Invalid param type (%i) to get", paramStruct->dg->params.at(index).type);
}
return 1;
}
// native DHookSetParam(Handle:hParams, param, any:value)
cell_t Native_SetParam(IPluginContext *pContext, const cell_t *params)
{
HookParamsStruct *paramStruct;
if(!GetCallbackArgHandleIfValidOrError(g_HookParamsHandle, g_HookReturnHandle, (void **)¶mStruct, pContext, params[1]))
{
return 0;
}
if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size())
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
int index = params[2] - 1;
size_t offset = GetParamOffset(paramStruct, index);
void *addr = (void **)((intptr_t)paramStruct->newParams + offset);
switch(paramStruct->dg->params.at(index).type)
{
case HookParamType_Int:
*(int *)addr = params[3];
break;
case HookParamType_Bool:
*(bool *)addr = (params[3] ? true : false);
break;
case HookParamType_CBaseEntity:
{
CBaseEntity *pEnt = gamehelpers->ReferenceToEntity(params[2]);
if(!pEnt)
{
return pContext->ThrowNativeError("Invalid entity index passed for param value");
}
*(CBaseEntity **)addr = pEnt;
break;
}
case HookParamType_Edict:
{
edict_t *pEdict = gamehelpers->EdictOfIndex(params[2]);
if(!pEdict || pEdict->IsFree())
{
pContext->ThrowNativeError("Invalid entity index passed for param value");
}
*(edict_t **)addr = pEdict;
break;
}
case HookParamType_Float:
*(float *)addr = sp_ctof(params[3]);
break;
default:
return pContext->ThrowNativeError("Invalid param type (%i) to set", paramStruct->dg->params.at(index).type);
}
paramStruct->isChanged[index] = true;
return 1;
}
// native any:DHookGetReturn(Handle:hReturn);
cell_t Native_GetReturn(IPluginContext *pContext, const cell_t *params)
{
HookReturnStruct *returnStruct;
if(!GetCallbackArgHandleIfValidOrError(g_HookReturnHandle, g_HookParamsHandle, (void **)&returnStruct, pContext, params[1]))
{
return 0;
}
switch(returnStruct->type)
{
case ReturnType_Int:
return *(int *)returnStruct->orgResult;
case ReturnType_Bool:
return *(bool *)returnStruct->orgResult? 1 : 0;
case ReturnType_CBaseEntity:
return gamehelpers->EntityToBCompatRef((CBaseEntity *)returnStruct->orgResult);
case ReturnType_Edict:
return gamehelpers->IndexOfEdict((edict_t *)returnStruct->orgResult);
case ReturnType_Float:
return sp_ftoc(*(float *)returnStruct->orgResult);
default:
return pContext->ThrowNativeError("Invalid param type (%i) to get", returnStruct->type);
}
return 1;
}
// native DHookSetReturn(Handle:hReturn, any:value)
cell_t Native_SetReturn(IPluginContext *pContext, const cell_t *params)
{
HookReturnStruct *returnStruct;
if(!GetCallbackArgHandleIfValidOrError(g_HookReturnHandle, g_HookParamsHandle, (void **)&returnStruct, pContext, params[1]))
{
return 0;
}
switch(returnStruct->type)
{
case ReturnType_Int:
*(int *)returnStruct->newResult = params[2];
break;
case ReturnType_Bool:
*(bool *)returnStruct->newResult = params[2] != 0;
break;
case ReturnType_CBaseEntity:
{
if(params[2] == -1) {
returnStruct->newResult = nullptr;
} else {
CBaseEntity *pEnt = gamehelpers->ReferenceToEntity(params[2]);
if(!pEnt)
{
return pContext->ThrowNativeError("Invalid entity index passed for return value");
}
returnStruct->newResult = pEnt;
}
break;
}
case ReturnType_Edict:
{
edict_t *pEdict = gamehelpers->EdictOfIndex(params[2]);
if(!pEdict || pEdict->IsFree())
{
pContext->ThrowNativeError("Invalid entity index passed for return value");
}
returnStruct->newResult = pEdict;
break;
}
case ReturnType_Float:
*(float *)returnStruct->newResult = sp_ctof(params[2]);
break;
default:
return pContext->ThrowNativeError("Invalid param type (%i) to get",returnStruct->type);
}
returnStruct->isChanged = true;
return 1;
}
// native DHookGetParamVector(Handle:hParams, num, Float:vec[3])
cell_t Native_GetParamVector(IPluginContext *pContext, const cell_t *params)
{
HookParamsStruct *paramStruct;
if(!GetCallbackArgHandleIfValidOrError(g_HookParamsHandle, g_HookReturnHandle, (void **)¶mStruct, pContext, params[1]))
{
return 0;
}
if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size())
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
int index = params[2] - 1;
size_t offset = GetParamOffset(paramStruct, index);
void *addr = (void **)((intptr_t)paramStruct->orgParams + offset);
if (*(void **)addr == NULL)
{
return pContext->ThrowNativeError("Trying to get value for null pointer.");
}
switch(paramStruct->dg->params.at(index).type)
{
case HookParamType_VectorPtr:
{
cell_t *buffer;
pContext->LocalToPhysAddr(params[3], &buffer);
SDKVector *vec = *(SDKVector **)addr;
buffer[0] = sp_ftoc(vec->x);
buffer[1] = sp_ftoc(vec->y);
buffer[2] = sp_ftoc(vec->z);
return 1;
}
}
return pContext->ThrowNativeError("Invalid param type to get. Param is not a vector.");
}
static void FreeChangedVector(void *pData)
{
delete (SDKVector *)pData;
}
// native DHookSetParamVector(Handle:hParams, num, Float:vec[3])
cell_t Native_SetParamVector(IPluginContext *pContext, const cell_t *params)
{
HookParamsStruct *paramStruct;
if(!GetCallbackArgHandleIfValidOrError(g_HookParamsHandle, g_HookReturnHandle, (void **)¶mStruct, pContext, params[1]))
{
return 0;
}
if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size())
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
int index = params[2] - 1;
size_t offset = GetParamOffset(paramStruct, index);
void *addr = (void **)((intptr_t)paramStruct->newParams + offset);
switch(paramStruct->dg->params.at(index).type)
{
case HookParamType_VectorPtr:
{
cell_t *buffer;
pContext->LocalToPhysAddr(params[3], &buffer);
*(SDKVector **)addr = new SDKVector(sp_ctof(buffer[0]), sp_ctof(buffer[1]), sp_ctof(buffer[2]));
paramStruct->isChanged[index] = true;
// Free it later (cheaply) after the function returned.
smutils->AddFrameAction(FreeChangedVector, *(SDKVector **)addr);
return 1;
}
}
return pContext->ThrowNativeError("Invalid param type to set. Param is not a vector.");
}
// native DHookGetParamString(Handle:hParams, num, String:buffer[], size)
cell_t Native_GetParamString(IPluginContext *pContext, const cell_t *params)
{
HookParamsStruct *paramStruct;
if(!GetCallbackArgHandleIfValidOrError(g_HookParamsHandle, g_HookReturnHandle, (void **)¶mStruct, pContext, params[1]))
{
return 0;
}
if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size())
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
int index = params[2] - 1;
size_t offset = GetParamOffset(paramStruct, index);
void *addr = (void **)((intptr_t)paramStruct->orgParams + offset);
if(*(void **)addr == NULL)
{
return pContext->ThrowNativeError("Trying to get value for null pointer.");
}
if(paramStruct->dg->params.at(index).type == HookParamType_CharPtr)
{
pContext->StringToLocal(params[3], params[4], *(const char **)addr);
}
return 1;
}
// native DHookGetReturnString(Handle:hReturn, String:buffer[], size)
cell_t Native_GetReturnString(IPluginContext *pContext, const cell_t *params)
{
HookReturnStruct *returnStruct;
if(!GetCallbackArgHandleIfValidOrError(g_HookReturnHandle, g_HookParamsHandle, (void **)&returnStruct, pContext, params[1]))
{
return 0;
}
switch(returnStruct->type)
{
case ReturnType_String:
pContext->StringToLocal(params[2], params[3], (*(string_t *)returnStruct->orgResult == NULL_STRING) ? "" : STRING(*(string_t *)returnStruct->orgResult));
return 1;
case ReturnType_StringPtr:
pContext->StringToLocal(params[2], params[3], ((string_t *)returnStruct->orgResult == NULL) ? "" : ((string_t *)returnStruct->orgResult)->ToCStr());
return 1;
case ReturnType_CharPtr:
pContext->StringToLocal(params[2], params[3], ((char *)returnStruct->orgResult == NULL) ? "" : (const char *)returnStruct->orgResult);
return 1;
default:
return pContext->ThrowNativeError("Invalid param type to get. Param is not a string.");
}
}
static void FreeChangedCharPtr(void *pData)
{
delete[](char *)pData;
}
//native DHookSetReturnString(Handle:hReturn, String:value[])
cell_t Native_SetReturnString(IPluginContext *pContext, const cell_t *params)
{
HookReturnStruct *returnStruct;
if(!GetCallbackArgHandleIfValidOrError(g_HookReturnHandle, g_HookParamsHandle, (void **)&returnStruct, pContext, params[1]))
{
return 0;
}
char *value;
pContext->LocalToString(params[2], &value);
switch(returnStruct->type)
{
case ReturnType_CharPtr:
{
returnStruct->newResult = new char[strlen(value) + 1];
strcpy((char *)returnStruct->newResult, value);
returnStruct->isChanged = true;
// Free it later (cheaply) after the function returned.
smutils->AddFrameAction(FreeChangedCharPtr, returnStruct->newResult);
return 1;
}
default:
return pContext->ThrowNativeError("Invalid param type to get. Param is not a char pointer.");
}
}
//native DHookSetParamString(Handle:hParams, num, String:value[])
cell_t Native_SetParamString(IPluginContext *pContext, const cell_t *params)
{
HookParamsStruct *paramStruct;
if(!GetCallbackArgHandleIfValidOrError(g_HookParamsHandle, g_HookReturnHandle, (void **)¶mStruct, pContext, params[1]))
{
return 0;
}
if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size())
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
int index = params[2] - 1;
size_t offset = GetParamOffset(paramStruct, index);
void *addr = (void **)((intptr_t)paramStruct->newParams + offset);
char *value;
pContext->LocalToString(params[3], &value);
if(paramStruct->dg->params.at(index).type == HookParamType_CharPtr)
{
*(char **)addr = new char[strlen(value)+1];
strcpy(*(char **)addr, value);
paramStruct->isChanged[index] = true;
// Free it later (cheaply) after the function returned.
smutils->AddFrameAction(FreeChangedCharPtr, *(char **)addr);
}
return 1;
}
//native DHookAddEntityListener(ListenType:type, ListenCB:callback);
cell_t Native_AddEntityListener(IPluginContext *pContext, const cell_t *params)
{
if(g_pEntityListener)
{
return g_pEntityListener->AddPluginEntityListener((ListenType)params[1], pContext->GetFunctionById(params[2]));;
}