-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextension.cpp
More file actions
1782 lines (1416 loc) · 54.3 KB
/
Copy pathextension.cpp
File metadata and controls
1782 lines (1416 loc) · 54.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
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 Sample Extension
* Copyright (C) 2004-2008 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 "extension.h"
/**
* @file extension.cpp
* @brief Implement extension code here.
*/
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <map>
#include <safetyhook.hpp>
#include <sm_namehashset.h>
#include <ISDKHooks.h>
#include <tier1/bitbuf.h>
#include <eiface.h>
#include <iserverunknown.h>
#include <iservernetworkable.h>
#include <server_class.h>
#include <dt_common.h>
#include <dt_send.h>
#include <edict.h>
SendVarEdit g_SendVarEdit; /**< Global singleton for extension's main interface */
SMEXT_LINK(&g_SendVarEdit);
enum EditAction
{
SET,
REPLACE,
OMIT,
};
class Variant
{
public:
DVariant variant;
const std::string string;
Variant(DVariant variant)
: variant(variant), string()
{
}
Variant(const char* string)
: string(string)
{
this->variant.m_Type = DPT_String;
this->variant.m_pString=this->string.c_str();
}
};
using SharedVariant = std::shared_ptr<Variant>;
struct Edit
{
EditAction action;
bool all_clients;
int propindex;
const SendProp* prop;
const SharedVariant variant;
};
using SharedEdit = std::shared_ptr<Edit>;
class EditEntry
{
public:
std::vector<SharedEdit> edits{};
bool has_setter = false;
uint8_t client;
uint16_t entity;
public:
EditEntry(uint8_t client, uint16_t entity)
: client(client), entity(entity)
{
edits.reserve(8);
}
};
class EntryList : public IClientListener, public ISMEntityListener
{
private:
class ClientEntryList
{
public:
std::vector<EditEntry> entries{};
};
class EntityEntryLookup
{
public:
uint16_t entity;
uint8_t num_clients = 0;
uint16_t indices[256];
public:
EntityEntryLookup(uint16_t entity)
: entity(entity)
{
std::fill(std::begin(indices), std::end(indices), 0xFF);
}
};
private:
ClientEntryList lists[256]{};
std::vector<EntityEntryLookup> lookups{};
int indices[MAX_EDICTS];
public:
virtual void OnClientDisconnecting(int client) override
{
Clear(client);
}
virtual void OnEntityDestroyed(CBaseEntity* entity) override
{
int index = gamehelpers->ReferenceToIndex(gamehelpers->EntityToReference(entity));
if (!( 0 < index && index < MAX_EDICTS ))
return;
if (indices[index] == -1)
return;
EntityEntryLookup &lookup = lookups[indices[index]];
lookup = std::move(lookups.back());
lookups.pop_back();
indices[lookup.entity] = indices[index];
indices[index] = -1;
}
private:
void RemoveLookup(uint8_t client, uint16_t entity)
{
if (indices[entity] == -1)
return;
EntityEntryLookup &lookup = lookups[indices[entity]];
if (lookup.indices[client] == 0xFF)
return;
lookup.indices[client] = 0xFF;
lookup.num_clients--;
if (lookup.num_clients > 0)
return;
lookup = std::move(lookups.back());
lookups.pop_back();
indices[lookup.entity] = indices[entity];
indices[entity] = -1;
}
public:
EntryList()
{
std::fill(std::begin(indices), std::end(indices), -1);
}
EditEntry* Get(uint8_t client, uint16_t entity)
{
if (indices[entity] == -1)
return nullptr;
EntityEntryLookup* lookup = &lookups[indices[entity]];
if (lookup->indices[client] == 0xFF)
return nullptr;
EditEntry* entry = &lists[client].entries[lookup->indices[client]];
return entry;
}
EditEntry* AddOrModify(uint8_t client, uint16_t entity)
{
EntityEntryLookup* lookup;
if (indices[entity] == -1) {
lookup = &lookups.emplace_back(entity);
indices[entity] = lookups.size() - 1;
}
else {
lookup = &lookups[indices[entity]];
}
EditEntry* entry;
if (lookup->indices[client] == 0xFF) {
entry = &lists[client].entries.emplace_back(client, entity);
lookup->indices[client] = lists[client].entries.size() - 1;
lookup->num_clients++;
}
else {
entry = &lists[client].entries[lookup->indices[client]];
}
return entry;
}
void Clear(uint8_t client)
{
ClientEntryList &list = lists[client];
for (const EditEntry &entry : list.entries)
RemoveLookup(client, entry.entity);
list.entries.clear();
}
};
EntryList g_entrylist;
struct RegisterInfo { const char* name; uint8_t offset; };
#define REG_INFO(NAME) { #NAME, (uint8_t)offsetof(safetyhook::Context, NAME) }
RegisterInfo g_registers[] {
#if SAFETYHOOK_ARCH_X86_64
REG_INFO(xmm0),
REG_INFO(xmm1),
REG_INFO(xmm2),
REG_INFO(xmm3),
REG_INFO(xmm4),
REG_INFO(xmm5),
REG_INFO(xmm6),
REG_INFO(xmm7),
REG_INFO(xmm8),
REG_INFO(xmm9),
REG_INFO(xmm10),
REG_INFO(xmm11),
REG_INFO(xmm12),
REG_INFO(xmm13),
REG_INFO(xmm14),
REG_INFO(xmm15),
REG_INFO(rflags),
REG_INFO(r15),
REG_INFO(r14),
REG_INFO(r13),
REG_INFO(r12),
REG_INFO(r11),
REG_INFO(r10),
REG_INFO(r9),
REG_INFO(r8),
REG_INFO(rdi),
REG_INFO(rsi),
REG_INFO(rdx),
REG_INFO(rcx),
REG_INFO(rbx),
REG_INFO(rax),
REG_INFO(rbp),
REG_INFO(rsp),
REG_INFO(trampoline_rsp),
REG_INFO(rip),
#else
REG_INFO(xmm0),
REG_INFO(xmm1),
REG_INFO(xmm2),
REG_INFO(xmm3),
REG_INFO(xmm4),
REG_INFO(xmm5),
REG_INFO(xmm6),
REG_INFO(xmm7),
REG_INFO(eflags),
REG_INFO(edi),
REG_INFO(esi),
REG_INFO(edx),
REG_INFO(ecx),
REG_INFO(ebx),
REG_INFO(eax),
REG_INFO(ebp),
REG_INFO(esp),
REG_INFO(trampoline_esp),
REG_INFO(eip),
#endif
};
const RegisterInfo* FindRegister(const char* name)
{
auto register_propindex = std::find_if(
std::begin(g_registers),
std::end(g_registers),
[name](const RegisterInfo &info) { return strcmp(info.name, name) == 0; }
);
return register_propindex != std::end(g_registers) ? register_propindex : nullptr;
}
struct VariableInfo
{
bool is_register;
bool is_read;
};
template <typename T>
struct Variable
{
VariableInfo info;
int32_t offset;
int32_t read;
};
class Context
{
public:
safetyhook::Context ®isters;
public:
Context(safetyhook::Context &context)
: registers(context)
{}
template<typename T>
static inline T& read(void* address, int32_t offset)
{
return *(T*)((uintptr_t)address + offset);
}
template<typename T>
inline T& reg(size_t reg) { return read<T>(®isters, reg); }
template<typename T>
inline T& stack(int32_t offset)
{
#if SAFETYHOOK_ARCH_X86_64
return read<T>(registers.rbp, offset);
#else
return read<T>(registers.ebp, offset);
#endif
}
inline uintptr_t bp()
{
#if SAFETYHOOK_ARCH_X86_64
return registers.rbp;
#else
return registers.ebp;
#endif
}
inline void*& ip()
{
#if SAFETYHOOK_ARCH_X86_64
return *(void**)®isters.rip;
#else
return *(void**)®isters.eip;
#endif
}
inline uintptr_t& flags()
{
#if SAFETYHOOK_ARCH_X86_64
return registers.rflags;
#else
return registers.eflags;
#endif
}
template<typename T>
inline T& var(Variable<T> &var)
{
void* base = var.info.is_register ? ®isters : (void*)bp();
T &value = read<T>(base, var.offset);
if (var.info.is_read)
value = read<T>((void*)&value, var.offset);
return value;
}
};
#if SAFETYHOOK_ARCH_X86_64
#if defined WIN32
#define _PLATFORM "windows64"
#elif defined _LINUX
#define _PLATFORM "linux64"
#elif defined _OSX
#define _PLATFORM "mac64"
#endif
#else
#if defined WIN32
#define _PLATFORM "windows"
#elif defined _LINUX
#define _PLATFORM "linux"
#elif defined _OSX
#define _PLATFORM "mac"
#endif
#endif
class VariableReader : public ITextListener_SMC
{
private:
int ignore_level;
std::pair<const std::string, Variable<void>>* current;
int current_level;
bool current_assigned;
bool current_read;
private:
std::map<std::string, Variable<void>, std::less<>> variables;
private:
bool IsPlatform(const char* string, bool* current_platform, bool* other_platform)
{
*current_platform = strcmp(string, _PLATFORM) == 0;
*other_platform = !*current_platform;
if (
strcmp(string, "linux") != 0 && strcmp(string, "linux64") != 0 &&
strcmp(string, "windows") != 0 && strcmp(string, "windows64") != 0 &&
strcmp(string, "mac") != 0 && strcmp(string, "mac64") != 0
) {
*other_platform = false;
}
return *current_platform || *other_platform;
}
bool ParseInt(const char* string, int32_t* value)
{
char* end = nullptr;
intptr_t parsed = std::strtoll(string, &end, 0);
if (errno == ERANGE || !(INT32_MIN <= parsed && parsed <= INT32_MAX) || string + strlen(string) != end)
return false;
*value = static_cast<int32_t>(parsed);
return true;
}
bool ParseRead(const char* string, Variable<void>* variable)
{
if (!ParseInt(string, &variable->read))
return false;
variable->info.is_read = true;
return true;
}
bool ParseStack(const char* string, Variable<void>* variable)
{
if (!ParseInt(string, &variable->offset))
return false;
variable->info.is_register = false;
return true;
}
bool ParseRegister(const char* string, Variable<void>* variable)
{
const RegisterInfo* reg = FindRegister(string);
if (!reg)
return false;
variable->info.is_register = true;
variable->offset = reg->offset;
return true;
}
public:
virtual void ReadSMC_ParseStart() override
{
ignore_level = 0;
current = nullptr;
current_level = 0;
current_assigned = false;
current_read = false;
};
virtual void ReadSMC_ParseEnd(bool halted, bool failed) override
{
if ((halted || failed) && current)
variables.erase(current->first);
ignore_level = 0;
current = nullptr;
current_level = 0;
current_assigned = false;
current_read = false;
}
virtual SMCResult ReadSMC_NewSection(const SMCStates* states, const char* name) override
{
bool current_platform, other_platform;
IsPlatform(name, ¤t_platform, &other_platform);
// skip over other platforms
if (ignore_level || other_platform) {
ignore_level++;
return SMCResult_Continue;
}
if (current)
current_level++;
// go into current platform
if (current_platform)
return SMCResult_Continue;
// can't start a new variable when one is already gettig parsed
if (current)
return SMCResult_HaltFail;
// start new variable
auto assignment = variables.insert_or_assign(name, Variable<void>{});
current = &*assignment.first;
current_level++;
return SMCResult_Continue;
}
virtual SMCResult ReadSMC_LeavingSection(const SMCStates* states) override
{
// inside skipped platform
if (ignore_level) {
ignore_level--;
return SMCResult_Continue;
}
if (current)
current_level--;
// continue if there is no current variable or is still parsing one
if (!current || current_level)
return SMCResult_Continue;
// finished parsing a variable
// fail if variable wasn't assigned a register or stack offset
if (!current_assigned)
return SMCResult_HaltFail;
current = nullptr;
current_assigned = false;
current_read = false;
return SMCResult_Continue;
}
virtual SMCResult ReadSMC_KeyValue(const SMCStates* states, const char* key, const char* value) override
{
// inside skipped platform
if (ignore_level)
return SMCResult_Continue;
// parse read offset
if (strcmp(key, "read") == 0) {
if (current_read)
return SMCResult_HaltFail;
if (!ParseRead(value, ¤t->second))
return SMCResult_HaltFail;
current_read = true;
return SMCResult_Continue;
}
// check for a platform specifier key
bool current_platform, other_platform;
if (value && IsPlatform(key, ¤t_platform, &other_platform)) {
if (other_platform)
return SMCResult_Continue;
// stack and register are parsed with key variable
key = value;
value = nullptr;
}
else {
return SMCResult_HaltFail;
}
if (current_assigned)
return SMCResult_HaltFail;
// try to parse stack offset
if (ParseStack(key, ¤t->second)) {
current_assigned = true;
return SMCResult_Continue;
}
// try to parse register name
if (ParseRegister(key, ¤t->second)) {
current_assigned = true;
return SMCResult_Continue;
}
return SMCResult_HaltFail;
}
public:
template<typename T>
bool GetVariable(const char* name, Variable<T>* variable)
{
auto search = variables.find(name);
if (search == variables.end())
return false;
*variable = *(Variable<T>*)&search->second;
return true;
}
std::map<std::string, Variable<void>, std::less<>>::const_iterator begin() const { return variables.begin(); }
std::map<std::string, Variable<void>, std::less<>>::const_iterator end() const { return variables.end(); }
};
VariableReader g_variables;
struct PackedEntity;
struct CEntityWriteInfo;
struct Struct_CEntityWriteInfo
{
int entity; // int
int output; // bf_write*
int client; // int
int oldpack; // PackedEntity*
int newpack; // PackedEntity*
};
struct Variables_SV_DetermineUpdateType
{
Variable<CEntityWriteInfo*> entitywriteinfo;
Variable<int> propcount;
};
struct Points_SV_DetermineUpdateType
{
void* props_changed_call;
void* propcount_positive_block;
};
struct Variables_SendTable_WritePropList
{
Variable<SendTable*> table;
Variable<bf_write*> output;
Variable<int> propindex;
Variable<int> output_lastpropindex;
};
struct Points_SendTable_WritePropList
{
void* loop_continue;
};
struct PropTypeFns
{
void (*Encode)(const unsigned char* object, const DVariant* variant, const SendProp* prop, bf_write* buffer, int entity);
void* other[8];
};
struct Struct_CSendTablePrecalc
{
int props; // SendProp**
int propcount; // int
};
struct GameInfo
{
Struct_CEntityWriteInfo struct_EWI;
Variables_SV_DetermineUpdateType vars_DUT;
Points_SV_DetermineUpdateType points_DUT;
Variables_SendTable_WritePropList vars_WPL;
Points_SendTable_WritePropList points_WPL;
PropTypeFns* proptypefns;
Struct_CSendTablePrecalc struct_STP;
};
GameInfo g_gameinfo;
bool GetEntityInfo(IPluginContext* pContext, int entref, int* index, edict_t** edict, CBaseEntity** entity, IServerUnknown** unknown, IServerNetworkable** networkable)
{
*index = gamehelpers->ReferenceToIndex(entref);
*edict = gamehelpers->EdictOfIndex(*index);
if (!(*edict))
return pContext->ThrowNativeError("Entity %d (%d) is invalid.", *index, entref);
*entity = gamehelpers->ReferenceToEntity(entref);
*unknown = (IServerUnknown*)(*entity);
*networkable = (*unknown)->GetNetworkable();
if (!(*networkable))
return pContext->ThrowNativeError("Edict %d (%d) is not networkable", *index, entref);
return true;
}
// float SendProxyAngle(float angle)
cell_t smn_SendProxyAngle(IPluginContext* pContext, const cell_t* params)
{
return sp_ftoc(anglemod(params[1]));
}
// void SendProxyQAnglesNative(const float qangles[3], float vec[3])
cell_t smn_SendProxyQAngles(IPluginContext* pContext, const cell_t* params)
{
cell_t* sp_qangles;
pContext->LocalToPhysAddr(params[1], &sp_qangles);
cell_t* sp_vec;
pContext->LocalToPhysAddr(params[2], &sp_vec);
for (int i = 0; i < 3; i++)
sp_vec[i] = sp_ftoc(anglemod(sp_ctof(sp_qangles[i])));
return 0;
}
// int SendProxyEHandle(int entity)
cell_t smn_SendProxyEHandle(IPluginContext* pContext, const cell_t* params)
{
int entref = params[1];
if (entref == -1)
return INVALID_NETWORKED_EHANDLE_VALUE;
int index;
edict_t* edict;
CBaseEntity* entity;
IServerUnknown* unknown;
IServerNetworkable* networkable;
if (!GetEntityInfo(pContext, entref, &index, &edict, &entity, &unknown, &networkable))
return INVALID_NETWORKED_EHANDLE_VALUE;
const CBaseHandle* handle = &unknown->GetRefEHandle();
// doing what SendProxy_EHandleToInt is doing
int serial = handle->GetSerialNumber() & ((1 << NUM_NETWORKED_EHANDLE_SERIAL_NUMBER_BITS) - 1);
return handle->GetEntryIndex() | (serial << MAX_EDICT_BITS);
}
HandleType_t g_SendVarType = 0;
class SendVarTypeHandler : public IHandleTypeDispatch
{
public:
virtual void OnHandleDestroy(HandleType_t type, void* object) override
{
SharedVariant* variant = (SharedVariant*)object;
delete variant;
}
};
SendVarTypeHandler g_SendVarTypeHandler;
std::vector<Handle_t> g_editdatahandles;
Handle_t CreateSendVarHandle(IPluginContext* pContext, Variant &&variant)
{
SharedVariant shared = std::make_shared<Variant>(std::move(variant));
Handle_t handle = handlesys->CreateHandle(
g_SendVarType,
(void*)new SharedVariant(shared),
pContext->GetIdentity(),
myself->GetIdentity(),
nullptr
);
g_editdatahandles.push_back(handle);
return handle;
}
const SharedVariant* ReadSendVarHandle(IPluginContext* pContext, Handle_t handle)
{
const SharedVariant* variant = nullptr;
HandleSecurity security(nullptr, myself->GetIdentity());
HandleError error = handlesys->ReadHandle(handle, g_SendVarType, &security, (void**)&variant);
if (error != HandleError_None) {
pContext->ThrowNativeError("Invalid sendvar data handle %x (error %d)", handle, error);
return nullptr;
}
return variant;
}
// Handle SendVarInt(any value)
cell_t smn_SendVarInt(IPluginContext* pContext, const cell_t* params)
{
int value = params[1];
DVariant var;
var.m_Type = DPT_Int;
var.m_Int = value;
return CreateSendVarHandle(pContext, Variant(var));
}
// SendVarFloat(float value)
cell_t smn_SendVarFloat(IPluginContext* pContext, const cell_t* params)
{
float value = params[1];
DVariant var;
var.m_Type = DPT_Float;
var.m_Float = value;
return CreateSendVarHandle(pContext, Variant(var));
}
// Handle SendVarVector(const float vec[3])
cell_t smn_SendVarVector(IPluginContext* pContext, const cell_t* params)
{
cell_t* sp_vec;
pContext->LocalToPhysAddr(params[1], &sp_vec);
Vector vec;
for (int i = 0; i < 3; i++)
vec[i] = sp_ctof(sp_vec[i]);
DVariant var;
var.m_Type = DPT_Vector;
for (int i = 0; i < 3; i++)
var.m_Vector[i] = vec[i];
return CreateSendVarHandle(pContext, Variant(var));
}
// Handle SendVarVectorXY(const float vec[2])
cell_t smn_SendVarVectorXY(IPluginContext* pContext, const cell_t* params)
{
cell_t* sp_vec;
pContext->LocalToPhysAddr(params[1], &sp_vec);
Vector vec;
for (int i = 0; i < 2; i++)
vec[i] = sp_ctof(sp_vec[i]);
DVariant var;
var.m_Type = DPT_VectorXY;
for (int i = 0; i < 2; i++)
var.m_Vector[i] = vec[i];
return CreateSendVarHandle(pContext, Variant(var));
}
// Handle SendVarString(const char[] string)
cell_t smn_SendVarString(IPluginContext* pContext, const cell_t* params)
{
char* sp_string;
pContext->LocalToString(params[1], &sp_string);
return CreateSendVarHandle(pContext, Variant(sp_string));
}
struct SendPropInfo
{
const SendProp* prop;
int propindex;
};
// basically DataTableInfo from HalfLife2.h
struct SendTableInfo
{
struct SendPropPolicy
{
static inline bool matches(const char* name, const SendPropInfo &info)
{
return strcmp(name, info.prop->GetName()) == 0;
}
static inline uint32_t hash(const detail::CharsAndLength &key)
{
return key.hash();
}
};
static inline bool matches(const char* name, const SendTableInfo* info)
{
return strcmp(name, info->serverclass->GetName()) == 0;
}
static inline uint32_t hash(const detail::CharsAndLength &key)
{
return key.hash();
}
SendTableInfo(ServerClass* serverclass) : serverclass(serverclass) {}
ServerClass* serverclass;
NameHashSet<SendPropInfo, SendPropPolicy> lookup;
};
NameHashSet<SendTableInfo*> g_classes;
bool FindClassSendPropInfo(ServerClass* serverclass, const char* propname, SendPropInfo* info)
{
SendTableInfo* sendtableinfo;
if (!g_classes.retrieve(serverclass->GetName(), &sendtableinfo)) {
sendtableinfo = new SendTableInfo(serverclass);
g_classes.insert(serverclass->GetName(), sendtableinfo);
}
if (!sendtableinfo->lookup.retrieve(propname, info)) {
CSendTablePrecalc* precalc = serverclass->m_pTable->m_pPrecalc;
int propcount = Context::read<int>(precalc, g_gameinfo.struct_STP.propcount);
SendProp** props = Context::read<SendProp**>(precalc, g_gameinfo.struct_STP.props);
SendProp** prop = std::find_if(props, props + propcount, [propname](SendProp* prop) { return strcmp(prop->GetName(), propname) == 0; });
int propindex = prop - props;
if (propindex == propcount)
return false;
*info = SendPropInfo { *prop, propindex };
sendtableinfo->lookup.insert(propname, *info);
}
return true;
}
// void HasNetworkableProp(int entity, const char[] prop)
cell_t smn_HasNetworkableProp(IPluginContext* pContext, const cell_t* params)
{
int entref = params[1];
char* propname;
pContext->LocalToString(params[2], &propname);
int index;
edict_t* edict;
CBaseEntity* entity;
IServerUnknown* unknown;
IServerNetworkable* networkable;
if (!GetEntityInfo(pContext, entref, &index, &edict, &entity, &unknown, &networkable))
return false;
SendPropInfo info;
return FindClassSendPropInfo(networkable->GetServerClass(), propname, &info);
}
bool FindEntitySendPropInfo(IPluginContext* pContext, int entref, const char* propname, SendPropInfo* info)
{
int index;
edict_t* edict;
CBaseEntity* entity;
IServerUnknown* unknown;
IServerNetworkable* networkable;
if (!GetEntityInfo(pContext, entref, &index, &edict, &entity, &unknown, &networkable))
return false;
if (!FindClassSendPropInfo(networkable->GetServerClass(), propname, info)) {
const char* classname = gamehelpers->GetEntityClassname(edict);
return pContext->ThrowNativeError(
"Networkable property \"%s\" not found (entity %d/%s)",
propname,
entref,
((classname) ? classname : "")
);
}
return true;
}