dem 0.2.3

GoldSrc demo parser and writer library
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
use std::{cell::RefCell, collections::HashMap, ffi::CStr, rc::Rc};

use bitvec::{order::Lsb0, slice::BitSlice as _BitSlice, vec::BitVec as _BitVec};

use crate::utils::get_initial_delta;

/// Auxillary data required for parsing/writing certain messages.
///
/// This includes delta decoders, custom messages, and max client
///
/// Basically storing some global values for demo to parse
#[derive(Clone, Debug)]
pub struct Aux {
    pub delta_decoders: DeltaDecoderTable,
    pub max_client: u8,
    pub custom_messages: CustomMessage,

    /// True if the demo was recorded by an HLTV client, false otherwise.
    ///
    /// HLTV clients can receive different data from the game server for messages like
    /// [SvcClientData], which affects parsing.
    pub is_hltv: bool,
}

impl Aux {
    pub fn new_raw() -> Self {
        Self {
            delta_decoders: get_initial_delta(),
            max_client: 1,
            custom_messages: CustomMessage::new(),
            is_hltv: false,
        }
    }

    pub fn new2() -> AuxRefCell {
        Rc::new(RefCell::new(Self::new_raw()))
    }
}

pub type AuxRefCell = Rc<RefCell<Aux>>;

// Everything not related to netmessage starts here
#[derive(Debug, Clone)]
pub struct Demo {
    pub header: Header,
    pub directory: Directory,
    /// Not part of a demo. Do not use this
    pub _aux: Option<AuxRefCell>,
}

#[derive(Debug, Clone)]
pub struct Header {
    /// `[u8; 8]`
    pub magic: Vec<u8>,
    pub demo_protocol: i32,
    pub network_protocol: i32,
    /// `[u8; 260]`
    pub map_name: ByteString,
    /// `[u8; 260]`
    pub game_directory: ByteString,
    pub map_checksum: u32,
    pub directory_offset: i32,
}

#[derive(Debug, Clone)]
pub struct Directory {
    pub entries: Vec<DirectoryEntry>,
}

#[derive(Debug, Clone)]
pub struct DirectoryEntry {
    pub type_: i32,
    /// `[u8; 64]`
    pub description: ByteString,
    pub flags: i32,
    pub cd_track: i32,
    pub track_time: f32,
    pub frame_count: i32,
    pub frame_offset: i32,
    pub file_length: i32,
    pub frames: Vec<Frame>,
}

#[derive(Debug, Clone)]
pub struct Frame {
    pub time: f32,
    pub frame: i32,
    pub frame_data: FrameData,
}

#[derive(Debug, Clone)]
pub enum FrameData {
    NetworkMessage(Box<(NetworkMessageType, NetworkMessage)>),
    DemoStart,
    ConsoleCommand(ConsoleCommand),
    ClientData(ClientData),
    NextSection,
    Event(Event),
    WeaponAnimation(WeaponAnimation),
    Sound(Sound),
    DemoBuffer(DemoBuffer),
}

#[derive(Debug, Clone)]
pub struct ConsoleCommand {
    /// `[u8; 64]`
    pub command: ByteString,
}

/// `[T; 3]`
type Point<T> = Vec<T>;

#[derive(Debug, Clone)]
pub struct ClientData {
    pub origin: Point<f32>,
    pub viewangles: Point<f32>,
    pub weapon_bits: i32,
    pub fov: f32,
}

/// This is different from [`EventS`], which is used for event types in netmessage
#[derive(Debug, Clone)]
pub struct Event {
    pub flags: i32,
    pub index: i32,
    pub delay: f32,
    pub args: EventArgs,
}

#[derive(Debug, Clone)]
pub struct EventArgs {
    pub flags: i32,
    pub entity_index: i32,
    pub origin: Point<f32>,
    pub angles: Point<f32>,
    pub velocity: Point<f32>,
    pub ducking: i32,
    pub fparam1: f32,
    pub fparam2: f32,
    pub iparam1: i32,
    pub iparam2: i32,
    pub bparam1: i32,
    pub bparam2: i32,
}

#[derive(Debug, Clone)]
pub struct Sound {
    pub channel: i32,
    /// `[u8; sample_length]`
    pub sample: Vec<u8>,
    pub attenuation: f32,
    pub volume: f32,
    pub flags: i32,
    pub pitch: i32,
}

#[derive(Debug, Clone)]
pub struct WeaponAnimation {
    pub anim: i32,
    pub body: i32,
}

#[derive(Debug, Clone)]
pub struct DemoBuffer {
    /// `[u8; buffer_length]`
    pub buffer: Vec<u8>,
}

/// <https://github.com/YaLTeR/hldemo-rs/blob/cbc1efa212a4fc49c776304058efd07e0369caa7/src/types.rs#L187>
#[derive(Debug, Clone)]
pub enum NetworkMessageType {
    Start,
    Normal,
    Unknown(u8),
}

impl TryFrom<u8> for NetworkMessageType {
    type Error = &'static str;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::Start),
            1 => Ok(Self::Normal),
            2..=9 => Err("network message type cannot overlap with other messages"),
            rest => Ok(Self::Unknown(rest)),
        }
    }
}

#[derive(Debug, Clone)]
pub struct NetworkMessage {
    pub info: DemoInfo,
    pub sequence_info: SequenceInfo,
    // need this so the messages type can be [`Parsed`] or [`Unparsed`]
    pub message_length: u32,
    pub messages: MessageData,
}

#[derive(Debug, Clone)]
pub struct DemoInfo {
    pub timestamp: f32,
    pub refparams: RefParams,
    pub usercmd: UserCmd,
    pub movevars: MoveVars,
    pub view: Point<f32>,
    pub viewmodel: i32,
}

#[derive(Debug, Clone)]
pub struct RefParams {
    pub view_origin: Point<f32>,
    pub view_angles: Point<f32>,
    pub forward: Point<f32>,
    pub right: Point<f32>,
    pub up: Point<f32>,
    pub frame_time: f32,
    pub time: f32,
    pub intermission: i32,
    pub paused: i32,
    pub spectator: i32,
    pub on_ground: i32,
    pub water_level: i32,
    pub sim_vel: Point<f32>,
    pub sim_org: Point<f32>,
    pub view_height: Point<f32>,
    pub ideal_pitch: f32,
    pub cl_viewangles: Point<f32>,
    pub health: i32,
    pub crosshair_angle: Point<f32>,
    pub view_size: f32,
    pub punch_angle: Point<f32>,
    pub max_clients: i32,
    pub view_entity: i32,
    pub player_num: i32,
    pub max_entities: i32,
    pub demo_playback: i32,
    pub hardware: i32,
    pub smoothing: i32,
    pub ptr_cmd: i32,
    pub ptr_move_vars: i32,
    /// `[i32; 4]`
    pub view_port: Vec<i32>,
    pub next_view: i32,
    pub only_client_draw: i32,
}

#[derive(Debug, Clone)]
pub struct UserCmd {
    pub lerp_msec: i16,
    pub msec: u8,
    pub unknown1: u8,
    pub view_angles: Point<f32>,
    pub forward_move: f32,
    pub side_move: f32,
    pub up_move: f32,
    pub light_level: i8,
    pub unknonwn2: u8,
    pub buttons: u16,
    pub impulse: i8,
    pub weapon_select: i8,
    pub unknown3: u8,
    pub unknown4: u8,
    pub impact_index: i32,
    pub impact_position: Point<f32>,
}

#[derive(Debug, Clone)]
pub struct MoveVars {
    pub gravity: f32,
    pub stopspeed: f32,
    pub maxspeed: f32,
    pub spectatormaxspeed: f32,
    pub accelerate: f32,
    pub airaccelerate: f32,
    pub wateraccelerate: f32,
    pub friction: f32,
    pub edgefriction: f32,
    pub waterfriction: f32,
    pub entgravity: f32,
    pub bounce: f32,
    pub stepsize: f32,
    pub maxvelocity: f32,
    pub zmax: f32,
    pub wave_height: f32,
    pub footsteps: i32,
    /// `[u8; 32]`
    pub sky_name: ByteString,
    pub rollangle: f32,
    pub rollspeed: f32,
    pub skycolor: Point<f32>,
    pub skyvec: Point<f32>,
}

#[derive(Debug, Clone)]
pub struct SequenceInfo {
    pub incoming_sequence: i32,
    pub incoming_acknowledged: i32,
    pub incoming_reliable_acknowledged: i32,
    pub incoming_reliable_sequence: i32,
    pub outgoing_sequence: i32,
    pub reliable_sequence: i32,
    pub last_reliable_sequence: i32,
}

#[derive(Debug, Clone)]
pub enum MessageData {
    Parsed(Vec<NetMessage>),
    Raw(Vec<u8>),
    None,
}

#[derive(Debug, Clone, Copy)]
pub enum MessageDataParseMode {
    /// Parses network messages
    Parse,
    /// Store raw network message bytes
    Raw,
    /// Does not store any network message bytes
    ///
    /// Use this if you want less memory footprint and you have no use for network messages
    None,
}

// Everything related to netmessage starts here

// Primitive
// pub type BitVec = _BitVec<u8>;
pub type BitVec = _BitVec<u8, Lsb0>;
pub type BitSlice = _BitSlice<u8, Lsb0>;
pub type ByteVec = Vec<u8>;

// Delta
pub type Delta = HashMap<String, ByteVec>;
pub type DeltaDecoder = Vec<DeltaDecoderS>;
pub type DeltaDecoderTable = HashMap<String, DeltaDecoder>;

#[derive(Debug, Clone)]
pub struct DeltaDecoderS {
    pub name: ByteVec,
    pub bits: u32,
    pub divisor: f32,
    pub flags: u32,
}

#[repr(u32)]
pub enum DeltaType {
    Byte = 1,
    Short = 1 << 1,
    Float = 1 << 2,
    Integer = 1 << 3,
    Angle = 1 << 4,
    TimeWindow8 = 1 << 5,
    TimeWindowBig = 1 << 6,
    String = 1 << 7,
    Signed = 1 << 31,
}

// Main
#[derive(Debug, Clone)]
pub enum NetMessage {
    UserMessage(UserMessage),
    EngineMessage(Box<EngineMessage>),
}

pub type CustomMessage = HashMap<u8, SvcNewUserMsg>;
#[derive(Debug, Clone)]
pub struct UserMessage {
    pub id: u8,
    /// `[bool; 16]`
    pub name: ByteVec,
    pub data: ByteVec,
}

// Messages
#[repr(u8)]
#[derive(Debug, Clone)]
pub enum EngineMessage {
    SvcBad = 0,
    SvcNop = 1,
    SvcDisconnect(SvcDisconnect) = 2,
    SvcEvent(SvcEvent) = 3,
    SvcVersion(SvcVersion) = 4,
    SvcSetView(SvcSetView) = 5,
    SvcSound(Box<SvcSound>) = 6,
    SvcTime(SvcTime) = 7,
    SvcPrint(SvcPrint) = 8,
    SvcStuffText(SvcStuffText) = 9,
    SvcSetAngle(SvcSetAngle) = 10,
    SvcServerInfo(SvcServerInfo) = 11,
    SvcLightStyle(SvcLightStyle) = 12,
    SvcUpdateUserInfo(SvcUpdateUserInfo) = 13,
    SvcDeltaDescription(SvcDeltaDescription) = 14,
    SvcClientData(SvcClientData) = 15,
    SvcStopSound(SvcStopSound) = 16,
    SvcPings(SvcPings) = 17,
    SvcParticle(SvcParticle) = 18,
    SvcDamage = 19,
    SvcSpawnStatic(SvcSpawnStatic) = 20,
    SvcEventReliable(SvcEventReliable) = 21,
    SvcSpawnBaseline(SvcSpawnBaseline) = 22,
    SvcTempEntity(SvcTempEntity) = 23,
    SvcSetPause(SvcSetPause) = 24,
    SvcSignOnNum(SvcSignOnNum) = 25,
    SvcCenterPrint(SvcCenterPrint) = 26,
    SvcKilledMonster = 27,
    SvcFoundSecret = 28,
    SvcSpawnStaticSound(SvcSpawnStaticSound) = 29,
    SvcIntermission = 30,
    SvcFinale(SvcFinale) = 31,
    SvcCdTrack(SvcCdTrack) = 32,
    SvcRestore(SvcRestore) = 33,
    SvcCutscene(SvcCutscene) = 34,
    SvcWeaponAnim(SvcWeaponAnim) = 35,
    SvcDecalName(SvcDecalName) = 36,
    SvcRoomType(SvcRoomType) = 37,
    SvcAddAngle(SvcAddAngle) = 38,
    SvcNewUserMsg(SvcNewUserMsg) = 39,
    SvcPacketEntities(SvcPacketEntities) = 40,
    SvcDeltaPacketEntities(SvcDeltaPacketEntities) = 41,
    SvcChoke = 42,
    SvcResourceList(SvcResourceList) = 43,
    SvcNewMovevars(SvcNewMovevars) = 44,
    SvcResourceRequest(SvcResourceRequest) = 45,
    SvcCustomization(SvcCustomization) = 46,
    SvcCrosshairAngle(SvcCrosshairAngle) = 47,
    SvcSoundFade(SvcSoundFade) = 48,
    SvcFileTxferFailed(SvcFileTxferFailed) = 49,
    SvcHltv(SvcHltv) = 50,
    SvcDirector(SvcDirector) = 51,
    SvcVoiceInit(SvcVoiceInit) = 52,
    SvcVoiceData(SvcVoiceData) = 53,
    SvcSendExtraInfo(SvcSendExtraInfo) = 54,
    SvcTimeScale(SvcTimeScale) = 55,
    SvcResourceLocation(SvcResourceLocation) = 56,
    SvcSendCvarValue(SvcSendCvarValue) = 57,
    SvcSendCvarValue2(SvcSendCvarValue2) = 58,
}

// SVC_BAD 0

// SVC_NOP 1

/// SVC_DISCONNECT 2
#[derive(Debug, Clone)]
pub struct SvcDisconnect {
    pub reason: ByteVec,
}

/// SVC_EVENT 3
#[derive(Debug, Clone)]
pub struct SvcEvent {
    /// `[bool; 5]`
    pub event_count: BitVec,
    pub events: Vec<EventS>,
}
#[derive(Debug, Clone)]
pub struct EventS {
    /// `[bool; 10]`
    pub event_index: BitVec,
    pub has_packet_index: bool,
    /// `[bool; 11]`
    pub packet_index: Option<BitVec>,
    pub has_delta: Option<bool>,
    pub delta: Option<Delta>,
    pub has_fire_time: bool,
    /// `[bool; 16]`
    pub fire_time: Option<BitVec>,
}

/// SVC_VERSION 4
#[derive(Debug, Clone)]
pub struct SvcVersion {
    pub protocol_version: u32,
}

/// SVC_SETVIEW 5
#[derive(Debug, Clone)]
pub struct SvcSetView {
    pub entity_index: i16,
}

/// SVC_SOUND 6
#[derive(Debug, Clone)]
pub struct SvcSound {
    /// `[bool; 9]`
    pub flags: BitVec,
    pub volume: Option<BitVec>,
    pub attenuation: Option<BitVec>,
    /// `[bool; 3]`
    pub channel: BitVec,
    /// `[bool; 11]`
    pub entity_index: BitVec,
    pub sound_index_long: Option<BitVec>,
    pub sound_index_short: Option<BitVec>,
    pub has_x: bool,
    pub has_y: bool,
    pub has_z: bool,
    pub origin_x: Option<OriginCoord>,
    pub origin_y: Option<OriginCoord>,
    pub origin_z: Option<OriginCoord>,
    pub pitch: BitVec,
}
#[derive(Debug, Clone)]
pub struct OriginCoord {
    pub int_flag: bool,
    pub fraction_flag: bool,
    pub is_negative: Option<bool>,
    /// `[bool; 12]`
    pub int_value: Option<BitVec>,
    /// `[bool; 3]`
    pub fraction_value: Option<BitVec>,
    // There is no unknow, Xd
    // `[bool; 2]`
    // pub unknown: BitVec,
}

/// SVC_TIME 7
#[derive(Debug, Clone)]
pub struct SvcTime {
    pub time: f32,
}

/// SVC_PRINT 8
#[derive(Debug, Clone)]
pub struct SvcPrint {
    pub message: ByteString,
}

/// SVC_STUFFTEXT 9
#[derive(Debug, Clone)]
pub struct SvcStuffText {
    pub command: ByteString,
}

/// SVC_SETANGLE 10
#[derive(Debug, Clone)]
pub struct SvcSetAngle {
    pub pitch: i16,
    pub yaw: i16,
    pub roll: i16,
}

/// SVC_SERVERINFO 11
#[derive(Debug, Clone)]
pub struct SvcServerInfo {
    pub protocol: i32,
    pub spawn_count: i32,
    pub map_checksum: i32,
    /// `[u8; 16]`
    pub client_dll_hash: ByteString,
    pub max_players: u8,
    pub player_index: u8,
    pub is_deathmatch: u8,
    pub game_dir: ByteVec,
    pub hostname: ByteVec,
    pub map_file_name: ByteVec,
    pub map_cycle: ByteVec,
    pub unknown: u8,
}

/// SVC_LIGHTSTYLE 12
#[derive(Debug, Clone)]
pub struct SvcLightStyle {
    pub index: u8,
    pub light_info: ByteVec,
}

/// SVC_UPDATEUSERINFO 13
#[derive(Debug, Clone)]
pub struct SvcUpdateUserInfo {
    pub index: u8,
    pub id: u32,
    pub user_info: ByteString,
    /// `[u8; 16]`
    pub cd_key_hash: ByteString,
}

/// SVC_DELTADESCRIPTION 14
#[derive(Debug, Clone)]
pub struct SvcDeltaDescription {
    pub name: ByteVec,
    pub total_fields: u16,
    pub fields: DeltaDecoder,
    pub clone: ByteVec,
}

/// SVC_CLIENTDATA 15
#[derive(Debug, Clone)]
pub struct SvcClientData {
    pub has_delta_update_mask: bool,
    /// `[bool; 8]`
    pub delta_update_mask: Option<BitVec>,
    pub client_data: Delta,
    pub weapon_data: Option<Vec<ClientDataWeaponData>>,
}
#[derive(Debug, Clone)]
pub struct ClientDataWeaponData {
    /// `[bool; 6]`
    pub weapon_index: BitVec,
    pub weapon_data: Delta,
}

/// SVC_STOPSOUND 16
#[derive(Debug, Clone)]
pub struct SvcStopSound {
    pub entity_index: i16,
}

/// SVC_PINGS 17
#[derive(Debug, Clone)]
pub struct SvcPings {
    pub pings: Vec<PingS>,
}
#[derive(Debug, Clone)]
pub struct PingS {
    pub has_ping_data: bool,
    pub player_id: Option<u8>,
    pub ping: Option<u8>,
    pub loss: Option<u8>,
}

/// SVC_PARTICLE 18
#[derive(Debug, Clone)]
pub struct SvcParticle {
    /// Vec3
    pub origin: Vec<i16>,
    /// Vec3
    pub direction: ByteVec,
    pub count: u8,
    pub color: u8,
}

// SVC_PARTICLE 19

/// SVC_SPAWNSTATIC 20
#[derive(Debug, Clone)]
pub struct SvcSpawnStatic {
    pub model_index: i16,
    pub sequence: i8,
    pub frame: i8,
    pub color_map: i16,
    pub skin: i8,
    pub origin_x: i16,
    pub rotation_x: i8,
    pub origin_y: i16,
    pub rotation_y: i8,
    pub origin_z: i16,
    pub rotation_z: i8,
    pub has_render_mode: i8,
    /// `[u8; 3]`
    pub render_color: Option<ByteVec>,
}

/// SVC_EVENTRELIABLE 21
#[derive(Debug, Clone)]
pub struct SvcEventReliable {
    /// `[bool; 10]`
    pub event_index: BitVec,
    pub event_args: Delta,
    pub has_fire_time: bool,
    /// `[bool; 16]`
    pub fire_time: Option<BitVec>,
}

/// SVC_SPAWNBASELINE 22
#[derive(Debug, Clone)]
pub struct SvcSpawnBaseline {
    pub entities: Vec<EntityS>,
    // These members are not inside EntityS like cgdangelo/talent suggests.
    /// `[bool; 6]`
    pub total_extra_data: BitVec,
    pub extra_data: Vec<Delta>,
}
#[derive(Debug, Clone)]
pub struct EntityS {
    // Goodies
    pub entity_index: u16,
    /// `[bool; 11]`
    pub index: BitVec,
    /// `[bool; 2]`
    pub type_: BitVec,
    // One delta for 3 types
    pub delta: Delta,
}

/// SVC_TEMPENTITY 23
#[derive(Debug, Clone)]
pub struct SvcTempEntity {
    pub entity_type: u8,
    pub entity: TempEntity,
}

#[repr(u8)]
#[derive(Debug, Clone)]
pub enum TempEntity {
    /// `[u8; 24]`
    TeBeamPoints(TeBeamPoints) = 0,
    /// `[u8; 20]`
    TeBeamEntPoint(ByteVec) = 1,
    /// `[u8; 6]`
    TeGunshot(ByteVec) = 2,
    // It is 11
    /// `[u8; 11]`
    TeExplosion(ByteVec) = 3,
    /// `[u8; 6]`
    TeTarExplosion(ByteVec) = 4,
    /// `[u8; 10]`
    TeSmoke(ByteVec) = 5,
    /// `[u8; 12]`
    TeTracer(ByteVec) = 6,
    /// `[u8; 17]`
    TeLightning(ByteVec) = 7,
    /// `[u8; 16]`
    TeBeamEnts(ByteVec) = 8,
    /// `[u8; 6]`
    TeSparks(ByteVec) = 9,
    /// `[u8; 6]`
    TeLavaSplash(ByteVec) = 10,
    /// `[u8; 6]`
    TeTeleport(ByteVec) = 11,
    /// `[u8; 8]`
    TeExplosion2(ByteVec) = 12,
    TeBspDecal(TeBspDecal) = 13,
    /// `[u8; 9]`
    TeImplosion(ByteVec) = 14,
    /// `[u8; 19]`
    TeSpriteTrail(ByteVec) = 15,
    /// `[u8; 10]`
    TeSprite(ByteVec) = 17,
    /// `[u8; 16]`
    TeBeamSprite(ByteVec) = 18,
    /// `[u8; 24]`
    TeBeamTorus(ByteVec) = 19,
    /// `[u8; 24]`
    TeBeamDisk(ByteVec) = 20,
    /// `[u8; 24]`
    TeBeamCylinder(ByteVec) = 21,
    /// `[u8; 10]`
    TeBeamFollow(ByteVec) = 22,
    /// `[u8; 11]`
    TeGlowSprite(ByteVec) = 23,
    /// `[u8; 16]`
    TeBeamRing(ByteVec) = 24,
    /// `[u8; 19]`
    TeStreakSplash(ByteVec) = 25,
    /// `[u8; 12]`
    TeDLight(ByteVec) = 27,
    /// `[u8; 16]`
    TeELight(ByteVec) = 28,
    TeTextMessage(TeTextMessage) = 29,
    /// `[u8; 17]`
    TeLine(ByteVec) = 30,
    /// `[u8; 17]`
    TeBox(ByteVec) = 31,
    /// `[u8; 2]`
    TeKillBeam(ByteVec) = 99,
    /// `[u8; 10]`
    TeLargeFunnel(ByteVec) = 100,
    /// `[u8; 14]`
    TeBloodStream(ByteVec) = 101,
    /// `[u8; 12]`
    TeShowLine(ByteVec) = 102,
    /// `[u8; 14]`
    TeBlood(ByteVec) = 103,
    /// `[u8; 9]`
    TeDecal(ByteVec) = 104,
    /// `[u8; 5]`
    TeFizz(ByteVec) = 105,
    /// `[u8; 17]`
    TeModel(ByteVec) = 106,
    /// `[u8; 13]`
    TeExplodeModel(ByteVec) = 107,
    // It is 24
    /// `[u8; 24]`
    TeBreakModel(ByteVec) = 108,
    /// `[u8; 9]`
    TeGunshotDecal(ByteVec) = 109,
    /// `[u8; 17]`
    TeSpriteSpray(ByteVec) = 110,
    /// `[u8; 7]`
    TeArmorRicochet(ByteVec) = 111,
    /// `[u8; 10]`
    TePlayerDecal(ByteVec) = 112,
    /// `[u8; 10]`
    TeBubbles(ByteVec) = 113,
    /// `[u8; 19]`
    TeBubbleTrail(ByteVec) = 114,
    /// `[u8; 12]`
    TeBloodSprite(ByteVec) = 115,
    /// `[u8; 7]`
    TeWorldDecal(ByteVec) = 116,
    /// `[u8; 7]`
    TeWorldDecalHigh(ByteVec) = 117,
    /// `[u8; 9]`
    TeDecalHigh(ByteVec) = 118,
    /// `[u8; 16]`
    TeProjectile(ByteVec) = 119,
    /// `[u8; 18]`
    TeSpray(ByteVec) = 120,
    /// `[u8; 5]`
    TePlayerSprites(ByteVec) = 121,
    /// `[u8; 10]`
    TeParticleBurst(ByteVec) = 122,
    /// `[u8; 9]`
    TeFireField(ByteVec) = 123,
    /// `[u8; 7]`
    TePlayerAttachment(ByteVec) = 124,
    /// `[u8; 1]`
    TeKillPlayerAttachment(ByteVec) = 125,
    // It is 18.
    /// `[u8; 18]`
    TeMultigunShot(ByteVec) = 126,
    /// `[u8; 15]`
    TeUserTracer(ByteVec) = 127,
}

impl TempEntity {
    pub fn id(&self) -> u8 {
        match self {
            TempEntity::TeBeamPoints(_) => 0,
            TempEntity::TeBeamEntPoint(_) => 1,
            TempEntity::TeGunshot(_) => 2,
            TempEntity::TeExplosion(_) => 3,
            TempEntity::TeTarExplosion(_) => 4,
            TempEntity::TeSmoke(_) => 5,
            TempEntity::TeTracer(_) => 6,
            TempEntity::TeLightning(_) => 7,
            TempEntity::TeBeamEnts(_) => 8,
            TempEntity::TeSparks(_) => 9,
            TempEntity::TeLavaSplash(_) => 10,
            TempEntity::TeTeleport(_) => 11,
            TempEntity::TeExplosion2(_) => 12,
            TempEntity::TeBspDecal(_) => 13,
            TempEntity::TeImplosion(_) => 14,
            TempEntity::TeSpriteTrail(_) => 15,
            TempEntity::TeSprite(_) => 17,
            TempEntity::TeBeamSprite(_) => 18,
            TempEntity::TeBeamTorus(_) => 19,
            TempEntity::TeBeamDisk(_) => 20,
            TempEntity::TeBeamCylinder(_) => 21,
            TempEntity::TeBeamFollow(_) => 22,
            TempEntity::TeGlowSprite(_) => 23,
            TempEntity::TeBeamRing(_) => 24,
            TempEntity::TeStreakSplash(_) => 25,
            TempEntity::TeDLight(_) => 27,
            TempEntity::TeELight(_) => 28,
            TempEntity::TeTextMessage(_) => 29,
            TempEntity::TeLine(_) => 30,
            TempEntity::TeBox(_) => 31,
            TempEntity::TeKillBeam(_) => 99,
            TempEntity::TeLargeFunnel(_) => 100,
            TempEntity::TeBloodStream(_) => 101,
            TempEntity::TeShowLine(_) => 102,
            TempEntity::TeBlood(_) => 103,
            TempEntity::TeDecal(_) => 104,
            TempEntity::TeFizz(_) => 105,
            TempEntity::TeModel(_) => 106,
            TempEntity::TeExplodeModel(_) => 107,
            TempEntity::TeBreakModel(_) => 108,
            TempEntity::TeGunshotDecal(_) => 109,
            TempEntity::TeSpriteSpray(_) => 110,
            TempEntity::TeArmorRicochet(_) => 111,
            TempEntity::TePlayerDecal(_) => 112,
            TempEntity::TeBubbles(_) => 113,
            TempEntity::TeBubbleTrail(_) => 114,
            TempEntity::TeBloodSprite(_) => 115,
            TempEntity::TeWorldDecal(_) => 116,
            TempEntity::TeWorldDecalHigh(_) => 117,
            TempEntity::TeDecalHigh(_) => 118,
            TempEntity::TeProjectile(_) => 119,
            TempEntity::TeSpray(_) => 120,
            TempEntity::TePlayerSprites(_) => 121,
            TempEntity::TeParticleBurst(_) => 122,
            TempEntity::TeFireField(_) => 123,
            TempEntity::TePlayerAttachment(_) => 124,
            TempEntity::TeKillPlayerAttachment(_) => 125,
            TempEntity::TeMultigunShot(_) => 126,
            TempEntity::TeUserTracer(_) => 127,
        }
    }
}

/// TE_BEAMPOINTS 0
#[derive(Debug, Clone)]
pub struct TeBeamPoints {
    /// `[i16; 3]`
    pub start_position: Vec<i16>,
    /// `[i16; 3]`
    pub end_position: Vec<i16>,
    pub sprite_index: i16,
    pub start_frame: u8,
    pub frame_rate: u8,
    pub life: u8,
    pub width: u8,
    pub noise: u8,
    /// `[u8; 4]` RGBA
    pub color: ByteVec,
    pub speed: u8,
}

/// TE_BEAMENTPOINTS 1
#[derive(Debug, Clone)]
pub struct TeBeamEntPoint {
    pub start_entity: i16,
    /// `[i16; 3]`
    pub end_position: Vec<i16>,
    pub sprite_index: i16,
    pub start_frame: u8,
    pub frame_rate: u8,
    pub life: u8,
    pub width: u8,
    pub noise: u8,
    /// `[i16; 4]` RGBA
    pub color: ByteVec,
    pub speed: u8,
}

/// TE_GUNSHOT 2
#[derive(Debug, Clone)]
pub struct TeGunShot {
    /// `[i16; 3]`
    pub position: Vec<i16>,
}

/// TE_EXPLOSION 3
#[derive(Debug, Clone)]
pub struct TeExplosion {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    pub sprite_index: i16,
    pub scale: u8,
    pub frame_rame: u8,
    pub flags: u8,
}

/// TE_TAREXPLOSION 4
#[derive(Debug, Clone)]
pub struct TeTarExplosion {
    /// `[i16; 3]`
    pub position: Vec<i16>,
}

/// TE_SMOKE 5
#[derive(Debug, Clone)]
pub struct TeSmoke {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    pub sprite_index: i16,
    pub scale: u8,
    pub frame_rate: u8,
}

/// TE_TRACER 6
#[derive(Debug, Clone)]
pub struct TeTracer {
    /// `[i16; 3]`
    pub start_position: Vec<i16>,
    /// `[i16; 3]`
    pub end_position: Vec<i16>,
}

/// TE_LIGHTNING 7
#[derive(Debug, Clone)]
pub struct TeLightning {
    /// `[i16; 3]`
    pub start_position: Vec<i16>,
    /// `[i16; 3]`
    pub end_position: Vec<i16>,
    pub life: u8,
    pub width: u8,
    pub noise: u8,
    pub model_index: i16,
}

/// TE_BEAMENTS 8
#[derive(Debug, Clone)]
pub struct TeBeamEnts {
    /// `[i16; 3]`
    pub start_entity: i16,
    pub end_entity: i16,
    pub sprite_index: i16,
    pub start_frame: u8,
    pub life: u8,
    pub width: u8,
    pub noise: u8,
    /// `[i16; 4]` RGBA
    pub color: ByteVec,
    pub speed: u8,
}

/// TE_SPARKS 9
#[derive(Debug, Clone)]
pub struct TeSparks {
    /// `[i16; 3]`
    pub position: Vec<i16>,
}

/// TE_LAVASPLASH 10
#[derive(Debug, Clone)]
pub struct TeLavaSplash {
    /// `[i16; 3]`
    pub position: Vec<i16>,
}

/// TE_TELEPORT 11
#[derive(Debug, Clone)]
pub struct TeTeleport {
    /// `[i16; 3]`
    pub position: Vec<i16>,
}

/// TE_EXPLOSION2 12
#[derive(Debug, Clone)]
pub struct TeExplosion2 {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    pub color: u8,
    pub count: u8,
}

/// TE_BSPDECAL 13
#[derive(Debug, Clone)]
pub struct TeBspDecal {
    /// `[u8; 8]`
    pub unknown1: ByteVec,
    pub entity_index: i16,
    /// `[u8; 2]`
    pub unknown2: Option<ByteVec>,
}

/// TE_IMPLOSION 14
#[derive(Debug, Clone)]
pub struct TeImplosion {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    pub radius: u8,
    pub count: u8,
    pub life: u8,
}

/// TE_SPRITETRAIL 15
#[derive(Debug, Clone)]
pub struct TeSpriteTrail {
    /// `[i16; 3]`
    pub start_position: Vec<i16>,
    /// `[i16; 3]`
    pub end_position: Vec<i16>,
    pub sprite_index: i16,
    pub count: u8,
    pub life: u8,
    pub scale: u8,
    pub velocity: u8,
    pub velocity_randomness: u8,
}

/// TE_SPRITE 17
#[derive(Debug, Clone)]
pub struct TeSprite {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    pub sprite_index: i16,
    pub scale: u8,
    pub brightness: u8,
}

/// TE_BEAMSPRITE 18
#[derive(Debug, Clone)]
pub struct TeBeamSprite {
    /// `[i16; 3]`
    pub start_position: Vec<i16>,
    pub end_position: Vec<i16>,
    pub beam_sprite_index: i16,
    pub end_sprite_index: i16,
}

/// TE_BEAMTORUS 19
#[derive(Debug, Clone)]
pub struct TeBeamTorus {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    /// `[i16; 3]`
    pub axis: Vec<i16>,
    pub sprite_index: i16,
    pub start_frame: u8,
    pub life: u8,
    pub width: u8,
    pub noise: u8,
    /// `[i16; 4] RGBA`
    pub color: ByteVec,
    pub speed: u8,
}

/// TE_BEAMDISK 20
#[derive(Debug, Clone)]
pub struct TeBeamDisk {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    /// `[i16; 3]`
    pub axis: Vec<i16>,
    pub sprite_index: i16,
    pub start_frame: u8,
    pub life: u8,
    pub width: u8,
    pub noise: u8,
    /// `[i16; 4]` RGBA
    pub color: ByteVec,
    pub speed: u8,
}

/// TE_BEAMCYLINDER 21
#[derive(Debug, Clone)]
pub struct TeBeamCylinder {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    /// `[i16; 3]`
    pub axis: Vec<i16>,
    pub sprite_index: i16,
    pub start_frame: u8,
    pub life: u8,
    pub width: u8,
    pub noise: u8,
    /// `[i16; 4]` RGBA
    pub color: ByteVec,
    pub speed: u8,
}

/// TE_BEAMFOLLOW 22
#[derive(Debug, Clone)]
pub struct TeBeamFollow {
    pub start_entity: i16,
    pub sprite_index: i16,
    pub start_frame: u8,
    pub life: u8,
    pub width: u8,
    /// `[i16; 4]` RGBA
    pub color: ByteVec,
}

/// TE_GLOWSPRITE 23
#[derive(Debug, Clone)]
pub struct TeGlowSprite {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    pub model_index: i16,
    pub scale: u8,
    pub size: u8,
    pub brightness: u8,
}

/// TE_BEAMRING 24
#[derive(Debug, Clone)]
pub struct TeBeamRing {
    pub start_entity: i16,
    pub end_entity: i16,
    pub sprite_index: i16,
    pub start_frame: u8,
    pub frame_rate: u8,
    pub life: u8,
    pub width: u8,
    pub noise: u8,
    /// `[i16; 4]` RGBA
    pub color: ByteVec,
    pub speed: u8,
}

/// TE_STREAKSPLASH 25
#[derive(Debug, Clone)]
pub struct TeStreakSplash {
    /// `[i16; 3]`
    pub start_position: Vec<i16>,
    /// `[i16; 3]`
    pub vector: Vec<i16>,
    pub color: i16,
    pub count: u8,
    pub velocity: i16,
    pub velocity_randomness: i16,
}
/// TE_DLIGHT 27
#[derive(Debug, Clone)]
pub struct TeDLight {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    pub radius: u8,
    /// `[i16; 3]`
    pub color: ByteVec,
    pub life: u8,
    pub decay_rate: u8,
}

/// TE_ELIGHT 28
#[derive(Debug, Clone)]
pub struct TeELight {
    pub entity_index: i16,
    /// `[i16; 3]`
    pub position: Vec<i16>,
    pub radius: i16,
    /// `[i8; 3]`
    pub color: ByteVec,
    pub life: u8,
    pub decay_rate: i16,
}
/// TE_TEXTMESSAGE 29
#[derive(Debug, Clone)]
pub struct TeTextMessage {
    pub channel: i8,
    pub x: i16,
    pub y: i16,
    pub effect: i8,
    /// `[u8; 4]`
    pub text_color: ByteVec,
    // THE docs forgot to mention this
    pub effect_color: ByteVec,
    pub fade_in_time: i16,
    pub fade_out_time: i16,
    pub hold_time: i16,
    pub effect_time: Option<i16>,
    pub message: ByteString,
}

/// TE_LINE 30
#[derive(Debug, Clone)]
pub struct TeLine {
    /// `[i16; 3]`
    pub start_position: Vec<i16>,
    /// `[i16; 3]`
    pub end_position: Vec<i16>,
    pub life: i16,
    /// `[i8; 3]`
    pub color: ByteVec,
}

/// TE_BOX 31
#[derive(Debug, Clone)]
pub struct TeBox {
    /// `[i16; 3]`
    pub start_position: Vec<i16>,
    /// `[i16; 3]`
    pub end_position: Vec<i16>,
    pub life: i16,
    /// `[i8; 3]`
    pub color: ByteVec,
}

/// TE_KILLBEAM 99
#[derive(Debug, Clone)]
pub struct TeKillBeam {
    pub entity_index: i16,
}

/// TE_LARGEFUNNEL 100
#[derive(Debug, Clone)]
pub struct TeLargeFunnel {
    /// `[i16; 3]`
    pub start_position: Vec<i16>,
    pub entity_index: i16,
    pub flags: i16,
}

/// TE_BLOODSTREAM 101
#[derive(Debug, Clone)]
pub struct TeBloodStream {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    /// `[i16; 3]`
    pub vector: i16,
    pub color: u8,
    pub count: u8,
}

/// TE_SHOWLINE 102
#[derive(Debug, Clone)]
pub struct TeShowLine {
    /// `[i16; 3]`
    pub start_position: Vec<i16>,
    /// `[i16; 3]`
    pub end_position: Vec<i16>,
}

/// TE_BLOOD 103
#[derive(Debug, Clone)]
pub struct TeBlood {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    /// `[i16; 3]`
    pub vector: i16,
    pub color: u8,
    pub count: u8,
}

/// TE_DECAL 104
#[derive(Debug, Clone)]
pub struct TeDecal {
    /// `[i16; 3]`
    pub positiion: Vec<i16>,
    pub decal_index: u8,
    pub entity_index: i16,
}

/// TE_FIZZ 105
#[derive(Debug, Clone)]
pub struct TeFizz {
    pub entity_index: i16,
    pub model_index: i16,
    pub scale: u8,
}

/// TE_MODEL 106
#[derive(Debug, Clone)]
pub struct TeModel {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    /// `[i16; 3]`
    pub velocity: Vec<i16>,
    pub angle_yaw: u8,
    pub model_index: i16,
    pub flags: u8,
    pub life: u8,
}

/// TE_EXPLODEMODEL 107
#[derive(Debug, Clone)]
pub struct TeExplodeModel {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    /// `[i16; 3]`
    pub velocity: Vec<i16>,
    pub model_index: i16,
    pub count: i16,
    pub life: u8,
}

/// TE_BREAKMODEL 108
#[derive(Debug, Clone)]
pub struct TeBreakModel {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    /// `[i16; 3]`
    pub size: Vec<i16>,
    /// `[i16; 3]`
    pub velocity: Vec<i16>,
    pub velocity_randomness: u8,
    pub object_index: i16,
    pub count: u8,
    pub life: u8,
    pub flags: u8,
}

/// TE_GUNSHOTDECAL 109
#[derive(Debug, Clone)]
pub struct TeGunshotDecal {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    pub entity_index: i16,
    pub decal: u8,
}

/// TE_SPRITESPRAY 110
#[derive(Debug, Clone)]
pub struct TeSpriteSpray {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    /// `[i16; 3]`
    pub velocity: Vec<i16>,
    pub model_index: i16,
    pub count: u8,
    pub speed: u8,
    pub random: u8,
}

/// TE_ARMORRICOCHET 111
#[derive(Debug, Clone)]
pub struct TeArmorRicochet {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    pub scale: u8,
}

/// TE_PLAYERDECAL 112
#[derive(Debug, Clone)]
pub struct TePlayerDecal {
    pub player_index: u8,
    /// `[i16; 3]`
    pub position: Vec<i16>,
    pub entity_index: i16,
    pub decal_index: u8,
}

/// TE_BUBBLES 113
#[derive(Debug, Clone)]
pub struct TeBubbles {
    /// `[i16; 3]`
    pub min_start_positition: Vec<i16>,
    /// `[i16; 3]`
    pub max_start_position: Vec<i16>,
    pub scale: i16,
    pub model_index: i16,
    pub count: u8,
    pub speed: i16,
}

/// TE_BUBBLETRAIL 114
#[derive(Debug, Clone)]
pub struct TeBubbleTrail {
    /// `[i16; 3]`
    pub min_start_positition: Vec<i16>,
    /// `[i16; 3]`
    pub max_start_position: Vec<i16>,
    pub scale: i16,
    pub model_index: i16,
    pub count: u8,
    pub speed: i16,
}

/// TE_BLOODSPRITE 115
#[derive(Debug, Clone)]
pub struct TeBloodSprite {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    pub model_index: i16,
    pub decal_index: i16,
    pub color: u8,
    pub scale: u8,
}

/// TE_WORLDDECAL 116
#[derive(Debug, Clone)]
pub struct TeWorldDecal {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    pub texture_index: u8,
}

/// TE_WORLDDECALHIGH 117
#[derive(Debug, Clone)]
pub struct TeWorldDecalHigh {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    pub texture_index: u8,
}

/// TE_DECALHIGH 118
#[derive(Debug, Clone)]
pub struct TeDecalHigh {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    pub decal_index: u8,
    pub entity_index: i16,
}

/// TE_PROJECTILE 119
#[derive(Debug, Clone)]
pub struct TeProjectile {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    /// `[i16; 3]`
    pub velocity: Vec<i16>,
    pub model_index: i16,
    pub life: u8,
    pub color: u8,
}

/// TE_SPRAY 120
#[derive(Debug, Clone)]
pub struct TeSpray {
    /// `[i16; 3]`
    pub position: Vec<i16>,
    /// `[i16; 3]`
    pub direction: Vec<i16>,
    pub model_index: i16,
    pub count: u8,
    pub life: u8,
    pub owner: u8,
}

/// TE_PLAYERSPRITES 121
#[derive(Debug, Clone)]
pub struct TePlayerSprites {
    pub entity_index: i16,
    pub model_index: i16,
    pub count: u8,
    pub variance: u8,
}

/// TE_PARTICLEBURST 122
#[derive(Debug, Clone)]
pub struct TeParticleBurst {
    /// `[i16; 3]`
    pub origin: Vec<i16>,
    pub scale: i16,
    pub color: u8,
    pub duration: u8,
}

/// TE_FIREFIELD 123
#[derive(Debug, Clone)]
pub struct TeFireField {
    /// `[i16; 3]`
    pub origin: Vec<i16>,
    pub scale: i16,
    pub model_index: i16,
    pub count: u8,
    pub flags: u8,
    pub duration: u8,
}

/// TE_PLAYERATTACHMENT 124
#[derive(Debug, Clone)]
pub struct TePlayerAttachment {
    pub entity_index: u8,
    pub scale: i16,
    pub model_index: i16,
    pub life: i16,
}

/// TE_KILLPLAYERATTACHMENT 125
#[derive(Debug, Clone)]
pub struct TeKillPlayerAttachment {
    pub entity_index: u8,
}

/// TE_MULTIGUNSHOT 126
#[derive(Debug, Clone)]
pub struct TeMultigunShot {
    /// `[i16; 3]`
    pub origin: Vec<i16>,
    /// `[i16; 3]`
    pub direction: Vec<i16>,
    /// `[i16; 2]`
    pub noise: Vec<i16>,
    pub count: u8,
    pub decal_index: u8,
}

/// TE_USERTRACER 127
#[derive(Debug, Clone)]
pub struct TeUserTracer {
    /// `[i16; 3]`
    pub origin: Vec<i16>,
    /// `[i16; 3]`
    pub velocity: Vec<i16>,
    pub life: u8,
    pub color: u8,
    pub scale: u8,
}

/// SVC_SETPAUSE 24
#[derive(Debug, Clone)]
pub struct SvcSetPause {
    pub is_paused: i8,
}

/// SVC_SIGNONNUM 25
#[derive(Debug, Clone)]
pub struct SvcSignOnNum {
    pub sign: i8,
}

/// SVC_CENTERPRINT 26
#[derive(Debug, Clone)]
pub struct SvcCenterPrint {
    pub message: ByteVec,
}

// SVC_KILLEDMONSTER 27

// SVC_FOUNDSECRET 28

/// SVC_SPAWNSTATICSOUND 29
#[derive(Debug, Clone)]
pub struct SvcSpawnStaticSound {
    // Vec3
    pub origin: Vec<i16>,
    pub sound_index: u16,
    pub volume: u8,
    pub attenuation: u8,
    pub entity_index: u16,
    pub pitch: u8,
    pub flags: u8,
}

// SVC_INTERMISSION 30

/// SVC_FINALE 31
#[derive(Debug, Clone)]
pub struct SvcFinale {
    pub text: ByteVec,
}

/// SVC_CDTRACK 32
#[derive(Debug, Clone)]
pub struct SvcCdTrack {
    pub track: i8,
    pub loop_track: i8,
}

/// SVC_RESTORE 33
#[derive(Debug, Clone)]
pub struct SvcRestore {
    pub save_name: ByteVec,
    pub map_count: u8,
    pub map_names: Vec<ByteVec>,
}

/// SVC_CUTSCENE 34
#[derive(Debug, Clone)]
pub struct SvcCutscene {
    pub text: ByteVec,
}

/// SVC_WEAPONANIM 35
#[derive(Debug, Clone)]
pub struct SvcWeaponAnim {
    pub sequence_number: i8,
    pub weapon_model_body_group: i8,
}

/// SVC_DECALNAME 36
#[derive(Debug, Clone)]
pub struct SvcDecalName {
    pub position_index: u8,
    pub decal_name: ByteVec,
}

/// SVC_ROOMTYPE 37
#[derive(Debug, Clone)]
pub struct SvcRoomType {
    pub room_type: u16,
}

/// SVC_ADDANGLE 38
#[derive(Debug, Clone)]
pub struct SvcAddAngle {
    pub angle_to_add: i16,
}

/// SVC_NEWUSERMSG 39
#[derive(Debug, Clone)]
pub struct SvcNewUserMsg {
    pub index: u8,
    // weird but it's for consistency
    pub size: i8,
    /// `[u8; 16]`
    pub name: ByteString,
}

/// SVC_PACKETENTITIES 40
#[derive(Debug, Clone)]
pub struct SvcPacketEntities {
    /// `[bool; 16]`
    pub entity_count: BitVec,
    pub entity_states: Vec<EntityState>,
}
#[derive(Debug, Clone)]
pub struct EntityState {
    pub entity_index: u16,
    pub increment_entity_number: bool,
    pub is_absolute_entity_index: Option<bool>,
    /// `[bool; 11]`
    pub absolute_entity_index: Option<BitVec>,
    /// `[bool; 6]`
    pub entity_index_difference: Option<BitVec>,
    pub has_custom_delta: bool,
    pub has_baseline_index: bool,
    /// `[bool; 6]`
    pub baseline_index: Option<BitVec>,
    pub delta: Delta,
}

/// SVC_DELTAPACKETENTITIES 41
#[derive(Debug, Clone)]
pub struct SvcDeltaPacketEntities {
    /// `[bool; 16]`
    pub entity_count: BitVec,
    /// `[bool; 8]`
    pub delta_sequence: BitVec,
    pub entity_states: Vec<EntityStateDelta>,
}
#[derive(Debug, Clone)]
pub struct EntityStateDelta {
    /// `[bool; 11]` but do u16 because arithmetic.
    pub entity_index: u16,
    pub remove_entity: bool,
    pub is_absolute_entity_index: bool,
    /// `[bool; 11]`
    pub absolute_entity_index: Option<BitVec>,
    /// `[bool; 6]`
    pub entity_index_difference: Option<BitVec>,
    // Need to be optional because if remove is true then it won't have delta.
    pub has_custom_delta: Option<bool>,
    pub delta: Option<Delta>,
}

// SVC_CHOKE 42

/// SVC_RESOURCELIST 43
#[derive(Debug, Clone)]
pub struct SvcResourceList {
    /// `[bool; 12]`
    pub resource_count: BitVec,
    pub resources: Vec<Resource>,
    pub consistencies: Vec<Consistency>,
}
#[derive(Debug, Clone)]
pub struct Resource {
    /// `[bool; 4]`
    pub type_: BitVec,
    /// `&'[u8]`
    pub name: BitVec,
    /// `[bool; 12]`
    pub index: BitVec,
    /// `[bool; 24]`
    pub size: BitVec,
    /// `[bool; 3]`
    pub flags: BitVec,
    /// `[bool; 128]`
    pub md5_hash: Option<BitVec>,
    pub has_extra_info: bool,
    /// `[bool; 256]`
    pub extra_info: Option<BitVec>,
}
#[derive(Debug, Clone)]
pub struct Consistency {
    pub is_short_index: Option<bool>,
    /// `[bool; 5]`
    pub short_index: Option<BitVec>,
    /// `[bool; 10]`
    pub long_index: Option<BitVec>,
}

/// SVC_NEWMOVEVARS 44
#[derive(Debug, Clone)]
pub struct SvcNewMovevars {
    pub gravity: f32,
    pub stop_speed: f32,
    pub max_speed: f32,
    pub spectator_max_speed: f32,
    pub accelerate: f32,
    pub airaccelerate: f32,
    pub water_accelerate: f32,
    pub friction: f32,
    pub edge_friction: f32,
    pub water_friction: f32,
    pub ent_garvity: f32,
    pub bounce: f32,
    pub step_size: f32,
    pub max_velocity: f32,
    pub z_max: f32,
    pub wave_height: f32,
    pub footsteps: u8,
    pub roll_angle: f32,
    pub roll_speed: f32,
    /// Vec3
    pub sky_color: Vec<f32>,
    /// Vec3
    pub sky_vec: Vec<f32>,
    pub sky_name: ByteVec,
}

/// SVC_RESOURCEREQUEST 45
#[derive(Debug, Clone)]
pub struct SvcResourceRequest {
    pub spawn_count: i32,
    pub unknown: Vec<u8>,
}

/// SVC_CUSTOMIZATION 46
#[derive(Debug, Clone)]
pub struct SvcCustomization {
    pub player_index: u8,
    pub type_: u8,
    pub name: ByteVec,
    pub index: u16,
    pub download_size: u32,
    pub flags: u8,
    /// `[u8; 16]`
    pub md5_hash: Option<ByteVec>,
}

/// SVC_CROSSHAIRANGLE 47
#[derive(Debug, Clone)]
pub struct SvcCrosshairAngle {
    pub pitch: i16,
    pub yaw: i16,
}

/// SVC_SOUNDFADE 48
#[derive(Debug, Clone)]
pub struct SvcSoundFade {
    pub initial_percent: u8,
    pub hold_time: u8,
    pub fade_out_time: u8,
    pub fade_in_time: u8,
}

/// SVC_FILETXFERFAILED 49
#[derive(Debug, Clone)]
pub struct SvcFileTxferFailed {
    pub file_name: ByteVec,
}

/// SVC_HLTV 50
#[derive(Debug, Clone)]
pub struct SvcHltv {
    pub mode: u8,
}

/// SVC_DIRECTOR 51
///
/// To interpret message, check
///
/// https://github.com/ValveSoftware/halflife/blob/b1b5cf5892918535619b2937bb927e46cb097ba1/cl_dll/hud_spectator.cpp#L682
#[derive(Debug, Clone)]
pub struct SvcDirector {
    pub length: u8,
    pub command: u8,
    /// To interpret message, check
    ///
    /// https://github.com/ValveSoftware/halflife/blob/b1b5cf5892918535619b2937bb927e46cb097ba1/cl_dll/hud_spectator.cpp#L682
    pub message: ByteVec,
}

/// SVC_VOINCEINIT 52
#[derive(Debug, Clone)]
pub struct SvcVoiceInit {
    pub codec_name: ByteVec,
    pub quality: i8,
}

/// SVC_VOICEDATA 53
#[derive(Debug, Clone)]
pub struct SvcVoiceData {
    pub player_index: u8,
    pub size: u16,
    pub data: ByteVec,
}

/// SVC_SENDEXTRAINFO 54
#[derive(Debug, Clone)]
pub struct SvcSendExtraInfo {
    pub fallback_dir: ByteVec,
    pub can_cheat: u8,
}

/// SVC_TIMESCALE 55
#[derive(Debug, Clone)]
pub struct SvcTimeScale {
    pub time_scale: f32,
}

/// SVC_RESOURCELOCATION 56
#[derive(Debug, Clone)]
pub struct SvcResourceLocation {
    pub download_url: ByteVec,
}

/// SVC_SENDCVARVALUE 57
#[derive(Debug, Clone)]
pub struct SvcSendCvarValue {
    pub name: ByteString,
}

/// SVC_SENDCVARVALUE2 58
#[derive(Debug, Clone)]
pub struct SvcSendCvarValue2 {
    pub request_id: u32,
    pub name: ByteString,
}

#[derive(Clone)]
pub struct ByteString(pub ByteVec);

impl ByteString {
    pub fn to_str(&self) -> eyre::Result<&str> {
        // wtf is this syntax?
        CStr::from_bytes_until_nul(self.0.as_slice())
            .map_err(|err| eyre::eyre!(err))
            .and_then(|cstr| Ok(cstr.to_str()?))
    }

    pub fn as_slice(&self) -> &[u8] {
        self.0.as_slice()
    }

    pub fn padded(&self, size: usize) -> Self {
        let mut res = self.clone();

        if size == res.0.len() {
            return res;
        }

        res.0.resize(size, 0);

        // not sure if this is needed
        // res.0[size - 1] = 0;

        res
    }
}

impl std::fmt::Debug for ByteString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("ByteString")
            .field(&self.to_str().unwrap_or("cannot print").trim())
            .field(&self.0)
            .finish()
    }
}

impl From<&[u8]> for ByteString {
    fn from(value: &[u8]) -> Self {
        ByteString(value.to_vec())
    }
}

impl From<Vec<u8>> for ByteString {
    fn from(value: Vec<u8>) -> Self {
        ByteString(value)
    }
}

impl From<&str> for ByteString {
    fn from(value: &str) -> Self {
        value.as_bytes().into()
    }
}

impl From<ByteString> for Vec<u8> {
    fn from(value: ByteString) -> Self {
        value.0
    }
}