diff --git a/crates/bevy_asset/src/lib.rs b/crates/bevy_asset/src/lib.rs index 1fa2f7d59fb22..7a444cfdc17fb 100644 --- a/crates/bevy_asset/src/lib.rs +++ b/crates/bevy_asset/src/lib.rs @@ -104,8 +104,10 @@ impl Plugin for AssetPlugin { app.insert_resource(asset_server); } - app.register_type::() - .add_systems(PreUpdate, asset_server::free_unused_assets_system); + app.register_type::(); + app.register_type::(); + + app.add_systems(PreUpdate, asset_server::free_unused_assets_system); app.init_schedule(LoadAssets); app.init_schedule(AssetEvents); diff --git a/crates/bevy_asset/src/path.rs b/crates/bevy_asset/src/path.rs index e9150c63a8834..032c8550985b1 100644 --- a/crates/bevy_asset/src/path.rs +++ b/crates/bevy_asset/src/path.rs @@ -1,4 +1,6 @@ -use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize}; +use bevy_reflect::{ + FromReflect, Reflect, ReflectDeserialize, ReflectFromReflect, ReflectSerialize, +}; use bevy_utils::AHasher; use serde::{Deserialize, Serialize}; use std::{ @@ -8,7 +10,8 @@ use std::{ }; /// Represents a path to an asset in the file system. -#[derive(Debug, Eq, PartialEq, Hash, Clone, Serialize, Deserialize)] +#[derive(Debug, Eq, PartialEq, Hash, Clone, Serialize, Deserialize, Reflect, FromReflect)] +#[reflect(Debug, PartialEq, Hash, Serialize, Deserialize, FromReflect)] pub struct AssetPath<'a> { path: Cow<'a, Path>, label: Option>, diff --git a/crates/bevy_core/src/lib.rs b/crates/bevy_core/src/lib.rs index daa81ad980e07..055e92c7dd3c6 100644 --- a/crates/bevy_core/src/lib.rs +++ b/crates/bevy_core/src/lib.rs @@ -28,7 +28,7 @@ use std::borrow::Cow; use std::ffi::OsString; use std::marker::PhantomData; use std::ops::Range; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; #[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))] @@ -59,6 +59,7 @@ fn register_rust_types(app: &mut App) { .register_type::>() .register_type::>() .register_type::>() + .register_type::>() .register_type::() .register_type::(); } diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index db8baa3d9d768..77b2e8ad40717 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -1,5 +1,5 @@ use crate::std_traits::ReflectDefault; -use crate::{self as bevy_reflect, ReflectFromPtr, ReflectOwned}; +use crate::{self as bevy_reflect, ReflectFromPtr, ReflectFromReflect, ReflectOwned}; use crate::{ map_apply, map_partial_eq, Array, ArrayInfo, ArrayIter, DynamicEnum, DynamicMap, Enum, EnumInfo, FromReflect, FromType, GetTypeRegistration, List, ListInfo, Map, MapInfo, MapIter, @@ -12,6 +12,7 @@ use crate::utility::{reflect_hasher, GenericTypeInfoCell, NonGenericTypeInfoCell use bevy_reflect_derive::{impl_from_reflect_value, impl_reflect_value}; use bevy_utils::HashSet; use bevy_utils::{Duration, Instant}; +use std::fmt; use std::{ any::Any, borrow::Cow, @@ -1169,6 +1170,114 @@ impl FromReflect for &'static Path { } } +impl Reflect for Cow<'static, Path> { + fn type_name(&self) -> &str { + std::any::type_name::() + } + + fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { + Some(::type_info()) + } + + fn into_any(self: Box) -> Box { + self + } + + fn as_any(&self) -> &dyn Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } + + fn into_reflect(self: Box) -> Box { + self + } + + fn as_reflect(&self) -> &dyn Reflect { + self + } + + fn as_reflect_mut(&mut self) -> &mut dyn Reflect { + self + } + + fn apply(&mut self, value: &dyn Reflect) { + let value = value.as_any(); + if let Some(value) = value.downcast_ref::() { + *self = value.clone(); + } else { + panic!("Value is not a {}.", std::any::type_name::()); + } + } + + fn set(&mut self, value: Box) -> Result<(), Box> { + *self = value.take()?; + Ok(()) + } + + fn reflect_ref(&self) -> ReflectRef { + ReflectRef::Value(self) + } + + fn reflect_mut(&mut self) -> ReflectMut { + ReflectMut::Value(self) + } + + fn reflect_owned(self: Box) -> ReflectOwned { + ReflectOwned::Value(self) + } + + fn clone_value(&self) -> Box { + Box::new(self.clone()) + } + + fn reflect_hash(&self) -> Option { + let mut hasher = reflect_hasher(); + Hash::hash(&std::any::Any::type_id(self), &mut hasher); + Hash::hash(self, &mut hasher); + Some(hasher.finish()) + } + + fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option { + let value = value.as_any(); + if let Some(value) = value.downcast_ref::() { + Some(std::cmp::PartialEq::eq(self, value)) + } else { + Some(false) + } + } + + fn debug(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fmt::Debug::fmt(&self, f) + } +} + +impl Typed for Cow<'static, Path> { + fn type_info() -> &'static TypeInfo { + static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new(); + CELL.get_or_set(|| TypeInfo::Value(ValueInfo::new::())) + } +} + +impl FromReflect for Cow<'static, Path> { + fn from_reflect(reflect: &dyn Reflect) -> Option { + Some(reflect.as_any().downcast_ref::()?.clone()) + } +} + +impl GetTypeRegistration for Cow<'static, Path> { + fn get_type_registration() -> TypeRegistration { + let mut registration = TypeRegistration::of::(); + registration.insert::(FromType::::from_type()); + registration.insert::(FromType::::from_type()); + registration.insert::(FromType::::from_type()); + registration.insert::(FromType::::from_type()); + registration + } +} + #[cfg(test)] mod tests { use crate as bevy_reflect;