2024-04-01 15:17:19 +00:00
|
|
|
use std::{any::{Any, TypeId}, ptr::NonNull};
|
2024-01-20 05:54:36 +00:00
|
|
|
|
2024-03-31 02:42:41 +00:00
|
|
|
use lyra_ecs::{AtomicRef, AtomicRefMut, ResourceObject, World};
|
2024-01-20 05:54:36 +00:00
|
|
|
|
|
|
|
use crate::{Reflect, FromType};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ReflectedResource {
|
|
|
|
pub type_id: TypeId,
|
|
|
|
|
2024-03-31 02:42:41 +00:00
|
|
|
fn_reflect: for<'a> fn (world: &'a World) -> Option<AtomicRef<'a, dyn Reflect>>,
|
|
|
|
fn_reflect_mut: for<'a> fn (world: &'a mut World) -> Option<AtomicRefMut<'a, dyn Reflect>>,
|
2024-01-20 05:54:36 +00:00
|
|
|
fn_reflect_ptr: fn (world: &mut World) -> Option<NonNull<u8>>,
|
2024-02-25 22:06:53 +00:00
|
|
|
fn_refl_insert: fn (world: &mut World, this: Box<dyn Reflect>),
|
2024-01-20 05:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ReflectedResource {
|
|
|
|
/// Retrieves the reflected resource from the world.
|
2024-03-31 02:42:41 +00:00
|
|
|
pub fn reflect<'a>(&self, world: &'a World) -> Option<AtomicRef<'a, dyn Reflect>> {
|
2024-01-20 05:54:36 +00:00
|
|
|
(self.fn_reflect)(world)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves a mutable reflected resource from the world.
|
2024-03-31 02:42:41 +00:00
|
|
|
pub fn reflect_mut<'a>(&self, world: &'a mut World) -> Option<AtomicRefMut<'a, dyn Reflect>> {
|
2024-01-20 05:54:36 +00:00
|
|
|
(self.fn_reflect_mut)(world)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reflect_ptr(&self, world: &mut World) -> Option<NonNull<u8>> {
|
|
|
|
(self.fn_reflect_ptr)(world)
|
|
|
|
}
|
2024-02-25 22:06:53 +00:00
|
|
|
|
|
|
|
/// Insert the resource into the world.
|
|
|
|
pub fn insert(&self, world: &mut World, this: Box<dyn Reflect>) {
|
|
|
|
(self.fn_refl_insert)(world, this)
|
|
|
|
}
|
2024-01-20 05:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: ResourceObject + Reflect> FromType<T> for ReflectedResource {
|
|
|
|
fn from_type() -> Self {
|
|
|
|
Self {
|
|
|
|
type_id: TypeId::of::<T>(),
|
|
|
|
fn_reflect: |world: &World| {
|
2024-09-28 01:03:27 +00:00
|
|
|
world.get_resource::<T>()
|
2024-03-31 02:42:41 +00:00
|
|
|
.map(|r| {
|
2024-09-28 01:03:27 +00:00
|
|
|
// TODO: figure out change tracking for reflected resource
|
|
|
|
let r = r.get_inner();
|
2024-03-31 02:42:41 +00:00
|
|
|
AtomicRef::map(r, |r| r as &dyn Reflect)
|
|
|
|
})
|
2024-01-20 05:54:36 +00:00
|
|
|
},
|
|
|
|
fn_reflect_mut: |world: &mut World| {
|
2024-09-28 01:03:27 +00:00
|
|
|
world.get_resource_mut::<T>()
|
2024-03-31 02:42:41 +00:00
|
|
|
.map(|r| {
|
2024-09-28 01:03:27 +00:00
|
|
|
// TODO: figure out change tracking for reflected resource
|
|
|
|
let r = r.get_inner();
|
2024-03-31 02:42:41 +00:00
|
|
|
AtomicRefMut::map(r, |r| r as &mut dyn Reflect)
|
|
|
|
})
|
2024-01-20 05:54:36 +00:00
|
|
|
},
|
|
|
|
fn_reflect_ptr: |world: &mut World| unsafe {
|
2024-09-28 01:03:27 +00:00
|
|
|
world.get_resource_ptr::<T>()
|
2024-01-20 05:54:36 +00:00
|
|
|
.map(|ptr| ptr.cast::<u8>())
|
|
|
|
},
|
2024-02-25 22:06:53 +00:00
|
|
|
fn_refl_insert: |world: &mut World, this: Box<dyn Reflect>| {
|
|
|
|
let res = this as Box<dyn Any>;
|
|
|
|
let res = res.downcast::<T>()
|
|
|
|
.expect("Provided a non-matching type to ReflectedResource insert method!");
|
|
|
|
let res = *res;
|
|
|
|
world.add_resource(res);
|
|
|
|
}
|
2024-01-20 05:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|