-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathextension.cpp
2398 lines (2029 loc) · 65.4 KB
/
extension.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* vim: set ts=4 :
* =============================================================================
* 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 <algorithm>
#include "extension.h"
#include <dt_send.h>
#include <unordered_map>
#include <vector>
#include <mathlib/vector.h>
#include <iserverentity.h>
#include <iservernetworkable.h>
#include <server_class.h>
#include <ehandle.h>
#include <predictioncopy.h>
#include <utility>
#include <CDetour/detours.h>
#include <memory>
#include "packed_entity.h"
#include <iclient.h>
#include <igameevents.h>
#include <cstdlib>
#include <mutex>
#include <thread>
#include <pthread.h>
#include <ISDKTools.h>
/**
* @file extension.cpp
* @brief Implement extension code here.
*/
static Sample g_Sample; /**< Global singleton for extension's main interface */
SMEXT_LINK(&g_Sample);
static IGameConfig *gameconf{nullptr};
static ISDKHooks *g_pSDKHooks{nullptr};
ISDKTools *g_pSDKTools = nullptr;
IServer *server = nullptr;
CBaseEntityList *g_pEntityList = nullptr;
static void *CGameClient_GetSendFrame_ptr{nullptr};
static int CBaseClient_UpdateSendState_idx{-1};
static int CBaseClient_SendSnapshot_idx{-1};
template <typename T>
int vfunc_index(T func)
{
SourceHook::MemFuncInfo info{};
SourceHook::GetFuncInfo<T>(func, info);
return info.vtblindex;
}
template <typename R, typename T, typename ...Args>
R call_mfunc(T *pThisPtr, void *offset, Args ...args)
{
class VEmptyClass {};
void **this_ptr = *reinterpret_cast<void ***>(&pThisPtr);
union
{
R (VEmptyClass::*mfpnew)(Args...);
#ifndef PLATFORM_POSIX
void *addr;
} u;
u.addr = offset;
#else
struct
{
void *addr;
intptr_t adjustor;
} s;
} u;
u.s.addr = offset;
u.s.adjustor = 0;
#endif
return (R)(reinterpret_cast<VEmptyClass *>(this_ptr)->*u.mfpnew)(args...);
}
template <typename R, typename T, typename ...Args>
R call_vfunc(T *pThisPtr, size_t offset, Args ...args)
{
void **vtable = *reinterpret_cast<void ***>(pThisPtr);
void *vfunc = vtable[offset];
return call_mfunc<R, T, Args...>(pThisPtr, vfunc, args...);
}
class CFrameSnapshot;
class CClientFrame;
class CBaseClient : public IGameEventListener2, public IClient, public IClientMessageHandler
{
};
class CGameClient : public CBaseClient
{
public:
inline void SendSnapshot(CClientFrame *pFrame)
{ call_vfunc<void>(this, CBaseClient_SendSnapshot_idx, pFrame); }
inline void UpdateSendState()
{ call_vfunc<void>(this, CBaseClient_UpdateSendState_idx); }
inline CClientFrame *GetSendFrame()
{ return call_mfunc<CClientFrame *>(this, CGameClient_GetSendFrame_ptr); }
};
class CBaseEntity : public IServerEntity
{
};
using prop_types = proxysend::prop_types;
static void global_send_proxy(const SendProp *pProp, const void *pStructBase, const void *pData, DVariant *pOut, int iElement, int objectID);
static const CStandardSendProxies *std_proxies;
#if SOURCE_ENGINE == SE_TF2
static const SendProp *m_nPlayerCond{nullptr};
static const SendProp *_condition_bits{nullptr};
static const SendProp *m_nPlayerCondEx{nullptr};
static const SendProp *m_nPlayerCondEx2{nullptr};
static const SendProp *m_nPlayerCondEx3{nullptr};
static const SendProp *m_nPlayerCondEx4{nullptr};
static bool is_prop_cond(const SendProp *pProp)
{
return (pProp == m_nPlayerCond ||
pProp == _condition_bits ||
pProp == m_nPlayerCondEx ||
pProp == m_nPlayerCondEx2 ||
pProp == m_nPlayerCondEx3 ||
pProp == m_nPlayerCondEx4);
}
#endif
struct proxyrestore_t final
{
inline proxyrestore_t(proxyrestore_t &&other) noexcept
{ operator=(std::move(other)); }
proxyrestore_t(SendProp *pProp_, prop_types type_) noexcept
: pProp{pProp_}, pRealProxy{pProp->GetProxyFn()}, type{type_}
{
#ifdef _DEBUG
printf("set %s proxy func\n", pProp->GetName());
#endif
pProp->SetProxyFn(global_send_proxy);
}
~proxyrestore_t() noexcept {
if(pProp && pRealProxy) {
#ifdef _DEBUG
printf("reset %s proxy func\n", pProp->GetName());
#endif
pProp->SetProxyFn(pRealProxy);
}
}
proxyrestore_t &operator=(proxyrestore_t &&other) noexcept
{
pProp = other.pProp;
other.pProp = nullptr;
pRealProxy = other.pRealProxy;
other.pRealProxy = nullptr;
type = other.type;
return *this;
}
SendProp *pProp{nullptr};
SendVarProxyFn pRealProxy{nullptr};
std::size_t ref{0};
prop_types type{prop_types::unknown};
private:
proxyrestore_t(const proxyrestore_t &) = delete;
proxyrestore_t &operator=(const proxyrestore_t &) = delete;
proxyrestore_t() = delete;
};
using restores_t = std::unordered_map<SendProp *, std::unique_ptr<proxyrestore_t>>;
static restores_t restores;
static SendVarProxyFn SendProxy_StringT_To_String_ptr{nullptr};
static SendVarProxyFn SendProxy_Color32ToInt_ptr{nullptr};
static SendVarProxyFn SendProxy_EHandleToInt_ptr{nullptr};
static prop_types guess_prop_type(const SendProp *pProp, const SendTable *pTable) noexcept
{
#if defined _DEBUG
printf("%s type is ", pProp->GetName());
#endif
SendVarProxyFn pRealProxy{pProp->GetProxyFn()};
if(pRealProxy == global_send_proxy) {
restores_t::const_iterator it_restore{restores.find(const_cast<SendProp *>(pProp))};
if(it_restore == restores.cend()) {
#if defined _DEBUG
printf("invalid (global send proxy)\n");
#endif
return prop_types::unknown;
}
#if defined _DEBUG
printf("from restore (global send proxy)\n");
#endif
return it_restore->second->type;
}
#if SOURCE_ENGINE == SE_TF2
if(is_prop_cond(pProp)) {
#if defined _DEBUG
printf("unsigned int (is cond)\n");
#endif
return prop_types::unsigned_int;
}
#endif
switch(pProp->GetType()) {
case DPT_Int: {
if(pProp->GetFlags() & SPROP_UNSIGNED) {
if(pRealProxy == std_proxies->m_UInt8ToInt32) {
if(pProp->m_nBits == 1) {
#if defined _DEBUG
printf("bool (bits == 1)\n");
#endif
return prop_types::bool_;
}
#if defined _DEBUG
printf("unsigned char (std proxy)\n");
#endif
return prop_types::unsigned_char;
} else if(pRealProxy == std_proxies->m_UInt16ToInt32) {
#if defined _DEBUG
printf("unsigned short (std proxy)\n");
#endif
return prop_types::unsigned_short;
} else if(pRealProxy == std_proxies->m_UInt32ToInt32) {
if(pTable && strcmp(pTable->GetName(), "DT_BaseEntity") == 0 && strcmp(pProp->GetName(), "m_clrRender") == 0) {
#if defined _DEBUG
printf("color32 (hardcode)\n");
#endif
return prop_types::color32_;
}
#if defined _DEBUG
printf("unsigned int (std proxy)\n");
#endif
return prop_types::unsigned_int;
} else {
if(SendProxy_Color32ToInt_ptr && pRealProxy == SendProxy_Color32ToInt_ptr) {
return prop_types::color32_;
} else if(SendProxy_EHandleToInt_ptr && pRealProxy == SendProxy_EHandleToInt_ptr) {
return prop_types::ehandle;
}
{
if(pProp->m_nBits == 32) {
struct dummy_t {
unsigned int val{256};
} dummy;
DVariant out{};
pRealProxy(pProp, static_cast<const void *>(&dummy), static_cast<const void *>(&dummy.val), &out, 0, -1);
if(out.m_Int == 65536) {
#if defined _DEBUG
printf("color32 (proxy)\n");
#endif
return prop_types::color32_;
}
}
}
{
if(pProp->m_nBits == NUM_NETWORKED_EHANDLE_BITS) {
struct dummy_t {
EHANDLE val{};
} dummy;
DVariant out{};
pRealProxy(pProp, static_cast<const void *>(&dummy), static_cast<const void *>(&dummy.val), &out, 0, -1);
if(out.m_Int == INVALID_NETWORKED_EHANDLE_VALUE) {
#if defined _DEBUG
printf("ehandle (proxy)\n");
#endif
return prop_types::ehandle;
}
}
}
#if defined _DEBUG
printf("unsigned int (flag)\n");
#endif
return prop_types::unsigned_int;
}
} else {
if(pRealProxy == std_proxies->m_Int8ToInt32) {
#if defined _DEBUG
printf("char (std proxy)\n");
#endif
return prop_types::char_;
} else if(pRealProxy == std_proxies->m_Int16ToInt32) {
#if defined _DEBUG
printf("short (std proxy)\n");
#endif
return prop_types::short_;
} else if(pRealProxy == std_proxies->m_Int32ToInt32) {
#if defined _DEBUG
printf("int (std proxy)\n");
#endif
return prop_types::int_;
} else {
{
struct dummy_t {
short val{SHRT_MAX-1};
} dummy;
DVariant out{};
pRealProxy(pProp, static_cast<const void *>(&dummy), static_cast<const void *>(&dummy.val), &out, 0, -1);
if(out.m_Int == dummy.val+1) {
#if defined _DEBUG
printf("short (proxy)\n");
#endif
return prop_types::short_;
}
}
#if defined _DEBUG
printf("int (type)\n");
#endif
return prop_types::int_;
}
}
}
case DPT_Float:
return prop_types::float_;
case DPT_Vector: {
if(pProp->m_fLowValue == 0.0f && pProp->m_fHighValue == 360.0f) {
return prop_types::qangle;
} else {
return prop_types::vector;
}
}
case DPT_VectorXY:
return prop_types::vector;
case DPT_String: {
if(SendProxy_StringT_To_String_ptr && pRealProxy == SendProxy_StringT_To_String_ptr) {
return prop_types::tstring;
} else {
return prop_types::cstring;
}
}
case DPT_Array:
return prop_types::unknown;
case DPT_DataTable:
return prop_types::unknown;
}
return prop_types::unknown;
}
proxysend::prop_types Sample::guess_prop_type(const SendProp *prop, const SendTable *table) const noexcept
{
return ::guess_prop_type(prop, table);
}
template <typename T>
class thread_var_base
{
protected:
using ptr_ret_t = std::conditional_t<std::is_pointer<T>::value, T, T *>;
public:
inline void reset(std::nullptr_t) noexcept
{ reset_ptr(nullptr); }
thread_var_base &operator=(std::nullptr_t) noexcept
{
reset_ptr(nullptr);
return *this;
}
inline thread_var_base(std::nullptr_t) noexcept
: thread_var_base{}
{
}
inline thread_var_base() noexcept
{
}
inline bool operator!() const noexcept;
inline ptr_ret_t operator->() noexcept
{ return get_ptr(); }
inline ~thread_var_base() noexcept
{
if(allocated_) {
unallocate();
}
}
protected:
static constexpr const pthread_key_t invalid_key{PTHREAD_KEYS_MAX+1};
pthread_key_t key{invalid_key};
bool allocated_{false};
static void dtor(void *ptr) noexcept
{ delete reinterpret_cast<T *>(ptr); }
T *get_ptr_raw() const noexcept
{
if(!allocated_) {
return nullptr;
}
return reinterpret_cast<T *>(pthread_getspecific(key));
}
ptr_ret_t get_ptr() const noexcept
{
T *ptr{get_ptr_raw()};
if(!ptr) {
return nullptr;
}
#if 0
if constexpr(std::is_pointer_v<T>) {
return *ptr;
} else
#endif
{
return ptr;
}
}
T *get_or_allocate_ptr() noexcept
{
if(!allocated_ && !allocate()) {
return nullptr;
}
T *ptr{reinterpret_cast<T *>(pthread_getspecific(key))};
if(!ptr) {
ptr = new T{};
pthread_setspecific(key, ptr);
}
return ptr;
}
bool allocate() noexcept
{
if(!__sync_bool_compare_and_swap(&allocated_, 0, 1)) {
return true;
}
if(pthread_key_create(&key, dtor) != 0) {
return false;
}
pthread_setspecific(key, new T{});
return true;
}
bool unallocate() noexcept
{
if(!__sync_bool_compare_and_swap(&allocated_, 1, 0)) {
return true;
}
T *old_ptr{reinterpret_cast<T *>(pthread_getspecific(key))};
if(old_ptr) {
delete old_ptr;
}
return (pthread_key_delete(key) == 0);
}
void reset_ptr(T *ptr) noexcept
{
T *old_ptr{get_ptr_raw()};
if(old_ptr) {
delete old_ptr;
}
if(!allocated_ && !allocate()) {
return;
}
pthread_setspecific(key, ptr);
}
private:
thread_var_base(const thread_var_base &) = delete;
thread_var_base &operator=(const thread_var_base &) = delete;
thread_var_base(thread_var_base &&) = delete;
thread_var_base &operator=(thread_var_base &&) = delete;
};
template <>
inline bool thread_var_base<bool>::operator!() const noexcept
{
const bool *ptr{get_ptr_raw()};
return (!ptr || !*ptr);
}
template <typename T>
inline bool thread_var_base<T>::operator!() const noexcept
{ return get_ptr() == nullptr; }
template <typename T>
class thread_var final : public thread_var_base<T>
{
public:
using thread_var_base<T>::thread_var_base;
using thread_var_base<T>::reset;
template <typename ...Args>
T &reset(Args &&...args) noexcept
{
T *ptr{this->get_or_allocate_ptr()};
ptr->~T();
new (ptr) T{std::forward<Args>(args)...};
return *ptr;
}
inline thread_var() noexcept
: thread_var_base<T>{}
{
}
inline thread_var(const T &val) noexcept
{ *this->get_or_allocate_ptr() = val; }
inline thread_var(T &&val) noexcept
{ *this->get_or_allocate_ptr() = std::move(val); }
template <typename ...Args>
inline thread_var(Args &&...args) noexcept
{
T *ptr{this->get_or_allocate_ptr()};
ptr->~T();
new (ptr) T{std::forward<Args>(args)...};
}
thread_var &operator=(std::nullptr_t) noexcept
{
this->reset_ptr(nullptr);
return *this;
}
thread_var &operator=(const T &val) noexcept
{
*this->get_or_allocate_ptr() = val;
return *this;
}
thread_var &operator=(T &&val) noexcept
{
*this->get_or_allocate_ptr() = std::move(val);
return *this;
}
inline T &operator*() noexcept
{ return *this->get_ptr_raw(); }
inline T &get() noexcept
{ return *this->get_ptr_raw(); }
inline operator T &() noexcept
{ return *this->get_ptr_raw(); }
inline explicit operator bool() const noexcept
{ return this->get_ptr() != nullptr; }
};
template <>
class thread_var<bool> final : public thread_var_base<bool>
{
public:
using thread_var_base<bool>::thread_var_base;
using thread_var_base<bool>::reset;
inline thread_var()
: thread_var_base<bool>{}
{
}
inline bool operator*() const noexcept
{ return get(); }
inline bool get() const noexcept
{
const bool *ptr{get_ptr_raw()};
return (ptr && *ptr);
}
inline operator bool() const noexcept
{ return get(); }
bool reset(bool val) noexcept
{
set_value(val);
return val;
}
thread_var &operator=(std::nullptr_t) noexcept
{
set_value(false);
return *this;
}
inline thread_var(bool val) noexcept
{ set_value(val); }
thread_var &operator=(bool val) noexcept
{
set_value(val);
return *this;
}
private:
void set_value(bool val) noexcept
{
*this->get_or_allocate_ptr() = val;
}
};
CBaseEntity *ReferenceToEntity(unsigned long ref)
{
union {
cell_t sp_val;
unsigned long e_val;
} u;
u.e_val = ref;
return gamehelpers->ReferenceToEntity(u.sp_val);
}
unsigned long EntityToReference(CBaseEntity *ent)
{
union {
cell_t sp_val;
unsigned long e_val;
} u;
u.sp_val = gamehelpers->EntityToReference(ent);
return u.e_val;
}
unsigned long IndexToReference(int objectID)
{
union {
cell_t sp_val;
unsigned long e_val;
} u;
u.sp_val = gamehelpers->IndexToReference(objectID);
return u.e_val;
}
struct packed_entity_data_t final
{
packed_entity_data_t(packed_entity_data_t &&other) noexcept
{ operator=(std::move(other)); }
packed_entity_data_t &operator=(packed_entity_data_t &&other) noexcept {
packedData = other.packedData;
other.packedData = nullptr;
writeBuf = other.writeBuf;
other.writeBuf = nullptr;
ref = other.ref;
other.ref = INVALID_EHANDLE_INDEX;
return *this;
}
char *packedData{nullptr};
bf_write *writeBuf{nullptr};
unsigned long ref{INVALID_EHANDLE_INDEX};
bool allocated() const noexcept
{ return (packedData && writeBuf); }
bool written() const noexcept
{ return allocated() && (writeBuf->GetNumBitsWritten() > 0); }
packed_entity_data_t() noexcept = default;
~packed_entity_data_t() noexcept {
reset();
}
void reset() noexcept {
if(writeBuf) {
delete writeBuf;
writeBuf = nullptr;
}
if(packedData) {
free(packedData);
packedData = nullptr;
}
}
void allocate() noexcept {
reset();
packedData = static_cast<char *>(aligned_alloc(4, MAX_PACKEDENTITY_DATA));
memset(packedData, 0x0, MAX_PACKEDENTITY_DATA);
writeBuf = new bf_write{"SV_PackEntity->writeBuf", packedData, MAX_PACKEDENTITY_DATA};
}
private:
packed_entity_data_t(const packed_entity_data_t &) = delete;
packed_entity_data_t &operator=(const packed_entity_data_t &) = delete;
};
struct pack_entity_params_t final
{
std::vector<std::vector<packed_entity_data_t>> entity_data{};
std::vector<int> slots{};
std::vector<unsigned long> entities{};
int snapshot_index{-1};
pack_entity_params_t(std::vector<int> &&slots_, std::vector<unsigned long> &&entities_, int snapshot_index_) noexcept
: slots{std::move(slots_)}, entities{std::move(entities_)}, snapshot_index{snapshot_index_}
{
entity_data.resize(slots.size());
}
~pack_entity_params_t() noexcept = default;
private:
pack_entity_params_t(const pack_entity_params_t &) = delete;
pack_entity_params_t &operator=(const pack_entity_params_t &) = delete;
pack_entity_params_t(pack_entity_params_t &&) = delete;
pack_entity_params_t &operator=(pack_entity_params_t &&) = delete;
};
static thread_var<bool> in_compute_packs{};
static thread_var<bool> do_calc_delta{};
static thread_var<bool> do_writedelta_entities{};
static thread_var<int> writedeltaentities_client{};
static thread_var<int> sendproxy_client_slot{};
static std::unique_ptr<pack_entity_params_t> packentity_params{};
static void Host_Error(const char *error, ...) noexcept
{
va_list argptr;
char string[1024];
va_start(argptr, error);
Q_vsnprintf(string, sizeof(string), error, argptr);
va_end(argptr);
Error("Host_Error: %s", string);
}
struct prop_reference_t
{
prop_reference_t(SendProp *pProp, prop_types type) noexcept
{
restores_t::iterator it_restore{restores.find(pProp)};
if(it_restore == restores.end()) {
std::unique_ptr<proxyrestore_t> ptr{new proxyrestore_t{pProp, type}};
it_restore = restores.emplace(std::pair<SendProp *, std::unique_ptr<proxyrestore_t>>{pProp, std::move(ptr)}).first;
}
restore = it_restore->second.get();
++restore->ref;
#ifdef _DEBUG
printf("added ref %zu for %s %p\n", restore->ref, pProp->GetName(), pProp);
#endif
}
virtual ~prop_reference_t() noexcept
{
if(restore) {
#ifdef _DEBUG
printf("removed ref %zu for %s %p\n", restore->ref-1u, restore->pProp->GetName(), restore->pProp);
#endif
if(--restore->ref == 0) {
restores_t::iterator it_restore{restores.begin()};
while(it_restore != restores.end()) {
if(it_restore->second.get() == restore) {
restores.erase(it_restore);
break;
}
++it_restore;
}
}
}
}
inline prop_reference_t(prop_reference_t &&other) noexcept
{ operator=(std::move(other)); }
prop_reference_t &operator=(prop_reference_t &&other) noexcept
{
restore = other.restore;
other.restore = nullptr;
return *this;
}
proxyrestore_t *restore{nullptr};
private:
prop_reference_t(const prop_reference_t &) = delete;
prop_reference_t &operator=(const prop_reference_t &) = delete;
prop_reference_t() = delete;
};
struct callback_t;
struct opaque_ptr final
{
inline opaque_ptr(opaque_ptr &&other) noexcept
{ operator=(std::move(other)); }
template <typename T>
static void del_hlpr_arr(void *ptr_) noexcept
{ delete[] static_cast<T *>(ptr_); }
template <typename T>
static void del_hlpr(void *ptr_) noexcept
{ delete static_cast<T *>(ptr_); }
opaque_ptr() = default;
template <typename T, typename ...Args>
void emplace(std::size_t num, Args &&...args) noexcept {
if(del_func && ptr) {
del_func(ptr);
}
if(num > 1) {
ptr = static_cast<void *>(new T[num]);
for(size_t i = 0; i < num; ++i) {
new (&static_cast<T *>(ptr)[i]) T{std::forward<Args>(args)...};
}
del_func = del_hlpr_arr<T>;
} else {
ptr = static_cast<void *>(new T{std::forward<Args>(args)...});
del_func = del_hlpr<T>;
}
}
void clear() noexcept {
if(del_func && ptr) {
del_func(ptr);
}
del_func = nullptr;
ptr = nullptr;
}
template <typename T>
T &get(std::size_t element) noexcept
{ return static_cast<T *>(ptr)[element]; }
template <typename T = void>
T *get() noexcept
{ return static_cast<T *>(ptr); }
template <typename T>
const T &get(std::size_t element) const noexcept
{ return static_cast<const T *>(ptr)[element]; }
template <typename T = void>
const T *get() const noexcept
{ return static_cast<const T *>(ptr); }
~opaque_ptr() noexcept {
if(del_func && ptr) {
del_func(ptr);
}
}
opaque_ptr &operator=(opaque_ptr &&other) noexcept
{
ptr = other.ptr;
other.ptr = nullptr;
del_func = other.del_func;
other.del_func = nullptr;
return *this;
}
private:
opaque_ptr(const opaque_ptr &) = delete;
opaque_ptr &operator=(const opaque_ptr &) = delete;
void *ptr{nullptr};
void (*del_func)(void *) {nullptr};
};
struct callback_t final : prop_reference_t
{
callback_t(unsigned long ref_, SendProp *pProp, std::string &&name_, int element_, prop_types type_, std::size_t offset_) noexcept
: prop_reference_t{pProp, type_}, offset{offset_}, type{type_}, element{element_}, name{std::move(name_)}, prop{pProp}, ref{ref_}
{
if(type == prop_types::cstring || type == prop_types::tstring) {
fwd = forwards->CreateForwardEx(nullptr, ET_Hook, 6, nullptr, Param_Cell, Param_String, Param_String, Param_Cell, Param_Cell, Param_Cell);
} else if(type == prop_types::color32_) {
fwd = forwards->CreateForwardEx(nullptr, ET_Hook, 8, nullptr, Param_Cell, Param_String, Param_CellByRef, Param_CellByRef, Param_CellByRef, Param_CellByRef, Param_Cell, Param_Cell);
} else {
ParamType value_param_type;
switch(type) {
case prop_types::int_:
case prop_types::short_:
case prop_types::char_:
case prop_types::unsigned_int:
case prop_types::unsigned_short:
case prop_types::unsigned_char:
case prop_types::bool_:
case prop_types::ehandle: {
value_param_type = Param_CellByRef;
} break;
case prop_types::float_: {
value_param_type = Param_FloatByRef;
} break;
case prop_types::vector:
case prop_types::qangle: {
value_param_type = Param_Array;
} break;
}
fwd = forwards->CreateForwardEx(nullptr, ET_Hook, 5, nullptr, Param_Cell, Param_String, value_param_type, Param_Cell, Param_Cell);
}
}
inline bool has_any_per_client_func() const noexcept
{ return !per_client_funcs.empty(); }
void change_edict_state() noexcept
{
if(ref != INVALID_EHANDLE_INDEX) {
CBaseEntity *pEntity{::ReferenceToEntity(ref)};
if(pEntity) {
edict_t *edict{pEntity->GetNetworkable()->GetEdict()};
if(edict) {
gamehelpers->SetEdictStateChanged(edict, offset);
}
}
}
}
void add_function(IPluginFunction *func, bool per_client) noexcept
{
fwd->RemoveFunction(func);
fwd->AddFunction(func);
if(per_client) {
bool found{false};
per_client_funcs_t::const_iterator it_func{per_client_funcs.cbegin()};
while(it_func != per_client_funcs.cend()) {
if(it_func->func == func) {
found = true;
break;
}
++it_func;
}
if(!found) {
per_client_funcs.emplace_back(per_client_func_t{func});
}
}
}
void remove_function(IPluginFunction *func) noexcept
{
fwd->RemoveFunction(func);
per_client_funcs_t::const_iterator it_func{per_client_funcs.cbegin()};
while(it_func != per_client_funcs.cend()) {
if(it_func->func == func) {
per_client_funcs.erase(it_func);
break;