Repository navigation
Expand file tree
/
Copy pathengine_adapter.cpp
More file actions
3531 lines (3423 loc) · 177 KB
/
Copy pathengine_adapter.cpp
File metadata and controls
3531 lines (3423 loc) · 177 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
#include "engine_adapter.h"
#include "mode.h"
#include "knife_targeting.h"
#include "surge_velocity.h"
#include <const.h>
#include <in_buttons.h>
#include <bspflags.h>
#include <entity2/entityinstance.h>
#include <entity2/entitykeyvalues.h>
#include <entity2/entitysystem.h>
#include <schemasystem/schemasystem.h>
#include <engine/igameeventsystem.h>
#include <networksystem/inetworkmessages.h>
#include <tier1/utlsymbollarge.h>
#include <tier1/utlstring.h>
#include <tier1/utlvector.h>
#include <variant.h>
#include <gametrace.h>
#include <igamesystemfactory.h>
#include <igameevents.h>
#include <irecipientfilter.h>
#include <tier1/utlstringtoken.h>
#include "gameevents.pb.h"
#include "usermessages.pb.h"
#include "cstrike15_usermessages.pb.h"
#ifdef _WIN32
#define NOMINMAX
#include <windows.h>
#elif defined(__linux__)
#include <dlfcn.h>
#include <link.h>
#endif
#include <array>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <filesystem>
#include <limits>
// CS2 passes this interface when it builds the map's client resource manifest.
// The layout follows the Source 2 game-system interface used by CS2 plugins.
class IEntityResourceManifest {
public:
virtual void AddResource(const char *) = 0;
virtual void AddResource(const char *, void *) = 0;
virtual void AddResource(const char *, void *, void *, void *) = 0;
virtual void Unknown04() = 0;
virtual void Unknown05() = 0;
virtual void Unknown06() = 0;
virtual void Unknown07() = 0;
virtual void Unknown08() = 0;
virtual void Unknown09() = 0;
virtual void Unknown10() = 0;
};
CBaseGameSystemFactory **CBaseGameSystemFactory::sm_pFirst = nullptr;
namespace {
class ShroudfireResourceGameSystem final : public CAutoGameSystem {
public:
DECLARE_GAME_SYSTEM();
void Shutdown() override;
GS_EVENT(BuildGameSessionManifest) {
if (!msg || !msg->m_pResourceManifest) return;
msg->m_pResourceManifest->AddResource(
"particles/blood_impact/blood_impact_mist1.vpcf");
std::fprintf(stderr, "[Shroudfire] Blood sprite resources added to map manifest.\n");
}
};
ShroudfireResourceGameSystem resource_game_system;
CGameSystemStaticFactory<ShroudfireResourceGameSystem> *resource_factory = nullptr;
void ShroudfireResourceGameSystem::Shutdown() {
delete resource_factory;
resource_factory = nullptr;
}
}
#include <fstream>
#include <tuple>
namespace {
struct StateChangeInfo {
int size = 0;
int padding = 0;
CUtlVector<uint32_t> offsets;
const char *field_name = nullptr;
const char *file_name = nullptr;
uint32_t unknown = 0xffffffffu;
uint32_t array_index = 0xffffffffu;
uint32_t path_index = 0xffffffffu;
uint16_t tail = 0;
explicit StateChangeInfo(uint32_t &offset) : offsets(&offset, 1, 1) {}
};
static_assert(sizeof(StateChangeInfo) == 64);
static_assert(offsetof(StateChangeInfo, offsets) == 8);
} // namespace
CEntityIdentity *CEntitySystem::GetEntityIdentity(CEntityIndex index) {
if (index.Get() < 0 || index.Get() >= MAX_TOTAL_ENTITIES - 1) return nullptr;
CEntityIdentity *chunk = m_EntityList.m_pIdentityChunks[index.Get() / MAX_ENTITIES_IN_LIST];
if (!chunk) return nullptr;
CEntityIdentity *identity = &chunk[index.Get() % MAX_ENTITIES_IN_LIST];
return identity->GetEntityIndex() == index ? identity : nullptr;
}
CEntityIdentity *CEntitySystem::GetEntityIdentity(const CEntityHandle &handle) {
if (!handle.IsValid()) return nullptr;
const int index = handle.GetEntryIndex();
if (index < 0 || index >= MAX_TOTAL_ENTITIES - 1) return nullptr;
CEntityIdentity *chunk = m_EntityList.m_pIdentityChunks[index / MAX_ENTITIES_IN_LIST];
if (!chunk) return nullptr;
CEntityIdentity *identity = &chunk[index % MAX_ENTITIES_IN_LIST];
return identity->GetRefEHandle() == handle ? identity : nullptr;
}
namespace {
// Private CS2 calls stay disabled after a game update until these builds are verified.
#ifdef _WIN32
// Windows CS2 1.42.0.0 (Steam build 26798866); native signatures and vtables reviewed.
constexpr std::uintmax_t kServerSize = 34545816;
constexpr uint32_t kServerCrc32 = 0x55d0741f;
constexpr size_t kEntitySystemOffset = 88;
constexpr size_t kChangeTeamVtable = 105;
constexpr size_t kRespawnVtable = 274;
constexpr size_t kDropWeaponVtable = 28;
constexpr size_t kSetStateChangedVtable = 28;
constexpr size_t kCommitSuicideVtable = 387;
constexpr size_t kTeleportVtable = 165;
constexpr size_t kNavPhysicsVtableRva = 0x1a05e10;
constexpr size_t kTraceShapeFilterVtable = 3;
constexpr size_t kPointContentsVtable = 5;
#else
// Linux CS2 1.42.0.0 (Steam build 26798866); native signatures and vtables reviewed.
constexpr std::uintmax_t kServerSize = 41628472;
constexpr uint32_t kServerCrc32 = 0xb24e4e68;
constexpr size_t kEntitySystemOffset = 80;
constexpr size_t kChangeTeamVtable = 104;
constexpr size_t kRespawnVtable = 276;
constexpr size_t kDropWeaponVtable = 29;
constexpr size_t kSetStateChangedVtable = 29;
constexpr size_t kCommitSuicideVtable = 387;
constexpr size_t kTeleportVtable = 164;
constexpr size_t kNavPhysicsVtableRva = 0x26a90e0;
constexpr size_t kTraceShapeFilterVtable = 5;
constexpr size_t kPointContentsVtable = 6;
#endif
// Verified against the fingerprinted modules: the CTraceFilter shape overload
// is slot 3 on Windows and 5 on Linux; point contents is slot 5 and 6 respectively.
bool schema_field(ISchemaSystem *schema, const char *class_name,
const char *field_name, uint32_t &offset) {
#ifdef _WIN32
CSchemaSystemTypeScope *scope = schema->FindTypeScopeForModule("server.dll");
#else
CSchemaSystemTypeScope *scope = schema->FindTypeScopeForModule("libserver.so");
#endif
SchemaClassInfoData_t *info = scope ? scope->FindDeclaredClass(class_name).Get() : nullptr;
if (!info) return false;
for (int i = 0; i < info->m_nFieldCount; ++i) {
if (std::strcmp(info->m_pFields[i].m_pszName, field_name) == 0) {
offset = info->m_pFields[i].m_nSingleInheritanceOffset;
return true;
}
}
return false;
}
bool global_schema_field(ISchemaSystem *schema, const char *class_name,
const char *field_name, uint32_t &offset) {
CSchemaSystemTypeScope *scope = schema->GlobalTypeScope();
SchemaClassInfoData_t *info = scope ? scope->FindDeclaredClass(class_name).Get() : nullptr;
if (!info) return false;
for (int i = 0; i < info->m_nFieldCount; ++i) {
if (std::strcmp(info->m_pFields[i].m_pszName, field_name) == 0) {
offset = info->m_pFields[i].m_nSingleInheritanceOffset;
return true;
}
}
return false;
}
uint32_t crc32_file(const std::filesystem::path &path) {
std::ifstream file(path, std::ios::binary);
if (!file) return 0;
uint32_t crc = 0xffffffffu;
std::array<char, 65536> bytes {};
while (file) {
file.read(bytes.data(), bytes.size());
const auto count = file.gcount();
for (std::streamsize i = 0; i < count; ++i) {
crc ^= static_cast<unsigned char>(bytes[static_cast<size_t>(i)]);
for (int bit = 0; bit < 8; ++bit)
crc = (crc >> 1) ^ (0xedb88320u & (0u - (crc & 1u)));
}
}
return ~crc;
}
#ifdef _WIN32
bool executable_in_module(void *function, void *module) {
MEMORY_BASIC_INFORMATION region {};
if (!function || !VirtualQuery(function, ®ion, sizeof(region)) ||
region.AllocationBase != module || region.State != MEM_COMMIT) return false;
const DWORD protection = region.Protect & 0xff;
return protection == PAGE_EXECUTE || protection == PAGE_EXECUTE_READ ||
protection == PAGE_EXECUTE_READWRITE || protection == PAGE_EXECUTE_WRITECOPY;
}
template <size_t N>
void *find_signature(HMODULE module, const std::array<int, N> &pattern) {
const auto *image = reinterpret_cast<const uint8_t *>(module);
const auto *dos = reinterpret_cast<const IMAGE_DOS_HEADER *>(image);
if (dos->e_magic != IMAGE_DOS_SIGNATURE) return nullptr;
const auto *nt = reinterpret_cast<const IMAGE_NT_HEADERS *>(image + dos->e_lfanew);
if (nt->Signature != IMAGE_NT_SIGNATURE) return nullptr;
const uint8_t *match = nullptr;
const IMAGE_SECTION_HEADER *section = IMAGE_FIRST_SECTION(nt);
for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i) {
if (!(section[i].Characteristics & IMAGE_SCN_MEM_EXECUTE)) continue;
const auto *start = image + section[i].VirtualAddress;
const size_t length = section[i].Misc.VirtualSize;
for (size_t position = 0; position + pattern.size() <= length; ++position) {
bool equal = true;
for (size_t byte = 0; byte < pattern.size(); ++byte) {
if (pattern[byte] >= 0 && start[position + byte] != pattern[byte]) {
equal = false;
break;
}
}
if (!equal) continue;
if (match) return nullptr;
match = start + position;
}
}
return const_cast<uint8_t *>(match);
}
#elif defined(__linux__)
bool executable_in_module(void *function, void *module) {
Dl_info info {};
return function && dladdr(function, &info) && info.dli_fbase == module;
}
template <size_t N>
void *find_signature(void *module, const std::array<int, N> &pattern) {
struct Search {
void *module;
const std::array<int, N> &pattern;
const uint8_t *match = nullptr;
bool ambiguous = false;
} search {module, pattern};
dl_iterate_phdr([](dl_phdr_info *image, size_t, void *context) {
auto &state = *static_cast<Search *>(context);
if (reinterpret_cast<void *>(image->dlpi_addr) != state.module) return 0;
for (int index = 0; index < image->dlpi_phnum; ++index) {
const auto &segment = image->dlpi_phdr[index];
if (segment.p_type != PT_LOAD || !(segment.p_flags & PF_X)) continue;
const auto *start = reinterpret_cast<const uint8_t *>(image->dlpi_addr + segment.p_vaddr);
const auto length = static_cast<size_t>(segment.p_memsz);
for (size_t offset = 0; offset + N <= length; ++offset) {
size_t byte = 0;
for (; byte < N; ++byte)
if (state.pattern[byte] >= 0 && start[offset + byte] != state.pattern[byte]) break;
if (byte != N) continue;
if (state.match) {
state.ambiguous = true;
return 1;
}
state.match = start + offset;
}
}
return 1;
}, &search);
return search.ambiguous ? nullptr : const_cast<uint8_t *>(search.match);
}
#endif
} // namespace
bool EngineAdapter::initialize(IGameResourceService *resources, ISchemaSystem *schema,
const void *server_interface, INetworkMessages *network_messages,
IGameEventSystem *event_system,
std::string &error) {
ready_ = false;
resources_ = nullptr;
server_module_ = nullptr;
terminate_round_function_ = nullptr;
remove_entity_function_ = nullptr;
create_entity_function_ = nullptr;
dispatch_spawn_function_ = nullptr;
take_damage_function_ = nullptr;
weapon_can_use_function_ = nullptr;
can_acquire_function_ = nullptr;
sound_filter_function_ = nullptr;
increment_mvp_function_ = nullptr;
process_movement_function_ = nullptr;
ignore_armor_damage_flag_ = 0;
set_player_pawn_function_ = nullptr;
switch_team_function_ = nullptr;
game_event_manager_init_function_ = nullptr;
accept_input_function_ = nullptr;
fade_fields_ok_ = false;
blood_fields_ok_ = false;
particle_fields_ok_ = false;
point_hurt_fields_ok_ = false;
glow_fields_ok_ = false;
keyvalues_constructor_function_ = nullptr;
keyvalues_find_or_create_function_ = nullptr;
keyvalues_set_string_function_ = nullptr;
hud_set_player_class_function_ = nullptr;
hud_set_player_dialog_function_ = nullptr;
win_layout_handle_ = 0;
spawn_rotation_ok_ = false;
html_restart_fields_ok_ = false;
buy_rules_fields_ok_ = false;
html_game_rules_index_ = -1;
fade_entity_ = 0;
blood_entity_ = 0;
blood_particles_.clear();
claw_hurt_entity_ = 0;
extra_spawn_handles_.clear();
base_spawn_counts_ = {};
#ifdef _WIN32
HMODULE module = nullptr;
const void *server_method = server_interface
? (*reinterpret_cast<void *const *const *>(server_interface))[0] : nullptr;
if (!server_method || !GetModuleHandleExW(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(server_method), &module)) {
error = "Cannot resolve the CS2 game server module";
return false;
}
std::array<wchar_t, 32768> path {};
const DWORD length = GetModuleFileNameW(module, path.data(), static_cast<DWORD>(path.size()));
if (!length || length >= path.size()) {
error = "Cannot find the loaded CS2 server.dll";
return false;
}
std::error_code filesystem_error;
const std::filesystem::path dll_path(path.data());
const auto size = std::filesystem::file_size(dll_path, filesystem_error);
const auto crc = filesystem_error ? 0 : crc32_file(dll_path);
if (filesystem_error || size != kServerSize || crc != kServerCrc32) {
error = "Unsupported server module " + dll_path.string() + " (size=" +
std::to_string(size) + ", CRC32=" + std::to_string(crc) +
"); engine actions are disabled";
return false;
}
#elif defined(__linux__)
const void *server_method = server_interface
? (*reinterpret_cast<void *const *const *>(server_interface))[0] : nullptr;
Dl_info module_info {};
if (!server_method || !dladdr(server_method, &module_info) ||
!module_info.dli_fbase || !module_info.dli_fname) {
error = "Cannot resolve the CS2 Linux game server module";
return false;
}
void *module = module_info.dli_fbase;
std::error_code filesystem_error;
const std::filesystem::path dll_path(module_info.dli_fname);
const auto size = std::filesystem::file_size(dll_path, filesystem_error);
const auto crc = filesystem_error ? 0 : crc32_file(dll_path);
if (filesystem_error || size != kServerSize || crc != kServerCrc32) {
error = "Unsupported server module " + dll_path.string() + " (size=" +
std::to_string(size) + ", CRC32=" + std::to_string(crc) +
"); engine actions are disabled";
return false;
}
#endif
if (!resources || !schema) {
error = "Required CS2 game resource or schema interface is unavailable";
return false;
}
for (CSchemaSystemTypeScope *scope : {schema->GlobalTypeScope(),
#ifdef _WIN32
schema->FindTypeScopeForModule("server.dll")
#else
schema->FindTypeScopeForModule("libserver.so")
#endif
}) {
CSchemaEnumInfo *flags = scope
? scope->FindDeclaredEnum("TakeDamageFlags_t").Get() : nullptr;
if (!flags) continue;
for (int i = 0; i < flags->m_nEnumeratorCount; ++i) {
const auto &entry = flags->m_pEnumerators[i];
if (std::strcmp(entry.m_pszName, "DFLAG_IGNORE_ARMOR") == 0 &&
entry.m_nValue > 0 && entry.m_nValue <= UINT32_MAX)
ignore_armor_damage_flag_ = static_cast<uint32_t>(entry.m_nValue);
}
}
if (!ignore_armor_damage_flag_) {
error = "CS2 schema has no DFLAG_IGNORE_ARMOR value";
return false;
}
bool fields_ok = true;
fields_ok &= schema_field(schema, "CCSPlayerController", "m_hPlayerPawn", fields_.player_pawn);
fields_ok &= schema_field(schema, "CCSPlayerController", "m_pInGameMoneyServices",
fields_.money_services);
fields_ok &= schema_field(schema, "CCSPlayerController_InGameMoneyServices", "m_iAccount",
fields_.money_account);
fields_ok &= schema_field(schema, "CBasePlayerPawn", "m_hController", fields_.pawn_controller);
fields_ok &= schema_field(schema, "CBaseEntity", "m_hOwnerEntity", fields_.owner_entity);
fields_ok &= schema_field(schema, "CBaseEntity", "m_iHealth", fields_.health);
fields_ok &= schema_field(schema, "CBaseEntity", "m_fFlags", fields_.flags);
fields_ok &= schema_field(schema, "CBaseEntity", "m_lifeState", fields_.life_state);
fields_ok &= schema_field(schema, "CBaseEntity", "m_MoveType", fields_.move_type);
fields_ok &= schema_field(schema, "CBaseEntity", "m_iTeamNum", fields_.team);
fields_ok &= schema_field(schema, "CBasePlayerPawn", "m_pWeaponServices", fields_.weapon_services);
fields_ok &= schema_field(schema, "CCSPlayer_WeaponServices", "m_flNextAttack",
fields_.next_attack_time);
fields_ok &= schema_field(schema, "CCSPlayerPawn", "m_pAimPunchServices",
fields_.aim_punch_services);
fields_ok &= schema_field(schema, "CCSPlayer_AimPunchServices", "m_predictableBaseAngle",
fields_.aim_punch_angle);
fields_ok &= schema_field(schema, "CCSPlayer_AimPunchServices", "m_predictableBaseAngleVel",
fields_.aim_punch_velocity);
fields_ok &= schema_field(schema, "CBasePlayerPawn", "m_pItemServices", fields_.item_services);
fields_ok &= schema_field(schema, "CEconItemView", "m_iItemDefinitionIndex", fields_.item_definition_index);
fields_ok &= schema_field(schema, "CCSPlayer_ItemServices", "m_bHasHelmet", fields_.has_helmet);
fields_ok &= schema_field(schema, "CCSPlayer_ItemServices", "m_bHasDefuser", fields_.has_defuser);
fields_ok &= schema_field(schema, "CPlayer_WeaponServices", "m_hActiveWeapon", fields_.active_weapon);
fields_ok &= schema_field(schema, "CPlayer_WeaponServices", "m_hLastWeapon", fields_.last_weapon);
fields_ok &= schema_field(schema, "CPlayer_WeaponServices", "m_hMyWeapons", fields_.weapons);
fields_ok &= schema_field(schema, "CBaseCombatCharacter", "m_hMyWearables", fields_.wearables);
fields_ok &= schema_field(schema, "CCSPlayerPawn", "m_pHostageServices", fields_.hostage_services);
fields_ok &= schema_field(schema, "CCSPlayer_HostageServices", "m_hCarriedHostageProp",
fields_.carried_hostage_prop);
fields_ok &= schema_field(schema, "CCSGameRulesProxy", "m_pGameRules", fields_.game_rules);
warmup_field_ok_ = schema_field(schema, "CCSGameRules", "m_bWarmupPeriod",
fields_.warmup_period);
freeze_field_ok_ = schema_field(schema, "CCSGameRules", "m_bFreezePeriod",
fields_.freeze_period);
html_restart_fields_ok_ = schema_field(schema, "CCSGameRules", "m_flRestartRoundTime",
fields_.restart_round_time);
html_restart_fields_ok_ &= schema_field(schema, "CCSGameRules", "m_bGameRestart",
fields_.game_restart);
round_presentation_fields_ok_ = schema_field(schema, "CCSGameRules", "m_nRoundEndCount",
fields_.round_end_count);
round_presentation_fields_ok_ &= schema_field(schema, "CCSGameRules", "m_nRoundStartCount",
fields_.round_start_count);
round_presentation_fields_ok_ &= schema_field(schema, "CCSGameRules", "m_bRoundEndNoMusic",
fields_.round_end_no_music);
buy_rules_fields_ok_ = schema_field(schema, "CCSGameRules", "m_bBuyTimeEnded",
fields_.buy_time_ended);
buy_rules_fields_ok_ &= schema_field(schema, "CCSGameRules", "m_bCTCantBuy",
fields_.ct_cant_buy);
buy_rules_fields_ok_ &= schema_field(schema, "CCSGameRules", "m_bTCantBuy",
fields_.t_cant_buy);
fields_ok &= schema_field(schema, "CCSPlayerPawn", "m_ArmorValue", fields_.armor);
fields_ok &= schema_field(schema, "CBaseEntity", "m_flGravityScale", fields_.gravity_scale);
fields_ok &= schema_field(schema, "CBaseEntity", "m_flActualGravityScale",
fields_.actual_gravity_scale);
fields_ok &= schema_field(schema, "CBaseEntity", "m_CBodyComponent", fields_.body_component);
fields_ok &= schema_field(schema, "CBodyComponent", "m_pSceneNode", fields_.scene_node);
fields_ok &= schema_field(schema, "CGameSceneNode", "m_vecAbsOrigin", fields_.abs_origin);
spawn_rotation_ok_ = schema_field(schema, "CGameSceneNode", "m_angAbsRotation",
fields_.abs_rotation);
fields_ok &= schema_field(schema, "CCSPlayerPawn", "m_angEyeAngles", fields_.eye_angles);
fields_ok &= schema_field(schema, "CCSPlayerPawn", "m_bInBuyZone", fields_.in_buy_zone);
fields_ok &= schema_field(schema, "CCSPlayerPawn", "m_bWasInBuyZone", fields_.was_in_buy_zone);
fields_ok &= schema_field(schema, "CCSPlayerPawn", "m_TouchingBuyZones",
fields_.touching_buy_zones);
fields_ok &= schema_field(schema, "CBasePlayerPawn", "m_pMovementServices", fields_.movement_services);
fields_ok &= schema_field(schema, "CPlayer_MovementServices", "m_nButtons", fields_.movement_buttons);
schema_field(schema, "CPlayer_MovementServices_Humanoid", "m_bDucked",
fields_.movement_ducked);
schema_field(schema, "CCSPlayer_MovementServices", "m_flDuckAmount",
fields_.movement_duck_amount);
fields_ok &= schema_field(schema, "CPlayer_MovementServices", "m_flMaxspeed", fields_.maxspeed);
fields_ok &= schema_field(schema, "CCSPlayerPawn", "m_flVelocityModifier",
fields_.velocity_modifier);
fields_ok &= schema_field(schema, "CBasePlayerPawn", "m_pObserverServices",
fields_.observer_services);
fields_ok &= schema_field(schema, "CCSPlayerController", "m_hObserverPawn",
fields_.observer_pawn);
fields_ok &= schema_field(schema, "CPlayer_ObserverServices", "m_iObserverMode",
fields_.observer_mode);
fields_ok &= schema_field(schema, "CPlayer_ObserverServices", "m_hObserverTarget",
fields_.observer_target);
fields_ok &= schema_field(schema, "CBasePlayerPawn", "m_pCameraServices",
fields_.camera_services);
fields_ok &= schema_field(schema, "CPlayer_CameraServices", "m_hTonemapController",
fields_.player_tonemap);
fields_ok &= global_schema_field(schema, "CInButtonState", "m_pButtonStates", fields_.button_states);
fields_ok &= schema_field(schema, "CBaseEntity", "m_vecVelocity", fields_.velocity);
fields_ok &= schema_field(schema, "CNetworkVelocityVector", "m_vecX", fields_.velocity_x);
fields_ok &= schema_field(schema, "CNetworkVelocityVector", "m_vecY", fields_.velocity_y);
fields_ok &= schema_field(schema, "CNetworkVelocityVector", "m_vecZ", fields_.velocity_z);
fields_ok &= schema_field(schema, "CBaseEntity", "m_bTakesDamage", fields_.takes_damage);
fields_ok &= schema_field(schema, "CBaseEntity", "m_nSubclassID", fields_.subclass_id);
fields_ok &= schema_field(schema, "CBasePlayerWeapon", "m_pReserveAmmo", fields_.reserve_ammo);
fields_ok &= schema_field(schema, "CBasePlayerWeapon", "m_iClip1", fields_.clip1);
fields_ok &= schema_field(schema, "CBasePlayerWeapon", "m_nNextPrimaryAttackTick",
fields_.primary_attack_tick);
fields_ok &= schema_field(schema, "CBasePlayerWeapon", "m_nNextSecondaryAttackTick",
fields_.secondary_attack_tick);
fields_ok &= schema_field(schema, "CBasePlayerWeaponVData", "m_iMaxClip1",
fields_.primary_clip_max);
fields_ok &= schema_field(schema, "CBasePlayerWeaponVData", "m_bReserveAmmoAsClips",
fields_.reserve_as_clips);
fields_ok &= schema_field(schema, "CCSWeaponBaseVData", "m_bReloadsSingleShells",
fields_.reloads_single_shells);
fields_ok &= schema_field(schema, "CCSWeaponBase", "m_bInReload", fields_.weapon_in_reload);
fields_ok &= schema_field(schema, "CCSWeaponBase", "m_fAccuracyPenalty",
fields_.accuracy_penalty);
fields_ok &= schema_field(schema, "CCSWeaponBaseVData", "m_nPrimaryReserveAmmoMax",
fields_.primary_reserve_max);
fields_ok &= schema_field(schema, "CCSWeaponBaseVData", "m_GearSlot", fields_.gear_slot);
fields_ok &= schema_field(schema, "CCSWeaponBaseVData", "m_flHeadshotMultiplier",
fields_.headshot_multiplier);
fields_ok &= schema_field(schema, "CTakeDamageInfo", "m_hAttacker", fields_.damage_attacker);
fields_ok &= schema_field(schema, "CTakeDamageInfo", "m_flDamage", fields_.damage_amount);
fields_ok &= schema_field(schema, "CTakeDamageInfo", "m_bitsDamageType", fields_.damage_type);
fields_ok &= schema_field(schema, "CTakeDamageInfo", "m_nDamageFlags", fields_.damage_flags);
fields_ok &= schema_field(schema, "CTakeDamageInfo", "m_iHitGroupId", fields_.damage_hitgroup);
point_hurt_fields_ok_ = schema_field(schema, "CPointHurt", "m_nDamage",
fields_.point_hurt_damage);
point_hurt_fields_ok_ &= schema_field(schema, "CPointHurt", "m_bitsDamageType",
fields_.point_hurt_type);
point_hurt_fields_ok_ &= schema_field(schema, "CPointHurt", "m_flRadius",
fields_.point_hurt_radius);
point_hurt_fields_ok_ &= schema_field(schema, "CPointHurt", "m_strTarget",
fields_.point_hurt_target);
glow_fields_ok_ = schema_field(schema, "CSkeletonInstance", "m_modelState",
fields_.model_state);
glow_fields_ok_ &= schema_field(schema, "CModelState", "m_ModelName",
fields_.model_name) ||
global_schema_field(schema, "CModelState", "m_ModelName",
fields_.model_name);
render_fields_ok_ = schema_field(schema, "CBaseModelEntity", "m_nRenderMode",
fields_.render_mode);
render_fields_ok_ &= schema_field(schema, "CBaseModelEntity", "m_clrRender",
fields_.render_color);
glow_fields_ok_ &= render_fields_ok_;
glow_fields_ok_ &= schema_field(schema, "CBaseModelEntity", "m_Glow",
fields_.glow_property);
glow_fields_ok_ &= schema_field(schema, "CGlowProperty", "m_glowColorOverride",
fields_.glow_color);
glow_fields_ok_ &= schema_field(schema, "CGlowProperty", "m_iGlowType",
fields_.glow_type);
glow_fields_ok_ &= schema_field(schema, "CGlowProperty", "m_iGlowTeam",
fields_.glow_team);
glow_fields_ok_ &= schema_field(schema, "CGlowProperty", "m_nGlowRange",
fields_.glow_range);
glow_fields_ok_ &= schema_field(schema, "CGlowProperty", "m_nGlowRangeMin",
fields_.glow_range_min);
fields_ok &= schema_field(schema, "CTonemapController2", "m_flAutoExposureMin",
fields_.auto_exposure_min);
fields_ok &= schema_field(schema, "CTonemapController2", "m_flAutoExposureMax",
fields_.auto_exposure_max);
fade_fields_ok_ = schema_field(schema, "CBaseEntity", "m_spawnflags",
fields_.spawn_flags);
fade_fields_ok_ &= schema_field(schema, "CEnvFade", "m_fadeColor",
fields_.fade_color);
fade_fields_ok_ &= schema_field(schema, "CEnvFade", "m_Duration",
fields_.fade_duration);
fade_fields_ok_ &= schema_field(schema, "CEnvFade", "m_HoldDuration",
fields_.fade_hold_duration);
blood_fields_ok_ = schema_field(schema, "CBlood", "m_flAmount", fields_.blood_amount);
blood_fields_ok_ &= schema_field(schema, "CBlood", "m_Color", fields_.blood_color);
blood_fields_ok_ &= schema_field(schema, "CBlood", "m_vecSprayDir",
fields_.blood_spray_dir);
particle_fields_ok_ = schema_field(schema, "CParticleSystem", "m_bStartActive",
fields_.particle_start_active);
particle_fields_ok_ &= schema_field(schema, "CParticleSystem", "m_iszEffectName",
fields_.particle_effect_name);
if (!fields_ok) {
error = "Required CS2 player visibility schema fields are unavailable";
return false;
}
#ifdef _WIN32
// These entry signatures are unique within the fingerprinted game DLL.
terminate_round_function_ = find_signature(module, std::array<int, 14> {
0x48, 0x8b, 0xc4, 0x4c, 0x89, 0x48, -1, 0x48, 0x89, 0x48, -1, 0x55, 0x41, 0x54
});
remove_entity_function_ = find_signature(module, std::array<int, 11> {
0x48, 0x85, 0xc9, 0x74, -1, 0x48, 0x8b, 0xd1, 0x48, 0x8b, 0x0d
});
create_entity_function_ = find_signature(module, std::array<int, 12> {
0x48, 0x83, 0xec, -1, 0xc6, 0x44, 0x24, -1, -1, 0x4c, 0x8b, 0xc1
});
dispatch_spawn_function_ = find_signature(module, std::array<int, 28> {
0x48, 0x89, 0x5c, 0x24, -1, 0x57, 0x48, 0x83, 0xec, -1, 0x48, 0x8b, 0xda,
0x48, 0x8b, 0xf9, 0x48, 0x85, 0xc9, 0x0f, 0x84, -1, -1, -1, -1, 0x48, 0x85, 0xd2
});
take_damage_function_ = find_signature(module, std::array<int, 22> {
0x40, 0x55, 0x53, 0x56, 0x57, 0x41, 0x54, 0x48, 0x8d, 0x6c, 0x24,
-1, 0x48, 0x81, 0xec, -1, -1, -1, -1, 0x4d, 0x8b, 0xe0
});
weapon_can_use_function_ = find_signature(module, std::array<int, 28> {
0x48, 0x89, 0x5c, 0x24, -1, 0x48, 0x89, 0x6c, 0x24, -1, 0x56, 0x57,
0x41, 0x54, 0x41, 0x56, 0x41, 0x57, 0x48, 0x83, 0xec, -1, 0x48,
0x8b, 0x01, 0x48, 0x8b, 0xfa
});
can_acquire_function_ = find_signature(module, std::array {
0x44, 0x89, 0x44, 0x24, -1, 0x48, 0x89, 0x54, 0x24, -1,
0x48, 0x89, 0x4c, 0x24, -1, 0x55, 0x53, 0x56, 0x57,
0x41, 0x54, 0x41, 0x55, 0x41, 0x56, 0x41, 0x57, 0x48, 0x8b, 0xec
});
increment_mvp_function_ = find_signature(module, std::array {
0x40, 0x55, 0x57, 0x41, 0x57, 0x48, 0x8b, 0xec,
0x48, 0x81, 0xec, -1, -1, -1, -1, 0x48, 0x8b, 0x05
});
sound_filter_function_ = find_signature(module, std::array<int, 17> {
0x40, 0x53, 0x48, 0x83, 0xec, -1, 0x4c, 0x89, 0x4c, 0x24,
-1, 0x48, 0x8b, 0xd9, 0x45, 0x8b, 0xc8
});
process_movement_function_ = find_signature(module, std::array<int, 14> {
0x40, 0x57, 0x41, 0x57, 0x48, 0x81, 0xec, -1, -1, -1, -1,
0x48, 0x83, 0x79
});
game_event_manager_init_function_ = find_signature(module, std::array<int, 18> {
0x40, 0x53, 0x48, 0x83, 0xec, -1, 0x48, 0x8b, 0x01,
0x48, 0x8b, 0xd9, 0xff, 0x50, -1, 0x48, 0x8b, 0x03
});
set_player_pawn_function_ = find_signature(module, std::array<int, 9> {
0x44, 0x88, 0x4c, 0x24, -1, 0x53, 0x57, 0x41, 0x54
});
switch_team_function_ = find_signature(module, std::array<int, 15> {
0x40, 0x53, 0x57, 0x48, 0x81, 0xec, -1, -1, -1, -1,
0x48, 0x8b, 0xd9, 0x8b, 0xfa
});
host_say_function_ = find_signature(module, std::array<int, 9> {
0x44, 0x89, 0x4c, 0x24, -1, 0x44, 0x88, 0x44, 0x24
});
accept_input_function_ = find_signature(module, std::array<int, 27> {
0x48, 0x89, 0x5c, 0x24, -1, 0x48, 0x89, 0x6c, 0x24, -1, 0x56, 0x57,
0x41, 0x56, 0x48, 0x81, 0xec, -1, -1, -1, -1, 0x4d, 0x8b, 0xf0,
0x48, 0x8b, 0xf1
});
keyvalues_constructor_function_ = find_signature(module, std::array<int, 26> {
0x48, 0x89, 0x5c, 0x24, -1, 0x57, 0x48, 0x83, 0xec, -1,
0x33, 0xff, 0x48, 0x8b, 0xd9, 0x48, 0x89, 0x79, -1,
0x48, 0x89, 0x79, -1, 0x44, 0x88, 0x41
});
keyvalues_find_or_create_function_ = find_signature(module, std::array<int, 9> {
0x48, 0x83, 0xec, 0x38, 0x8b, 0x02, 0x45, 0x33, 0xc0
});
keyvalues_set_string_function_ = find_signature(module, std::array<int, 19> {
0x48, 0x89, 0x5c, 0x24, 0x10, 0x48, 0x89, 0x6c, 0x24, 0x18,
0x56, 0x48, 0x83, 0xec, -1, 0x48, 0x83, 0x39, -1
});
#else
// Byte patterns from CounterStrikeSharp gamedata, checked against the
// fingerprinted libserver.so. Linux call ABIs still need live validation.
terminate_round_function_ = find_signature(module, std::array{
0x55, 0x48, 0x89, 0xe5, 0x41, 0x57, 0x41, 0x56, 0x49, 0x89, 0xfe,
0x41, 0x55, 0x41, 0x54, 0x53, 0x48, 0x81, 0xec, -1, -1, -1, -1,
0x48, 0x8d, 0x05, -1, -1, -1, -1, 0xf3, 0x0f, 0x11, 0x85,
-1, -1, -1, -1, 0x89, 0xb5
});
remove_entity_function_ = find_signature(module, std::array{
0x48, 0x89, 0xfe, 0x48, 0x85, 0xff, 0x74, -1,
0x48, 0x8d, 0x05, -1, -1, -1, -1, 0x48
});
create_entity_function_ = find_signature(module, std::array{
0x48, 0x8d, 0x05, -1, -1, -1, -1, 0x55, 0x48, 0x89, 0xfa
});
dispatch_spawn_function_ = find_signature(module, std::array{
0x48, 0x85, 0xff, 0x74, -1, 0x55, 0x48, 0x89, 0xe5,
0x41, 0x55, 0x41, 0x54, 0x49, 0x89, 0xfc
});
take_damage_function_ = find_signature(module, std::array{
0x55, 0x66, 0x0f, 0xef, 0xc0, 0x48, 0x89, 0xe5, 0x41, 0x57,
0x41, 0x56, 0x41, 0x55, 0x49, 0x89, 0xfd, 0x31, 0xff
});
weapon_can_use_function_ = find_signature(module, std::array{
0x55, 0x48, 0x8d, 0x15, -1, -1, -1, -1, 0x48, 0x89, 0xe5,
0x41, 0x55, 0x41, 0x54, 0x49, 0x89, 0xf4, 0x53, 0x48, 0x89, 0xfb,
0x48, 0x83, 0xec, -1, 0x48, 0x8b, 0x07, 0x48, 0x8b, 0x80,
-1, -1, -1, -1, 0x48, 0x39, 0xd0, 0x0f, 0x85,
-1, -1, -1, -1, 0x80, 0xbf
});
can_acquire_function_ = find_signature(module, std::array {
0x55, 0x48, 0x89, 0xe5, 0x41, 0x57, 0x41, 0x56, 0x49, 0x89, 0xf6,
0x41, 0x55, 0x41, 0x54, 0x49, 0x89, 0xcc, 0x53, 0x48, 0x89, 0xfb,
0x48, 0x83, 0xec, 0x78
});
increment_mvp_function_ = find_signature(module, std::array {
0x48, 0x8b, 0x05, -1, -1, -1, -1, 0x80, 0x78, -1, -1,
0x0f, 0x85, -1, -1, -1, -1, 0x55, 0x48, 0x89, 0xe5,
0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54, 0x41, 0x89, 0xf4
});
sound_filter_function_ = find_signature(module, std::array{
0x55, 0x48, 0x89, 0xe5, 0x53, 0x48, 0x89, 0xfb, 0x48, 0x83, 0xec,
-1, 0xe8, -1, -1, -1, -1, 0x48, 0x89, 0xd8, 0x48, 0x8b, 0x5d,
-1, 0xc9, 0xc3, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x48, 0xb8
});
process_movement_function_ = find_signature(module, std::array{
0x55, 0x48, 0x89, 0xe5, 0x41, 0x57, 0x41, 0x56, 0x41, 0x55,
0x49, 0x89, 0xf5, 0x41, 0x54, 0x53, 0x48, 0x89, 0xfb,
0x48, 0x83, 0xec, -1, 0x48, 0x8b, 0x7f
});
game_event_manager_init_function_ = find_signature(module, std::array{
0x55, 0x48, 0x89, 0xe5, 0x53, 0x48, 0x89, 0xfb, 0x48, 0x83,
0xec, -1, 0x48, 0x8b, 0x07, 0xff, 0x50, -1, 0x48, 0x8b, 0x03
});
set_player_pawn_function_ = find_signature(module, std::array{
0x55, 0x48, 0x8d, 0x87, -1, -1, -1, -1, 0x48, 0x89, 0xe5,
0x41, 0x57, 0x41, 0x56, 0x41, 0x89, 0xce, 0x41, 0x55, 0x45, 0x89, 0xcd
});
switch_team_function_ = find_signature(module, std::array{
0x55, 0x48, 0x89, 0xe5, 0x41, 0x54, 0x49, 0x89, 0xfc, 0x89, 0xf7
});
host_say_function_ = find_signature(module, std::array{
0x55, 0x48, 0x89, 0xe5, 0x41, 0x57, 0x49, 0x89, 0xf7,
0x41, 0x56, 0x41, 0x55, 0x41, 0x89, 0xd5
});
accept_input_function_ = find_signature(module, std::array{
0x55, 0x48, 0x89, 0xe5, 0x41, 0x57, 0x49, 0x89, 0xff,
0x41, 0x56, 0x48, 0x8d, 0xbd, -1, -1, -1, -1,
0x41, 0x55, 0x4c, 0x8d, 0xad
});
// Verified unique against the fingerprinted Linux libserver.so. The HUD
// setters store per-player state on custom_hud_layout's networked entity.
keyvalues_constructor_function_ = find_signature(module, std::array{
0x55, 0x48, 0x89, 0xe5, 0x53, 0x48, 0x89, 0xfb, 0x48, 0x89,
0xf7, 0x48, 0x83, 0xec, -1, 0x48, 0x8d, 0x05
});
keyvalues_find_or_create_function_ = find_signature(module, std::array{
0x55, 0x48, 0x89, 0xe5, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54,
0x53, 0x48, 0x83, 0xec, -1, 0x8b, 0x06, 0x66, 0x83, 0x7f,
-1, -1, 0x89, 0x45, -1, 0x8b, 0x46, -1, 0x89, 0x45, -1, 0x0f, 0x8f
});
keyvalues_set_string_function_ = find_signature(module, std::array{
0x55, 0x48, 0x89, 0xe5, 0x41, 0x56, 0x41, 0x55, 0x49, 0x89,
0xd5, 0x41, 0x54, 0x49, 0x89, 0xf4, 0x53, 0x48, 0x89, 0xfb,
0x48, 0x8b, 0x3f
});
hud_set_player_dialog_function_ = find_signature(module, std::array{
0x55, 0x48, 0x89, 0xe5, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54,
0x53, 0x89, 0xf3, 0x48, 0x83, 0xec, -1, 0x85, 0xf6, 0x78, -1,
0x49, 0x89, 0xfd, 0x39, 0xb7, -1, -1, -1, -1, 0x7f, -1,
0x48, 0x83, 0xc4, -1, 0x5b, 0x41, 0x5c, 0x41, 0x5d,
0x41, 0x5e, 0x5d, 0xc3, -1, -1, -1, 0x48, 0x89, 0xd6,
0x31, 0xd2, 0x49, 0x89, 0xce, 0x66, 0x89, 0x55, -1,
0x48, 0x8d, 0x55, -1, 0x4d, 0x89, 0xc4
});
hud_set_player_class_function_ = find_signature(module, std::array{
0x55, 0x48, 0x89, 0xe5, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54,
0x53, 0x89, 0xf3, 0x48, 0x83, 0xec, -1, 0x85, 0xf6, 0x78, -1,
0x49, 0x89, 0xfd, 0x39, 0xb7, -1, -1, -1, -1, 0x7f, -1,
0x48, 0x83, 0xc4, -1, 0x5b, 0x41, 0x5c, 0x41, 0x5d,
0x41, 0x5e, 0x5d, 0xc3, -1, -1, -1, 0x48, 0x89, 0xd6,
0x31, 0xd2, 0x49, 0x89, 0xce, 0x66, 0x89, 0x55, -1,
0x48, 0x8d, 0x55, -1, 0x45, 0x89, 0xc4, 0xe8
});
#endif
if (!terminate_round_function_ || !increment_mvp_function_ ||
!remove_entity_function_ || !take_damage_function_ || !weapon_can_use_function_ || !can_acquire_function_ ||
!create_entity_function_ || !dispatch_spawn_function_ ||
!game_event_manager_init_function_ || !set_player_pawn_function_ ||
!switch_team_function_ || !host_say_function_) {
error = "Required CS2 gameplay functions are unavailable or ambiguous";
return false;
}
resources_ = resources;
network_messages_ = network_messages;
event_system_ = event_system;
server_module_ = module;
#ifdef _WIN32
auto *factory_reference = static_cast<uint8_t *>(find_signature(module,
std::array<int, 17> {0x48, 0x8b, 0x1d, -1, -1, -1, -1, 0x48,
0x85, 0xdb, 0x0f, 0x84, -1, -1, -1, -1, 0xbd}));
if (factory_reference && !resource_factory) {
int32_t relative = 0;
std::memcpy(&relative, factory_reference + 3, sizeof(relative));
auto **head = reinterpret_cast<CBaseGameSystemFactory **>(
factory_reference + 7 + relative);
MEMORY_BASIC_INFORMATION region {};
if (VirtualQuery(head, ®ion, sizeof(region)) &&
region.AllocationBase == module && region.State == MEM_COMMIT) {
CBaseGameSystemFactory::sm_pFirst = head;
resource_factory = new CGameSystemStaticFactory<ShroudfireResourceGameSystem>(
"ShroudfireResourceGameSystem", &resource_game_system);
std::fprintf(stderr, "[Shroudfire] Blood resource game system registered.\n");
}
}
#elif defined(__linux__)
auto *factory_reference = static_cast<uint8_t *>(find_signature(module,
std::array<int, 13> {0x4c, 0x8b, 0x35, -1, -1, -1, -1,
0x4d, 0x85, 0xf6, 0x75, -1, 0xe9}));
if (factory_reference && !resource_factory) {
int32_t relative = 0;
std::memcpy(&relative, factory_reference + 3, sizeof(relative));
auto **head = reinterpret_cast<CBaseGameSystemFactory **>(
factory_reference + 7 + relative);
Dl_info region {};
if (dladdr(head, ®ion) && region.dli_fbase == module) {
CBaseGameSystemFactory::sm_pFirst = head;
resource_factory = new CGameSystemStaticFactory<ShroudfireResourceGameSystem>(
"ShroudfireResourceGameSystem", &resource_game_system);
std::fprintf(stderr, "[Shroudfire] Blood resource game system registered.\n");
}
}
#endif
ready_ = true;
return true;
}
CEntityInstance *EngineAdapter::controller(int slot) const {
if (!ready_ || !resources_ || slot < 1 || slot > 64) return nullptr;
auto *system = static_cast<CGameEntitySystem *>(entity_system());
return system ? system->GetEntityInstance(CEntityIndex(slot)) : nullptr;
}
CEntityInstance *EngineAdapter::pawn(int slot) const {
auto *system = static_cast<CGameEntitySystem *>(entity_system());
CEntityInstance *owner = controller(slot);
if (!system || !owner) return nullptr;
const auto &pawn_handle = *reinterpret_cast<const CEntityHandle *>(
reinterpret_cast<const char *>(owner) + fields_.player_pawn);
CEntityInstance *entity = system->GetEntityInstance(pawn_handle);
if (!entity || !entity->m_pEntity) return nullptr;
const auto &owner_handle = *reinterpret_cast<const CEntityHandle *>(
reinterpret_cast<const char *>(entity) + fields_.pawn_controller);
return system->GetEntityInstance(owner_handle) == owner ? entity : nullptr;
}
int EngineAdapter::sound_source_slot(int entity_index) const {
auto *system = static_cast<CGameEntitySystem *>(entity_system());
if (!system || entity_index <= 0 || entity_index >= MAX_TOTAL_ENTITIES) return 0;
CEntityInstance *source = system->GetEntityInstance(CEntityIndex(entity_index));
for (int depth = 0; source && source->m_pEntity && depth < 3; ++depth) {
for (int slot = 1; slot <= 64; ++slot)
if (source == controller(slot) || source == pawn(slot)) return slot;
const auto &owner = *reinterpret_cast<const CEntityHandle *>(
reinterpret_cast<const char *>(source) + fields_.owner_entity);
CEntityInstance *next = system->GetEntityInstance(owner);
if (next == source) break;
source = next;
}
return 0;
}
void *EngineAdapter::entity_system() const {
if (!ready_ || !resources_) return nullptr;
CGameEntitySystem *system = nullptr;
std::memcpy(&system, reinterpret_cast<const char *>(resources_) + kEntitySystemOffset,
sizeof(system));
return system;
}
bool EngineAdapter::collect_visual_group(int slot, int expected_team,
std::array<int, kMaxVisualEntities> &indices,
size_t &count) const {
count = 0;
auto *system = static_cast<CGameEntitySystem *>(entity_system());
CEntityInstance *pawn = this->pawn(slot);
if (!system || !pawn) return false;
if (*reinterpret_cast<const int *>(reinterpret_cast<const char *>(pawn) + fields_.health) <= 0 ||
*reinterpret_cast<const uint8_t *>(reinterpret_cast<const char *>(pawn) + fields_.life_state) != 0 ||
*reinterpret_cast<const uint8_t *>(reinterpret_cast<const char *>(pawn) + fields_.team) !=
expected_team) return false;
const auto append = [&](CEntityHandle handle) {
if (!handle.IsValid()) return true;
CEntityInstance *entity = system->GetEntityInstance(handle);
// Weapon handles can outlive a dropped or removed weapon for a frame.
// That stale entry must not make the entire Invisant pawn visible.
if (!entity || !entity->m_pEntity) return true;
const int index = entity->m_pEntity->m_EHandle.GetEntryIndex();
if (index <= 0 || index >= MAX_EDICTS) return true;
for (size_t i = 0; i < count; ++i) if (indices[i] == index) return true;
if (count >= indices.size()) return false;
indices[count++] = index;
return true;
};
const auto append_handles = [&](const void *base, uint32_t offset, int maximum) {
const auto *handles = reinterpret_cast<const CUtlVector<CEntityHandle> *>(
reinterpret_cast<const char *>(base) + offset);
const int entries = handles->Count();
if (entries < 0 || entries > maximum) return false;
for (int i = 0; i < entries; ++i)
if (!append((*handles)[i])) return false;
return true;
};
if (!append(pawn->m_pEntity->GetRefEHandle())) return false;
void *weapons = *reinterpret_cast<void *const *>(
reinterpret_cast<const char *>(pawn) + fields_.weapon_services);
if (!weapons || !append(*reinterpret_cast<CEntityHandle *>(
reinterpret_cast<char *>(weapons) + fields_.active_weapon)) ||
!append(*reinterpret_cast<CEntityHandle *>(
reinterpret_cast<char *>(weapons) + fields_.last_weapon)) ||
!append_handles(weapons, fields_.weapons, 64) ||
!append_handles(pawn, fields_.wearables, 32)) return false;
void *hostage = *reinterpret_cast<void *const *>(
reinterpret_cast<const char *>(pawn) + fields_.hostage_services);
if (hostage && !append(*reinterpret_cast<CEntityHandle *>(
reinterpret_cast<char *>(hostage) + fields_.carried_hostage_prop))) return false;
return count > 0;
}
bool EngineAdapter::set_reveal_alpha(int slot, int expected_team, int alpha) {
if (!ready_ || !render_fields_ok_) return false;
auto *system = static_cast<CGameEntitySystem *>(entity_system());
std::array<int, kMaxVisualEntities> indices {};
size_t count = 0;
if (!system || !collect_visual_group(slot, expected_team, indices, count)) return false;
for (size_t index = 0; index < count; ++index) {
CEntityInstance *entity = system->GetEntityInstance(CEntityIndex(indices[index]));
if (!entity || !entity->m_pEntity) return false;
auto *base = reinterpret_cast<char *>(entity);
auto *mode = reinterpret_cast<uint8_t *>(base + fields_.render_mode);
auto *color = reinterpret_cast<uint8_t *>(base + fields_.render_color);
const uint32_t handle = entity->m_pEntity->GetRefEHandle().ToInt();
auto saved = original_reveal_rendering_.find(handle);
if (saved == original_reveal_rendering_.end()) {
RevealRenderState state;
state.mode = *mode;
std::memcpy(state.color.data(), color, state.color.size());
saved = original_reveal_rendering_.emplace(handle, state).first;
}
const uint8_t next_mode = alpha >= 255 ? saved->second.mode : kRenderTransAlpha;
const uint8_t next_alpha = static_cast<uint8_t>(
saved->second.color[3] * std::clamp(alpha, 0, 255) / 255);
if (*mode != next_mode) {
if (!mark_state_changed(entity, fields_.render_mode)) return false;
*mode = next_mode;
}
if (color[3] != next_alpha) {
if (!mark_state_changed(entity, fields_.render_color)) return false;
color[3] = next_alpha;
}
}
return true;
}
void EngineAdapter::restore_reveal_rendering() {
auto *system = static_cast<CGameEntitySystem *>(entity_system());
if (system && render_fields_ok_) {
for (const auto &[handle, saved] : original_reveal_rendering_) {
CEntityInstance *entity = system->GetEntityInstance(CEntityHandle(handle));
if (!entity || !entity->m_pEntity) continue;
auto *base = reinterpret_cast<char *>(entity);
if (mark_state_changed(entity, fields_.render_mode))
*reinterpret_cast<uint8_t *>(base + fields_.render_mode) = saved.mode;
if (mark_state_changed(entity, fields_.render_color))
std::memcpy(base + fields_.render_color, saved.color.data(), saved.color.size());
}
}
original_reveal_rendering_.clear();
}
bool EngineAdapter::mark_state_changed(CEntityInstance *entity, uint32_t offset) const {
#if defined(_WIN32) || defined(__linux__)
if (!entity) return false;
void *function = (*reinterpret_cast<void ***>(entity))[kSetStateChangedVtable];
if (!executable_in_module(function, server_module_)) return false;
StateChangeInfo info(offset);
reinterpret_cast<void (*)(void *, StateChangeInfo *)>(function)(entity, &info);
return true;
#else
(void)entity; (void)offset;
return false;
#endif
}
bool EngineAdapter::heal(int slot, int amount, int maximum_health) const {
CEntityInstance *entity = pawn(slot);
if (!entity || amount <= 0 || maximum_health <= 0) return false;
auto *value = reinterpret_cast<int *>(reinterpret_cast<char *>(entity) + fields_.health);
if (*value <= 0) return false;
const int updated = (std::min)(maximum_health, *value + amount);
if (updated == *value) return true;
if (!mark_state_changed(entity, fields_.health)) return false;
*value = updated;
return true;
}
bool EngineAdapter::restore_armor(int slot, int amount, int maximum_armor) const {
CEntityInstance *entity = pawn(slot);
if (!entity || amount <= 0 || maximum_armor <= 0) return false;
auto *value = reinterpret_cast<int *>(reinterpret_cast<char *>(entity) + fields_.armor);
const int updated = (std::min)(maximum_armor, *value + amount);
if (updated == *value) return true;
if (!mark_state_changed(entity, fields_.armor)) return false;
*value = updated;
return true;
}
bool EngineAdapter::apply_spawn_attributes(int slot, int team, bool invisant_role, int health, int armor,
bool helmet, float gravity_scale) const {
CEntityInstance *entity = pawn(slot);
if (!entity || health < 1 || armor < 0 || gravity_scale <= 0.0f ||
*reinterpret_cast<const uint8_t *>(reinterpret_cast<const char *>(entity) + fields_.team) != team)
return false;
auto *health_value = reinterpret_cast<int *>(reinterpret_cast<char *>(entity) + fields_.health);
if (*health_value <= 0) return false;
if (*health_value != health) {
if (!mark_state_changed(entity, fields_.health)) return false;