Skip to content

Commit cfa2b72

Browse files
committed
Update to new spec
1 parent f0fee03 commit cfa2b72

13 files changed

Lines changed: 302 additions & 105 deletions

File tree

Cargo.lock

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rlbot-flatbuffers-py"
3-
version = "0.9.1"
3+
version = "0.10.0"
44
edition = "2021"
55
description = "A Python module implemented in Rust for serializing and deserializing RLBot's flatbuffers"
66
repository = "https://github.com/VirxEC/rlbot_flatbuffers_py"

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ All values are optional when creating a class and have the proper defaults.
5858
```python
5959
import rlbot_flatbuffers as flat
6060

61-
def handle_packet(packet: flat.GameTickPacket):
62-
if packet.game_info.game_state_type not in {
63-
flat.GameStateType.Active,
64-
flat.GameStateType.Kickoff,
61+
def handle_packet(packet: flat.GamePacket):
62+
if packet.game_info.game_status not in {
63+
flat.GameStatus.Active,
64+
flat.GameStatus.Kickoff,
6565
}:
6666
# Return early if the game isn't active
6767
return

codegen/enums.rs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,17 @@ impl EnumBindGenerator {
117117
continue;
118118
}
119119

120-
let definition = line_trim.trim_start_matches("pub const ").trim_end_matches(';');
120+
let definition = line_trim
121+
.trim_start_matches("pub const ")
122+
.trim_end_matches(';');
121123

122124
let mut parts = definition.split(": Self = ");
123125

124126
let variable_name = parts.next()?;
125-
let variable_value = parts.next()?.trim_start_matches("Self(").trim_end_matches(')');
127+
let variable_value = parts
128+
.next()?
129+
.trim_start_matches("Self(")
130+
.trim_end_matches(')');
126131

127132
types.push((variable_name, variable_value));
128133

@@ -143,11 +148,14 @@ impl EnumBindGenerator {
143148
let union_end_definition = "}\n";
144149
let union_end = contents[union_start..].find(union_end_definition).unwrap();
145150

146-
let union_definition =
147-
&contents[union_start + union_definition.len()..union_start + union_end - union_end_definition.len()];
151+
let union_definition = &contents[union_start + union_definition.len()
152+
..union_start + union_end - union_end_definition.len()];
148153

149154
for (line, variable) in union_definition.split('\n').zip(&mut custom_types) {
150-
let line_trim = line.trim().trim_start_matches(&variable.name).trim_end_matches(',');
155+
let line_trim = line
156+
.trim()
157+
.trim_start_matches(&variable.name)
158+
.trim_end_matches(',');
151159

152160
if line_trim.is_empty() {
153161
variable.value = None;
@@ -156,7 +164,9 @@ impl EnumBindGenerator {
156164

157165
variable.snake_case_name = camel_to_snake_case(variable.name.as_str());
158166

159-
let new_type = line_trim.trim_start_matches("(Box<").trim_end_matches("T>)");
167+
let new_type = line_trim
168+
.trim_start_matches("(Box<")
169+
.trim_end_matches("T>)");
160170
variable.value = Some(new_type.to_string());
161171
}
162172

@@ -225,7 +235,10 @@ impl Generator for EnumBindGenerator {
225235
contents = contents.replace("\r\n", "\n");
226236
}
227237

228-
contents = contents.replace("use self::flatbuffers", "use get_size::GetSize;\nuse self::flatbuffers");
238+
contents = contents.replace(
239+
"use self::flatbuffers",
240+
"use get_size::GetSize;\nuse self::flatbuffers",
241+
);
229242

230243
contents = contents.replace(
231244
"#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]\n",
@@ -241,7 +254,10 @@ impl Generator for EnumBindGenerator {
241254
self,
242255
"#[pyclass(module = \"rlbot_flatbuffers\", frozen, hash, eq, eq_int)]"
243256
);
244-
write_str!(self, "#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]");
257+
write_str!(
258+
self,
259+
"#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]"
260+
);
245261
write_fmt!(self, "pub enum {} {{", self.struct_name);
246262
write_str!(self, " #[default]");
247263

@@ -261,7 +277,11 @@ impl Generator for EnumBindGenerator {
261277
self.struct_name,
262278
self.struct_name
263279
);
264-
write_fmt!(self, " fn from(flat_t: flat::{}) -> Self {{", self.struct_name);
280+
write_fmt!(
281+
self,
282+
" fn from(flat_t: flat::{}) -> Self {{",
283+
self.struct_name
284+
);
265285
write_str!(self, " match flat_t {");
266286

267287
for variable_info in &self.types {
@@ -274,7 +294,10 @@ impl Generator for EnumBindGenerator {
274294
);
275295
}
276296

277-
write_str!(self, " v => unreachable!(\"Unknown value: {v:?}\"),");
297+
write_str!(
298+
self,
299+
" v => unreachable!(\"Unknown value: {v:?}\"),"
300+
);
278301

279302
write_str!(self, " }");
280303
write_str!(self, " }");
@@ -289,7 +312,11 @@ impl Generator for EnumBindGenerator {
289312
self.struct_name,
290313
self.struct_name
291314
);
292-
write_fmt!(self, " fn from(py_type: &{}) -> Self {{", self.struct_name);
315+
write_fmt!(
316+
self,
317+
" fn from(py_type: &{}) -> Self {{",
318+
self.struct_name
319+
);
293320
write_str!(self, " match *py_type {");
294321

295322
for variable_info in &self.types {

codegen/main.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ pub enum PythonBindType {
2525

2626
impl PythonBindType {
2727
pub const BASE_TYPES: [&'static str; 6] = ["bool", "i32", "u32", "f32", "String", "u8"];
28-
pub const FROZEN_TYPES: [&'static str; 18] = [
28+
pub const FROZEN_TYPES: [&'static str; 21] = [
2929
"FieldInfo",
3030
"BoostPad",
3131
"GoalInfo",
32-
"GameTickPacket",
32+
"GamePacket",
3333
"PlayerInfo",
3434
"ScoreInfo",
3535
"BallInfo",
@@ -44,9 +44,17 @@ impl PythonBindType {
4444
"BallPrediction",
4545
"PredictionSlice",
4646
"Physics",
47+
"Vector2",
48+
"ControllableInfo",
49+
"ControllableTeamInfo",
50+
];
51+
pub const FROZEN_NEEDS_PY: [&'static str; 3] = ["GamePacket", "BallInfo", "CollisionShape"];
52+
pub const UNIONS: [&'static str; 4] = [
53+
"PlayerClass",
54+
"CollisionShape",
55+
"RelativeAnchor",
56+
"RenderType",
4757
];
48-
pub const FROZEN_NEEDS_PY: [&'static str; 3] = ["GameTickPacket", "BallInfo", "CollisionShape"];
49-
pub const UNIONS: [&'static str; 4] = ["PlayerClass", "CollisionShape", "RelativeAnchor", "RenderType"];
5058

5159
fn new(path: &Path) -> Option<Self> {
5260
// get the filename without the extension
@@ -84,11 +92,15 @@ impl PythonBindType {
8492
)?));
8593
}
8694

87-
if let Some((types, enum_type)) = enums::EnumBindGenerator::get_types(&contents, &struct_name) {
95+
if let Some((types, enum_type)) =
96+
enums::EnumBindGenerator::get_types(&contents, &struct_name)
97+
{
8898
return Some(match enum_type {
89-
enums::EnumType::Enum => {
90-
Self::Enum(enums::EnumBindGenerator::new(filename.to_string(), struct_name, types)?)
91-
}
99+
enums::EnumType::Enum => Self::Enum(enums::EnumBindGenerator::new(
100+
filename.to_string(),
101+
struct_name,
102+
types,
103+
)?),
92104
enums::EnumType::Union => Self::Union(unions::UnionBindGenerator::new(
93105
filename.to_string(),
94106
struct_name,
@@ -138,7 +150,10 @@ fn mod_rs_generator(type_data: &[PythonBindType]) -> io::Result<()> {
138150

139151
file_contents.push(Cow::Borrowed(""));
140152

141-
fs::write(format!("{PYTHON_OUT_FOLDER}/mod.rs"), file_contents.join("\n"))?;
153+
fs::write(
154+
format!("{PYTHON_OUT_FOLDER}/mod.rs"),
155+
file_contents.join("\n"),
156+
)?;
142157

143158
Ok(())
144159
}
@@ -155,7 +170,10 @@ fn run_flatc() -> io::Result<()> {
155170
let mut schema_folder = Path::new(SCHEMA_FOLDER);
156171
if !schema_folder.exists() {
157172
schema_folder = Path::new(SCHEMA_FOLDER_BACKUP);
158-
assert!(schema_folder.exists(), "Could not find flatbuffers schema folder");
173+
assert!(
174+
schema_folder.exists(),
175+
"Could not find flatbuffers schema folder"
176+
);
159177
}
160178

161179
let schema_folder_str = schema_folder.display();

codegen/pyi.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
8888
);
8989
write_str!(file, " \"\"\"");
9090
write_str!(file, " def __int__(self) -> int: ...");
91-
write_fmt!(file, " def __eq__(self, other: {type_name}) -> bool: ...");
91+
write_fmt!(
92+
file,
93+
" def __eq__(self, other: {type_name}) -> bool: ..."
94+
);
9295
write_str!(file, " def __hash__(self) -> str: ...");
9396
}
9497
PythonBindType::Struct(gen) => {
@@ -111,6 +114,20 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
111114
python_types.push("bytes".to_string());
112115
write_fmt!(file, " {variable_name}: bytes");
113116
}
117+
RustType::Vec(InnerVecType::Base(type_name)) => {
118+
let python_type = if type_name == "bool" {
119+
"bool"
120+
} else if type_name == "i32" || type_name == "u32" {
121+
"int"
122+
} else if type_name == "f32" {
123+
"float"
124+
} else {
125+
type_name
126+
};
127+
128+
python_types.push(format!("Sequence[{python_type}]"));
129+
write_fmt!(file, " {variable_name}: Sequence[{python_type}]");
130+
}
114131
RustType::Vec(InnerVecType::String) => {
115132
python_types.push("Sequence[str]".to_string());
116133
write_fmt!(file, " {variable_name}: Sequence[str]");
@@ -166,16 +183,26 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
166183
let union_types = type_data
167184
.iter()
168185
.find_map(|item| match item {
169-
PythonBindType::Union(gen) if gen.struct_name() == type_name => {
170-
Some(gen.types.iter().skip(1).map(|v| v.name.as_str()).collect::<Vec<_>>())
186+
PythonBindType::Union(gen)
187+
if gen.struct_name() == type_name =>
188+
{
189+
Some(
190+
gen.types
191+
.iter()
192+
.skip(1)
193+
.map(|v| v.name.as_str())
194+
.collect::<Vec<_>>(),
195+
)
171196
}
172197
_ => None,
173198
})
174199
.unwrap();
175200
let types = union_types.join(" | ");
176201
python_types.push(format!("Optional[{types}]"));
177202
}
178-
RustType::Custom(type_name) | RustType::Other(type_name) | RustType::Base(type_name) => {
203+
RustType::Custom(type_name)
204+
| RustType::Other(type_name)
205+
| RustType::Base(type_name) => {
179206
python_types.push(type_name.to_string());
180207
write_fmt!(file, " {variable_name}: {type_name}");
181208
}
@@ -212,7 +239,9 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
212239
"String" => Cow::Borrowed("\"\""),
213240
"Vec<u8>" => Cow::Borrowed("b\"\""),
214241
t => {
215-
if python_type.starts_with("Optional") || t.starts_with("Option<") {
242+
if python_type.starts_with("Optional")
243+
|| t.starts_with("Option<")
244+
{
216245
Cow::Borrowed("None")
217246
} else if t.starts_with("Vec<") {
218247
Cow::Borrowed("[]")
@@ -228,7 +257,10 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
228257
}
229258
};
230259

231-
write_fmt!(file, " {variable_name}: {python_type} = {default_value},");
260+
write_fmt!(
261+
file,
262+
" {variable_name}: {python_type} = {default_value},"
263+
);
232264
}
233265

234266
write_str!(file, " ): ...");

0 commit comments

Comments
 (0)