Skip to content

Commit 34c0bcc

Browse files
committed
make all packet stuff take from a file that i can update instead of it being hardcoded
1 parent e7593c0 commit 34c0bcc

22 files changed

Lines changed: 265 additions & 264 deletions

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ lazy_static = "1.5.0"
3030
byteorder = "1.5.0"
3131
console-subscriber = { version = "0.5.0", optional = true }
3232

33+
[build-dependencies]
34+
serde_json = "1.0.149"
35+
3336
[features]
3437
default = []
3538
console = ["console-subscriber"]

build.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)