Skip to content

Update to the newest schema #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
352 changes: 176 additions & 176 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rlbot_flatbuffers"
version = "0.16.1"
version = "0.17.0"
edition = "2024"
description = "A Python module implemented in Rust for serializing and deserializing RLBot's flatbuffers"
repository = "https://github.com/VirxEC/rlbot_flatbuffers_py"
Expand All @@ -18,7 +18,7 @@ all = "warn"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.24.0", features = [] }
pyo3 = "0.25.0"
serde = "1.0.217"
flatbuffers = "=25.2.10"
# get-size appears to be unmaintained but it's too useful here
Expand All @@ -27,7 +27,7 @@ get-size = { git = "https://github.com/VirxEC/get-size", branch = "update", feat

[build-dependencies]
reqwest = { version = "0.12.15", features = ["blocking", "rustls-tls"], default-features = false }
zip = { version = "2.6.1", features = ["deflate"], default-features = false }
zip = { version = "4.0.0", features = ["deflate"], default-features = false }

[profile.dev]
opt-level = 2
Expand Down
2 changes: 1 addition & 1 deletion codegen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl EnumBindGenerator {
Cow::Borrowed("use crate::{flat_err_to_py, generated::rlbot::flat};"),
Cow::Borrowed("use flatbuffers::root;"),
Cow::Borrowed(
"use pyo3::{exceptions::PyValueError, pyclass, pymethods, types::PyBytes, Bound, PyResult, Python};",
"use pyo3::{Bound, PyResult, Python, exceptions::PyValueError, pyclass, pymethods, types::PyBytes};",
),
Cow::Borrowed(""),
];
Expand Down
121 changes: 72 additions & 49 deletions codegen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ use std::{borrow::Cow, env::set_current_dir, fs, io, path::Path, process::Comman
use structs::StructBindGenerator;
use zip::ZipArchive;

const FLATC_BINARY: &str = if cfg!(windows) { "flatc.exe" } else { "flatc" };
const OUT_FOLDER: &str = "./src/generated";
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 FLATC_DOWNLOAD_URL: &str = "https://github.com/google/flatbuffers/releases/download/v25.2.10/";

const OUT_FOLDER: &str = "./src/generated";
pub const PYTHON_OUT_FOLDER: &str = "./src/python";

pub enum PythonBindType {
Expand All @@ -29,15 +34,14 @@ pub enum PythonBindType {

impl PythonBindType {
pub const BASE_TYPES: [&'static str; 6] = ["bool", "i32", "u32", "f32", "String", "u8"];
pub const FROZEN_TYPES: [&'static str; 6] = [
pub const FROZEN_TYPES: [&'static str; 26] = [
"ControllableInfo",
"ControllableTeamInfo",
"PredictionSlice",
"BallPrediction",
"GoalInfo",
"BoostPad",
"FieldInfo",
"ControllableInfo",
"ControllableTeamInfo",
"PlayerClass",
];
pub const NO_SET_TYPES: [&'static str; 16] = [
"Physics",
"GamePacket",
"PlayerInfo",
Expand All @@ -52,13 +56,25 @@ impl PythonBindType {
"MatchInfo",
"TeamInfo",
"Vector2",
"PredictionSlice",
"BallPrediction",
"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 UNIONS: [&'static str; 4] = ["PlayerClass", "CollisionShape", "RelativeAnchor", "RenderType"];

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> {
Expand Down Expand Up @@ -176,15 +192,15 @@ fn mod_rs_generator(type_data: &[PythonBindType]) -> io::Result<()> {
Ok(())
}

fn run_flatc() -> io::Result<()> {
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"))?;
set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap();

let mut schema_folder = Path::new(SCHEMA_FOLDER);
if !schema_folder.exists() {
Expand All @@ -197,51 +213,58 @@ fn run_flatc() -> io::Result<()> {
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")
})?;
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")
})?;
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))?;
zip.extract(schema_folder)?;
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",
"--gen-object-api",
"--gen-all",
"--filename-suffix",
"",
"--rust-module-root-file",
"-o",
OUT_FOLDER,
&format!("{schema_folder_str}/rlbot.fbs"),
"--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()?
.wait()?;

assert!(proc.status()?.success(), "flatc failed to run");
.spawn()
.unwrap()
.wait()
.unwrap();

Ok(())
assert!(proc.status().unwrap().success(), "flatc failed to run");
}

fn main() -> io::Result<()> {
run_flatc()?;
fn main() {
run_flatc();

let out_folder = Path::new(OUT_FOLDER).join("rlbot").join("flat");

Expand All @@ -252,9 +275,11 @@ fn main() -> io::Result<()> {
);

// read the current contents of the generated folder
let generated_files = fs::read_dir(out_folder)?
let generated_files = fs::read_dir(out_folder)
.unwrap()
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, io::Error>>()?;
.collect::<Result<Vec<_>, io::Error>>()
.unwrap();

let mut type_data = Vec::new();

Expand All @@ -263,13 +288,11 @@ fn main() -> io::Result<()> {
continue;
};

bind_generator.generate(&path)?;
bind_generator.generate(&path).unwrap();
type_data.push(bind_generator);
}

mod_rs_generator(&type_data)?;
pyi::generator(&type_data)?;
class_inject::classes_to_lib_rs(&type_data)?;

Ok(())
mod_rs_generator(&type_data).unwrap();
pyi::generator(&type_data).unwrap();
class_inject::classes_to_lib_rs(&type_data).unwrap();
}
11 changes: 0 additions & 11 deletions codegen/pyi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,6 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
write_str!(file, " Serializes this instance into a byte array");
write_str!(file, " \"\"\"");

if !bind.is_frozen {
write_str!(file, " def unpack_with(self, data: bytes) -> None:");
write_str!(file, " \"\"\"");
write_str!(file, " Deserializes the data into this instance\n");
write_str!(
file,
" :raises InvalidFlatbuffer: If the `data` is invalid for this type"
);
write_str!(file, " \"\"\"");
}

write_str!(file, " @staticmethod");
write_fmt!(file, " def unpack(data: bytes) -> {type_name}:");
write_str!(file, " \"\"\"");
Expand Down
Loading