-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_reader.py
More file actions
1381 lines (1240 loc) · 53.1 KB
/
Copy pathmemory_reader.py
File metadata and controls
1381 lines (1240 loc) · 53.1 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
"""Direct CS2 memory reader for realtime inference."""
from __future__ import annotations
import json
import logging
import struct
import time
from functools import lru_cache
from pathlib import Path
from typing import Any
import pymem
import pymem.process
logger = logging.getLogger(__name__)
REPO_ROOT = Path(__file__).resolve().parents[2]
MEMORY_DATA_DIR = REPO_ROOT / "src" / "memory_reader"
OFFSETS_PATH = MEMORY_DATA_DIR / "offsets.json"
CLIENT_DLL_JSON = MEMORY_DATA_DIR / "client_dll.json"
ENTITY_LIST_STRIDE = 0x70
MAX_PLAYERS = 64
TEAM_NAMES = {2: "T", 3: "CT", 1: "SPEC", 0: "NONE"}
_INVALID_HANDLE = 0xFFFFFFFF
_TEAM_SCAN_LIMIT = 256
_CT_TEAM_NAMES = {"CT", "COUNTERTERRORIST", "COUNTER-TERRORIST", "COUNTER_TERRORIST"}
_T_TEAM_NAMES = {"T", "TERRORIST", "TERRORISTS"}
_SMOKE_DURATION_SECONDS = 18.0
_MOLOTOV_DURATION_SECONDS = 7.0
_MAX_INVENTORY_WEAPONS = 64
_MAX_INFERNO_POINTS = 64
# Derive CT/T match score from C_CSGameRules.m_iMatchStats_RoundResults — a
# 30-slot int32 array where each entry is a Source 2 RoundEndReason_t enum.
# Demo playback exposes no C_CSTeam entity in the entity list, so the old
# designer-name scan always returned 0,0. Using the gamerules array lets us
# recover cumulative scores in both live and demo modes.
_ROUND_RESULT_SLOTS = 30
_ROUND_RESULT_CT_WIN = {5, 6, 7, 8, 11, 12, 14, 17}
_ROUND_RESULT_T_WIN = {1, 3, 4, 9, 13, 15, 18}
def _require_offset(table: dict[str, Any], module_name: str, offset_name: str) -> int:
try:
return int(table[module_name][offset_name])
except KeyError as exc: # pragma: no cover - configuration failure
raise RuntimeError(f"offsets.json missing {module_name}.{offset_name}") from exc
def _require_field(classes: dict[str, Any], class_name: str, field_name: str) -> int:
try:
return int(classes[class_name]["fields"][field_name])
except KeyError as exc: # pragma: no cover - configuration failure
raise RuntimeError(f"client_dll.json missing {class_name}.{field_name}") from exc
def _optional_field(classes: dict[str, Any], class_name: str, field_name: str) -> int | None:
field = classes.get(class_name, {}).get("fields", {}).get(field_name)
return int(field) if field is not None else None
@lru_cache(maxsize=1)
def load_offsets() -> tuple[dict[str, Any], dict[str, Any]]:
"""Load static module offsets and client class field offsets once."""
with OFFSETS_PATH.open("r", encoding="utf-8") as handle:
offsets = json.load(handle)
with CLIENT_DLL_JSON.open("r", encoding="utf-8") as handle:
classes = json.load(handle)["client.dll"]["classes"]
return offsets, classes
def _safe_read_string(pm: pymem.Pymem, ptr: int, max_len: int = 64) -> str:
if ptr == 0:
return ""
try:
return pm.read_string(ptr, max_len)
except Exception:
return ""
def _clean_inline_string(text: str) -> str:
return text.split("\x00", 1)[0].strip()
def read_vec3(pm: pymem.Pymem, addr: int) -> tuple[float, float, float]:
"""Read a Source2 Vector at the given address."""
buf = pm.read_bytes(addr, 12)
return struct.unpack("<fff", buf)
def read_player_name(pm: pymem.Pymem, ctrl: int, name_offset: int) -> str:
"""Probe string_t / CUtlString-like storage layouts for player names."""
field_addr = ctrl + name_offset
try:
p1 = pm.read_longlong(field_addr)
if 0x10000 < p1 < 0x7FFFFFFFFFFF:
text = _clean_inline_string(pm.read_string(p1, 64))
if text and text.isprintable():
return text
except Exception:
pass
try:
p1 = pm.read_longlong(field_addr)
if 0x10000 < p1 < 0x7FFFFFFFFFFF:
p2 = pm.read_longlong(p1)
if 0x10000 < p2 < 0x7FFFFFFFFFFF:
text = _clean_inline_string(pm.read_string(p2, 64))
if text and text.isprintable():
return text
except Exception:
pass
try:
text = _clean_inline_string(pm.read_string(field_addr, 64))
if text and text.isprintable():
return text
except Exception:
pass
return ""
def resolve_handle(pm: pymem.Pymem, ent_list_base: int, handle: int) -> int:
"""Resolve a CHandle<T> through the verified Source2 entity-list layout."""
if handle in (0, _INVALID_HANDLE):
return 0
index = handle & 0x7FFF
block = pm.read_longlong(ent_list_base + 0x8 * (index >> 9) + 0x10)
if block == 0:
return 0
return pm.read_longlong(block + ENTITY_LIST_STRIDE * (index & 0x1FF))
def entity_class_name(
pm: pymem.Pymem,
entity_ptr: int,
classes: dict[str, Any] | None = None,
) -> str:
"""Read CEntityIdentity.m_designerName for an entity pointer."""
if entity_ptr == 0:
return ""
if classes is None:
_, classes = load_offsets()
try:
identity_offset = _require_field(classes, "CEntityInstance", "m_pEntity")
designer_offset = _require_field(classes, "CEntityIdentity", "m_designerName")
identity = pm.read_longlong(entity_ptr + identity_offset)
if identity == 0:
return ""
name_ptr = pm.read_longlong(identity + designer_offset)
if name_ptr == 0:
return ""
return _clean_inline_string(pm.read_string(name_ptr, 64))
except Exception:
return ""
class CS2MemoryReader:
"""Attach to CS2 and read player/map state directly from memory."""
def __init__(
self,
pm: pymem.Pymem,
client_base: int,
engine_base: int,
offsets: dict[str, Any],
classes: dict[str, Any],
) -> None:
self.pm = pm
self.client_base = int(client_base)
self.engine_base = int(engine_base)
self.offsets = offsets
self.classes = classes
self._warned: set[str] = set()
self._team_handles: dict[str, int] = {}
self._smoke_seen_at: dict[int, tuple[int, float]] = {}
self._inferno_seen_at: dict[int, tuple[int, float]] = {}
self._synthetic_molotovs: list[dict[str, float | int]] = []
self._molotov_projectile_state: dict[int, dict[str, float | int | bool]] = {}
self._client_offsets = {
"dwEntityList": _require_offset(offsets, "client.dll", "dwEntityList"),
"dwGameRules": _require_offset(offsets, "client.dll", "dwGameRules"),
"dwPlantedC4": _require_offset(offsets, "client.dll", "dwPlantedC4"),
"dwWeaponC4": _require_offset(offsets, "client.dll", "dwWeaponC4"),
}
self._engine_offsets = {
"dwBuildNumber": _require_offset(offsets, "engine2.dll", "dwBuildNumber"),
}
self._controller_fields = {
name: _require_field(classes, "CCSPlayerController", name)
for name in (
"m_hPlayerPawn",
"m_bPawnIsAlive",
"m_iPawnHealth",
"m_iPawnArmor",
"m_bPawnHasHelmet",
"m_bPawnHasDefuser",
"m_pInGameMoneyServices",
)
}
self._base_controller_fields = {
name: _require_field(classes, "CBasePlayerController", name)
for name in ("m_iszPlayerName", "m_steamID")
}
self._pawn_fields = {
name: _require_field(classes, "C_CSPlayerPawn", name)
for name in ("m_angEyeAngles", "m_bIsScoped", "m_bIsDefusing", "m_pClippingWeapon")
}
self._base_pawn_fields = {
"m_pWeaponServices": _require_field(classes, "C_BasePlayerPawn", "m_pWeaponServices"),
}
self._base_entity_fields = {
name: _require_field(classes, "C_BaseEntity", name)
for name in ("m_iTeamNum", "m_pGameSceneNode", "m_hOwnerEntity")
}
self._scene_node_fields = {
"m_vecAbsOrigin": _require_field(classes, "CGameSceneNode", "m_vecAbsOrigin"),
}
self._weapon_service_fields = {
name: _require_field(classes, "CPlayer_WeaponServices", name)
for name in ("m_hMyWeapons", "m_hActiveWeapon")
}
self._planted_c4_fields = {
name: _require_field(classes, "C_PlantedC4", name)
for name in ("m_bBombTicking", "m_bBombDefused", "m_nBombSite")
}
self._smoke_fields = {
name: _require_field(classes, "C_SmokeGrenadeProjectile", name)
for name in ("m_bDidSmokeEffect", "m_nSmokeEffectTickBegin", "m_vSmokeDetonationPos")
}
self._inferno_fields = {
name: _require_field(classes, "C_Inferno", name)
for name in ("m_bFireIsBurning", "m_nFireLifetime", "m_nFireEffectTickBegin", "m_firePositions", "m_fireCount")
}
self._grenade_fields = {
name: _require_field(classes, "C_BaseGrenade", name)
for name in ("m_bIsLive", "m_flDetonateTime", "m_hThrower")
}
self._grenade_projectile_fields = {
name: _require_field(classes, "C_BaseCSGrenadeProjectile", name)
for name in (
"m_bExplodeEffectBegan",
"m_nExplodeEffectTickBegin",
"m_vecExplodeEffectOrigin",
"vecLastTrailLinePos",
"m_nBounces",
)
}
self._molotov_projectile_fields = {
"m_bIsIncGrenade": _require_field(classes, "C_MolotovProjectile", "m_bIsIncGrenade"),
}
self._money_fields = {
"m_iAccount": _require_field(classes, "CCSPlayerController_InGameMoneyServices", "m_iAccount"),
}
self._game_rules_fields = classes.get("C_CSGameRules", {}).get("fields", {})
self._game_rules_proxy_fields = classes.get("C_CSGameRulesProxy", {}).get("fields", {})
self._team_fields = classes.get("C_Team", {}).get("fields", {})
@classmethod
def attach(cls) -> CS2MemoryReader:
"""Attach to a live cs2.exe process and load the offset JSON payloads."""
try:
pm = pymem.Pymem("cs2.exe")
except pymem.exception.ProcessNotFound as exc:
raise RuntimeError("cs2.exe not running") from exc
except Exception as exc: # pragma: no cover - environment dependent
raise RuntimeError(f"failed to attach to cs2.exe: {exc}") from exc
client = pymem.process.module_from_name(pm.process_handle, "client.dll")
if client is None:
raise RuntimeError("client.dll not located in cs2.exe")
engine = pymem.process.module_from_name(pm.process_handle, "engine2.dll")
if engine is None:
raise RuntimeError("engine2.dll not located in cs2.exe")
offsets, classes = load_offsets()
return cls(
pm=pm,
client_base=client.lpBaseOfDll,
engine_base=engine.lpBaseOfDll,
offsets=offsets,
classes=classes,
)
def _warn_once(self, key: str, message: str, *args: Any) -> None:
if key in self._warned:
return
self._warned.add(key)
logger.warning(message, *args)
def _read_optional_int(
self,
addr: int,
class_name: str,
field_name: str,
*,
warn_key: str | None = None,
default: int | None = None,
) -> int | None:
field_offset = _optional_field(self.classes, class_name, field_name)
if field_offset is None:
if warn_key is not None:
self._warn_once(warn_key, "client_dll.json missing %s.%s", class_name, field_name)
return default
try:
return int(self.pm.read_int(addr + field_offset))
except Exception as exc:
if warn_key is not None:
self._warn_once(warn_key, "failed reading %s.%s: %s", class_name, field_name, exc)
return default
def _read_optional_float(
self,
addr: int,
class_name: str,
field_name: str,
*,
warn_key: str | None = None,
default: float | None = None,
) -> float | None:
field_offset = _optional_field(self.classes, class_name, field_name)
if field_offset is None:
if warn_key is not None:
self._warn_once(warn_key, "client_dll.json missing %s.%s", class_name, field_name)
return default
try:
return float(self.pm.read_float(addr + field_offset))
except Exception as exc:
if warn_key is not None:
self._warn_once(warn_key, "failed reading %s.%s: %s", class_name, field_name, exc)
return default
def _read_optional_bool(
self,
addr: int,
class_name: str,
field_name: str,
*,
warn_key: str | None = None,
default: bool = False,
) -> bool:
field_offset = _optional_field(self.classes, class_name, field_name)
if field_offset is None:
if warn_key is not None:
self._warn_once(warn_key, "client_dll.json missing %s.%s", class_name, field_name)
return default
try:
return bool(self.pm.read_uchar(addr + field_offset))
except Exception as exc:
if warn_key is not None:
self._warn_once(warn_key, "failed reading %s.%s: %s", class_name, field_name, exc)
return default
def _resolve_game_rules_ptr(self) -> int:
try:
ptr = self.pm.read_longlong(self.client_base + self._client_offsets["dwGameRules"])
except Exception as exc:
self._warn_once("dwGameRules.read", "failed reading client.dll.dwGameRules: %s", exc)
return 0
if ptr == 0:
self._warn_once("dwGameRules.null", "client.dll.dwGameRules resolved to null")
return 0
candidates = [ptr]
proxy_offset = self._game_rules_proxy_fields.get("m_pGameRules")
if proxy_offset is not None:
try:
proxy_target = self.pm.read_longlong(ptr + int(proxy_offset))
except Exception:
proxy_target = 0
if proxy_target:
candidates.append(proxy_target)
for candidate in candidates:
if self._looks_like_game_rules(candidate):
return candidate
return candidates[0]
def _looks_like_game_rules(self, addr: int) -> bool:
total_rounds_offset = _optional_field(self.classes, "C_CSGameRules", "m_totalRoundsPlayed")
round_time_offset = _optional_field(self.classes, "C_CSGameRules", "m_iRoundTime")
if total_rounds_offset is None or round_time_offset is None:
return addr != 0
try:
total_rounds = self.pm.read_int(addr + total_rounds_offset)
round_time = self.pm.read_int(addr + round_time_offset)
except Exception:
return False
return -1 <= total_rounds <= 60 and 0 <= round_time <= 180
def _read_match_scores(self, rules_ptr: int) -> tuple[int, int]:
"""Derive CT/T scores from C_CSGameRules.m_iMatchStats_RoundResults.
Demo playback exposes no C_CSTeam entity, so cumulative scores must
be reconstructed from the per-round result enum array bounded by
m_totalRoundsPlayed. Unknown enum values (e.g. GameStart=16, Draw=10)
count toward neither side.
"""
array_offset = _optional_field(self.classes, "C_CSGameRules", "m_iMatchStats_RoundResults")
total_offset = _optional_field(self.classes, "C_CSGameRules", "m_totalRoundsPlayed")
if array_offset is None or total_offset is None:
self._warn_once(
"match_scores.fields",
"client_dll.json missing m_iMatchStats_RoundResults or m_totalRoundsPlayed",
)
return 0, 0
try:
total_rounds = int(self.pm.read_int(rules_ptr + total_offset))
except Exception:
total_rounds = 0
total_rounds = max(0, min(total_rounds, _ROUND_RESULT_SLOTS))
if total_rounds == 0:
return 0, 0
try:
raw = self.pm.read_bytes(rules_ptr + array_offset, total_rounds * 4)
except Exception as exc:
self._warn_once(
"match_scores.read",
"failed reading RoundResults array: %s",
exc,
)
return 0, 0
values = struct.unpack(f"<{total_rounds}i", raw)
ct = sum(1 for v in values if v in _ROUND_RESULT_CT_WIN)
t = sum(1 for v in values if v in _ROUND_RESULT_T_WIN)
return ct, t
def _read_team_scores(self, ent_list_base: int) -> tuple[int, int]:
score_offset = _optional_field(self.classes, "C_Team", "m_iScore")
name_offset = _optional_field(self.classes, "C_Team", "m_szTeamname")
if score_offset is None or name_offset is None:
self._warn_once(
"team_scores.fields",
"client_dll.json missing C_Team score/name fields; team scores will default to 0",
)
return 0, 0
scores: dict[str, int] = {}
found_handles: dict[str, int] = {}
for side, team_handle in tuple(self._team_handles.items()):
entity_ptr = resolve_handle(self.pm, ent_list_base, team_handle)
if entity_ptr == 0:
continue
team_name = _clean_inline_string(
_safe_read_string(self.pm, entity_ptr + name_offset, 32)
).upper()
if not team_name:
continue
try:
score_value = max(0, int(self.pm.read_int(entity_ptr + score_offset)))
except Exception:
continue
scores[side] = score_value
found_handles[side] = team_handle
if "CT" in scores and "T" in scores:
self._team_handles = found_handles
return scores["CT"], scores["T"]
for handle in range(1, _TEAM_SCAN_LIMIT + 1):
entity_ptr = resolve_handle(self.pm, ent_list_base, handle)
if entity_ptr == 0:
continue
team_name = _clean_inline_string(
_safe_read_string(self.pm, entity_ptr + name_offset, 32)
).upper()
if team_name in _CT_TEAM_NAMES:
side = "CT"
elif team_name in _T_TEAM_NAMES:
side = "T"
else:
continue
try:
score_value = max(0, int(self.pm.read_int(entity_ptr + score_offset)))
except Exception:
continue
scores[side] = score_value
found_handles[side] = handle
if "CT" in scores and "T" in scores:
self._team_handles = found_handles
return scores["CT"], scores["T"]
self._warn_once(
"team_scores.scan",
"unable to resolve live C_Team scores from the entity list; team scores will default to 0",
)
self._team_handles = found_handles
return scores.get("CT", 0), scores.get("T", 0)
def _read_map_name(self, rules_ptr: int) -> str:
for field_name in ("m_nMapName", "m_szMapName", "m_mapName"):
field_offset = _optional_field(self.classes, "C_CSGameRules", field_name)
if field_offset is None:
continue
text = _clean_inline_string(_safe_read_string(self.pm, rules_ptr + field_offset, 64))
if text:
return text
self._warn_once(
"map_name.field",
"no C_CSGameRules map-name field found in client_dll.json; map_name will default to empty",
)
return ""
def _read_entity_list_base(self) -> int:
try:
return int(self.pm.read_longlong(self.client_base + self._client_offsets["dwEntityList"]))
except Exception as exc:
self._warn_once("dwEntityList.read", "failed reading client.dll.dwEntityList: %s", exc)
return 0
def _read_utl_vector(self, addr: int) -> tuple[int, int]:
try:
size = int(self.pm.read_int(addr))
elems = int(self.pm.read_longlong(addr + 8))
except Exception:
return 0, 0
if size <= 0 or elems == 0:
return 0, 0
return size, elems
def _read_entity_origin(self, entity_ptr: int) -> tuple[float, float, float]:
if entity_ptr == 0:
return 0.0, 0.0, 0.0
try:
scene_node = self.pm.read_longlong(entity_ptr + self._base_entity_fields["m_pGameSceneNode"])
if scene_node:
return read_vec3(self.pm, scene_node + self._scene_node_fields["m_vecAbsOrigin"])
except Exception:
pass
return 0.0, 0.0, 0.0
@staticmethod
def _empty_inventory() -> dict[str, Any]:
return {
"weapons": [],
"active_weapon_class": None,
"has_smoke": False,
"has_flash": False,
"has_he": False,
"has_molotov": False,
"has_c4": False,
}
def _read_player_inventory_with_entity_list(
self,
pawn: int,
ent_list_base: int,
) -> dict[str, Any]:
inventory = self._empty_inventory()
if pawn == 0 or ent_list_base == 0:
return inventory
try:
weapon_services = self.pm.read_longlong(pawn + self._base_pawn_fields["m_pWeaponServices"])
except Exception:
return inventory
if weapon_services == 0:
return inventory
size, elems = self._read_utl_vector(weapon_services + self._weapon_service_fields["m_hMyWeapons"])
weapons: list[str] = []
for slot in range(min(size, _MAX_INVENTORY_WEAPONS)):
try:
handle = int(self.pm.read_uint(elems + 4 * slot))
except Exception:
continue
weapon_ptr = resolve_handle(self.pm, ent_list_base, handle)
if weapon_ptr == 0:
continue
class_name = entity_class_name(self.pm, weapon_ptr, self.classes)
if class_name:
weapons.append(class_name)
active_weapon_ptr = 0
try:
active_weapon_ptr = int(self.pm.read_longlong(pawn + self._pawn_fields["m_pClippingWeapon"]))
except Exception:
active_weapon_ptr = 0
if active_weapon_ptr == 0:
try:
active_handle = int(
self.pm.read_uint(weapon_services + self._weapon_service_fields["m_hActiveWeapon"])
)
except Exception:
active_handle = 0
active_weapon_ptr = resolve_handle(self.pm, ent_list_base, active_handle)
active_weapon_class = None
if active_weapon_ptr:
class_name = entity_class_name(self.pm, active_weapon_ptr, self.classes)
if class_name:
active_weapon_class = class_name
weapon_set = set(weapons)
inventory["weapons"] = weapons
inventory["active_weapon_class"] = active_weapon_class
inventory["has_smoke"] = "weapon_smokegrenade" in weapon_set
inventory["has_flash"] = "weapon_flashbang" in weapon_set
inventory["has_he"] = "weapon_hegrenade" in weapon_set
inventory["has_molotov"] = (
"weapon_molotov" in weapon_set or "weapon_incgrenade" in weapon_set
)
inventory["has_c4"] = "weapon_c4" in weapon_set
return inventory
def _projectile_remain(
self,
cache: dict[int, tuple[int, float]],
entity_ptr: int,
tick_begin: int,
duration_seconds: float,
) -> float:
if duration_seconds <= 0.0:
return 0.0
now = time.monotonic()
cached = cache.get(entity_ptr)
if cached is None or cached[0] != tick_begin:
cache[entity_ptr] = (tick_begin, now)
return 1.0
elapsed = max(0.0, now - cached[1])
return max(0.0, 1.0 - elapsed / duration_seconds)
@staticmethod
def _trim_projectile_cache(
cache: dict[int, tuple[int, float]],
active_entities: set[int],
) -> None:
stale = [entity_ptr for entity_ptr in cache if entity_ptr not in active_entities]
for entity_ptr in stale:
cache.pop(entity_ptr, None)
def _seed_synthetic_molotov(
self,
*,
x: float,
y: float,
duration_seconds: float,
signature: int,
) -> None:
if x == 0.0 and y == 0.0:
return
now = time.monotonic()
duration = max(1.0, float(duration_seconds or _MOLOTOV_DURATION_SECONDS))
for item in self._synthetic_molotovs:
item_x = float(item.get("x", 0.0))
item_y = float(item.get("y", 0.0))
item_signature = int(item.get("signature", 0))
item_started = float(item.get("started_at", 0.0))
item_duration = max(1.0, float(item.get("duration", _MOLOTOV_DURATION_SECONDS)))
if item_signature and signature and item_signature == signature:
return
if abs(item_x - x) <= 96.0 and abs(item_y - y) <= 96.0 and (now - item_started) <= item_duration:
return
self._synthetic_molotovs.append(
{
"x": float(x),
"y": float(y),
"started_at": now,
"duration": duration,
"signature": int(signature),
}
)
def _active_synthetic_molotovs(self) -> list[tuple[float, float, float]]:
now = time.monotonic()
active: list[tuple[float, float, float]] = []
keep: list[dict[str, float | int]] = []
for item in self._synthetic_molotovs:
duration = max(1.0, float(item.get("duration", _MOLOTOV_DURATION_SECONDS)))
started_at = float(item.get("started_at", 0.0))
elapsed = max(0.0, now - started_at)
remain = max(0.0, 1.0 - elapsed / duration)
if remain <= 0.0:
continue
keep.append(item)
active.append((float(item.get("x", 0.0)), float(item.get("y", 0.0)), remain))
self._synthetic_molotovs = keep
active.sort(key=lambda entry: (-entry[2], entry[0], entry[1]))
return active
def _read_inferno_candidate(self, entity_ptr: int, class_name: str) -> dict[str, Any] | None:
lowered = class_name.lower()
if not any(token in lowered for token in ("inferno", "molotov", "incendiary")):
return None
inferno_parent_positions = _optional_field(self.classes, "C_Inferno", "m_fireParentPositions")
inferno_post_effect = _optional_field(self.classes, "C_Inferno", "m_bInPostEffectTime")
inferno_type_field = _optional_field(self.classes, "C_Inferno", "m_nInfernoType")
inferno_min_bounds = _optional_field(self.classes, "C_Inferno", "m_minBounds")
inferno_max_bounds = _optional_field(self.classes, "C_Inferno", "m_maxBounds")
is_burning = False
in_post_effect = False
fire_count = 0
tick_begin = 0
fire_lifetime = 0.0
inferno_type = -1
explode_effect_began = False
is_live = False
is_inc_grenade = False
bounces = 0
synthetic_seedable = False
if "inferno" in lowered:
try:
is_burning = bool(
self.pm.read_uchar(entity_ptr + self._inferno_fields["m_bFireIsBurning"])
)
except Exception:
is_burning = False
if inferno_post_effect is not None:
try:
in_post_effect = bool(self.pm.read_uchar(entity_ptr + inferno_post_effect))
except Exception:
in_post_effect = False
try:
fire_count = max(
0,
min(
int(self.pm.read_int(entity_ptr + self._inferno_fields["m_fireCount"])),
_MAX_INFERNO_POINTS,
),
)
except Exception:
fire_count = 0
try:
tick_begin = int(self.pm.read_int(entity_ptr + self._inferno_fields["m_nFireEffectTickBegin"]))
except Exception:
tick_begin = 0
try:
fire_lifetime = float(self.pm.read_float(entity_ptr + self._inferno_fields["m_nFireLifetime"]))
except Exception:
fire_lifetime = 0.0
if inferno_type_field is not None:
try:
inferno_type = int(self.pm.read_int(entity_ptr + inferno_type_field))
except Exception:
inferno_type = -1
elif lowered == "molotov_projectile":
try:
explode_effect_began = bool(
self.pm.read_uchar(entity_ptr + self._grenade_projectile_fields["m_bExplodeEffectBegan"])
)
except Exception:
explode_effect_began = False
try:
tick_begin = int(
self.pm.read_int(entity_ptr + self._grenade_projectile_fields["m_nExplodeEffectTickBegin"])
)
except Exception:
tick_begin = 0
try:
is_live = bool(self.pm.read_uchar(entity_ptr + self._grenade_fields["m_bIsLive"]))
except Exception:
is_live = False
try:
is_inc_grenade = bool(
self.pm.read_uchar(entity_ptr + self._molotov_projectile_fields["m_bIsIncGrenade"])
)
except Exception:
is_inc_grenade = False
try:
bounces = max(0, int(self.pm.read_int(entity_ptr + self._grenade_projectile_fields["m_nBounces"])))
except Exception:
bounces = 0
x = 0.0
y = 0.0
position_source = ""
points: list[tuple[float, float]] = []
if "inferno" in lowered and fire_count > 0:
base_addrs = [entity_ptr + self._inferno_fields["m_firePositions"]]
if inferno_parent_positions is not None:
base_addrs.append(entity_ptr + inferno_parent_positions)
for base_addr in base_addrs:
if points:
break
for flame_idx in range(fire_count):
try:
px, py, _ = read_vec3(self.pm, base_addr + flame_idx * 12)
except Exception:
continue
if px == 0.0 and py == 0.0:
continue
points.append((float(px), float(py)))
if points:
x = sum(point[0] for point in points) / len(points)
y = sum(point[1] for point in points) / len(points)
position_source = "fire_positions"
if (x == 0.0 and y == 0.0) and "inferno" in lowered:
if inferno_min_bounds is not None and inferno_max_bounds is not None:
try:
min_x, min_y, _ = read_vec3(self.pm, entity_ptr + inferno_min_bounds)
max_x, max_y, _ = read_vec3(self.pm, entity_ptr + inferno_max_bounds)
x = float((min_x + max_x) / 2.0)
y = float((min_y + max_y) / 2.0)
if x != 0.0 or y != 0.0:
position_source = "bounds_center"
except Exception:
x = 0.0
y = 0.0
if lowered == "molotov_projectile":
try:
x, y, _ = read_vec3(self.pm, entity_ptr + self._grenade_projectile_fields["m_vecExplodeEffectOrigin"])
x = float(x)
y = float(y)
if x != 0.0 or y != 0.0:
position_source = "explode_origin"
except Exception:
x = 0.0
y = 0.0
if lowered == "molotov_projectile" and x == 0.0 and y == 0.0:
try:
x, y, _ = read_vec3(self.pm, entity_ptr + self._grenade_projectile_fields["vecLastTrailLinePos"])
x = float(x)
y = float(y)
if x != 0.0 or y != 0.0:
position_source = "trail_last"
except Exception:
x = 0.0
y = 0.0
if x == 0.0 and y == 0.0:
ox, oy, _ = self._read_entity_origin(entity_ptr)
x = float(ox)
y = float(oy)
if x != 0.0 or y != 0.0:
position_source = "origin"
inferno_live_signal = bool(
is_burning or in_post_effect or fire_count > 0 or tick_begin > 0 or fire_lifetime > 0.0
)
projectile_live_signal = bool(
lowered == "molotov_projectile"
and (
explode_effect_began
or position_source == "explode_origin"
)
)
synthetic_seedable = bool(
lowered == "molotov_projectile"
and (x != 0.0 or y != 0.0)
and (
explode_effect_began
or position_source == "explode_origin"
)
)
accepted = bool(
(("inferno" in lowered) and inferno_live_signal and (x != 0.0 or y != 0.0))
or (projectile_live_signal and (x != 0.0 or y != 0.0))
)
return {
"class_name": class_name,
"entity_ptr": int(entity_ptr),
"is_burning": is_burning,
"in_post_effect": in_post_effect,
"explode_effect_began": explode_effect_began,
"is_live": is_live,
"is_inc_grenade": is_inc_grenade,
"bounces": int(bounces),
"synthetic_seedable": synthetic_seedable,
"fire_count": int(fire_count),
"tick_begin": int(tick_begin),
"fire_lifetime": float(fire_lifetime),
"inferno_type": int(inferno_type),
"x": float(x),
"y": float(y),
"position_source": position_source or "-",
"accepted": accepted,
}
def debug_inferno_candidates(self) -> list[dict[str, Any]]:
"""Return inferno/molotov-related entity candidates for live debugging."""
rows: list[dict[str, Any]] = []
for entity_index, entity_ptr, class_name in self.iterate_entities((1, 8192)):
info = self._read_inferno_candidate(entity_ptr, class_name)
if info is None:
continue
info = dict(info)
info["entity_index"] = int(entity_index)
rows.append(info)
rows.sort(
key=lambda item: (
0 if bool(item.get("accepted")) else 1,
str(item.get("class_name", "")),
int(item.get("entity_index", 0)),
)
)
return rows
def is_attached(self) -> bool:
"""Return True while the process handle and engine module remain readable."""
try:
self.pm.read_int(self.engine_base + self._engine_offsets["dwBuildNumber"])
except Exception:
return False
return True
def read_players(self) -> list[dict[str, Any]]:
"""Read all active T/CT player controllers and their current pawn state."""
ent_list_base = self._read_entity_list_base()
if ent_list_base == 0:
return []
rows: list[dict[str, Any]] = []
for entity_index in range(1, MAX_PLAYERS + 1):
try:
ctrl = resolve_handle(self.pm, ent_list_base, entity_index)
except Exception:
continue
if ctrl == 0:
continue
if entity_class_name(self.pm, ctrl, self.classes) != "cs_player_controller":
continue
try:
pawn_handle = self.pm.read_uint(ctrl + self._controller_fields["m_hPlayerPawn"])
except Exception:
continue
if pawn_handle == _INVALID_HANDLE:
continue
try:
name = read_player_name(self.pm, ctrl, self._base_controller_fields["m_iszPlayerName"])
if not name:
fallback_name_offset = _optional_field(
self.classes,
"CCSPlayerController",
"m_sSanitizedPlayerName",
)
if fallback_name_offset is not None:
name = read_player_name(self.pm, ctrl, fallback_name_offset)
steam_id = int(self.pm.read_ulonglong(ctrl + self._base_controller_fields["m_steamID"]))
team_num = int(self.pm.read_uchar(ctrl + self._base_entity_fields["m_iTeamNum"]))
team_name = TEAM_NAMES.get(team_num, f"T{team_num}")
if team_name not in {"T", "CT"}:
continue
alive = bool(self.pm.read_uchar(ctrl + self._controller_fields["m_bPawnIsAlive"]))
hp = int(self.pm.read_int(ctrl + self._controller_fields["m_iPawnHealth"]))
armor = int(self.pm.read_int(ctrl + self._controller_fields["m_iPawnArmor"]))
helmet = bool(self.pm.read_uchar(ctrl + self._controller_fields["m_bPawnHasHelmet"]))
defuser = bool(self.pm.read_uchar(ctrl + self._controller_fields["m_bPawnHasDefuser"]))
money_services = int(
self.pm.read_longlong(ctrl + self._controller_fields["m_pInGameMoneyServices"])
)
money = (
int(self.pm.read_int(money_services + self._money_fields["m_iAccount"]))
if money_services
else -1
)
except Exception:
continue
x = y = z = yaw = 0.0
scoped = False
defusing = False
inventory = self._empty_inventory()
try:
pawn = resolve_handle(self.pm, ent_list_base, pawn_handle)
except Exception:
pawn = 0
if pawn:
inventory = self._read_player_inventory_with_entity_list(pawn, ent_list_base)
try:
x, y, z = self._read_entity_origin(pawn)
yaw = float(self.pm.read_float(pawn + self._pawn_fields["m_angEyeAngles"] + 4))
scoped = bool(self.pm.read_uchar(pawn + self._pawn_fields["m_bIsScoped"]))
defusing = bool(self.pm.read_uchar(pawn + self._pawn_fields["m_bIsDefusing"]))
except Exception: