use bevy_app::App;
use bevy_ecs::{entity::Entity, system::Commands};
use std::sync::OnceLock;
use tracing::trace;
use crate::interop::{GodotAccess, GodotNodeHandle};
pub type BundleCreatorFn = fn(&mut Commands, Entity, &mut GodotAccess, GodotNodeHandle) -> bool;
pub struct AutoSyncBundleRegistry {
pub godot_class_name: &'static str,
pub create_bundle_fn: BundleCreatorFn,
}
crate::inventory::collect!(AutoSyncBundleRegistry);
static BUNDLE_REGISTRY: OnceLock<Vec<&'static AutoSyncBundleRegistry>> = OnceLock::new();
pub fn register_all_autosync_bundles(_app: &mut App) {
BUNDLE_REGISTRY.get_or_init(|| {
let entries: Vec<&'static AutoSyncBundleRegistry> =
crate::inventory::iter::<AutoSyncBundleRegistry>
.into_iter()
.collect();
tracing::debug!("Registered {} AutoSyncBundle entries", entries.len());
entries
});
}
pub fn try_add_bundles_for_node(
commands: &mut Commands,
entity: Entity,
godot: &mut GodotAccess,
node_handle: GodotNodeHandle,
) {
if let Some(entries) = BUNDLE_REGISTRY.get() {
for entry in entries {
if (entry.create_bundle_fn)(commands, entity, godot, node_handle) {
trace!(
"Added bundle for {} to entity {:?}",
entry.godot_class_name, entity
);
}
}
}
}