/
vnatives.cpp
1831 lines (1597 loc) · 54.6 KB
/
vnatives.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 :
* =============================================================================
* SourceMod SDKTools Extension
* Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, 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 <stdlib.h>
#include <sh_string.h>
#include "extension.h"
#include "vcallbuilder.h"
#include "vnatives.h"
#include "vhelpers.h"
#include "vglobals.h"
#include "CellRecipientFilter.h"
#include <inetchannel.h>
#include <iclient.h>
#include "iserver.h"
#include "am-string.h"
#include <sm_argbuffer.h>
SourceHook::List<ValveCall *> g_RegCalls;
SourceHook::List<ICallWrapper *> g_CallWraps;
#define ENTINDEX_TO_CBASEENTITY(ref, buffer) \
buffer = gamehelpers->ReferenceToEntity(ref); \
if (!buffer) \
{ \
return pContext->ThrowNativeError("Entity %d (%d) is not a CBaseEntity", gamehelpers->ReferenceToIndex(ref), ref); \
}
#define SET_VECTOR(addr, vec) \
addr[0] = sp_ftoc(vec.x); \
addr[1] = sp_ftoc(vec.y); \
addr[2] = sp_ftoc(vec.z);
inline void InitPass(ValvePassInfo &info, ValveType vtype, PassType type, unsigned int flags, unsigned int decflags=0)
{
info.decflags = decflags;
info.encflags = 0;
info.flags = flags;
info.type = type;
info.vtype = vtype;
}
#define START_CALL() \
unsigned char *vptr = pCall->stk_get();
#define FINISH_CALL_SIMPLE(vret) \
pCall->call->Execute(vptr, vret); \
pCall->stk_put(vptr);
#define ENCODE_VALVE_PARAM(num, which, vnum) \
if (EncodeValveParam(pContext, \
params[num], \
pCall, \
&pCall->which[vnum], \
vptr) \
== Data_Fail) \
{ \
return 0; \
}
#define DECODE_VALVE_PARAM(num, which, vnum) \
if (DecodeValveParam(pContext, \
params[num], \
pCall, \
&pCall->which[vnum], \
vptr) \
== Data_Fail) \
{ \
return 0; \
}
bool CreateBaseCall(const char *name,
ValveCallType vcalltype,
const ValvePassInfo *retinfo,
const ValvePassInfo params[],
unsigned int numParams,
ValveCall **vaddr)
{
int offset;
ValveCall *call;
if (g_pGameConf->GetOffset(name, &offset))
{
call = CreateValveVCall(offset, vcalltype, retinfo, params, numParams);
if (call)
{
g_RegCalls.push_back(call);
}
*vaddr = call;
return true;
} else {
void *addr = NULL;
if (g_pGameConf->GetMemSig(name, &addr) && addr != NULL)
{
call = CreateValveCall(addr, vcalltype, retinfo, params, numParams);
if (call)
{
g_RegCalls.push_back(call);
}
*vaddr = call;
return true;
}
}
return false;
}
static cell_t RemovePlayerItem(IPluginContext *pContext, const cell_t *params)
{
static ValveCall *pCall = NULL;
if (!pCall)
{
ValvePassInfo pass[2];
InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
if (!CreateBaseCall("RemovePlayerItem", ValveCall_Player, &pass[1], pass, 1, &pCall))
{
return pContext->ThrowNativeError("\"RemovePlayerItem\" not supported by this mod");
} else if (!pCall) {
return pContext->ThrowNativeError("\"RemovePlayerItem\" wrapper failed to initialize");
}
}
bool ret;
START_CALL();
DECODE_VALVE_PARAM(1, thisinfo, 0);
DECODE_VALVE_PARAM(2, vparams, 0);
FINISH_CALL_SIMPLE(&ret);
return ret ? 1 : 0;
}
#if SOURCE_ENGINE == SE_CSGO
class CEconItemView;
static cell_t GiveNamedItem(IPluginContext *pContext, const cell_t *params)
{
if (g_SdkTools.ShouldFollowCSGOServerGuidelines())
{
char *pWeaponName;
pContext->LocalToString(params[2], &pWeaponName);
// Don't allow knives other than weapon_knife, weapon_knifegg, and weapon_knife_t.
// Others follow pattern weapon_knife_*
size_t len = strlen(pWeaponName);
if ((len >= 14 && strnicmp(pWeaponName, "weapon_knife_", 13) == 0 && !(pWeaponName[13] == 't' && pWeaponName[14] == '\0')) || (len == 14 && stricmp(pWeaponName, "weapon_bayonet") == 0))
{
return pContext->ThrowNativeError("Blocked giving of %s due to core.cfg option FollowCSGOServerGuidelines", pWeaponName);
}
}
static ValveCall *pCall = NULL;
if (!pCall)
{
ValvePassInfo pass[6];
InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[2], Valve_Object, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[4], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[5], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
if (!CreateBaseCall("GiveNamedItem", ValveCall_Player, &pass[5], pass, 5, &pCall))
{
return pContext->ThrowNativeError("\"GiveNamedItem\" not supported by this mod");
} else if (!pCall) {
return pContext->ThrowNativeError("\"GiveNamedItem\" wrapper failed to initialize");
}
}
CBaseEntity *pEntity = NULL;
START_CALL();
DECODE_VALVE_PARAM(1, thisinfo, 0);
DECODE_VALVE_PARAM(2, vparams, 0);
DECODE_VALVE_PARAM(3, vparams, 1);
*(CEconItemView **)(vptr + pCall->vparams[2].offset) = NULL;
*(bool *)(vptr + pCall->vparams[3].offset) = false;
*(void **)(vptr + pCall->vparams[4].offset) = NULL;
FINISH_CALL_SIMPLE(&pEntity);
return gamehelpers->EntityToBCompatRef(pEntity);
}
#elif SOURCE_ENGINE == SE_BLADE || SOURCE_ENGINE == SE_MCV
class CEconItemView;
static cell_t GiveNamedItem(IPluginContext *pContext, const cell_t *params)
{
static ValveCall *pCall = NULL;
if (!pCall)
{
ValvePassInfo pass[5];
InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[2], Valve_Object, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[4], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
if (!CreateBaseCall("GiveNamedItem", ValveCall_Player, &pass[4], pass, 4, &pCall))
{
return pContext->ThrowNativeError("\"GiveNamedItem\" not supported by this mod");
} else if (!pCall) {
return pContext->ThrowNativeError("\"GiveNamedItem\" wrapper failed to initialize");
}
}
CBaseEntity *pEntity = NULL;
START_CALL();
DECODE_VALVE_PARAM(1, thisinfo, 0);
DECODE_VALVE_PARAM(2, vparams, 0);
DECODE_VALVE_PARAM(3, vparams, 1);
*(CEconItemView **)(vptr + pCall->vparams[2].offset) = NULL;
*(bool *)(vptr + pCall->vparams[3].offset) = false;
FINISH_CALL_SIMPLE(&pEntity);
return gamehelpers->EntityToBCompatRef(pEntity);
}
#elif SOURCE_ENGINE == SE_BMS
// CBaseEntity *GiveNamedItem( const char *szName, int iSubType = 0, int iPrimaryAmmo = -1, int iSecondaryAmmo = -1 )
static cell_t GiveNamedItem(IPluginContext *pContext, const cell_t *params)
{
static ValveCall *pCall = NULL;
if (!pCall)
{
ValvePassInfo pass[5];
InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[2], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[3], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[4], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
if (!CreateBaseCall("GiveNamedItem", ValveCall_Player, &pass[4], pass, 4, &pCall))
{
return pContext->ThrowNativeError("\"GiveNamedItem\" not supported by this mod");
} else if (!pCall) {
return pContext->ThrowNativeError("\"GiveNamedItem\" wrapper failed to initialize");
}
}
CBaseEntity *pEntity = NULL;
START_CALL();
DECODE_VALVE_PARAM(1, thisinfo, 0);
DECODE_VALVE_PARAM(2, vparams, 0);
DECODE_VALVE_PARAM(3, vparams, 1);
*(int *)(vptr + pCall->vparams[2].offset) = -1;
*(int *)(vptr + pCall->vparams[3].offset) = -1;
FINISH_CALL_SIMPLE(&pEntity);
return gamehelpers->EntityToBCompatRef(pEntity);
}
#elif SOURCE_ENGINE == SE_LEFT4DEAD2
// CBaseEntity *CTerrorPlayer::GiveNamedItem( const char *pchName, int iSubType, bool bForce, CBaseEntity *pUnk)
static cell_t GiveNamedItem(IPluginContext *pContext, const cell_t *params)
{
static ValveCall *pCall = NULL;
if (!pCall)
{
ValvePassInfo pass[5];
InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[3], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[4], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
if (!CreateBaseCall("GiveNamedItem", ValveCall_Player, &pass[4], pass, 4, &pCall)) {
return pContext->ThrowNativeError("\"GiveNamedItem\" not supported by this mod");
} else if (!pCall) {
return pContext->ThrowNativeError("\"GiveNamedItem\" wrapper failed to initialize");
}
}
CBaseEntity *pEntity = NULL;
START_CALL();
DECODE_VALVE_PARAM(1, thisinfo, 0);
DECODE_VALVE_PARAM(2, vparams, 0);
DECODE_VALVE_PARAM(3, vparams, 1);
*(bool *)(vptr + pCall->vparams[2].offset) = false;
*(void **)(vptr + pCall->vparams[3].offset) = NULL;
FINISH_CALL_SIMPLE(&pEntity);
return gamehelpers->EntityToBCompatRef(pEntity);
}
#else
// CBaseEntity *GiveNamedItem( const char *szName, int iSubType = 0 )
static cell_t GiveNamedItem(IPluginContext *pContext, const cell_t *params)
{
static ValveCall *pCall = NULL;
if (!pCall)
{
ValvePassInfo pass[3];
InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
if (!CreateBaseCall("GiveNamedItem", ValveCall_Player, &pass[2], pass, 2, &pCall))
{
return pContext->ThrowNativeError("\"GiveNamedItem\" not supported by this mod");
} else if (!pCall) {
return pContext->ThrowNativeError("\"GiveNamedItem\" wrapper failed to initialize");
}
}
CBaseEntity *pEntity = NULL;
START_CALL();
DECODE_VALVE_PARAM(1, thisinfo, 0);
DECODE_VALVE_PARAM(2, vparams, 0);
DECODE_VALVE_PARAM(3, vparams, 1);
FINISH_CALL_SIMPLE(&pEntity);
return gamehelpers->EntityToBCompatRef(pEntity);
}
#endif
static cell_t GetPlayerWeaponSlot(IPluginContext *pContext, const cell_t *params)
{
static ValveCall *pCall = NULL;
if (!pCall)
{
ValvePassInfo pass[2];
InitPass(pass[0], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[1], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
if (!CreateBaseCall("Weapon_GetSlot", ValveCall_Player, &pass[1], pass, 1, &pCall))
{
return pContext->ThrowNativeError("\"Weapon_GetSlot\" not supported by this mod");
} else if (!pCall) {
return pContext->ThrowNativeError("\"Weapon_GetSlot\" wrapper failed to initialize");
}
}
CBaseEntity *pEntity;
START_CALL();
DECODE_VALVE_PARAM(1, thisinfo, 0);
DECODE_VALVE_PARAM(2, vparams, 0);
FINISH_CALL_SIMPLE(&pEntity);
return gamehelpers->EntityToBCompatRef(pEntity);
}
#if SOURCE_ENGINE != SE_DARKMESSIAH
static cell_t IgniteEntity(IPluginContext *pContext, const cell_t *params)
{
static ValveCall *pCall = NULL;
if (!pCall)
{
#if SOURCE_ENGINE == SE_SDK2013
if (!strcmp(g_pSM->GetGameFolderName(), "nmrih"))
{
ValvePassInfo pass[6];
InitPass(pass[0], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[2], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[4], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[5], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
if (!CreateBaseCall("Ignite", ValveCall_Entity, NULL, pass, 6, &pCall))
{
return pContext->ThrowNativeError("\"Ignite\" not supported by this mod");
}
else if (!pCall)
{
return pContext->ThrowNativeError("\"Ignite\" wrapper failed to initialize");
}
}
else
#endif // SDK2013
{
#if SOURCE_ENGINE == SE_MCV
ValvePassInfo pass[6];
InitPass(pass[0], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[2], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[4], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[5], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
if (!CreateBaseCall("Ignite", ValveCall_Entity, NULL, pass, 6, &pCall)) {
return pContext->ThrowNativeError("\"Ignite\" not supported by this mod");
} else if (!pCall) {
return pContext->ThrowNativeError("\"Ignite\" wrapper failed to initialize");
}
#else
ValvePassInfo pass[4];
InitPass(pass[0], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[2], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
if (!CreateBaseCall("Ignite", ValveCall_Entity, NULL, pass, 4, &pCall)) {
return pContext->ThrowNativeError("\"Ignite\" not supported by this mod");
} else if (!pCall) {
return pContext->ThrowNativeError("\"Ignite\" wrapper failed to initialize");
}
#endif // MCV
}
}
START_CALL();
DECODE_VALVE_PARAM(1, thisinfo, 0);
DECODE_VALVE_PARAM(2, vparams, 0);
DECODE_VALVE_PARAM(3, vparams, 1);
DECODE_VALVE_PARAM(4, vparams, 2);
DECODE_VALVE_PARAM(5, vparams, 3);
#if SOURCE_ENGINE == SE_SDK2013
if (!strcmp(g_pSM->GetGameFolderName(), "nmrih"))
{
*(int *) (vptr + pCall->vparams[4].offset) = 0;
*(int *) (vptr + pCall->vparams[5].offset) = 0;
}
#elif SOURCE_ENGINE == SE_MCV
*(CBaseEntity **) (vptr + pCall->vparams[4].offset) = nullptr; // pAttacker
*(string_t *) (vptr + pCall->vparams[5].offset) = NULL_STRING; // sRootWeaponClassname
#endif // SDK2013
FINISH_CALL_SIMPLE(NULL);
return 1;
}
#else
/* Dark Messiah specific version */
static cell_t IgniteEntity(IPluginContext *pContext, const cell_t *params)
{
static ValveCall *pCall = NULL;
if (!pCall)
{
ValvePassInfo pass[6];
InitPass(pass[0], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[2], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[4], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[5], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
if (!CreateBaseCall("Ignite", ValveCall_Entity, NULL, pass, 6, &pCall))
{
return pContext->ThrowNativeError("\"Ignite\" not supported by this mod");
} else if (!pCall) {
return pContext->ThrowNativeError("\"Ignite\" wrapper failed to initialize");
}
}
START_CALL();
DECODE_VALVE_PARAM(1, thisinfo, 0);
DECODE_VALVE_PARAM(2, vparams, 0);
DECODE_VALVE_PARAM(3, vparams, 1);
DECODE_VALVE_PARAM(4, vparams, 2);
DECODE_VALVE_PARAM(5, vparams, 3);
/* Not sure what these params do, but they appear to be the default values */
*(int *)(vptr + pCall->vparams[4].offset) = 3;
*(int *)(vptr + pCall->vparams[5].offset) = 0;
FINISH_CALL_SIMPLE(NULL);
return 1;
}
#endif
static cell_t ExtinguishEntity(IPluginContext *pContext, const cell_t *params)
{
static ValveCall *pCall = NULL;
if (!pCall)
{
if (!CreateBaseCall("Extinguish", ValveCall_Entity, NULL, NULL, 0, &pCall))
{
return pContext->ThrowNativeError("\"Extinguish\" not supported by this mod");
} else if (!pCall) {
return pContext->ThrowNativeError("\"Extinguish\" wrapper failed to initialize");
}
}
START_CALL();
DECODE_VALVE_PARAM(1, thisinfo, 0);
FINISH_CALL_SIMPLE(NULL);
return 1;
}
static cell_t TeleportEntity(IPluginContext *pContext, const cell_t *params)
{
static ValveCall *pCall = NULL;
if (!pCall)
{
#if SOURCE_ENGINE >= SE_PORTAL2
constexpr size_t paramCount = 4;
#else
constexpr size_t paramCount = 3;
#endif
ValvePassInfo pass[paramCount];
InitPass(pass[0], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
InitPass(pass[1], Valve_QAngle, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
InitPass(pass[2], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
#if SOURCE_ENGINE >= SE_PORTAL2
InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
#endif
if (!CreateBaseCall("Teleport", ValveCall_Entity, NULL, pass, paramCount, &pCall))
{
return pContext->ThrowNativeError("\"Teleport\" not supported by this mod");
} else if (!pCall) {
return pContext->ThrowNativeError("\"Teleport\" wrapper failed to initialize");
}
}
START_CALL();
DECODE_VALVE_PARAM(1, thisinfo, 0);
DECODE_VALVE_PARAM(2, vparams, 0);
DECODE_VALVE_PARAM(3, vparams, 1);
DECODE_VALVE_PARAM(4, vparams, 2);
#if SOURCE_ENGINE >= SE_PORTAL2
*(bool *)(vptr + pCall->vparams[3].offset) = true;
#endif
FINISH_CALL_SIMPLE(NULL);
return 1;
}
#if SOURCE_ENGINE >= SE_ORANGEBOX
static cell_t ForcePlayerSuicide(IPluginContext *pContext, const cell_t *params)
{
static ValveCall *pCall = NULL;
if (!pCall)
{
ValvePassInfo pass[2];
InitPass(pass[0], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
if (!CreateBaseCall("CommitSuicide", ValveCall_Player, NULL, pass, 2, &pCall))
{
return pContext->ThrowNativeError("\"CommitSuicide\" not supported by this mod");
}
else if (!pCall)
{
return pContext->ThrowNativeError("\"CommitSuicide\" wrapper failed to initialize");
}
}
START_CALL();
DECODE_VALVE_PARAM(1, thisinfo, 0);
if (params[0] >= 2)
{
DECODE_VALVE_PARAM(2, vparams, 0);
}
else
{
*(bool *)(vptr + pCall->vparams[0].offset) = false;
}
*(bool *)(vptr + pCall->vparams[1].offset) = true;
FINISH_CALL_SIMPLE(NULL);
return 1;
}
#else
static cell_t ForcePlayerSuicide(IPluginContext *pContext, const cell_t *params)
{
static ValveCall *pCall = NULL;
if (!pCall)
{
if (!CreateBaseCall("CommitSuicide", ValveCall_Player, NULL, NULL, 0, &pCall))
{
return pContext->ThrowNativeError("\"CommitSuicide\" not supported by this mod");
} else if (!pCall) {
return pContext->ThrowNativeError("\"CommitSuicide\" wrapper failed to initialize");
}
}
START_CALL();
DECODE_VALVE_PARAM(1, thisinfo, 0);
FINISH_CALL_SIMPLE(NULL);
return 1;
}
#endif
static cell_t SetClientViewEntity(IPluginContext *pContext, const cell_t *params)
{
IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
if (player == NULL)
{
return pContext->ThrowNativeError("Invalid client index %d", params[1]);
}
if (!player->IsInGame())
{
return pContext->ThrowNativeError("Client %d is not in game", params[1]);
}
edict_t *pEdict = PEntityOfEntIndex(gamehelpers->ReferenceToIndex(params[2]));
if (!pEdict || pEdict->IsFree())
{
return pContext->ThrowNativeError("Entity %d is not valid", params[2]);
}
engine->SetView(player->GetEdict(), pEdict);
return 1;
}
static SourceHook::String *g_lightstyle[MAX_LIGHTSTYLES] = {NULL};
static cell_t SetLightStyle(IPluginContext *pContext, const cell_t *params)
{
int style = params[1];
if (style >= MAX_LIGHTSTYLES)
{
return pContext->ThrowNativeError("Light style %d is invalid (range: 0-%d)", style, MAX_LIGHTSTYLES - 1);
}
if (g_lightstyle[style] == NULL)
{
/* We allocate and never free this because the Engine wants to hold onto it :\
* in theory we could hook light style and know whether we're supposed to free
* this or not on shutdown, but for ~4K of memory MAX, it doesn't seem worth it yet.
* So, it's a :TODO:!
*/
g_lightstyle[style] = new SourceHook::String();
}
char *str;
pContext->LocalToString(params[2], &str);
g_lightstyle[style]->assign(str);
engine->LightStyle(style, g_lightstyle[style]->c_str());
return 1;
}
static cell_t SlapPlayer(IPluginContext *pContext, const cell_t *params)
{
static bool s_slap_supported = false;
static bool s_slap_setup = false;
static ICallWrapper *s_teleport = NULL;
static int s_health_offs = 0;
static int s_sound_count = 0;
static int s_frag_offs = 0;
if (!s_slap_setup)
{
int tries = 0;
s_slap_setup = true;
if (IsTeleportSupported())
{
tries++;
}
if (IsGetVelocitySupported())
{
tries++;
}
/* Setup health */
if (g_pGameConf->GetOffset("m_iHealth", &s_health_offs) && s_health_offs)
{
tries++;
}
if (tries == 3)
{
s_slap_supported = true;
const char *key;
if ((key = g_pGameConf->GetKeyValue("SlapSoundCount")) != NULL)
{
s_sound_count = atoi(key);
}
}
}
if (!s_slap_supported)
{
return pContext->ThrowNativeError("This function is not supported on this mod");
}
/* First check if the client is valid */
IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
if (!player)
{
return pContext->ThrowNativeError("Client %d is not valid", params[1]);
} else if (!player->IsInGame()) {
return pContext->ThrowNativeError("Client %d is not in game", params[1]);
}
edict_t *pEdict = player->GetEdict();
CBaseEntity *pEntity = pEdict->GetUnknown()->GetBaseEntity();
/* See if we should be taking away health */
bool should_slay = false;
if (params[2])
{
#if SOURCE_ENGINE != SE_DARKMESSIAH
int *health = (int *)((char *)pEntity + s_health_offs);
if (*health - params[2] <= 0)
{
*health = 1;
should_slay = true;
} else {
*health -= params[2];
}
#else
float *health = (float *)((char *)pEntity + s_health_offs);
if (*health - (float)params[2] <= 0)
{
*health = 1.0f;
should_slay = true;
} else {
*health -= (float)params[2];
}
#endif
}
/* Teleport in a random direction - thank you, Mani!*/
Vector velocity;
GetVelocity(pEntity, &velocity, NULL);
velocity.x += ((rand() % 180) + 50) * (((rand() % 2) == 1) ? -1 : 1);
velocity.y += ((rand() % 180) + 50) * (((rand() % 2) == 1) ? -1 : 1);
velocity.z += rand() % 200 + 100;
Teleport(pEntity, NULL, NULL, &velocity);
/* Play a random sound */
if (params[3] && s_sound_count > 0)
{
char name[48];
const char *sound_name;
cell_t player_list[SM_MAXPLAYERS], total_players = 0;
int maxClients = playerhelpers->GetMaxClients();
int r = (rand() % s_sound_count) + 1;
ke::SafeSprintf(name, sizeof(name), "SlapSound%d", r);
if ((sound_name = g_pGameConf->GetKeyValue(name)) != NULL)
{
IGamePlayer *other;
for (int i=1; i<=maxClients; i++)
{
other = playerhelpers->GetGamePlayer(i);
if (other->IsInGame())
{
player_list[total_players++] = i;
}
}
const Vector & pos = pEdict->GetCollideable()->GetCollisionOrigin();
CellRecipientFilter rf;
rf.SetToReliable(true);
rf.Initialize(player_list, total_players);
#if SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_HL2DM || SOURCE_ENGINE == SE_DODS || SOURCE_ENGINE == SE_SDK2013 \
|| SOURCE_ENGINE == SE_BMS || SOURCE_ENGINE == SE_TF2 || SOURCE_ENGINE == SE_PVKII
engsound->EmitSound(rf, params[1], CHAN_AUTO, sound_name, VOL_NORM, ATTN_NORM, 0, PITCH_NORM, 0, &pos);
#elif SOURCE_ENGINE < SE_PORTAL2
engsound->EmitSound(rf, params[1], CHAN_AUTO, sound_name, VOL_NORM, ATTN_NORM, 0, PITCH_NORM, &pos);
#else
engsound->EmitSound(rf, params[1], CHAN_AUTO, sound_name, -1, sound_name, VOL_NORM, ATTN_NORM, 0, 0, PITCH_NORM, &pos);
#endif
}
}
if (!s_frag_offs)
{
const char *frag_prop = g_pGameConf->GetKeyValue("m_iFrags");
if (frag_prop)
{
datamap_t *pMap = gamehelpers->GetDataMap(pEntity);
sm_datatable_info_t info;
if (gamehelpers->FindDataMapInfo(pMap, frag_prop, &info))
{
s_frag_offs = info.actual_offset;
}
}
if (!s_frag_offs)
{
s_frag_offs = -1;
}
}
int old_frags = 0;
if (s_frag_offs > 0)
{
old_frags = *(int *)((char *)pEntity + s_frag_offs);
}
/* Force suicide */
if (should_slay)
{
pluginhelpers->ClientCommand(pEdict, "kill\n");
}
if (s_frag_offs > 0)
{
*(int *)((char *)pEntity + s_frag_offs) = old_frags;
}
return 1;
}
static cell_t GetClientEyePosition(IPluginContext *pContext, const cell_t *params)
{
IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
if (player == NULL)
{
return pContext->ThrowNativeError("Invalid client index %d", params[1]);
}
if (!player->IsInGame())
{
return pContext->ThrowNativeError("Client %d is not in game", params[1]);
}
Vector pos;
serverClients->ClientEarPosition(player->GetEdict(), &pos);
cell_t *addr;
pContext->LocalToPhysAddr(params[2], &addr);
addr[0] = sp_ftoc(pos.x);
addr[1] = sp_ftoc(pos.y);
addr[2] = sp_ftoc(pos.z);
return 1;
}
static cell_t GetClientEyeAngles(IPluginContext *pContext, const cell_t *params)
{
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(params[1]);
if (!pPlayer)
{
return pContext->ThrowNativeError("Invalid client index %d", params[1]);
}
else if (!pPlayer->IsInGame())
{
return pContext->ThrowNativeError("Client %d is not in game", params[1]);
}
edict_t *pEdict = pPlayer->GetEdict();
CBaseEntity *pEntity = pEdict->GetUnknown() ? pEdict->GetUnknown()->GetBaseEntity() : NULL;
/* We always set the angles for backwards compatibility --
* The original function had no return value.
*/
QAngle angles;
bool got_angles = false;
if (pEntity != NULL)
{
got_angles = GetEyeAngles(pEntity, &angles);
}
cell_t *addr;
pContext->LocalToPhysAddr(params[2], &addr);
addr[0] = sp_ftoc(angles.x);
addr[1] = sp_ftoc(angles.y);
addr[2] = sp_ftoc(angles.z);
return got_angles ? 1 : 0;
}
#if SOURCE_ENGINE >= SE_ORANGEBOX
static cell_t NativeFindEntityByClassname(IPluginContext *pContext, const cell_t *params)
{
char *searchname;
CBaseEntity *pEntity;
if (params[1] == -1)
{
pEntity = (CBaseEntity *)servertools->FirstEntity();
}
else
{
pEntity = gamehelpers->ReferenceToEntity(params[1]);
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d (%d) is invalid",
gamehelpers->ReferenceToIndex(params[1]),
params[1]);
}
pEntity = (CBaseEntity *)servertools->NextEntity(pEntity);
}
// it's tough to find a good ent these days
if (!pEntity)
{
return -1;
}
pContext->LocalToString(params[2], &searchname);
const char *classname;
int lastletterpos;
static int offset = -1;
if (offset == -1)
{
sm_datatable_info_t info;
if (!gamehelpers->FindDataMapInfo(gamehelpers->GetDataMap(pEntity), "m_iClassname", &info))
{
return -1;
}
offset = info.actual_offset;
}
string_t s;
while (pEntity)
{
if ((s = *(string_t *)((uint8_t *)pEntity + offset)) == NULL_STRING)
{
pEntity = (CBaseEntity *)servertools->NextEntity(pEntity);
continue;
}
classname = STRING(s);
lastletterpos = strlen(searchname) - 1;
if (searchname[lastletterpos] == '*')
{
if (strncasecmp(searchname, classname, lastletterpos) == 0)
{
return gamehelpers->EntityToBCompatRef(pEntity);
}
}
else if (strcasecmp(searchname, classname) == 0)
{
return gamehelpers->EntityToBCompatRef(pEntity);
}
pEntity = (CBaseEntity *)servertools->NextEntity(pEntity);
}
return -1;
}
#endif // >= ORANGEBOX && != TF2
static cell_t FindEntityByClassname(IPluginContext *pContext, const cell_t *params)
{
#if SOURCE_ENGINE == SE_TF2 \
|| SOURCE_ENGINE == SE_DODS \
|| SOURCE_ENGINE == SE_HL2DM \
|| SOURCE_ENGINE == SE_CSS \
|| SOURCE_ENGINE == SE_BMS \
|| SOURCE_ENGINE == SE_SDK2013 \
|| SOURCE_ENGINE == SE_BLADE \
|| SOURCE_ENGINE == SE_NUCLEARDAWN \
|| SOURCE_ENGINE == SE_PVKII
static bool bHasServerTools3 = !!g_SMAPI->GetServerFactory(false)("VSERVERTOOLS003", nullptr);
if (bHasServerTools3)
{
CBaseEntity *pStartEnt = NULL;
if (params[1] != -1)
{
pStartEnt = gamehelpers->ReferenceToEntity(params[1]);
if (!pStartEnt)
{
return pContext->ThrowNativeError("Entity %d (%d) is invalid",
gamehelpers->ReferenceToIndex(params[1]),
params[1]);
}
}
char *searchname;
pContext->LocalToString(params[2], &searchname);
CBaseEntity *pEntity = servertools->FindEntityByClassname(pStartEnt, searchname);
return gamehelpers->EntityToBCompatRef(pEntity);
}
#endif
static ValveCall *pCall = NULL;
static bool bProbablyNoFEBC = false;
#if SOURCE_ENGINE >= SE_ORANGEBOX
if (bProbablyNoFEBC)
{
return NativeFindEntityByClassname(pContext, params);
}
#endif // >= SE_ORANGEBOX
if (!pCall)
{
ValvePassInfo pass[3];
InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL|VDECODE_FLAG_ALLOWWORLD);
InitPass(pass[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);