This repository was archived by the owner on Feb 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstructures.py
More file actions
255 lines (222 loc) · 9.4 KB
/
structures.py
File metadata and controls
255 lines (222 loc) · 9.4 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
import struct
import copy
import consts as c
from ByteReader import Bytebuffer
class DemoHeader:
"""1072 Byte header for .DEM file.
This header has the following format:
+-----------+---------------------------------------+
| Byte | Description |
+===========+=======================================+
| 0-7 | Fixed string 'HL2DEMO\0'. |
+-----------+---------------------------------------+
| 8-11 | Demo file protocol version. |
+-----------+---------------------------------------+
| 12-15 | Network protocol version. |
+-----------+---------------------------------------+
| 16-275 | Server name. |
+-----------+---------------------------------------+
| 276-535 | Name of client who recorded the demo. |
+-----------+---------------------------------------+
| 536-795 | Map name. |
+-----------+---------------------------------------+
| 796-1055 | Game directory. |
+-----------+---------------------------------------+
| 1056-1059 | Playback time in seconds. |
+-----------+---------------------------------------+
| 1060-1063 | Number of ticks in demo. |
+-----------+---------------------------------------+
| 1064-1067 | Number of frames in demo. |
+-----------+---------------------------------------+
| 1068-1071 | Length of signon data. |
+-----------+---------------------------------------+
"""
def __init__(self, data):
buf = Bytebuffer(data)
self.header = struct.unpack("8s", buf.read(8))[0].strip(b"\0").decode()
self.demo_protocol = struct.unpack("<I", buf.read(4))[0]
self.network_protocol = struct.unpack("<I", buf.read(4))[0]
self.server_name = struct.unpack("260s", buf.read(c.MAX_PATH))[0].strip(b"\0").decode()
self.client_name = struct.unpack("260s", buf.read(c.MAX_PATH))[0].strip(b"\0").decode()
self.map_name = struct.unpack("260s", buf.read(c.MAX_PATH))[0].strip(b"\0").decode()
self.game_directory = struct.unpack("260s", buf.read(c.MAX_PATH))[0].strip(b"\0").decode()
self.playback_time = struct.unpack("<f", buf.read(4))[0]
self.ticks = struct.unpack("<I", buf.read(4))[0]
self.frames = struct.unpack("<I", buf.read(4))[0]
self.signon_length = struct.unpack("<I", buf.read(4))[0]
del buf
class CommandHeader:
"""Header for each command packet.
.. _header-format:
The header has the following format:
+------+--------------+
| Byte | Description |
+======+==============+
| 0 | Command ID |
+------+--------------+
| 1-4 | Current tick |
+------+--------------+
| 5 | Player ID |
+------+--------------+
"""
def __init__(self, data):
buf = Bytebuffer(data)
self.command = struct.unpack("<B", buf.read(1))[0]
self.tick = struct.unpack("<I", buf.read(4))[0]
self.player = struct.unpack("<B", buf.read(1))[0]
del buf
# class QAngle(Structure):
# pitch = SLFloat32()
# yaw = SLFloat32()
# roll = SLFloat32()
#
#
# class Vector(Structure):
# x = SLFloat32()
# y = SLFloat32()
# z = SLFloat32()
#
#
# class OriginViewAngles(Structure):
# view_origin = SubstructureField(Vector)
# view_angles = SubstructureField(QAngle)
# local_view_angles = SubstructureField(QAngle)
#
#
# class SplitCommandInfo(Structure):
# flags = ULInt32()
# original = SubstructureField(OriginViewAngles)
# resampled = SubstructureField(OriginViewAngles)
#
#
# class CommandInfo(Structure):
# commands = FieldArray(SplitCommandInfo)
class UserInfo:
"""Player data.
This structure has the following format:
+---------+---------------------------------------+
| Byte | Description |
+=========+=======================================+
| 0-7 | Version. Same for all players. |
+---------+---------------------------------------+
| 8-15 | xuid. Some sort of unique ID. |
+---------+---------------------------------------+
| 15-142 | Player name. |
+---------+---------------------------------------+
| 143-146 | Local server user ID. |
+---------+---------------------------------------+
| 147-179 | GUID |
+---------+---------------------------------------+
| 180-183 | Friend's ID. |
+---------+---------------------------------------+
| 184-312 | Friend's Name. |
+---------+---------------------------------------+
| 313 | Is player a bot? |
+---------+---------------------------------------+
| 314 | Is player an HLTV proxy? |
+---------+---------------------------------------+
| 314-329 | Custom files CRC. |
+---------+---------------------------------------+
| 330 | Numbre of files downloaded by server. |
+---------+---------------------------------------+
| 331-335 | Entity index. |
+---------+---------------------------------------+
| 336-340 | No idea. |
+---------+---------------------------------------+
"""
def __init__(self, data):
buf = Bytebuffer(data)
self.version = struct.unpack(">Q", buf.read(8))[0]
self.xuid = struct.unpack(">Q", buf.read(8))[0]
self.name = struct.unpack("128s", buf.read(c.MAX_PLAYER_NAME_LENGTH))[0].strip(b"\0").decode(
errors="replace").strip("\n")
self.user_id = struct.unpack(">I", buf.read(4))[0]
self.guid = struct.unpack("33s", buf.read(c.SIGNED_GUID_LEN))[0].strip(b"\0").decode(errors="replace")
self.friends_id = struct.unpack(">I", buf.read(4))[0]
self.friends_name = struct.unpack("128s", buf.read(c.MAX_PLAYER_NAME_LENGTH))[0].strip(b"\0").decode(
errors="replace").strip("\n")
self.fake_player = struct.unpack(">B", buf.read(1))[0]
self.is_hltv = struct.unpack(">B", buf.read(1))[0]
self.custom_files = struct.unpack(">IIII", buf.read(c.MAX_CUSTOM_FILES * 4))
self.files_downloaded = struct.unpack(">B", buf.read(1))[0]
self.entity_id = struct.unpack(">I", buf.read(4))[0]
self.tbd = struct.unpack(">I", buf.read(4))[0]
self.eid = None
del buf
class StringTable:
def __init__(self, msg):
self.name = msg.name
self.max_entries = msg.max_entries
self.udfs = msg.user_data_fixed_size
self.uds = msg.user_data_size
self.udsb = msg.user_data_size_bits
self.data = []
for i in range(self.max_entries):
self.data.append({"entry": None, "user_data": None})
class ServerClass:
def __init__(self, id2, name, dt_name):
self.id = id2
self.name = name
self.dt = dt_name
self.fprops = None
class Entity:
def __init__(self, parser, entity_id, cls_id, serial, parse=True):
self.class_id = cls_id
if cls_id:
self.class_name = parser._serv_class_dict[cls_id].name
self.parse = parse
if parse:
self.parser = parser
self.entity_id = entity_id
self.serial = serial
self.props = dict()
self.props = copy.deepcopy(parser._baselines_dict[cls_id])
def update(self, table, key, value):
# if self.parse and self.props.get(table) and self.props[table].get(key):
self.props[table][key] = value
def find(self, name: str):
name = name.upper()
ret = dict()
for table in self.props.items():
if table[0].upper().find(name) != -1:
ret.update({table[0]: table[1]})
for prop in table[1].items():
if prop[0].upper().find(name) != -1:
ret.update({prop[0]: prop[1]})
if len(ret.items()):
return ret
else:
return None
def get_table(self, name: str):
if self.props.get(name):
return self.props[name]
else:
return None
def get_prop(self, name: str):
for keyd in self.props.values():
for key in keyd.items():
if key[0] == name:
return key[1]
return None
def is_player(self):
# entity id = userinfo key + 1
# in resource table, player is entityid.zfill(3)
for x in self.parser._string_tables_list:
if x.name == "userinfo":
for x2 in x.data:
if x2["entry"] + 1 == self.entity_id:
return True
return False
def get_userinfo(self):
for x in self.parser._string_tables_list:
if x.name == "userinfo":
for x2 in x.data:
if x2["entry"] + 1 == self.entity_id:
return x2["user_data"]
return None
# EVENT
# name
# eventid
# keys
# name
# type