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
#![allow(dead_code)]
#![allow(non_camel_case_types)]
#![allow(unused_parens)]
#![allow(unused_mut)]

#[cfg(test)]
mod tests;

/// Predefined client related bancho packets.
pub mod client;
/// Predefined server related bancho packets.
pub mod server;

pub use client::*;
pub use server::*;

#[cfg(feature = "derive")]
pub use bancho_packets_derive::*;

use enum_primitive_derive::Primitive;
use num_traits::FromPrimitive;
use std::{
    borrow::Cow,
    convert::TryInto,
    ops::{Deref, DerefMut},
};

pub type CowStr<'a> = Cow<'a, str>;

pub trait BanchoPacket {
    const ID: PacketId;

    fn into_packet_data(self) -> Vec<u8>;
}

/// Packet header length
///
/// - 0..=1: packet id
/// - 2: null
/// - 3..=6: packet length
/// - 7..=N: packet data length(uleb128) + data
pub const BANCHO_PACKET_HEADER_LENGTH: usize = 7;

pub const EMPTY_STRING_PACKET: &[u8; 2] = b"\x0b\x00";

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
/// Bancho packet, including [`PacketHeader`] structure and payload bytes.
pub struct Packet<'a> {
    pub id: PacketId,
    pub payload: Option<&'a [u8]>,
}

impl<'a> std::fmt::Display for Packet<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!(
            "Packet {{ {}, payload: {} }}",
            self.id,
            self.payload.is_some()
        ))
    }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
/// Bancho packet header, including [`PacketId`] and payload (data) length.
pub struct PacketHeader {
    pub id: PacketId,
    pub payload_length: u32,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
/// The login result of the bancho client.
/// Returns `Success(user_id)` if success.
pub enum LoginResult {
    Success(i32),
    Failed(LoginFailedResaon),
}

#[rustfmt::skip]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[repr(i32)]
/// The bancho client will handle these failure reasons when the user login fails.
pub enum LoginFailedResaon {
    InvalidCredentials        = -1,
    OutdatedClient            = -2,
    UserBanned                = -3,
    MultiaccountDetected      = -4,
    ServerError               = -5,
    CuttingEdgeMultiplayer    = -6,
    AccountPasswordRest       = -7,
    VerificationRequired      = -8,
}

#[rustfmt::skip]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Primitive)]
#[repr(u8)]
/// Known packet ids for bancho clients.
pub enum PacketId {
    OSU_USER_CHANGE_ACTION                = 0,
    OSU_SEND_PUBLIC_MESSAGE               = 1,
    OSU_USER_LOGOUT                       = 2,
    OSU_USER_REQUEST_STATUS_UPDATE        = 3,
    OSU_PING                              = 4,
    BANCHO_USER_LOGIN_REPLY               = 5,
    BANCHO_SEND_MESSAGE                   = 7,
    BANCHO_PONG                           = 8,
    BANCHO_HANDLE_IRC_CHANGE_USERNAME     = 9,
    BANCHO_HANDLE_IRC_QUIT                = 10,
    BANCHO_USER_STATS                     = 11,
    BANCHO_USER_LOGOUT                    = 12,
    BANCHO_SPECTATOR_JOINED               = 13,
    BANCHO_SPECTATOR_LEFT                 = 14,
    BANCHO_SPECTATE_FRAMES                = 15,
    OSU_SPECTATE_START                    = 16,
    OSU_SPECTATE_STOP                     = 17,
    OSU_SPECTATE_FRAMES                   = 18,
    BANCHO_VERSION_UPDATE                 = 19,
    OSU_ERROR_REPORT                      = 20,
    OSU_SPECTATE_CANT                     = 21,
    BANCHO_SPECTATOR_CANT_SPECTATE        = 22,
    BANCHO_GET_ATTENTION                  = 23,
    BANCHO_NOTIFICATION                   = 24,
    OSU_SEND_PRIVATE_MESSAGE              = 25,
    BANCHO_UPDATE_MATCH                   = 26,
    BANCHO_NEW_MATCH                      = 27,
    BANCHO_DISBAND_MATCH                  = 28,
    OSU_USER_PART_LOBBY                   = 29,
    OSU_USER_JOIN_LOBBY                   = 30,
    OSU_USER_CREATE_MATCH                 = 31,
    OSU_USER_JOIN_MATCH                   = 32,
    OSU_USER_PART_MATCH                   = 33,
    BANCHO_TOGGLE_BLOCK_NON_FRIEND_DMS    = 34,
    BANCHO_MATCH_JOIN_SUCCESS             = 36,
    BANCHO_MATCH_JOIN_FAIL                = 37,
    OSU_MATCH_CHANGE_SLOT                 = 38,
    OSU_USER_MATCH_READY                  = 39,
    OSU_MATCH_LOCK                        = 40,
    OSU_MATCH_CHANGE_SETTINGS             = 41,
    BANCHO_FELLOW_SPECTATOR_JOINED        = 42,
    BANCHO_FELLOW_SPECTATOR_LEFT          = 43,
    OSU_MATCH_START                       = 44,
    BANCHO_ALL_PLAYERS_LOADED             = 45,
    BANCHO_MATCH_START                    = 46,
    OSU_MATCH_SCORE_UPDATE                = 47,
    BANCHO_MATCH_SCORE_UPDATE             = 48,
    OSU_MATCH_COMPLETE                    = 49,
    BANCHO_MATCH_TRANSFER_HOST            = 50,
    OSU_MATCH_CHANGE_MODS                 = 51,
    OSU_MATCH_LOAD_COMPLETE               = 52,
    BANCHO_MATCH_ALL_PLAYERS_LOADED       = 53,
    OSU_MATCH_NO_BEATMAP                  = 54,
    OSU_MATCH_NOT_READY                   = 55,
    OSU_MATCH_FAILED                      = 56,
    BANCHO_MATCH_PLAYER_FAILED            = 57,
    BANCHO_MATCH_COMPLETE                 = 58,
    OSU_MATCH_HAS_BEATMAP                 = 59,
    OSU_MATCH_SKIP_REQUEST                = 60,
    BANCHO_MATCH_SKIP                     = 61,
    BANCHO_UNAUTHORIZED                   = 62,
    OSU_USER_CHANNEL_JOIN                 = 63,
    BANCHO_CHANNEL_JOIN_SUCCESS           = 64,
    BANCHO_CHANNEL_INFO                   = 65,
    BANCHO_CHANNEL_KICK                   = 66,
    BANCHO_CHANNEL_AUTO_JOIN              = 67,
    OSU_BEATMAP_INFO_REQUEST              = 68,
    BANCHO_BEATMAP_INFO_REPLY             = 69,
    OSU_MATCH_TRANSFER_HOST               = 70,
    BANCHO_PRIVILEGES                     = 71,
    BANCHO_FRIENDS_LIST                   = 72,
    OSU_USER_FRIEND_ADD                   = 73,
    OSU_USER_FRIEND_REMOVE                = 74,
    BANCHO_PROTOCOL_VERSION               = 75,
    BANCHO_MAIN_MENU_ICON                 = 76,
    OSU_MATCH_CHANGE_TEAM                 = 77,
    OSU_USER_CHANNEL_PART                 = 78,
    OSU_USER_RECEIVE_UPDATES              = 79,
    BANCHO_MONITOR                        = 80,
    BANCHO_MATCH_PLAYER_SKIPPED           = 81,
    OSU_USER_SET_AWAY_MESSAGE             = 82,
    BANCHO_USER_PRESENCE                  = 83,
    OSU_IRC_ONLY                          = 84,
    OSU_USER_STATS_REQUEST                = 85,
    BANCHO_RESTART                        = 86,
    OSU_MATCH_INVITE                      = 87,
    BANCHO_MATCH_INVITE                   = 88,
    BANCHO_CHANNEL_INFO_END               = 89,
    OSU_MATCH_CHANGE_PASSWORD             = 90,
    BANCHO_MATCH_CHANGE_PASSWORD          = 91,
    BANCHO_SILENCE_END                    = 92,
    OSU_TOURNAMENT_MATCH_INFO_REQUEST     = 93,
    BANCHO_USER_SILENCED                  = 94,
    BANCHO_USER_PRESENCE_SINGLE           = 95,
    BANCHO_USER_PRESENCE_BUNDLE           = 96,
    OSU_USER_PRESENCE_REQUEST             = 97,
    OSU_USER_PRESENCE_REQUEST_ALL         = 98,
    OSU_USER_TOGGLE_BLOCK_NON_FRIEND_DMS  = 99,
    BANCHO_USER_DM_BLOCKED                = 100,
    BANCHO_TARGET_IS_SILENCED             = 101,
    BANCHO_VERSION_UPDATE_FORCED          = 102,
    BANCHO_SWITCH_SERVER                  = 103,
    BANCHO_ACCOUNT_RESTRICTED             = 104,
    BANCHO_RTX                            = 105,
    BANCHO_MATCH_ABORT                    = 106,
    BANCHO_SWITCH_TOURNAMENT_SERVER       = 107,
    OSU_TOURNAMENT_JOIN_MATCH_CHANNEL     = 108,
    OSU_TOURNAMENT_LEAVE_MATCH_CHANNEL    = 109,
    OSU_UNKNOWN_PACKET                    = 255,
}

impl std::fmt::Display for PacketId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{:?}", self))
    }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, ReadPacket)]
/// [`BanchoMessage`] is the message structure of the bancho client.
pub struct BanchoMessage {
    pub sender: String,
    pub content: String,
    pub target: String,
    pub sender_id: i32,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PacketLength)]
/// [`MatchData`] is the data of bancho client multiplayer game room.
pub struct MatchData {
    pub match_id: i32,
    pub in_progress: bool,
    pub match_type: i8,
    pub play_mods: u32,
    pub match_name: String,
    #[length(self.password.as_ref().map(|pw| pw.packet_len()).unwrap_or(2))]
    pub password: Option<String>,
    pub beatmap_name: String,
    pub beatmap_id: i32,
    pub beatmap_md5: String,
    pub slot_status: Vec<u8>,
    pub slot_teams: Vec<u8>,
    pub slot_players: Vec<i32>,
    pub host_player_id: i32,
    pub match_game_mode: u8,
    pub win_condition: u8,
    pub team_type: u8,
    pub freemods: bool,
    pub player_mods: Vec<i32>,
    pub match_seed: i32,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PacketLength)]
pub struct MatchUpdate {
    pub data: MatchData,
    pub send_password: bool,
}

impl Deref for MatchUpdate {
    type Target = MatchData;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl DerefMut for MatchUpdate {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.data
    }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, ReadPacket, WritePacket, PacketLength)]
/// The [`ScoreFrame`] uploaded by the bancho client during multiplayer games.
pub struct ScoreFrame {
    pub timestamp: i32,
    pub id: u8,
    pub n300: u16,
    pub n100: u16,
    pub n50: u16,
    pub geki: u16,
    pub katu: u16,
    pub miss: u16,
    pub score: i32,
    pub combo: u16,
    pub max_combo: u16,
    pub perfect: bool,
    pub hp: u8,
    pub tag_byte: u8,
    pub score_v2: bool,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, ReadPacket, WritePacket, PacketLength)]
pub struct ClientChangeAction {
    pub online_status: u8,
    pub description: String,
    pub beatmap_md5: String,
    pub mods: u32,
    pub mode: u8,
    pub beatmap_id: i32,
}

#[derive(Debug, Clone)]
/// [`PayloadReader`] helps to read Bacho packet data.
///
/// ### Usage:
/// ```
/// use bancho_packets::{PacketReader, PayloadReader};
///
///
/// let mut reader = PacketReader::new(&[
///     4, 0, 0, 0, 0, 0, 0, 24, 0, 0, 19, 0, 0, 0, 11, 17, 72, 101, 108,
///     108, 111, 44, 32, 87, 111, 114, 108, 100, 33, 240, 159, 146, 150,
///     4, 0, 0, 0, 0, 0, 0, 24, 0, 0, 18, 0, 0, 0, 11, 16, 229, 147, 136,
///     229, 147, 136, 227, 128, 144, 240, 159, 152, 131, 227, 128, 145,
///     104, 0, 0, 0, 0, 0, 0, 24, 0, 0, 23, 0, 0, 0, 11, 21, 232, 175,
///     187, 229, 143, 150, 229, 174, 140, 228, 186, 134, 239, 188, 129,
///     239, 188, 129, 226, 156, 168,
/// ]);
/// while let Some(packet) = reader.next() {
///     print!("packet id: {:?}: ", packet.id);
///     match packet.payload {
///         None => println!("Non-payload"),
///         Some(payload) => {
///             let mut payload_reader = PayloadReader::new(payload);
///             println!("payload as string: {:?}", payload_reader.read::<String>());
///         },
///     }
/// }
///
/// ```
pub struct PayloadReader<'a> {
    pub(crate) payload: &'a [u8],
    pub(crate) index: usize,
}

impl<'a> PayloadReader<'a> {
    #[inline]
    pub fn new(payload: &'a [u8]) -> Self {
        PayloadReader { payload, index: 0 }
    }

    #[inline]
    /// Try to read `<T>` from payloads.
    /// Returns [`None`] when no data matching the given type can be read.
    pub fn read<T>(&mut self) -> Option<T>
    where
        T: BanchoPacketRead<T>,
    {
        T::read(self)
    }

    #[inline]
    pub fn index(&self) -> usize {
        self.index
    }

    #[inline]
    pub fn payload(&self) -> &'a [u8] {
        self.payload
    }

    #[inline]
    /// Reset read progress to `0`.
    pub fn reset(&mut self) {
        self.index = 0
    }

    #[inline]
    pub(crate) fn increase_index(&mut self, offset: usize) -> usize {
        self.index += offset;
        self.index
    }

    #[inline]
    pub(crate) fn decrease_index(&mut self, offset: usize) -> usize {
        self.index -= offset;
        self.index
    }

    #[inline]
    pub(crate) fn next_with_length(&mut self, length: usize) -> Option<&[u8]> {
        self.index += length;
        self.payload.get(self.index - length..self.index)
    }

    #[inline]
    pub(crate) fn next_with_length_type<Len: Sized>(
        &mut self,
    ) -> Option<&[u8]> {
        self.next_with_length(std::mem::size_of::<Len>())
    }

    #[inline]
    pub(crate) fn read_uleb128(&mut self) -> Option<u32> {
        let (val, length) = uleb128_to_u32(self.payload.get(self.index..)?)?;
        self.index += length;
        Some(val)
    }
}

#[derive(Debug, Clone)]
/// [`PacketReader`] helps to read Bacho packets.
///
/// ### Usage:
/// ```
/// use bancho_packets::{PacketReader, PayloadReader};
///
/// // Parsing [`PacketHeader`] from bytes.
/// let header = PacketReader::parse_header(&[
///     24, 0, 0, 7, 0, 0, 0, 11, 5, 104, 101, 108, 108, 111
/// ]);
///
/// // Read sequentially.
/// let mut reader = PacketReader::new(&[
///     24, 0, 0, 7, 0, 0, 0, 11, 5, 104, 101, 108, 108, 111, 4, 0, 0, 0,
///     0, 0, 0, 24, 0, 0, 7, 0, 0, 0, 11, 5, 104, 101, 108, 108, 111,
/// ]);
/// println!("packet 0: {:?}", reader.next());
/// println!("packet 1: {:?}", reader.next());
/// println!("packet 2: {:?}", reader.next());
/// println!("packet 3 (outside): {:?}", reader.next());
///
/// // Full example.
/// let mut reader = PacketReader::new(&[
///     4, 0, 0, 0, 0, 0, 0, 24, 0, 0, 19, 0, 0, 0, 11, 17, 72, 101, 108,
///     108, 111, 44, 32, 87, 111, 114, 108, 100, 33, 240, 159, 146, 150,
///     4, 0, 0, 0, 0, 0, 0, 24, 0, 0, 18, 0, 0, 0, 11, 16, 229, 147, 136,
///     229, 147, 136, 227, 128, 144, 240, 159, 152, 131, 227, 128, 145,
///     104, 0, 0, 0, 0, 0, 0, 24, 0, 0, 23, 0, 0, 0, 11, 21, 232, 175,
///     187, 229, 143, 150, 229, 174, 140, 228, 186, 134, 239, 188, 129,
///     239, 188, 129, 226, 156, 168,
/// ]);
/// while let Some(packet) = reader.next() {
///     print!("packet id: {:?}: ", packet.id);
///     match packet.payload {
///         None => println!("Non-payload"),
///         Some(payload) => {
///             let mut payload_reader = PayloadReader::new(payload);
///             println!("payload as string: {:?}", payload_reader.read::<String>());
///         },
///     }
/// }
///
/// ```
pub struct PacketReader<'a> {
    buf: &'a [u8],
    ptr: usize,
}

impl<'a> PacketReader<'a> {
    #[inline]
    /// Create a new [`PacketReader`] with bytes.
    pub fn new(buf: &'a [u8]) -> Self {
        PacketReader { buf, ptr: 0 }
    }

    #[inline]
    pub fn index(&self) -> usize {
        self.ptr
    }

    #[inline]
    pub fn buffer(&self) -> &'a [u8] {
        self.buf
    }

    #[inline]
    /// Reset the reading progress of [`PacketReader`].
    pub fn reset(&mut self) {
        self.ptr = 0;
    }

    #[inline]
    /// Parse [`PacketHeader`] from bytes.
    pub fn parse_header(header: &[u8]) -> Option<PacketHeader> {
        let packet_id = *header.first()?;
        Some(PacketHeader {
            id: PacketId::from_u8(packet_id)
                .unwrap_or(PacketId::OSU_UNKNOWN_PACKET),
            payload_length: u32::from_le_bytes(
                header[3..BANCHO_PACKET_HEADER_LENGTH].try_into().ok()?,
            ),
        })
    }
}

impl<'a> Iterator for PacketReader<'a> {
    type Item = Packet<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if (self.buf.len() - self.ptr) < BANCHO_PACKET_HEADER_LENGTH {
            return None;
        }
        // Slice packet header data `[u8; 7]`,
        // including packet id, payload length
        let header =
            &self.buf[self.ptr..self.ptr + BANCHO_PACKET_HEADER_LENGTH];
        self.ptr += BANCHO_PACKET_HEADER_LENGTH;

        // Get packet id and payload length
        let PacketHeader { id, payload_length } =
            PacketReader::parse_header(header)?;

        // Read the payload
        let payload = if payload_length == 0 {
            None
        } else {
            let next_payload_length = payload_length as usize;

            self.ptr += next_payload_length;
            self.buf.get(self.ptr - next_payload_length..self.ptr)
        };

        Some(Packet { id, payload })
    }
}

#[derive(Debug, Clone)]
/// [`PacketBuilder`] can help pack bancho packets.
///
/// ### Usages:
/// ```
/// use bancho_packets::{server, PacketBuilder, LoginResult};
///
/// let packet = PacketBuilder::new()
///     .add(server::login_reply(LoginResult::Success(1009)))
///     .add(server::protocol_version(19))
///     .add(server::notification("Welcome to osu!"))
///     .add(server::main_menu_icon(
///         "https://image.png",
///         "https://url.link",
///     ))
///     .add(server::silence_end(0))
///     .add(server::channel_info_end())
///     .build();
/// ```
///
pub struct PacketBuilder {
    buffer: Vec<u8>,
}

impl Default for PacketBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl Deref for PacketBuilder {
    type Target = Vec<u8>;

    fn deref(&self) -> &Self::Target {
        &self.buffer
    }
}

impl DerefMut for PacketBuilder {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.buffer
    }
}

impl PacketBuilder {
    #[inline]
    /// Create an empty [`PacketBuilder`].
    pub fn new() -> Self {
        Self { buffer: Vec::new() }
    }

    #[inline]
    /// Create [`PacketBuilder`] with capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self { buffer: Vec::with_capacity(capacity) }
    }

    #[inline]
    /// Create [`PacketBuilder`] with [`PacketId`].
    pub fn with_packet_id(packet_id: PacketId) -> Self {
        Self { buffer: new_empty_packet(packet_id) }
    }

    #[inline]
    /// Consume this [`PacketBuilder`] and return data.
    pub fn build(self) -> Vec<u8> {
        self.buffer
    }

    #[inline]
    pub fn buffer(&self) -> &Vec<u8> {
        &self.buffer
    }

    #[inline]
    pub fn buffer_mut(&mut self) -> &mut Vec<u8> {
        &mut self.buffer
    }

    #[inline]
    /// Create a [`PacketBuilder`] from multiple packets.
    pub fn from_batch<P, I>(packets: P) -> Self
    where
        I: IntoIterator<Item = u8>,
        P: IntoIterator<Item = I>,
    {
        Self::new().add_batch(packets)
    }

    #[inline]
    /// Batch add packets to [`PacketBuilder`].
    pub fn add_batch<P, I>(mut self, packets: P) -> Self
    where
        I: IntoIterator<Item = u8>,
        P: IntoIterator<Item = I>,
    {
        self.add_batch_ref(packets);
        self
    }

    #[inline]
    /// Batch add packets to [`PacketBuilder`].
    pub fn add_batch_ref<P, I>(&mut self, packets: P) -> &Self
    where
        I: IntoIterator<Item = u8>,
        P: IntoIterator<Item = I>,
    {
        for p in packets {
            self.buffer.extend(p)
        }
        self
    }

    #[inline]
    /// Add an packet to [`PacketBuilder`].
    pub fn add<P>(mut self, packet: P) -> Self
    where
        P: IntoIterator<Item = u8>,
    {
        self.add_ref(packet);
        self
    }

    #[inline]
    /// Add an packet to [`PacketBuilder`].
    pub fn add_ref<P>(&mut self, packet: P) -> &Self
    where
        P: IntoIterator<Item = u8>,
    {
        self.buffer.extend(packet);
        self
    }
}

impl From<Vec<u8>> for PacketBuilder {
    #[inline]
    fn from(buffer: Vec<u8>) -> Self {
        Self { buffer }
    }
}

/// Can use [`PayloadReader`] to read data from type `T` which implements this trait.
pub trait BanchoPacketRead<T> {
    fn read(reader: &mut PayloadReader) -> Option<T>;
}

impl BanchoPacketRead<String> for String {
    #[inline]
    fn read(reader: &mut PayloadReader) -> Option<String> {
        if reader.payload.get(reader.index())? != &0xb {
            return None;
        }
        reader.increase_index(1);
        let data_length = reader.read_uleb128()? as usize;

        let cur = reader.index;
        reader.increase_index(data_length);
        let data = reader.payload.get(cur..reader.index)?;

        Some(std::str::from_utf8(data).ok()?.into())
    }
}

impl BanchoPacketRead<bool> for bool {
    #[inline]
    fn read(reader: &mut PayloadReader) -> Option<bool> {
        Some(reader.read::<i8>()? == 1)
    }
}

macro_rules! impl_number {
    ($($t:ty),+) => {
        $(impl BanchoPacketRead<$t> for $t {
            #[inline]
            fn read(reader: &mut PayloadReader) -> Option<$t> {
                Some(<$t>::from_le_bytes(
                    reader.next_with_length_type::<$t>()?.try_into().ok()?,
                ))
            }
        })+
    };
}

impl_number!(i8, u8, i16, u16, i32, u32, i64, u64);

macro_rules! impl_read_number_array {
    ($($t:ty),+) => {
        $(impl BanchoPacketRead<Vec<$t>> for Vec<$t> {
            #[inline]
            fn read(reader: &mut PayloadReader) -> Option<Vec<$t>> {
                let length_data = reader.next_with_length_type::<i16>()?;
                let int_count = <i16>::from_le_bytes(length_data.try_into().ok()?) as usize;

                let mut data = Vec::with_capacity(int_count);
                for _ in 0..int_count {
                    data.push(<$t>::from_le_bytes(reader.next_with_length_type::<i32>()?.try_into().ok()?));
                }
                Some(data)
            }
        })+
    };
}

impl_read_number_array!(i8, u8, i16, u16, i32, u32, i64, u64);

/// [`BanchoPacketWrite`] is a trait used to convert rust internal data types to bancho packets ([`Vec<u8>`]).
pub trait BanchoPacketWrite {
    /// Convert [`self`] into a bancho packet and write it into `buf` [`Vec<u8>`].
    fn write_buf(self, buf: &mut Vec<u8>);

    #[inline]
    /// Convert [`self`] into a bancho packet [`Vec<u8>`].
    fn into_packet(self) -> Vec<u8>
    where
        Self: Sized,
    {
        let mut buf = Vec::new();
        self.write_buf(&mut buf);
        buf
    }
}

impl BanchoPacketWrite for Cow<'_, str> {
    #[inline]
    fn write_buf(self, buf: &mut Vec<u8>) {
        match self {
            Cow::Borrowed(s) => s.write_buf(buf),
            Cow::Owned(s) => s.write_buf(buf),
        }
    }
}

impl BanchoPacketWrite for &str {
    #[inline]
    fn write_buf(self, buf: &mut Vec<u8>) {
        let byte_length = self.len();
        if byte_length > 0 {
            // string length (uleb128)
            let (data, ptr) = u32_to_uleb128(byte_length as u32);
            let length_uleb128 = &data[0..ptr];

            let estimate_len = self.packet_len();
            if buf.capacity() < estimate_len {
                buf.reserve(estimate_len);
            }

            buf.push(0xb);
            buf.extend(length_uleb128);
            buf.extend(self.as_bytes());
        } else {
            buf.push(0);
        }
    }
}

impl BanchoPacketWrite for String {
    #[inline]
    fn write_buf(self, buf: &mut Vec<u8>) {
        let byte_length = self.len();
        if byte_length > 0 {
            // string length (uleb128)
            let (data, ptr) = u32_to_uleb128(byte_length as u32);
            let length_uleb128 = &data[0..ptr];

            let estimate_len = self.packet_len();
            if buf.capacity() < estimate_len {
                buf.reserve(estimate_len);
            }

            buf.push(0xb);
            buf.extend(length_uleb128);
            buf.extend(self.into_bytes());
        } else {
            buf.push(0);
        }
    }
}

impl BanchoPacketWrite for u8 {
    #[inline]
    fn write_buf(self, buf: &mut Vec<u8>) {
        buf.push(self);
    }
}

impl BanchoPacketWrite for &[u8] {
    #[inline]
    fn write_buf(self, buf: &mut Vec<u8>) {
        buf.extend(self);
    }
}

impl BanchoPacketWrite for Vec<u8> {
    #[inline]
    fn write_buf(self, buf: &mut Vec<u8>) {
        buf.extend(self);
    }
}

impl BanchoPacketWrite for bool {
    #[inline]
    fn write_buf(self, buf: &mut Vec<u8>) {
        buf.push(if self { 1 } else { 0 });
    }
}

macro_rules! impl_write_number {
    ($($t:ty),+) => {
        $(
            impl BanchoPacketWrite for $t {
                #[inline]
                fn write_buf(self, buf: &mut Vec<u8>) {
                    buf.extend(self.to_le_bytes())
                }
            }

            impl BanchoPacketLength for $t {
                #[inline]
                fn packet_len(&self) -> usize {
                    std::mem::size_of::<$t>()
                }
            }
        )+
    }
}

impl_write_number!(i8, u16, i16, i32, u32, i64, u64, f32, f64);

macro_rules! impl_write_number_array {
    ($($t:ty),+) => {$(
        impl BanchoPacketWrite for &[$t] { impl_write_number_array!(@bancho_packet_write_inner $t); }
        impl BanchoPacketWrite for Vec<$t> { impl_write_number_array!(@bancho_packet_write_inner $t); }

        impl BanchoPacketLength for &[$t] { impl_write_number_array!(@bancho_packet_length_inner $t); }
        impl BanchoPacketLength for Vec<$t> { impl_write_number_array!(@bancho_packet_length_inner $t); }
    )+};
    (@bancho_packet_write_inner $t:ty) => {
        #[inline]
        fn write_buf(self, buf: &mut Vec<u8>) {
            let estimate_len = self.packet_len();
            if buf.capacity() < estimate_len {
                buf.reserve(estimate_len);
            }

            buf.extend((self.len() as u16).to_le_bytes());
            for int in self {
                buf.extend(int.to_le_bytes())
            }
        }
    };
    (@bancho_packet_length_inner $t:ty) => {
        #[inline]
        fn packet_len(&self) -> usize {
            std::mem::size_of::<u16>() + (std::mem::size_of::<$t>() * self.len())
        }
    }
}

impl_write_number_array!(i8, u16, i16, i32, u32, i64, u64, f32, f64);

impl BanchoPacketWrite for LoginResult {
    #[inline]
    fn write_buf(self, buf: &mut Vec<u8>) {
        match self {
            LoginResult::Success(user_id) => user_id,
            LoginResult::Failed(reason) => reason as i32,
        }
        .write_buf(buf)
    }
}

impl BanchoPacketWrite for MatchUpdate {
    #[inline]
    fn write_buf(mut self, buf: &mut Vec<u8>) {
        let raw_password = std::mem::take(&mut self.password)
            .map(|password| {
                if self.send_password {
                    let mut raw = Vec::with_capacity(password.packet_len());
                    password.write_buf(&mut raw);
                    raw
                } else {
                    EMPTY_STRING_PACKET.to_vec()
                }
            })
            .unwrap_or(b"\x00".to_vec());

        buf.extend(data!(
            self.match_id as u16,
            self.in_progress,
            self.match_type,
            self.play_mods,
            std::mem::take(&mut self.match_name),
            raw_password,
            std::mem::take(&mut self.beatmap_name),
            self.beatmap_id,
            std::mem::take(&mut self.beatmap_md5),
            std::mem::take(&mut self.slot_status),
            std::mem::take(&mut self.slot_teams),
            std::mem::take(&mut self.slot_players),
            self.host_player_id,
            self.match_game_mode,
            self.win_condition,
            self.team_type,
            self.freemods,
            std::mem::take(&mut self.player_mods),
            self.match_seed
        ));
    }
}

impl BanchoPacketWrite for MatchData {
    #[inline]
    fn write_buf(self, buf: &mut Vec<u8>) {
        MatchUpdate { data: self, send_password: true }.write_buf(buf);
    }
}

/// [`BanchoPacketLength`] is a trait used to calculate the byte length of the data converted to bancho packet.
pub trait BanchoPacketLength {
    #[inline]
    /// Calculate the byte length of `self` after being converted into a bancho packet,
    /// which is used to allocate [`Vec`] space in advance to improve performance.
    ///
    /// If not implemented, return `0`.
    fn packet_len(&self) -> usize {
        0
    }
}

impl BanchoPacketLength for Cow<'_, str> {
    #[inline]
    fn packet_len(&self) -> usize {
        match self {
            Cow::Borrowed(s) => s.packet_len(),
            Cow::Owned(s) => s.packet_len(),
        }
    }
}

impl BanchoPacketLength for &str {
    #[inline]
    fn packet_len(&self) -> usize {
        if !self.is_empty() {
            self.as_bytes().len() + 6
        } else {
            1
        }
    }
}

impl BanchoPacketLength for String {
    #[inline]
    fn packet_len(&self) -> usize {
        if !self.is_empty() {
            self.as_bytes().len() + 6
        } else {
            1
        }
    }
}

impl BanchoPacketLength for u8 {
    #[inline]
    fn packet_len(&self) -> usize {
        std::mem::size_of::<u8>()
    }
}

impl BanchoPacketLength for &[u8] {
    #[inline]
    fn packet_len(&self) -> usize {
        self.len()
    }
}

impl BanchoPacketLength for Vec<u8> {
    #[inline]
    fn packet_len(&self) -> usize {
        self.len()
    }
}

impl BanchoPacketLength for bool {
    #[inline]
    fn packet_len(&self) -> usize {
        std::mem::size_of::<bool>()
    }
}

impl BanchoPacketLength for LoginResult {
    #[inline]
    fn packet_len(&self) -> usize {
        std::mem::size_of::<i32>()
    }
}

impl<T> BanchoPacketLength for Option<T>
where
    T: BanchoPacketLength,
{
    #[inline]
    fn packet_len(&self) -> usize {
        self.as_ref().map(|t| t.packet_len()).unwrap_or(0)
    }
}

#[inline]
/// Convert [`u32`] to `uleb128`
pub fn u32_to_uleb128(mut unsigned: u32) -> ([u8; 5], usize) {
    let mut data = [0, 0, 0, 0, 0];
    let mut ptr = 0;

    loop {
        if unsigned < 0x80 {
            break;
        }
        data[ptr] = ((unsigned & 0x7f) | 0x80) as u8;
        ptr += 1;
        unsigned >>= 7;
    }
    data[ptr] = unsigned as u8;

    (data, ptr + 1)
}

#[inline]
/// Convert `uleb128` bytes to [`u32`]
pub fn uleb128_to_u32(uleb128_bytes: &[u8]) -> Option<(u32, usize)> {
    let (mut val, mut shift, mut index) = (0, 0, 0);
    loop {
        let byte = uleb128_bytes.get(index)?;
        index += 1;
        if (byte & 0x80) == 0 {
            val |= (*byte as u32) << shift;
            return Some((val, index));
        }
        val |= ((byte & 0x7f) as u32) << shift;
        shift += 7;
    }
}

#[inline(always)]
/// Initial a packet with PacketId
///
/// Packets posits:
///
/// - 0..=1: [`PacketId`]
/// - 2: `0x0`
/// - 3..=6: packet length ([`i32`] as bytes)
/// - 7..=N: packet data length (`uleb128` as bytes) + data
///
/// Note: The maximum value of [`u8`] is `255`,
/// currently the largest packet id of bancho is `109`, so never overflow.
pub fn new_empty_packet(packet_id: PacketId) -> Vec<u8> {
    vec![packet_id as u8, 0, 0, 0, 0, 0, 0]
}

/// Provide some convenient declarative macros to help build bancho packets.
pub mod macros {
    #[macro_export]
    /// Pack bancho packet data
    ///
    /// ### Usages:
    /// ```
    /// use bancho_packets::*;
    ///
    /// let val_1: i32 = 123;
    /// let val_2: i16 = 50;
    ///
    /// // Single data
    /// data!(val_1);
    ///
    /// // Mutiple data
    /// data!(val_1, val_2);
    ///
    /// // Create packet data with additional capacity
    /// data!(@capacity { 100 }, val_1, val_2);
    /// ```
    macro_rules! data {
        ($($item:expr$(,)*)*) => {
            {
                let mut estimate_capacity = 0;
                $(estimate_capacity += $item.packet_len();)*

                let mut buf = Vec::<u8>::with_capacity(estimate_capacity);
                $($item.write_buf(&mut buf);)*
                buf
            }
        };
        (@capacity { $capacity:expr }, $($item:expr$(,)*)*) => {
            {
                let mut estimate_capacity = 0;
                $(estimate_capacity += $item.packet_len();)*

                let mut buf = Vec::<u8>::with_capacity($capacity + estimate_capacity);
                $($item.write_buf(&mut buf);)*
                buf
            }
        }
    }

    #[macro_export]
    /// Pack bancho packets
    ///
    ///
    /// ### Usages:
    /// ```
    /// use bancho_packets::*;
    ///
    /// // Basic
    /// packet!(PacketId::BANCHO_USER_STATS);
    ///
    /// // With data
    /// let data: i32 = 6;
    /// packet!(PacketId::BANCHO_USER_STATS, data);
    ///
    /// // With data
    /// let data = vec![1, 2, 3];
    /// packet!(PacketId::BANCHO_USER_STATS, data);
    ///
    ///
    /// // With complex data
    /// let user_id: i32 = 1000;
    /// let username: &str = "username";
    ///
    /// packet!(
    ///     PacketId::BANCHO_USER_PRESENCE,
    ///     data!(
    ///         user_id,
    ///         username
    ///     )
    /// );
    /// ```
    macro_rules! packet {
        ($packet_id:expr $(,$data:expr)*) => {
            {
                let mut estimate_capacity = 0;
                $(estimate_capacity += $data.packet_len();)*

                let mut packet = Vec::<u8>::with_capacity(estimate_capacity);
                packet.extend(&[$packet_id as u8, 0, 0, 0, 0, 0, 0]);

                $($data.write_buf(&mut packet);)*

                let packet_length_bytes =
                    ((packet.len() - $crate::BANCHO_PACKET_HEADER_LENGTH) as i32)
                        .to_le_bytes();

                // `new_empty_packet` always returns a vector of length 7, there will be no null pointer.
                packet[3] = packet_length_bytes[0];
                packet[4] = packet_length_bytes[1];
                packet[5] = packet_length_bytes[2];
                packet[6] = packet_length_bytes[3];

                packet
            }
        }
    }

    #[macro_export]
    /// Impl bancho packet
    ///
    /// Example
    /// ```rust
    /// use crate::{
    ///     packet_struct, BanchoPacketLength,
    ///     BanchoPacketWrite,
    /// };
    ///
    /// packet_struct!(
    ///     PacketId::OSU_USER_CHANGE_ACTION,
    ///     /// #0: OSU_USER_CHANGE_ACTION
    ///     UserChangeAction<'a> {
    ///         online_status: u8,
    ///         description: Cow<'a, str>,
    ///         beatmap_md5: Cow<'a, str>,
    ///         mods: u32,
    ///         mode: u8,
    ///         beatmap_id: i32,
    ///     }
    /// );
    /// ```
    macro_rules! packet_struct {
        (
            $packet_id: expr,
            $(#[$struct_meta:meta])*
            $struct_name:ident $(< $($lifetimes:lifetime),* >)? {
                $(
                    $(#[$field_meta:meta])*
                    $field_name:ident: $field_type:ty$(,)*
                )*
            },
            fn $fn:ident ($self:ident) -> $ret:ty $body:block
        ) => {
            $(#[$struct_meta])*
            #[derive(Debug, Clone)]
            pub struct $struct_name $(< $($lifetimes),* >)? {
                $(
                    $(#[$field_meta])*
                    pub $field_name: $field_type,
                )*
            }

            impl $(< $($lifetimes),* >)? $crate::BanchoPacketLength for $struct_name $(< $($lifetimes),* >)? {
                #[inline]
                fn packet_len(&self) -> usize {
                    let mut _len = 0;
                    $(_len += self.$field_name.packet_len();)*
                    _len
                }
            }

            impl$(< $($lifetimes),* >)? $struct_name$(< $($lifetimes),* >)? {
                #[inline]
                pub fn new(
                    $($field_name: $field_type,)*
                ) -> Self {
                    Self { $($field_name,)* }
                }

                #[inline]
                pub fn pack(
                    $($field_name: $field_type,)*
                ) -> Vec<u8> {
                    $crate::BanchoPacket::into_packet_data(Self { $($field_name,)* })
                }
            }

            impl$(< $($lifetimes),* >)? $crate::BanchoPacket for $struct_name$(< $($lifetimes),* >)? {
                const ID: $crate::PacketId = $packet_id;

                #[inline]
                fn $fn ($self) -> $ret $body
            }

            impl$(< $($lifetimes),* >)? IntoIterator for $struct_name$(< $($lifetimes),* >)? {
                type Item = u8;

                type IntoIter = std::vec::IntoIter<u8>;

                fn into_iter(self) -> Self::IntoIter {
                    $crate::BanchoPacket::into_packet_data(self).into_iter()
                }
            }

            impl$(< $($lifetimes),* >)? From<$struct_name$(< $($lifetimes),* >)?> for Vec<u8> {
                fn from(packet: $struct_name$(< $($lifetimes),* >)?) -> Vec<u8> {
                    $crate::BanchoPacket::into_packet_data(packet)
                }
            }
        };
        (
            $packet_id: expr,
            $(#[$struct_meta:meta])*
            $struct_name:ident $(< $($lifetimes:lifetime),* >)? {
                $(
                    $(#[$field_meta:meta])*
                    $field_name:ident: $field_type:ty$(,)*
                )*
            }
        ) => {
            packet_struct!(
                $packet_id,
                $(#[$struct_meta])*
                $struct_name $(< $($lifetimes),* >)? {
                    $(
                        $(#[$field_meta])*
                        $field_name: $field_type,
                    )*
                },
                fn into_packet_data(self) -> Vec<u8> {
                    $crate::packet!(
                        Self::ID
                        $(,self.$field_name)*
                    )
                }
            );
        }
    }
}