use std::{any::TypeId, sync::Arc};
use bevy_app::{App, Plugin};
use bevy_ecs::world::World;
use bevy_mod_scripting_bindings_domain::ReflectionPrimitiveKind;
use bevy_reflect::TypeRegistration;
use super::{AppScriptNamespaceMembers, AppScriptTypeCatalogue, NamespaceValue};
use crate::{ScriptValue, docgen::typed_through::ThroughTypeInfo};
pub struct CoreScriptGlobalsPlugin {
pub filter: fn(&TypeRegistration) -> bool,
}
impl Default for CoreScriptGlobalsPlugin {
fn default() -> Self {
Self { filter: |_| true }
}
}
impl Plugin for CoreScriptGlobalsPlugin {
fn build(&self, app: &mut App) {
let members = app
.world_mut()
.get_resource_or_init::<AppScriptNamespaceMembers>()
.clone();
members.write().register_value(
"bms",
"world",
NamespaceValue {
maker: Arc::new(|_| Ok(ScriptValue::Type(TypeId::of::<World>()))),
type_information: ThroughTypeInfo::Primitive(
ReflectionPrimitiveKind::StaticTypeRef,
),
documentation: Some(
"The ECS world, main point of interaction with the app.".into(),
),
},
);
app.insert_resource(AppScriptTypeCatalogue::new(self.filter));
}
}