-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
298 lines (249 loc) · 9.1 KB
/
main.rs
File metadata and controls
298 lines (249 loc) · 9.1 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
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
#![allow(clippy::literal_string_with_formatting_args)]
mod class_inject;
mod enums;
mod generator;
mod pyi;
mod structs;
mod unions;
use generator::Generator;
use std::{borrow::Cow, env::set_current_dir, fs, io, path::Path, process::Command};
use structs::StructBindGenerator;
use zip::ZipArchive;
const FLATC_DOWNLOAD_URL: &str = "https://github.com/google/flatbuffers/releases/download/v25.2.10/";
const SCHEMA_FOLDER: &str = "./flatbuffers-schema";
const SCHEMA_FOLDER_BACKUP: &str = "../flatbuffers-schema";
const RLBOT_FBS: &str = "schema/rlbot.fbs";
const FLATC_BINARY: &str = if cfg!(windows) {
"binaries\\flatc.exe"
} else {
"binaries/flatc"
};
const OUT_FOLDER: &str = "./src/generated";
pub const PYTHON_OUT_FOLDER: &str = "./src/python";
pub enum PythonBindType {
Struct(structs::StructBindGenerator),
Enum(enums::EnumBindGenerator),
Union(unions::UnionBindGenerator),
}
impl PythonBindType {
pub const BASE_TYPES: [&'static str; 6] = ["bool", "i32", "u32", "f32", "String", "u8"];
pub const FROZEN_TYPES: [&'static str; 26] = [
"ControllableInfo",
"ControllableTeamInfo",
"PredictionSlice",
"BallPrediction",
"GoalInfo",
"BoostPad",
"FieldInfo",
"Physics",
"GamePacket",
"PlayerInfo",
"ScoreInfo",
"BallInfo",
"Touch",
"CollisionShape",
"BoxShape",
"SphereShape",
"CylinderShape",
"BoostPadState",
"MatchInfo",
"TeamInfo",
"Vector2",
"CoreMessage",
"InterfaceMessage",
"CorePacket",
"InterfacePacket",
"PlayerInput",
];
pub const NO_SET_TYPES: [&'static str; 1] = ["PlayerClass"];
pub const UNIONS: [&'static str; 6] = [
"PlayerClass",
"CollisionShape",
"RelativeAnchor",
"RenderType",
"CoreMessage",
"InterfaceMessage",
];
pub const OPTIONAL_UNIONS: [&'static str; 1] = ["RelativeAnchor"];
pub const DEFAULT_OVERRIDES: [(&'static str, &'static str, &'static str); 1] = [("Color", "a", "255")];
pub const FIELD_ALIASES: [(&'static str, &'static str, &'static str); 1] = [("PlayerInfo", "player_id", "spawn_id")];
pub const FREELIST_TYPES: [(&'static str, usize); 0] = [];
fn new(path: &Path) -> Option<Self> {
// get the filename without the extension
let filename = path.file_stem().unwrap().to_str().unwrap();
if filename == "mod" {
return None;
}
// convert snake_case to CamelCase to get the struct name
let mut struct_name = String::new();
for c in filename.split('_') {
struct_name.push_str(&c[..1].to_uppercase());
struct_name.push_str(&c[1..]);
}
struct_name = struct_name
.replace("Rlbot", "RLBot")
.replace("Halign", "HAlign")
.replace("Valign", "VAlign");
let struct_t_name = format!("{struct_name}T");
let contents = fs::read_to_string(path).ok()?;
#[cfg(windows)]
let contents = contents.replace("\r\n", "\n");
let struct_def = format!("pub struct {struct_name}");
let struct_def_pos = contents.find(&struct_def).unwrap();
let mut docs = Vec::new();
for line in contents[..struct_def_pos].lines().rev() {
if line.starts_with("///") {
docs.push(line.trim_start_matches("///").trim());
} else {
break;
}
}
let struct_doc_str = if docs.is_empty() {
None
} else {
Some(docs.into_iter().map(|s| s.to_string()).rev().collect::<Vec<_>>())
};
if let Some(types) = StructBindGenerator::get_types(&contents, &struct_t_name) {
return Some(Self::Struct(StructBindGenerator::new(
filename.to_string(),
struct_name,
struct_t_name,
struct_doc_str,
contents,
types,
)?));
}
if let Some((types, enum_type)) = enums::EnumBindGenerator::get_types(&contents, &struct_name) {
return Some(match enum_type {
enums::EnumType::Enum => {
Self::Enum(enums::EnumBindGenerator::new(filename.to_string(), struct_name, types)?)
}
enums::EnumType::Union => Self::Union(unions::UnionBindGenerator::new(
filename.to_string(),
struct_name,
struct_t_name,
types,
)?),
});
}
None
}
pub fn filename(&self) -> &str {
match self {
Self::Struct(bind) => bind.filename(),
Self::Enum(bind) => bind.filename(),
Self::Union(bind) => bind.filename(),
}
}
pub fn struct_name(&self) -> &str {
match self {
Self::Struct(bind) => bind.struct_name(),
Self::Enum(bind) => bind.struct_name(),
Self::Union(bind) => bind.struct_name(),
}
}
pub fn generate(&mut self, filepath: &Path) -> io::Result<()> {
match self {
Self::Struct(bind) => bind.generate(filepath),
Self::Enum(bind) => bind.generate(filepath),
Self::Union(bind) => bind.generate(filepath),
}
}
}
fn mod_rs_generator(type_data: &[PythonBindType]) -> io::Result<()> {
let mut file_contents = Vec::new();
for generator in type_data {
let filename = generator.filename();
file_contents.push(Cow::Owned(format!("mod {filename};")));
file_contents.push(Cow::Owned(format!("pub use {filename}::*;")));
}
file_contents.push(Cow::Borrowed(""));
fs::write(format!("{PYTHON_OUT_FOLDER}/mod.rs"), file_contents.join("\n"))?;
Ok(())
}
fn run_flatc() {
println!("cargo:rerun-if-changed=flatbuffers-schema/comms.fbs");
println!("cargo:rerun-if-changed=flatbuffers-schema/gamedata.fbs");
println!("cargo:rerun-if-changed=flatbuffers-schema/gamestatemanip.fbs");
println!("cargo:rerun-if-changed=flatbuffers-schema/matchconfig.fbs");
println!("cargo:rerun-if-changed=flatbuffers-schema/rendering.fbs");
println!("cargo:rerun-if-changed=flatbuffers-schema/rlbot.fbs");
set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap();
let mut schema_folder = Path::new(SCHEMA_FOLDER);
if !schema_folder.exists() {
schema_folder = Path::new(SCHEMA_FOLDER_BACKUP);
assert!(schema_folder.exists(), "Could not find flatbuffers schema folder");
}
let schema_folder_str = schema_folder.display();
let flatc_str = format!("{schema_folder_str}/{FLATC_BINARY}");
let flatc_path = Path::new(&flatc_str);
if !flatc_path.exists() {
fs::create_dir_all(flatc_path).unwrap();
// if the flatc binary isn't found, download it
let file_name = if cfg!(windows) {
"Windows.flatc.binary.zip"
} else {
"Linux.flatc.binary.g++-13.zip"
};
let response = reqwest::blocking::get(format!("{FLATC_DOWNLOAD_URL}/{file_name}"))
.map_err(|e| {
eprintln!("Failed to download flatc binary: {e}");
io::Error::other("Failed to download flatc binary")
})
.unwrap();
let bytes = response
.bytes()
.map_err(|e| {
eprintln!("Failed to read response stream when downloading flatc binary: {e}");
io::Error::other("Failed to read response stream when downloading flatc binary")
})
.unwrap();
// extract zip
let mut zip = ZipArchive::new(io::Cursor::new(bytes)).unwrap();
zip.extract(schema_folder).unwrap();
assert!(flatc_path.exists(), "Failed to download flatc binary");
}
let mut proc = Command::new(flatc_str);
proc.args([
"--rust".as_ref(),
"--gen-object-api".as_ref(),
"--gen-all".as_ref(),
"--filename-suffix".as_ref(),
"".as_ref(),
"--rust-module-root-file".as_ref(),
"-o".as_ref(),
OUT_FOLDER.as_ref(),
schema_folder.join(RLBOT_FBS).as_os_str(),
])
.spawn()
.unwrap()
.wait()
.unwrap();
assert!(proc.status().unwrap().success(), "flatc failed to run");
}
fn main() {
run_flatc();
let out_folder = Path::new(OUT_FOLDER).join("rlbot").join("flat");
assert!(
out_folder.exists(),
"Could not find generated folder: {}",
out_folder.display()
);
// read the current contents of the generated folder
let generated_files = fs::read_dir(out_folder)
.unwrap()
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, io::Error>>()
.unwrap();
let mut type_data = Vec::new();
for path in generated_files {
let Some(mut bind_generator) = PythonBindType::new(&path) else {
continue;
};
bind_generator.generate(&path).unwrap();
type_data.push(bind_generator);
}
mod_rs_generator(&type_data).unwrap();
pyi::generator(&type_data).unwrap();
class_inject::classes_to_lib_rs(&type_data).unwrap();
}