use crate::{
AppScriptNamespaceMembers, NamespaceMembers, ReflectNoTypeFunctions, ScriptFunctionRegistryArc,
docgen::info::GetFunctionInfo,
function::script_function::{AppScriptFunctionRegistry, ScriptFunction, documented_function},
};
use ::bevy_reflect::{GetTypeRegistration, Reflect};
use bevy_ecs::{reflect::AppTypeRegistry, world::World};
use bevy_mod_scripting_derive::DebugWithTypeInfo;
use bevy_mod_scripting_display::{DisplayWithTypeInfo, WithTypeInfo};
use bevy_mod_scripting_world::WorldGuard;
use std::{any::TypeId, borrow::Cow, marker::PhantomData};
use super::type_dependencies::GetFunctionTypeDependencies;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Reflect, DebugWithTypeInfo)]
#[debug_with_type_info(bms_display_path = "bevy_mod_scripting_display")]
#[reflect(NoTypeFunctions)]
pub enum FunctionScope {
OnType(TypeId),
Member,
Builtin,
}
#[profiling::all_functions]
impl FunctionScope {
pub fn all_references() -> Self {
FunctionScope::OnType(TypeId::of::<crate::ReflectReference>())
}
pub fn all_types() -> Self {
FunctionScope::OnType(TypeId::of::<crate::StaticTypeRef>())
}
pub fn all_variants() -> Self {
FunctionScope::OnType(TypeId::of::<crate::EnumVariantRef>())
}
}
pub struct NamespaceBuilder<'a, N> {
registry: ScriptFunctionRegistryArc,
namespace: PhantomData<N>,
members_of: Option<Cow<'static, str>>,
pub world: &'a mut World,
}
impl<'a> NamespaceBuilder<'a, NamespaceMembers> {
pub fn in_namespace(world: &'a mut World, namespace: impl Into<Cow<'static, str>>) -> Self {
let mut builder = Self::new_unregistered(world);
builder.members_of = Some(namespace.into());
builder
}
}
#[profiling::all_functions]
impl<'a, S: 'static> NamespaceBuilder<'a, S> {
pub fn new(world: &'a mut World) -> Self
where
S: GetTypeRegistration,
{
{
let registry = world.get_resource_or_init::<AppTypeRegistry>();
let mut registry = registry.write();
registry.register::<S>();
}
Self::new_unregistered(world)
}
pub fn new_unregistered(world: &'a mut World) -> Self {
Self {
registry: world
.get_resource_or_init::<AppScriptFunctionRegistry>()
.0
.clone(),
namespace: Default::default(),
members_of: None,
world,
}
}
pub fn register<'env, N, F, M>(&mut self, name: N, function: F) -> &mut Self
where
N: Into<Cow<'static, str>>,
F: ScriptFunction<'env, M> + GetFunctionTypeDependencies<M> + GetFunctionInfo<M>,
{
self.register_inner(name, function, None, None)
}
pub fn register_documented<'env, N, F, M>(
&mut self,
name: N,
function: F,
docstring: &'static str,
arg_names: &'static [&'static str],
) -> &mut Self
where
N: Into<Cow<'static, str>>,
F: ScriptFunction<'env, M> + GetFunctionTypeDependencies<M> + GetFunctionInfo<M>,
{
self.register_inner(name, function, Some(docstring), Some(arg_names))
}
fn register_inner<'env, N, F, M>(
&mut self,
name: N,
function: F,
docstring: Option<&'static str>,
arg_names: Option<&'static [&'static str]>,
) -> &mut Self
where
N: Into<Cow<'static, str>>,
F: ScriptFunction<'env, M> + GetFunctionTypeDependencies<M> + GetFunctionInfo<M>,
{
if let Some(namespace) = &self.members_of {
let name = name.into();
let function = documented_function(
FunctionScope::Member,
name.clone(),
function,
docstring,
arg_names,
);
let members = self
.world
.get_resource_or_init::<AppScriptNamespaceMembers>();
members
.write()
.register_function(namespace.clone(), name, function);
} else {
let mut registry = self.registry.write();
registry.register_with_arg_names(
TypeId::of::<S>(),
name,
function,
docstring.unwrap_or_default(),
arg_names.unwrap_or(&[]),
);
}
{
let type_registry = self.world.get_resource_or_init::<AppTypeRegistry>();
let mut type_registry = type_registry.write();
F::register_type_dependencies(&mut type_registry);
}
self
}
}
impl From<TypeId> for FunctionScope {
fn from(value: TypeId) -> Self {
FunctionScope::OnType(value)
}
}
impl DisplayWithTypeInfo for FunctionScope {
fn display_with_type_info(
&self,
f: &mut std::fmt::Formatter<'_>,
type_info_provider: Option<&WorldGuard>,
) -> std::fmt::Result {
match self {
FunctionScope::Member => f.write_str("Namespace member"),
FunctionScope::Builtin => f.write_str("Builtin"),
FunctionScope::OnType(type_id) => {
write!(
f,
"Namespace for type {}",
WithTypeInfo::new_with_opt_info(type_id, type_info_provider)
)
}
}
}
}