steamkit-rs 0.1.2

Rust port of SteamKit2, a .NET library that aims to interoperate with the Steam network.
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
//! Callbacks posted by [`SteamApps`](super::steam_apps::SteamApps).
//!
//! Maps are `BTreeMap`s so iteration order is stable. `ELicenseFlags` is a bit
//! set, but the generated enum can only hold one value, so the raw bits are
//! kept too (see [`License::license_flags_value`]).

use std::any::Any;
use std::collections::BTreeMap;
use std::io::{Cursor, Read};

use chrono::{DateTime, Utc};
use steamkit_protos::steam::{
    c_msg_client_license_list, c_msg_client_pics_product_info_response,
    CMsgClientCheckAppBetaPasswordResponse, CMsgClientGameConnectTokens,
    CMsgClientGetAppOwnershipTicketResponse, CMsgClientGetDepotDecryptionKeyResponse,
    CMsgClientLicenseList, CMsgClientPicsAccessTokenResponse, CMsgClientPicsChangesSinceResponse,
    CMsgClientPicsPrivateBetaResponse, CMsgClientPicsProductInfoResponse,
    CMsgClientPurchaseResponse, CMsgClientRedeemGuestPassResponse,
    CMsgClientRequestFreeLicenseResponse,
};

use crate::base::client_msg::{ClientMsg, ClientMsgProtobuf, IClientMsg};
use crate::base::generated::steam_language::internal::{
    MsgClientGetLegacyGameKeyResponse, MsgClientUpdateGuestPassesList, MsgClientVACBanStatus,
};
use crate::base::generated::steam_language::{
    ELicenseFlags, ELicenseType, EPaymentMethod, EPurchaseResultDetail, EResult,
};
use crate::base::msg_base::MsgBase;
use crate::base::packet_base::IPacketMsg;
use crate::error::SteamResult;
use crate::steam::steam_client::callback_mgr::callback_msg::{CallbackMsg, CallbackMsgBase};
use crate::types::job_id::JobID;
use crate::types::key_value::KeyValue;

/// What an absent `eresult` field defaults to in the Steam protocol.
const ERESULT_DEFAULT: i32 = 2;

/// Converts an optional `eresult` into an [`EResult`].
fn eresult_from_i32(value: Option<i32>) -> EResult {
    EResult::from_value(value.unwrap_or(ERESULT_DEFAULT)).unwrap_or(EResult::Invalid)
}

/// Converts an optional `eresult` into an [`EResult`].
fn eresult_from_u32(value: Option<u32>) -> EResult {
    EResult::from_value(value.unwrap_or(ERESULT_DEFAULT as u32) as i32).unwrap_or(EResult::Invalid)
}

/// Converts a Unix timestamp to a UTC date time.
fn date_time_from_unix_time(seconds: u64) -> DateTime<Utc> {
    match crate::util::date_utils::DateUtils::date_time_from_unix_time(seconds) {
        Ok(time) => DateTime::<Utc>::from(time),
        Err(_) => DateTime::<Utc>::from(std::time::UNIX_EPOCH),
    }
}

/// A [`Read`] adapter over the payload of a client message.
///
/// It hands out one byte per call on purpose: the binary [`KeyValue`] reader
/// buffers 4 KiB at a time, which would move the payload position way past the
/// entry it parsed and break every entry after the first.
struct PayloadReader<'a> {
    payload: &'a mut MsgBase,
}

impl Read for PayloadReader<'_> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        if buf.is_empty() {
            return Ok(0);
        }

        let start = (self.payload.position as usize).min(self.payload.payload.len());
        if start >= self.payload.payload.len() {
            return Ok(0);
        }

        buf[0] = self.payload.payload[start];
        self.payload.position += 1;
        Ok(1)
    }
}

/// Posted during logon with the account's licenses, and again whenever the
/// license list changes (a purchase, a redeemed key, a refund, ...).
#[derive(Clone, Debug)]
pub struct LicenseListCallback {
    base: CallbackMsgBase,
    result: EResult,
    license_list: Vec<License>,
}

impl LicenseListCallback {
    /// Builds the callback from a received `ClientLicenseList` message.
    pub(crate) fn from_packet(packet_msg: &dyn IPacketMsg) -> SteamResult<Self> {
        let license_list = ClientMsgProtobuf::<CMsgClientLicenseList>::from_packet(packet_msg)?;
        let body = license_list.body();

        let licenses = body.licenses.iter().map(License::from_proto).collect();

        Ok(Self {
            base: CallbackMsgBase::new(),
            result: eresult_from_i32(body.eresult),
            license_list: licenses,
        })
    }

    /// The result of the message.
    pub fn result(&self) -> EResult {
        self.result
    }

    /// The license list.
    pub fn license_list(&self) -> &[License] {
        &self.license_list
    }
}

impl CallbackMsg for LicenseListCallback {
    fn job_id(&self) -> JobID {
        self.base.job_id()
    }

    fn set_job_id(&mut self, id: JobID) {
        self.base.set_job_id(id);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// A granted license (steam3 subscription) for one or more games.
#[derive(Clone, Debug)]
pub struct License {
    package_id: u32,
    last_change_number: i32,
    time_created: DateTime<Utc>,
    time_next_process: DateTime<Utc>,
    minute_limit: i32,
    minutes_used: i32,
    payment_method: EPaymentMethod,
    payment_method_value: u32,
    license_flags: ELicenseFlags,
    license_flags_value: u32,
    purchase_country_code: String,
    license_type: ELicenseType,
    territory_code: i32,
    owner_account_id: u32,
    access_token: u64,
    master_package_id: u32,
}

impl License {
    /// Builds a license from its protobuf form.
    fn from_proto(license: &c_msg_client_license_list::License) -> Self {
        let payment_method_value = license.payment_method.unwrap_or_default();
        let license_flags_value = license.flags.unwrap_or_default();

        Self {
            package_id: license.package_id.unwrap_or_default(),
            last_change_number: license.change_number.unwrap_or_default(),
            time_created: date_time_from_unix_time(u64::from(
                license.time_created.unwrap_or_default(),
            )),
            time_next_process: date_time_from_unix_time(u64::from(
                license.time_next_process.unwrap_or_default(),
            )),
            minute_limit: license.minute_limit.unwrap_or_default(),
            minutes_used: license.minutes_used.unwrap_or_default(),
            payment_method: EPaymentMethod::from_value(payment_method_value as i32)
                .unwrap_or(EPaymentMethod::None),
            payment_method_value,
            license_flags: ELicenseFlags::from_value(license_flags_value as i32)
                .unwrap_or(ELicenseFlags::None),
            license_flags_value,
            purchase_country_code: license.purchase_country_code.clone().unwrap_or_default(),
            license_type: ELicenseType::from_value(license.license_type.unwrap_or_default() as i32)
                .unwrap_or(ELicenseType::NoLicense),
            territory_code: license.territory_code.unwrap_or_default(),
            owner_account_id: license.owner_id.unwrap_or_default(),
            access_token: license.access_token.unwrap_or_default(),
            master_package_id: license.master_package_id.unwrap_or_default(),
        }
    }

    /// The package ID used to identify the license.
    pub fn package_id(&self) -> u32 {
        self.package_id
    }

    /// The last change number for this license.
    pub fn last_change_number(&self) -> i32 {
        self.last_change_number
    }

    /// The time the license was created.
    pub fn time_created(&self) -> DateTime<Utc> {
        self.time_created
    }

    /// The next process time for the license.
    pub fn time_next_process(&self) -> DateTime<Utc> {
        self.time_next_process
    }

    /// The minute limit of the license.
    pub fn minute_limit(&self) -> i32 {
        self.minute_limit
    }

    /// The minutes used of the license.
    pub fn minutes_used(&self) -> i32 {
        self.minutes_used
    }

    /// The payment method used when the license was created.
    pub fn payment_method(&self) -> EPaymentMethod {
        self.payment_method
    }

    /// The raw value of the payment method.
    pub fn payment_method_value(&self) -> u32 {
        self.payment_method_value
    }

    /// The license flags.
    pub fn license_flags(&self) -> ELicenseFlags {
        self.license_flags
    }

    /// The raw bits of the license flags.
    pub fn license_flags_value(&self) -> u32 {
        self.license_flags_value
    }

    /// The two letter country code where the license was purchased.
    pub fn purchase_country_code(&self) -> &str {
        &self.purchase_country_code
    }

    /// The type of the license.
    pub fn license_type(&self) -> ELicenseType {
        self.license_type
    }

    /// The territory code of the license.
    pub fn territory_code(&self) -> i32 {
        self.territory_code
    }

    /// The owner account id of the license.
    pub fn owner_account_id(&self) -> u32 {
        self.owner_account_id
    }

    /// The PICS access token for this package.
    pub fn access_token(&self) -> u64 {
        self.access_token
    }

    /// The master package id.
    pub fn master_package_id(&self) -> u32 {
        self.master_package_id
    }
}

/// This callback is received in response to requesting a free license.
#[derive(Clone, Debug)]
pub struct FreeLicenseCallback {
    base: CallbackMsgBase,
    result: EResult,
    granted_apps: Vec<u32>,
    granted_packages: Vec<u32>,
}

impl FreeLicenseCallback {
    /// Builds the callback from a received `ClientRequestFreeLicenseResponse` message.
    pub(crate) fn from_packet(packet_msg: &dyn IPacketMsg) -> SteamResult<Self> {
        let granted =
            ClientMsgProtobuf::<CMsgClientRequestFreeLicenseResponse>::from_packet(packet_msg)?;
        let body = granted.body();

        Ok(Self {
            base: CallbackMsgBase::with_job_id(granted.target_job_id()),
            result: eresult_from_u32(body.eresult),
            granted_apps: body.granted_appids.clone(),
            granted_packages: body.granted_packageids.clone(),
        })
    }

    /// The result of the message.
    pub fn result(&self) -> EResult {
        self.result
    }

    /// The list of granted apps.
    pub fn granted_apps(&self) -> &[u32] {
        &self.granted_apps
    }

    /// The list of granted packages.
    pub fn granted_packages(&self) -> &[u32] {
        &self.granted_packages
    }
}

impl CallbackMsg for FreeLicenseCallback {
    fn job_id(&self) -> JobID {
        self.base.job_id()
    }

    fn set_job_id(&mut self, id: JobID) {
        self.base.set_job_id(id);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}
/// This callback is received in response to requesting an app ownership ticket.
#[derive(Clone, Debug)]
pub struct AppOwnershipTicketCallback {
    base: CallbackMsgBase,
    result: EResult,
    app_id: u32,
    ticket: Vec<u8>,
}

impl AppOwnershipTicketCallback {
    /// Builds the callback from a received `ClientGetAppOwnershipTicketResponse` message.
    pub(crate) fn from_packet(packet_msg: &dyn IPacketMsg) -> SteamResult<Self> {
        let response =
            ClientMsgProtobuf::<CMsgClientGetAppOwnershipTicketResponse>::from_packet(packet_msg)?;
        let body = response.body();

        Ok(Self {
            base: CallbackMsgBase::with_job_id(response.target_job_id()),
            result: eresult_from_u32(body.eresult),
            app_id: body.app_id.unwrap_or_default(),
            ticket: body.ticket.clone().unwrap_or_default(),
        })
    }

    /// The result of requesting the ticket.
    pub fn result(&self) -> EResult {
        self.result
    }

    /// The app id this ticket is for.
    pub fn app_id(&self) -> u32 {
        self.app_id
    }

    /// The ticket data.
    pub fn ticket(&self) -> &[u8] {
        &self.ticket
    }
}

impl CallbackMsg for AppOwnershipTicketCallback {
    fn job_id(&self) -> JobID {
        self.base.job_id()
    }

    fn set_job_id(&mut self, id: JobID) {
        self.base.set_job_id(id);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// This callback is received in response to requesting a depot decryption key.
#[derive(Clone, Debug)]
pub struct DepotKeyCallback {
    base: CallbackMsgBase,
    result: EResult,
    depot_id: u32,
    depot_key: Vec<u8>,
}

impl DepotKeyCallback {
    /// Builds the callback from a received `ClientGetDepotDecryptionKeyResponse` message.
    pub(crate) fn from_packet(packet_msg: &dyn IPacketMsg) -> SteamResult<Self> {
        let response =
            ClientMsgProtobuf::<CMsgClientGetDepotDecryptionKeyResponse>::from_packet(packet_msg)?;
        let body = response.body();

        Ok(Self {
            base: CallbackMsgBase::with_job_id(response.target_job_id()),
            result: eresult_from_i32(body.eresult),
            depot_id: body.depot_id.unwrap_or_default(),
            depot_key: body.depot_encryption_key.clone().unwrap_or_default(),
        })
    }

    /// The result of requesting this encryption key.
    pub fn result(&self) -> EResult {
        self.result
    }

    /// The depot id this encryption key is for.
    pub fn depot_id(&self) -> u32 {
        self.depot_id
    }

    /// The encryption key for this depot.
    pub fn depot_key(&self) -> &[u8] {
        &self.depot_key
    }
}

impl CallbackMsg for DepotKeyCallback {
    fn job_id(&self) -> JobID {
        self.base.job_id()
    }

    fn set_job_id(&mut self, id: JobID) {
        self.base.set_job_id(id);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// Decodes bytes as ASCII; anything above `0x7F` becomes `?`.
fn ascii_decode(bytes: &[u8]) -> String {
    bytes
        .iter()
        .map(|byte| if *byte < 0x80 { *byte as char } else { '?' })
        .collect()
}

/// This callback is received in response to requesting a legacy game key.
#[derive(Clone, Debug)]
pub struct LegacyGameKeyCallback {
    base: CallbackMsgBase,
    result: EResult,
    app_id: u32,
    key: Option<String>,
}

impl LegacyGameKeyCallback {
    /// Builds the callback from a received `ClientGetLegacyGameKeyResponse` message.
    pub(crate) fn from_packet(packet_msg: &dyn IPacketMsg) -> SteamResult<Self> {
        let response = ClientMsg::<MsgClientGetLegacyGameKeyResponse>::from_packet(packet_msg)?;
        let body = response.body();

        let key = if body.length > 0 {
            let payload = response.payload().to_bytes();
            let length = (body.length as usize).saturating_sub(1).min(payload.len());
            Some(ascii_decode(&payload[..length]))
        } else {
            None
        };

        Ok(Self {
            base: CallbackMsgBase::with_job_id(response.target_job_id()),
            result: body.result,
            app_id: body.app_id,
            key,
        })
    }

    /// The result of requesting this game key.
    pub fn result(&self) -> EResult {
        self.result
    }

    /// The app id that this game key is for.
    pub fn app_id(&self) -> u32 {
        self.app_id
    }

    /// The game key.
    pub fn key(&self) -> Option<&str> {
        self.key.as_deref()
    }
}

impl CallbackMsg for LegacyGameKeyCallback {
    fn job_id(&self) -> JobID {
        self.base.job_id()
    }

    fn set_job_id(&mut self, id: JobID) {
        self.base.set_job_id(id);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// This callback is fired when the client receives a list of game connect tokens.
#[derive(Clone, Debug)]
pub struct GameConnectTokensCallback {
    base: CallbackMsgBase,
    tokens_to_keep: u32,
    tokens: Vec<Vec<u8>>,
}

impl GameConnectTokensCallback {
    /// Builds the callback from a received `ClientGameConnectTokens` message.
    pub(crate) fn from_packet(packet_msg: &dyn IPacketMsg) -> SteamResult<Self> {
        let tokens = ClientMsgProtobuf::<CMsgClientGameConnectTokens>::from_packet(packet_msg)?;
        let body = tokens.body();

        Ok(Self {
            base: CallbackMsgBase::new(),
            tokens_to_keep: body.max_tokens_to_keep.unwrap_or_default(),
            tokens: body.tokens.clone(),
        })
    }

    /// The count of tokens to keep.
    pub fn tokens_to_keep(&self) -> u32 {
        self.tokens_to_keep
    }

    /// The list of tokens.
    pub fn tokens(&self) -> &[Vec<u8>] {
        &self.tokens
    }
}

impl CallbackMsg for GameConnectTokensCallback {
    fn job_id(&self) -> JobID {
        self.base.job_id()
    }

    fn set_job_id(&mut self, id: JobID) {
        self.base.set_job_id(id);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// This callback is fired when the client receives its VAC banned status.
#[derive(Clone, Debug)]
pub struct VACStatusCallback {
    base: CallbackMsgBase,
    banned_apps: Vec<u32>,
}

impl VACStatusCallback {
    /// Builds the callback from a received `ClientVACBanStatus` message.
    pub(crate) fn from_packet(packet_msg: &dyn IPacketMsg) -> SteamResult<Self> {
        let vac_status = ClientMsg::<MsgClientVACBanStatus>::from_packet(packet_msg)?;
        let num_bans = vac_status.body().num_bans as usize;

        let mut banned_apps = Vec::with_capacity(num_bans);
        let mut reader = vac_status.payload().reader();

        for _ in 0..num_bans {
            banned_apps.push(reader.read_u32()?);
        }

        Ok(Self {
            base: CallbackMsgBase::new(),
            banned_apps,
        })
    }

    /// The list of VAC banned apps the client is banned from.
    pub fn banned_apps(&self) -> &[u32] {
        &self.banned_apps
    }
}

impl CallbackMsg for VACStatusCallback {
    fn job_id(&self) -> JobID {
        self.base.job_id()
    }

    fn set_job_id(&mut self, id: JobID) {
        self.base.set_job_id(id);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// Posted when PICS returns access tokens for a list of app and package ids.
#[derive(Clone, Debug)]
pub struct PICSTokensCallback {
    base: CallbackMsgBase,
    package_tokens_denied: Vec<u32>,
    app_tokens_denied: Vec<u32>,
    package_tokens: BTreeMap<u32, u64>,
    app_tokens: BTreeMap<u32, u64>,
}

impl PICSTokensCallback {
    /// Builds the callback from a received `ClientPICSAccessTokenResponse` message.
    pub(crate) fn from_packet(packet_msg: &dyn IPacketMsg) -> SteamResult<Self> {
        let response =
            ClientMsgProtobuf::<CMsgClientPicsAccessTokenResponse>::from_packet(packet_msg)?;
        let body = response.body();

        let mut package_tokens = BTreeMap::new();
        for token in &body.package_access_tokens {
            package_tokens.insert(
                token.packageid.unwrap_or_default(),
                token.access_token.unwrap_or_default(),
            );
        }

        let mut app_tokens = BTreeMap::new();
        for token in &body.app_access_tokens {
            app_tokens.insert(
                token.appid.unwrap_or_default(),
                token.access_token.unwrap_or_default(),
            );
        }

        Ok(Self {
            base: CallbackMsgBase::with_job_id(response.target_job_id()),
            package_tokens_denied: body.package_denied_tokens.clone(),
            app_tokens_denied: body.app_denied_tokens.clone(),
            package_tokens,
            app_tokens,
        })
    }

    /// The list of denied package tokens.
    pub fn package_tokens_denied(&self) -> &[u32] {
        &self.package_tokens_denied
    }

    /// The list of denied app tokens.
    pub fn app_tokens_denied(&self) -> &[u32] {
        &self.app_tokens_denied
    }

    /// The requested package tokens.
    pub fn package_tokens(&self) -> &BTreeMap<u32, u64> {
        &self.package_tokens
    }

    /// The requested app tokens.
    pub fn app_tokens(&self) -> &BTreeMap<u32, u64> {
        &self.app_tokens
    }
}

impl CallbackMsg for PICSTokensCallback {
    fn job_id(&self) -> JobID {
        self.base.job_id()
    }

    fn set_job_id(&mut self, id: JobID) {
        self.base.set_job_id(id);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}
/// Posted when PICS returns the changes since a given change number.
#[derive(Clone, Debug)]
pub struct PICSChangesCallback {
    base: CallbackMsgBase,
    last_change_number: u32,
    current_change_number: u32,
    requires_full_update: bool,
    requires_full_app_update: bool,
    requires_full_package_update: bool,
    package_changes: BTreeMap<u32, PICSChangeData>,
    app_changes: BTreeMap<u32, PICSChangeData>,
}

impl PICSChangesCallback {
    /// Builds the callback from a received `ClientPICSChangesSinceResponse` message.
    pub(crate) fn from_packet(packet_msg: &dyn IPacketMsg) -> SteamResult<Self> {
        let response =
            ClientMsgProtobuf::<CMsgClientPicsChangesSinceResponse>::from_packet(packet_msg)?;
        let body = response.body();

        let mut package_changes = BTreeMap::new();
        for change in &body.package_changes {
            package_changes.insert(
                change.packageid.unwrap_or_default(),
                PICSChangeData::new(
                    change.packageid.unwrap_or_default(),
                    change.change_number.unwrap_or_default(),
                    change.needs_token.unwrap_or_default(),
                ),
            );
        }

        let mut app_changes = BTreeMap::new();
        for change in &body.app_changes {
            app_changes.insert(
                change.appid.unwrap_or_default(),
                PICSChangeData::new(
                    change.appid.unwrap_or_default(),
                    change.change_number.unwrap_or_default(),
                    change.needs_token.unwrap_or_default(),
                ),
            );
        }

        Ok(Self {
            base: CallbackMsgBase::with_job_id(response.target_job_id()),
            last_change_number: body.since_change_number.unwrap_or_default(),
            current_change_number: body.current_change_number.unwrap_or_default(),
            requires_full_update: body.force_full_update.unwrap_or_default(),
            requires_full_app_update: body.force_full_app_update.unwrap_or_default(),
            requires_full_package_update: body.force_full_package_update.unwrap_or_default(),
            package_changes,
            app_changes,
        })
    }

    /// The change number that was supplied with the request.
    pub fn last_change_number(&self) -> u32 {
        self.last_change_number
    }

    /// The current change number.
    pub fn current_change_number(&self) -> u32 {
        self.current_change_number
    }

    /// Whether this update requires a full update of the information.
    pub fn requires_full_update(&self) -> bool {
        self.requires_full_update
    }

    /// Whether this update requires a full update of the app information.
    pub fn requires_full_app_update(&self) -> bool {
        self.requires_full_app_update
    }

    /// Whether this update requires a full update of the package information.
    pub fn requires_full_package_update(&self) -> bool {
        self.requires_full_package_update
    }

    /// The package changes.
    pub fn package_changes(&self) -> &BTreeMap<u32, PICSChangeData> {
        &self.package_changes
    }

    /// The app changes.
    pub fn app_changes(&self) -> &BTreeMap<u32, PICSChangeData> {
        &self.app_changes
    }
}

impl CallbackMsg for PICSChangesCallback {
    fn job_id(&self) -> JobID {
        self.base.job_id()
    }

    fn set_job_id(&mut self, id: JobID) {
        self.base.set_job_id(id);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// The change data of a single app or package.
#[derive(Clone, Copy, Debug)]
pub struct PICSChangeData {
    id: u32,
    change_number: u32,
    needs_token: bool,
}

impl PICSChangeData {
    /// Creates a change data entry.
    fn new(id: u32, change_number: u32, needs_token: bool) -> Self {
        Self {
            id,
            change_number,
            needs_token,
        }
    }

    /// The app or package id this change data represents.
    pub fn id(&self) -> u32 {
        self.id
    }

    /// The current change number of this app or package.
    pub fn change_number(&self) -> u32 {
        self.change_number
    }

    /// Whether an access token is needed for this request.
    pub fn needs_token(&self) -> bool {
        self.needs_token
    }
}

/// Posted when PICS returns requested product information.
#[derive(Clone, Debug)]
pub struct PICSProductInfoCallback {
    base: CallbackMsgBase,
    meta_data_only: bool,
    response_pending: bool,
    unknown_packages: Vec<u32>,
    unknown_apps: Vec<u32>,
    apps: BTreeMap<u32, PICSProductInfo>,
    packages: BTreeMap<u32, PICSProductInfo>,
}

impl PICSProductInfoCallback {
    /// Builds the callback from a received `ClientPICSProductInfoResponse` message.
    pub(crate) fn from_packet(packet_msg: &dyn IPacketMsg) -> SteamResult<Self> {
        let response =
            ClientMsgProtobuf::<CMsgClientPicsProductInfoResponse>::from_packet(packet_msg)?;
        let body = response.body();

        let http_host = body.http_host.clone().unwrap_or_default();
        let http_min_size = body.http_min_size.unwrap_or_default();

        let mut packages = BTreeMap::new();
        for package in &body.packages {
            packages.insert(
                package.packageid.unwrap_or_default(),
                PICSProductInfo::from_package(package),
            );
        }

        let mut apps = BTreeMap::new();
        for app in &body.apps {
            apps.insert(
                app.appid.unwrap_or_default(),
                PICSProductInfo::from_app(app, &http_host, http_min_size),
            );
        }

        Ok(Self {
            base: CallbackMsgBase::with_job_id(response.target_job_id()),
            meta_data_only: body.meta_data_only.unwrap_or_default(),
            response_pending: body.response_pending.unwrap_or_default(),
            unknown_packages: body.unknown_packageids.clone(),
            unknown_apps: body.unknown_appids.clone(),
            apps,
            packages,
        })
    }

    /// Whether this response contains only product metadata.
    pub fn meta_data_only(&self) -> bool {
        self.meta_data_only
    }

    /// Whether there are more product information responses pending.
    pub fn response_pending(&self) -> bool {
        self.response_pending
    }

    /// The list of unknown package ids.
    pub fn unknown_packages(&self) -> &[u32] {
        &self.unknown_packages
    }

    /// The list of unknown app ids.
    pub fn unknown_apps(&self) -> &[u32] {
        &self.unknown_apps
    }

    /// The requested app info.
    pub fn apps(&self) -> &BTreeMap<u32, PICSProductInfo> {
        &self.apps
    }

    /// The requested package info.
    pub fn packages(&self) -> &BTreeMap<u32, PICSProductInfo> {
        &self.packages
    }
}

impl CallbackMsg for PICSProductInfoCallback {
    fn job_id(&self) -> JobID {
        self.base.job_id()
    }

    fn set_job_id(&mut self, id: JobID) {
        self.base.set_job_id(id);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// The information of a single app or package.
#[derive(Clone, Debug)]
pub struct PICSProductInfo {
    id: u32,
    change_number: u32,
    missing_token: bool,
    sha_hash: Option<Vec<u8>>,
    key_values: KeyValue,
    only_public: bool,
    use_http: bool,
    http_host: String,
}

impl PICSProductInfo {
    /// Builds the product info of an app.
    fn from_app(
        app_info: &c_msg_client_pics_product_info_response::AppInfo,
        http_host: &str,
        http_min_size: u32,
    ) -> Self {
        let sha_hash = app_info.sha.clone();
        let key_values = decode_text_key_values(app_info.buffer.as_deref());

        let has_valid_http_uri =
            sha_hash.as_ref().is_some_and(|sha| !sha.is_empty()) && !http_host.is_empty();

        Self {
            id: app_info.appid.unwrap_or_default(),
            change_number: app_info.change_number.unwrap_or_default(),
            missing_token: app_info.missing_token.unwrap_or_default(),
            sha_hash,
            key_values,
            only_public: app_info.only_public.unwrap_or_default(),
            use_http: has_valid_http_uri && app_info.size.unwrap_or_default() >= http_min_size,
            http_host: http_host.to_string(),
        }
    }

    /// Builds the product info of a package.
    fn from_package(package_info: &c_msg_client_pics_product_info_response::PackageInfo) -> Self {
        let key_values = decode_binary_key_values(package_info.buffer.as_deref());

        Self {
            id: package_info.packageid.unwrap_or_default(),
            change_number: package_info.change_number.unwrap_or_default(),
            missing_token: package_info.missing_token.unwrap_or_default(),
            sha_hash: package_info.sha.clone(),
            key_values,
            only_public: false,
            use_http: false,
            http_host: String::new(),
        }
    }

    /// The id of the app or package.
    pub fn id(&self) -> u32 {
        self.id
    }

    /// The current change number for the app or package.
    pub fn change_number(&self) -> u32 {
        self.change_number
    }

    /// Whether an access token was required for the request.
    pub fn missing_token(&self) -> bool {
        self.missing_token
    }

    /// The hash of the content.
    pub fn sha_hash(&self) -> Option<&[u8]> {
        self.sha_hash.as_deref()
    }

    /// The key value info.
    pub fn key_values(&self) -> &KeyValue {
        &self.key_values
    }

    /// For an app request, gets whether only the public information was requested.
    pub fn only_public(&self) -> bool {
        self.only_public
    }

    /// Whether HTTP should be used to load the key values data.
    pub fn use_http(&self) -> bool {
        self.use_http
    }

    /// For an app metadata-only request, gets the URI for the HTTP appinfo request.
    pub fn http_uri(&self) -> Option<String> {
        let sha_hash = self.sha_hash.as_ref()?;
        if sha_hash.is_empty() || self.http_host.is_empty() {
            return None;
        }

        Some(format!(
            "http://{}/appinfo/{}/sha/{}.txt.gz",
            self.http_host,
            self.id,
            hex::encode(sha_hash)
        ))
    }
}

/// Decodes the text key values of an app info buffer.
fn decode_text_key_values(buffer: Option<&[u8]>) -> KeyValue {
    let mut key_value = KeyValue::new();

    let Some(buffer) = buffer else {
        return key_value;
    };

    if buffer.is_empty() {
        return key_value;
    }

    // The trailing null byte isn't part of the text.
    let text = &buffer[..buffer.len() - 1];
    let mut cursor = Cursor::new(text);

    let _ = key_value.read_as_text(&mut cursor);

    key_value
}

/// Decodes the binary key values of a package info buffer.
fn decode_binary_key_values(buffer: Option<&[u8]>) -> KeyValue {
    let mut key_value = KeyValue::new();

    let Some(buffer) = buffer else {
        return key_value;
    };

    // The first four bytes are a flag the Steam client checks before reading
    // the key values.
    let payload = &buffer[buffer.len().min(4)..];
    let mut cursor = Cursor::new(payload);

    let _ = key_value.try_read_as_binary(&mut cursor);

    key_value
}

/// This callback is received when the list of guest passes is updated.
#[derive(Clone, Debug)]
pub struct GuestPassListCallback {
    base: CallbackMsgBase,
    result: EResult,
    count_guest_passes_to_give: i32,
    count_guest_passes_to_redeem: i32,
    guest_passes: Vec<KeyValue>,
}

impl GuestPassListCallback {
    /// Builds the callback from a received `ClientUpdateGuestPassesList` message.
    pub(crate) fn from_packet(packet_msg: &dyn IPacketMsg) -> SteamResult<Self> {
        let mut guest_passes =
            ClientMsg::<MsgClientUpdateGuestPassesList>::from_packet(packet_msg)?;

        let result = guest_passes.body().result;
        let count_guest_passes_to_give = guest_passes.body().count_guest_passes_to_give;
        let count_guest_passes_to_redeem = guest_passes.body().count_guest_passes_to_redeem;

        let total = (count_guest_passes_to_give + count_guest_passes_to_redeem).max(0) as usize;

        let mut passes = Vec::with_capacity(total);
        let mut reader = PayloadReader {
            payload: &mut guest_passes.payload,
        };

        for _ in 0..total {
            let mut key_value = KeyValue::new();
            if !key_value.try_read_as_binary(&mut reader) {
                break;
            }
            passes.push(key_value);
        }

        Ok(Self {
            base: CallbackMsgBase::new(),
            result,
            count_guest_passes_to_give,
            count_guest_passes_to_redeem,
            guest_passes: passes,
        })
    }

    /// The result of the operation.
    pub fn result(&self) -> EResult {
        self.result
    }

    /// The number of guest passes to be given out.
    pub fn count_guest_passes_to_give(&self) -> i32 {
        self.count_guest_passes_to_give
    }

    /// The number of guest passes to be redeemed.
    pub fn count_guest_passes_to_redeem(&self) -> i32 {
        self.count_guest_passes_to_redeem
    }

    /// The guest pass list.
    pub fn guest_passes(&self) -> &[KeyValue] {
        &self.guest_passes
    }
}

impl CallbackMsg for GuestPassListCallback {
    fn job_id(&self) -> JobID {
        self.base.job_id()
    }

    fn set_job_id(&mut self, id: JobID) {
        self.base.set_job_id(id);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// This callback is received in response to activating a guest pass or a gift.
#[derive(Clone, Debug)]
pub struct RedeemGuestPassResponseCallback {
    base: CallbackMsgBase,
    result: EResult,
    package_id: u32,
    must_own_app_id: u32,
}

impl RedeemGuestPassResponseCallback {
    /// Builds the callback from a received `ClientRedeemGuestPassResponse` message.
    pub(crate) fn from_packet(packet_msg: &dyn IPacketMsg) -> SteamResult<Self> {
        let response =
            ClientMsgProtobuf::<CMsgClientRedeemGuestPassResponse>::from_packet(packet_msg)?;
        let body = response.body();

        Ok(Self {
            base: CallbackMsgBase::with_job_id(response.target_job_id()),
            result: eresult_from_u32(body.eresult),
            package_id: body.package_id.unwrap_or_default(),
            must_own_app_id: body.must_own_appid.unwrap_or_default(),
        })
    }

    /// The result of the operation.
    pub fn result(&self) -> EResult {
        self.result
    }

    /// The package id which was activated.
    pub fn package_id(&self) -> u32 {
        self.package_id
    }

    /// The app id which must be owned to activate this guest pass.
    pub fn must_own_app_id(&self) -> u32 {
        self.must_own_app_id
    }
}

impl CallbackMsg for RedeemGuestPassResponseCallback {
    fn job_id(&self) -> JobID {
        self.base.job_id()
    }

    fn set_job_id(&mut self, id: JobID) {
        self.base.set_job_id(id);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// This callback is received in a response to activating a Steam key.
#[derive(Clone, Debug)]
pub struct PurchaseResponseCallback {
    base: CallbackMsgBase,
    result: EResult,
    purchase_result_detail: EPurchaseResultDetail,
    purchase_receipt_info: KeyValue,
}

impl PurchaseResponseCallback {
    /// Builds the callback from a received `ClientPurchaseResponse` message.
    pub(crate) fn from_packet(packet_msg: &dyn IPacketMsg) -> SteamResult<Self> {
        let response = ClientMsgProtobuf::<CMsgClientPurchaseResponse>::from_packet(packet_msg)?;
        let body = response.body();

        let purchase_result_detail =
            EPurchaseResultDetail::from_value(body.purchase_result_details.unwrap_or_default())
                .unwrap_or(EPurchaseResultDetail::NoDetail);

        let mut purchase_receipt_info = KeyValue::new();
        if let Some(receipt) = body.purchase_receipt_info.as_deref() {
            let mut cursor = Cursor::new(receipt);
            let _ = purchase_receipt_info.try_read_as_binary(&mut cursor);
        }

        Ok(Self {
            base: CallbackMsgBase::with_job_id(response.target_job_id()),
            result: eresult_from_i32(body.eresult),
            purchase_result_detail,
            purchase_receipt_info,
        })
    }

    /// The result of the operation.
    pub fn result(&self) -> EResult {
        self.result
    }

    /// The purchase result of the operation.
    pub fn purchase_result_detail(&self) -> EPurchaseResultDetail {
        self.purchase_result_detail
    }

    /// The purchase receipt of the operation.
    pub fn purchase_receipt_info(&self) -> &KeyValue {
        &self.purchase_receipt_info
    }
}

impl CallbackMsg for PurchaseResponseCallback {
    fn job_id(&self) -> JobID {
        self.base.job_id()
    }

    fn set_job_id(&mut self, id: JobID) {
        self.base.set_job_id(id);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// This callback is received when a beta password check has been completed.
#[derive(Clone, Debug)]
pub struct CheckAppBetaPasswordCallback {
    base: CallbackMsgBase,
    result: EResult,
    beta_passwords: BTreeMap<String, Vec<u8>>,
}

impl CheckAppBetaPasswordCallback {
    /// Builds the callback from a received `ClientCheckAppBetaPasswordResponse` message.
    pub(crate) fn from_packet(packet_msg: &dyn IPacketMsg) -> SteamResult<Self> {
        let response =
            ClientMsgProtobuf::<CMsgClientCheckAppBetaPasswordResponse>::from_packet(packet_msg)?;
        let body = response.body();

        let mut beta_passwords = BTreeMap::new();
        for password in &body.betapasswords {
            let Some(name) = password.betaname.as_deref() else {
                continue;
            };

            match crate::util::utils::decode_hex_string(password.betapassword.as_deref()) {
                Ok(Some(decoded)) => {
                    beta_passwords.insert(name.to_string(), decoded);
                }
                Ok(None) => {}
                Err(error) => {
                    tracing::warn!("failed to decode the beta password of '{name}': {error}");
                }
            }
        }

        Ok(Self {
            base: CallbackMsgBase::with_job_id(response.target_job_id()),
            result: eresult_from_i32(body.eresult),
            beta_passwords,
        })
    }

    /// The result of the operation.
    pub fn result(&self) -> EResult {
        self.result
    }

    /// The map of beta names to their encryption keys.
    pub fn beta_passwords(&self) -> &BTreeMap<String, Vec<u8>> {
        &self.beta_passwords
    }
}

impl CallbackMsg for CheckAppBetaPasswordCallback {
    fn job_id(&self) -> JobID {
        self.base.job_id()
    }

    fn set_job_id(&mut self, id: JobID) {
        self.base.set_job_id(id);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// This callback is received when a private beta request has been completed.
#[derive(Clone, Debug)]
pub struct PrivateBetaCallback {
    base: CallbackMsgBase,
    result: EResult,
    depot_section: KeyValue,
}

impl PrivateBetaCallback {
    /// Builds the callback from a received `ClientPICSPrivateBetaResponse` message.
    pub(crate) fn from_packet(packet_msg: &dyn IPacketMsg) -> SteamResult<Self> {
        let response =
            ClientMsgProtobuf::<CMsgClientPicsPrivateBetaResponse>::from_packet(packet_msg)?;
        let body = response.body();

        let depot_section = match body.depot_section.as_deref() {
            // The trailing null byte isn't part of the text.
            Some(section) if !section.is_empty() => {
                let text = &section[..section.len() - 1];
                let mut cursor = Cursor::new(text);
                let mut key_value = KeyValue::new();
                let _ = key_value.read_as_text(&mut cursor);
                key_value
            }
            _ => KeyValue::new(),
        };

        Ok(Self {
            base: CallbackMsgBase::with_job_id(response.target_job_id()),
            result: eresult_from_i32(body.eresult),
            depot_section,
        })
    }

    /// The result of the operation.
    pub fn result(&self) -> EResult {
        self.result
    }

    /// The key value info to be merged into the main appinfo.
    pub fn depot_section(&self) -> &KeyValue {
        &self.depot_section
    }
}

impl CallbackMsg for PrivateBetaCallback {
    fn job_id(&self) -> JobID {
        self.base.job_id()
    }

    fn set_job_id(&mut self, id: JobID) {
        self.base.set_job_id(id);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}