-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayer_input.rs
More file actions
92 lines (80 loc) · 2.67 KB
/
player_input.rs
File metadata and controls
92 lines (80 loc) · 2.67 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
use crate::{FromGil, IntoGil, PyDefault, flat, flat_err_to_py};
use planus::{Builder, ReadAsRoot};
use pyo3::{prelude::*, types::*};
#[pyclass(module = "rlbot_flatbuffers", subclass, frozen, get_all)]
pub struct PlayerInput {
pub player_index: u32,
pub controller_state: Py<super::ControllerState>,
}
impl crate::PyDefault for PlayerInput {
fn py_default(py: Python) -> Py<Self> {
Py::new(
py,
Self {
player_index: Default::default(),
controller_state: super::ControllerState::py_default(py),
},
)
.unwrap()
}
}
impl FromGil<&flat::PlayerInput> for PlayerInput {
#[allow(unused_variables)]
fn from_gil(py: Python, flat_t: &flat::PlayerInput) -> Self {
PlayerInput {
player_index: flat_t.player_index,
controller_state: crate::into_py_from(py, &flat_t.controller_state),
}
}
}
impl FromGil<&PlayerInput> for flat::PlayerInput {
#[allow(unused_variables)]
fn from_gil(py: Python, py_type: &PlayerInput) -> Self {
Self {
player_index: py_type.player_index,
controller_state: crate::from_py_into(py, &py_type.controller_state),
}
}
}
#[pymethods]
impl PlayerInput {
#[new]
#[pyo3(signature = (player_index=0, controller_state=None))]
pub fn new(
py: Python,
player_index: u32,
controller_state: Option<Py<super::ControllerState>>,
) -> Self {
Self {
player_index,
controller_state: controller_state
.unwrap_or_else(|| super::ControllerState::py_default(py)),
}
}
pub fn __str__(&self, py: Python) -> String {
self.__repr__(py)
}
#[allow(unused_variables)]
pub fn __repr__(&self, py: Python) -> String {
format!(
"PlayerInput(player_index={}, controller_state={})",
self.player_index,
self.controller_state.borrow(py).__repr__(py),
)
}
#[classattr]
fn __match_args__() -> (&'static str, &'static str) {
("player_index", "controller_state")
}
fn pack<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
let mut builder = Builder::with_capacity(u16::MAX as usize);
let flat_t = flat::PlayerInput::from_gil(py, self);
PyBytes::new(py, builder.finish(flat_t, None))
}
#[staticmethod]
fn unpack(py: Python, data: &[u8]) -> PyResult<Py<Self>> {
let flat_t_ref = flat::PlayerInputRef::read_as_root(data).map_err(flat_err_to_py)?;
let flat_t = flat::PlayerInput::try_from(flat_t_ref).map_err(flat_err_to_py)?;
Ok(crate::into_py_from(py, &flat_t))
}
}