Skip to content

Commit 0e46268

Browse files
committed
Custom setters for float type coercion
Closes #8
1 parent 1c89ce9 commit 0e46268

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
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"
3-
version = "0.14.1"
3+
version = "0.14.2"
44
edition = "2024"
55
description = "A Python module implemented in Rust for serializing and deserializing RLBot's flatbuffers"
66
repository = "https://github.com/VirxEC/rlbot_flatbuffers_py"

codegen/structs.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,33 @@ impl StructBindGenerator {
466466

467467
write_str!(self, " }");
468468
write_str!(self, " }");
469+
470+
if self.is_frozen || self.is_no_set {
471+
return;
472+
}
473+
474+
for variable_info in &self.types {
475+
let variable_name = variable_info.name.as_str();
476+
477+
match &variable_info.rust_type {
478+
RustType::Base(inner_type) => match inner_type.as_str() {
479+
"f32" => {
480+
write_str!(self, "\n #[setter]");
481+
write_fmt!(
482+
self,
483+
" pub fn {variable_name}(&mut self, py: Python, value: f32) {{",
484+
);
485+
write_fmt!(
486+
self,
487+
" self.{variable_name} = crate::float_to_py(py, value);"
488+
);
489+
write_str!(self, " }");
490+
}
491+
_ => continue,
492+
},
493+
_ => continue,
494+
}
495+
}
469496
}
470497

471498
fn generate_str_method(&mut self) {
@@ -919,10 +946,8 @@ impl Generator for StructBindGenerator {
919946
"#[pyclass(module = \"rlbot_flatbuffers\", subclass, get_all, frozen)]"
920947
} else if self.types.is_empty() {
921948
"#[pyclass(module = \"rlbot_flatbuffers\", subclass, frozen)]"
922-
} else if self.is_no_set {
923-
"#[pyclass(module = \"rlbot_flatbuffers\", subclass, get_all)]"
924949
} else {
925-
"#[pyclass(module = \"rlbot_flatbuffers\", subclass, get_all, set_all)]"
950+
"#[pyclass(module = \"rlbot_flatbuffers\", subclass, get_all)]"
926951
}
927952
);
928953

@@ -933,11 +958,14 @@ impl Generator for StructBindGenerator {
933958
return;
934959
}
935960

961+
let gen_set = !(self.is_no_set || self.is_frozen);
962+
936963
write_fmt!(self, "pub struct {} {{", self.struct_name);
937964

938965
for variable_info in &self.types {
939966
let variable_name = variable_info.name.as_str();
940967

968+
let mut add_set = true;
941969
let variable_type = match &variable_info.rust_type {
942970
RustType::Vec(InnerVecType::U8) => String::from("Py<PyBytes>"),
943971
RustType::Vec(InnerVecType::String) => String::from("Vec<String>"),
@@ -952,7 +980,10 @@ impl Generator for StructBindGenerator {
952980
}
953981
RustType::Option(_, inner_type) => format!("Option<Py<super::{inner_type}>>"),
954982
RustType::Base(inner_type) => match inner_type.as_str() {
955-
"f32" => String::from("Py<PyFloat>"),
983+
"f32" => {
984+
add_set = false;
985+
String::from("Py<PyFloat>")
986+
}
956987
_ => inner_type.clone(),
957988
},
958989
RustType::String => String::from("Py<PyString>"),
@@ -962,6 +993,10 @@ impl Generator for StructBindGenerator {
962993
RustType::Other(inner_type) => format!("super::{inner_type}"),
963994
};
964995

996+
if gen_set && add_set {
997+
write_str!(self, " #[pyo3(set)]");
998+
}
999+
9651000
write_fmt!(self, " pub {variable_name}: {variable_type},");
9661001
}
9671002

pytest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from random import randrange
22
from time import time_ns
33

4+
import numpy as np
45
from rlbot_flatbuffers import *
56

67

@@ -35,6 +36,11 @@ def random_script_config():
3536

3637

3738
if __name__ == "__main__":
39+
controller = ControllerState()
40+
controller.throttle = 1
41+
controller.steer = 0.5
42+
controller.pitch = np.array([0.1], dtype=np.float32)[0]
43+
3844
vec1 = MyVector(1, 2, 3)
3945
vec2 = Vector3(4, 5, 6)
4046
print(vec1 + vec2)

0 commit comments

Comments
 (0)