From bed0f261b971af7cd6c5219cc56e06ba0841d67b Mon Sep 17 00:00:00 2001 From: Mincho Paskalev Date: Tue, 2 May 2023 17:32:51 +0300 Subject: [PATCH 1/5] Add Reflect and FromReflect for AssetPath --- crates/bevy_asset/src/path.rs | 4 +- crates/bevy_reflect/src/impls/std.rs | 108 +++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) diff --git a/crates/bevy_asset/src/path.rs b/crates/bevy_asset/src/path.rs index e9150c63a8834..0cf5a30e14cb3 100644 --- a/crates/bevy_asset/src/path.rs +++ b/crates/bevy_asset/src/path.rs @@ -1,4 +1,4 @@ -use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize}; +use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize, FromReflect}; use bevy_utils::AHasher; use serde::{Deserialize, Serialize}; use std::{ @@ -8,7 +8,7 @@ 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)] pub struct AssetPath<'a> { path: Cow<'a, Path>, label: Option>, diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index db8baa3d9d768..bef3dc2e6f131 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -1169,6 +1169,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) + } + } +} + +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 crate::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 + } +} + #[cfg(test)] mod tests { use crate as bevy_reflect; From 7be0f6366c1e62c0ac6c504480ee96e557bb5a40 Mon Sep 17 00:00:00 2001 From: Mincho Paskalev Date: Tue, 2 May 2023 17:39:22 +0300 Subject: [PATCH 2/5] Fix lint errors --- crates/bevy_asset/src/path.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_asset/src/path.rs b/crates/bevy_asset/src/path.rs index 0cf5a30e14cb3..0081093ba4398 100644 --- a/crates/bevy_asset/src/path.rs +++ b/crates/bevy_asset/src/path.rs @@ -1,4 +1,4 @@ -use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize, FromReflect}; +use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize, ReflectSerialize}; use bevy_utils::AHasher; use serde::{Deserialize, Serialize}; use std::{ From 4364841a4cc772792d756fc576041a189c825f1d Mon Sep 17 00:00:00 2001 From: Mincho Paskalev Date: Thu, 4 May 2023 19:33:00 +0300 Subject: [PATCH 3/5] Add type registration for AssetPath Implement Reflect::debug for Cow<'static, Path> Style fixes --- crates/bevy_asset/src/path.rs | 3 ++- crates/bevy_reflect/src/impls/std.rs | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/crates/bevy_asset/src/path.rs b/crates/bevy_asset/src/path.rs index 0081093ba4398..e3a605cdcb5e2 100644 --- a/crates/bevy_asset/src/path.rs +++ b/crates/bevy_asset/src/path.rs @@ -1,4 +1,4 @@ -use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize, ReflectSerialize}; +use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize, ReflectSerialize, ReflectFromReflect}; use bevy_utils::AHasher; use serde::{Deserialize, Serialize}; use std::{ @@ -9,6 +9,7 @@ use std::{ /// Represents a path to an asset in the file system. #[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_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index bef3dc2e6f131..6e420c0780362 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, ReflectOwned, ReflectFromReflect}; 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, @@ -1247,6 +1248,10 @@ impl Reflect for Cow<'static, Path> { Some(false) } } + + fn debug(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fmt::Debug::fmt(&self, f) + } } impl Typed for Cow<'static, Path> { @@ -1257,7 +1262,7 @@ impl Typed for Cow<'static, Path> { } impl FromReflect for Cow<'static, Path> { - fn from_reflect(reflect: &dyn crate::Reflect) -> Option { + fn from_reflect(reflect: &dyn Reflect) -> Option { Some( reflect .as_any() @@ -1269,10 +1274,11 @@ impl FromReflect for Cow<'static, Path> { 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()); + 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 } } From bb8cb467ffb2c72bbc27fa0ecbb62006993c4707 Mon Sep 17 00:00:00 2001 From: Mincho Paskalev Date: Thu, 4 May 2023 19:35:26 +0300 Subject: [PATCH 4/5] Fix style --- crates/bevy_asset/src/path.rs | 4 +++- crates/bevy_reflect/src/impls/std.rs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/bevy_asset/src/path.rs b/crates/bevy_asset/src/path.rs index e3a605cdcb5e2..032c8550985b1 100644 --- a/crates/bevy_asset/src/path.rs +++ b/crates/bevy_asset/src/path.rs @@ -1,4 +1,6 @@ -use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize, ReflectSerialize, ReflectFromReflect}; +use bevy_reflect::{ + FromReflect, Reflect, ReflectDeserialize, ReflectFromReflect, ReflectSerialize, +}; use bevy_utils::AHasher; use serde::{Deserialize, Serialize}; use std::{ diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index 6e420c0780362..b6f6d6a9d8787 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, ReflectFromReflect}; +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, From 9c1dc9d5bc4e8871180b9320e16bc37c7f6132fc Mon Sep 17 00:00:00 2001 From: Mincho Paskalev Date: Thu, 4 May 2023 20:33:27 +0300 Subject: [PATCH 5/5] Register AssetPath in core and AssetPlugin Style changes --- crates/bevy_asset/src/lib.rs | 6 ++++-- crates/bevy_core/src/lib.rs | 3 ++- crates/bevy_reflect/src/impls/std.rs | 7 +------ 3 files changed, 7 insertions(+), 9 deletions(-) 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_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 b6f6d6a9d8787..77b2e8ad40717 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -1263,12 +1263,7 @@ impl Typed for Cow<'static, Path> { impl FromReflect for Cow<'static, Path> { fn from_reflect(reflect: &dyn Reflect) -> Option { - Some( - reflect - .as_any() - .downcast_ref::>()? - .clone(), - ) + Some(reflect.as_any().downcast_ref::()?.clone()) } }