forked from Awpteamoose/demoinfocs2_lite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
148 lines (123 loc) · 4.58 KB
/
Copy pathbuild.rs
File metadata and controls
148 lines (123 loc) · 4.58 KB
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
use std::{
fs::File,
io::{BufWriter, Write},
path::Path,
};
use kv3::Value;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
prost_build::Config::new()
.bytes(["."])
.default_package_filename("game_messages")
.compile_protos(
&[
"GameTracking-CS2/Protobufs/gameevents.proto",
"GameTracking-CS2/Protobufs/cstrike15_usermessages.proto",
"GameTracking-CS2/Protobufs/netmessages.proto",
"GameTracking-CS2/Protobufs/usercmd.proto",
"GameTracking-CS2/Protobufs/cs_usercmd.proto",
"GameTracking-CS2/Protobufs/demo.proto",
],
&["GameTracking-CS2/Protobufs/"],
)?;
// generate demo info table
let demo_info_path = Path::new(&manifest_dir).join("demoinfo2.txt");
if !demo_info_path.exists() {
panic!("Demoinfo file not found: {}", demo_info_path.display());
}
println!("cargo:rerun-if-changed={}", demo_info_path.display());
let path = Path::new(&std::env::var("OUT_DIR")?).join("demoinfo2.rs");
let mut file = BufWriter::new(File::create(&path)?);
let demo_info_content = std::fs::read_to_string(demo_info_path)?;
let demo_info = kv3::from_str(&demo_info_content)?;
let Value::File(_, demo_info) = demo_info else {
panic!("Missing demoinfo2 in demoinfo2.txt");
};
let Some(Value::Array(basic_encodings)) = demo_info.get("m_BasicEncodings") else {
panic!("Missing m_BasicEncodings in demoinfo2.txt");
};
let Some(Value::Array(field_encoder_overrides)) = demo_info.get("m_FieldEncoderOverrides")
else {
panic!("Missing m_FieldEncoderOverrides in demoinfo2.txt");
};
let Some(Value::Array(aliases)) = demo_info.get("m_Aliases") else {
panic!("Missing m_Aliases in demoinfo2.txt");
};
let mut encodings_map = &mut phf_codegen::Map::new();
for encoding in basic_encodings {
let Some(Value::String(name)) = encoding.get("m_Name") else {
panic!("Missing m_Name in basic encoding");
};
let Some(Value::String(var_type)) = encoding.get("m_VarType") else {
panic!("Missing m_VarType in basic encoding");
};
let components = match encoding.get("m_nComponents") {
Some(Value::String(s)) => s,
_ => "1",
};
encodings_map = encodings_map.entry(name, format!("(\"{var_type}\", {components})",));
}
writeln!(
&mut file,
"pub static BASIC_ENCODINGS: phf::Map<&'static str, (&'static str, usize)> = {};",
encodings_map.build()
)?;
let mut field_overrides_map = &mut phf_codegen::Map::new();
for override_ in field_encoder_overrides {
let Some(Value::String(name)) = override_.get("m_Name") else {
panic!("Missing m_Name in field encoder override");
};
let Some(Value::String(var_type)) = override_.get("m_VarType") else {
panic!("Missing m_VarType in field encoder override");
};
field_overrides_map = field_overrides_map.entry(name, format!("\"{var_type}\""));
}
writeln!(
&mut file,
"pub static FIELD_ENCODER_OVERRIDES: phf::Map<&'static str, &'static str> = {};",
field_overrides_map.build()
)?;
let mut aliases_map = &mut phf_codegen::Map::new();
for alias in aliases {
let Some(Value::String(type_alias)) = alias.get("m_TypeAlias") else {
panic!("Missing m_TypeAlias in alias");
};
let Some(Value::String(underlying_type)) = alias.get("m_UnderlyingType") else {
panic!("Missing m_UnderlyingType in alias");
};
aliases_map = aliases_map.entry(type_alias, format!("\"{underlying_type}\""));
}
writeln!(
&mut file,
"pub static ALIASES: phf::Map<&'static str, &'static str> = {};",
aliases_map.build()
)?;
file.flush()?;
Ok(())
}
trait ObjectKeyExt {
fn as_str(&self) -> &str;
}
impl ObjectKeyExt for kv3::ObjectKey {
fn as_str(&self) -> &str {
match self {
kv3::ObjectKey::String(s) => s.as_str(),
kv3::ObjectKey::Identifier(s) => s.as_str(),
}
}
}
trait ValueExt {
fn get(&self, key: &str) -> Option<&Value>;
}
impl ValueExt for Value {
fn get(&self, key: &str) -> Option<&Value> {
if let Value::Object(obj) = self {
for (k, v) in obj {
if k.as_str() == key {
return Some(v);
}
}
}
None
}
}