use std::{any::TypeId, marker::PhantomData, ops::Deref};
use bevy_app::App;
use bevy_asset::Asset;
use bevy_ecs::resource::Resource;
use bevy_mod_scripting_world::{CallScope, WorldGuard};
use bevy_reflect::{CreateTypeData, Reflect, TypeInfo, TypeRegistry, Typed};
use crate::{
ReflectReference, ScriptValue,
docgen::{TypedThrough, typed_through::ThroughTypeInfo},
error::InteropError,
function::{
arg_meta::ArgMeta, from::FromScript, into::IntoScript,
type_dependencies::GetTypeDependencies,
},
};
#[derive(Reflect, Asset, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[reflect(GenericMarker)]
pub struct A;
#[derive(Clone)]
pub struct ReflectGenericMarker;
impl<T> CreateTypeData<T> for ReflectGenericMarker {
fn create_type_data(_: ()) -> Self {
ReflectGenericMarker
}
}
pub fn is_generic_marker(type_registry: &TypeRegistry, type_id: TypeId) -> bool {
[TypeId::of::<A>(), TypeId::of::<B>(), TypeId::of::<C>()].contains(&type_id)
|| type_registry
.get_type_data::<ReflectGenericMarker>(type_id)
.is_some()
}
#[macro_export]
macro_rules! generic_marker {
($(#[$meta:meta])* $vis:vis struct $name:ident;) => {
$(#[$meta])*
#[derive(
::bevy_reflect::Reflect, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug,
)]
#[reflect($crate::function::generics::GenericMarker)]
$vis struct $name;
};
}
generic_marker! {
pub struct B;
}
generic_marker! {
pub struct C;
}
pub struct TypeOf<R, M>(pub R, PhantomData<fn() -> M>);
impl<R, M> TypeOf<R, M> {
pub fn new(inner: R) -> Self {
TypeOf(inner, PhantomData)
}
pub fn into_inner(self) -> R {
self.0
}
}
impl<R, M> Deref for TypeOf<R, M> {
type Target = R;
fn deref(&self) -> &R {
&self.0
}
}
impl<R: FromScript, M> FromScript for TypeOf<R, M> {
type This<'w> = TypeOf<R::This<'w>, M>;
fn from_script<'s>(
value: ScriptValue,
scope: &'s CallScope<'_>,
) -> Result<Self::This<'s>, InteropError> {
R::from_script(value, scope).map(|inner| TypeOf(inner, PhantomData))
}
}
impl<R: IntoScript, M> IntoScript for TypeOf<R, M> {
fn into_script(self, world: WorldGuard) -> Result<ScriptValue, InteropError> {
self.0.into_script(world)
}
}
impl<R: ArgMeta, M> ArgMeta for TypeOf<R, M> {
fn default_value() -> Option<ScriptValue> {
R::default_value()
}
}
impl<R: GetTypeDependencies, M> GetTypeDependencies for TypeOf<R, M> {
type Underlying = R::Underlying;
fn register_type_dependencies(registry: &mut TypeRegistry) {
R::register_type_dependencies(registry)
}
}
impl<R: TypedThrough, M: Typed> TypedThrough for TypeOf<R, M> {
fn through_type_info() -> ThroughTypeInfo {
ThroughTypeInfo::TypeOf {
of: M::type_info(),
erased: Box::new(R::through_type_info()),
}
}
}
pub struct ValueOf<M>(pub ReflectReference, PhantomData<fn() -> M>);
impl<M> ValueOf<M> {
pub fn new(reference: ReflectReference) -> Self {
ValueOf(reference, PhantomData)
}
pub fn into_inner(self) -> ReflectReference {
self.0
}
}
impl<M> Deref for ValueOf<M> {
type Target = ReflectReference;
fn deref(&self) -> &ReflectReference {
&self.0
}
}
impl<M> FromScript for ValueOf<M> {
type This<'w> = Self;
fn from_script(value: ScriptValue, scope: &CallScope) -> Result<Self, InteropError> {
ReflectReference::from_script(value, scope).map(ValueOf::new)
}
}
impl<M> IntoScript for ValueOf<M> {
fn into_script(self, world: WorldGuard) -> Result<ScriptValue, InteropError> {
self.0.into_script(world)
}
}
impl<M> ArgMeta for ValueOf<M> {}
impl<M> GetTypeDependencies for ValueOf<M> {
type Underlying = ReflectReference;
fn register_type_dependencies(registry: &mut TypeRegistry) {
ReflectReference::register_type_dependencies(registry)
}
}
impl<M: Typed> TypedThrough for ValueOf<M> {
fn through_type_info() -> ThroughTypeInfo {
ThroughTypeInfo::Val(M::type_info())
}
}
#[derive(Clone, Debug)]
pub struct GenericTypeDeclaration {
pub type_info: &'static TypeInfo,
pub namespace: &'static str,
pub params: Vec<&'static str>,
pub documentation: &'static str,
}
#[derive(Resource, Default, Clone, Debug)]
pub struct AppGenericTypes(pub Vec<GenericTypeDeclaration>);
pub trait GenericTypesAppExt {
fn register_generic_type<G: Typed>(
&mut self,
namespace: &'static str,
params: &[&'static str],
documentation: &'static str,
) -> &mut Self;
}
impl GenericTypesAppExt for App {
fn register_generic_type<G: Typed>(
&mut self,
namespace: &'static str,
params: &[&'static str],
documentation: &'static str,
) -> &mut Self {
self.register_type::<A>()
.register_type::<B>()
.register_type::<C>();
self.world_mut()
.get_resource_or_init::<AppGenericTypes>()
.0
.push(GenericTypeDeclaration {
type_info: G::type_info(),
namespace,
params: params.to_vec(),
documentation,
});
self
}
}