1+ use std:: collections:: HashMap ;
2+ use std:: env;
3+ use std:: fs;
4+ use std:: path:: Path ;
5+
6+ fn main ( ) {
7+ let manifest = env:: var ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ;
8+ let json_path = Path :: new ( & manifest) . join ( "src/assets/packets.json" ) ;
9+ println ! ( "cargo:rerun-if-changed={}" , json_path. display( ) ) ;
10+
11+ let bytes = fs:: read ( & json_path) . expect ( "Failed to read packets.json" ) ;
12+
13+ let json: HashMap < String , serde_json:: Value > =
14+ serde_json:: from_slice ( & bytes) . expect ( "Failed to parse packets.json" ) ;
15+
16+ let mut out = String :: new ( ) ;
17+
18+ const PATHS : & [ ( & str , & str ) ] = & [
19+ ( "handshake" , "serverbound" ) ,
20+ ( "configuration" , "clientbound" ) ,
21+ ( "configuration" , "serverbound" ) ,
22+ ( "login" , "clientbound" ) ,
23+ ( "login" , "serverbound" ) ,
24+ ( "play" , "clientbound" ) ,
25+ ( "play" , "serverbound" ) ,
26+ ( "status" , "clientbound" ) ,
27+ ( "status" , "serverbound" ) ,
28+ ] ;
29+
30+ let mut tree: HashMap < & str , HashMap < & str , Vec < ( String , i64 ) > > > = HashMap :: new ( ) ;
31+ for ( state, direction) in PATHS {
32+ let obj = json[ * state] [ * direction] . as_object ( ) . unwrap ( ) ;
33+
34+ for ( k, v) in obj {
35+ let protocol_id = v[ "protocol_id" ] . as_i64 ( ) . unwrap ( ) ;
36+ tree. entry ( state) . or_default ( ) . entry ( direction) . or_default ( ) . push ( ( k. clone ( ) , protocol_id) ) ;
37+ }
38+ }
39+
40+ for ( state, directions) in & tree {
41+ out. push_str ( & format ! ( "pub mod {} {{\n " , state) ) ;
42+
43+ for ( direction, packets) in directions {
44+ out. push_str ( & format ! ( " pub mod {} {{\n " , direction) ) ;
45+
46+ for ( name, protocol_id) in packets {
47+ out. push_str (
48+ & format ! (
49+ " pub const {}: u32 = {};\n " ,
50+ name. to_uppercase( ) . replace( "MINECRAFT:" , "" ) . replace( "/" , "_" ) ,
51+ protocol_id
52+ )
53+ ) ;
54+ }
55+
56+ out. push_str ( " }\n " ) ;
57+ }
58+
59+ out. push_str ( "}\n " ) ;
60+ }
61+
62+ dbg ! ( tree) ;
63+
64+ let out_path = Path :: new ( & env:: var ( "OUT_DIR" ) . unwrap ( ) ) . join ( "packets.rs" ) ;
65+ fs:: write ( out_path, out) . unwrap ( ) ;
66+ }
0 commit comments