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