# Objective
- Introduce a stable alternative to
[`std::any::type_name`](https://doc.rust-lang.org/std/any/fn.type_name.html).
- Rewrite of #5805 with heavy inspiration in design.
- On the path to #5830.
- Part of solving #3327.
## Solution
- Add a `TypePath` trait for static stable type path/name information.
- Add a `TypePath` derive macro.
- Add a `impl_type_path` macro for implementing internal and foreign
types in `bevy_reflect`.
---
## Changelog
- Added `TypePath` trait.
- Added `DynamicTypePath` trait and `get_type_path` method to `Reflect`.
- Added a `TypePath` derive macro.
- Added a `bevy_reflect::impl_type_path` for implementing `TypePath` on
internal and foreign types in `bevy_reflect`.
- Changed `bevy_reflect::utility::(Non)GenericTypeInfoCell` to
`(Non)GenericTypedCell<T>` which allows us to be generic over both
`TypeInfo` and `TypePath`.
- `TypePath` is now a supertrait of `Asset`, `Material` and
`Material2d`.
- `impl_reflect_struct` needs a `#[type_path = "..."]` attribute to be
specified.
- `impl_reflect_value` needs to either specify path starting with a
double colon (`::core::option::Option`) or an `in my_crate::foo`
declaration.
- Added `bevy_reflect_derive::ReflectTypePath`.
- Most uses of `Ident` in `bevy_reflect_derive` changed to use
`ReflectTypePath`.
## Migration Guide
- Implementors of `Asset`, `Material` and `Material2d` now also need to
derive `TypePath`.
- Manual implementors of `Reflect` will need to implement the new
`get_type_path` method.
## Open Questions
- [x] ~This PR currently does not migrate any usages of
`std::any::type_name` to use `bevy_reflect::TypePath` to ease the review
process. Should it?~ Migration will be left to a follow-up PR.
- [ ] This PR adds a lot of `#[derive(TypePath)]` and `T: TypePath` to
satisfy new bounds, mostly when deriving `TypeUuid`. Should we make
`TypePath` a supertrait of `TypeUuid`? [Should we remove `TypeUuid` in
favour of
`TypePath`?](2afbd85532 (r961067892))
130 lines
5.1 KiB
Rust
130 lines
5.1 KiB
Rust
use bevy_app::AppTypeRegistry;
|
|
use bevy_ecs::{
|
|
entity::EntityMap,
|
|
reflect::{ReflectComponent, ReflectMapEntities, ReflectResource},
|
|
world::World,
|
|
};
|
|
use bevy_reflect::{TypePath, TypeUuid};
|
|
|
|
use crate::{DynamicScene, InstanceInfo, SceneSpawnError};
|
|
|
|
/// To spawn a scene, you can use either:
|
|
/// * [`SceneSpawner::spawn`](crate::SceneSpawner::spawn)
|
|
/// * adding the [`SceneBundle`](crate::SceneBundle) to an entity
|
|
/// * adding the [`Handle<Scene>`](bevy_asset::Handle) to an entity (the scene will only be
|
|
/// visible if the entity already has [`Transform`](bevy_transform::components::Transform) and
|
|
/// [`GlobalTransform`](bevy_transform::components::GlobalTransform) components)
|
|
#[derive(Debug, TypeUuid, TypePath)]
|
|
#[uuid = "c156503c-edd9-4ec7-8d33-dab392df03cd"]
|
|
pub struct Scene {
|
|
pub world: World,
|
|
}
|
|
|
|
impl Scene {
|
|
pub fn new(world: World) -> Self {
|
|
Self { world }
|
|
}
|
|
|
|
/// Create a new scene from a given dynamic scene.
|
|
pub fn from_dynamic_scene(
|
|
dynamic_scene: &DynamicScene,
|
|
type_registry: &AppTypeRegistry,
|
|
) -> Result<Scene, SceneSpawnError> {
|
|
let mut world = World::new();
|
|
let mut entity_map = EntityMap::default();
|
|
dynamic_scene.write_to_world_with(&mut world, &mut entity_map, type_registry)?;
|
|
|
|
Ok(Self { world })
|
|
}
|
|
|
|
/// Clone the scene.
|
|
///
|
|
/// This method will return a [`SceneSpawnError`] if a type either is not registered in the
|
|
/// provided [`AppTypeRegistry`] or doesn't reflect the [`Component`](bevy_ecs::component::Component) trait.
|
|
pub fn clone_with(&self, type_registry: &AppTypeRegistry) -> Result<Scene, SceneSpawnError> {
|
|
let mut new_world = World::new();
|
|
self.write_to_world_with(&mut new_world, type_registry)?;
|
|
Ok(Self { world: new_world })
|
|
}
|
|
|
|
/// Write the entities and their corresponding components to the given world.
|
|
///
|
|
/// This method will return a [`SceneSpawnError`] if a type either is not registered in the
|
|
/// provided [`AppTypeRegistry`] or doesn't reflect the [`Component`](bevy_ecs::component::Component) trait.
|
|
pub fn write_to_world_with(
|
|
&self,
|
|
world: &mut World,
|
|
type_registry: &AppTypeRegistry,
|
|
) -> Result<InstanceInfo, SceneSpawnError> {
|
|
let mut instance_info = InstanceInfo {
|
|
entity_map: EntityMap::default(),
|
|
};
|
|
|
|
let type_registry = type_registry.read();
|
|
|
|
// Resources archetype
|
|
for (component_id, _) in self.world.storages().resources.iter() {
|
|
let component_info = self
|
|
.world
|
|
.components()
|
|
.get_info(component_id)
|
|
.expect("component_ids in archetypes should have ComponentInfo");
|
|
|
|
let type_id = component_info
|
|
.type_id()
|
|
.expect("reflected resources must have a type_id");
|
|
|
|
let registration =
|
|
type_registry
|
|
.get(type_id)
|
|
.ok_or_else(|| SceneSpawnError::UnregisteredType {
|
|
type_name: component_info.name().to_string(),
|
|
})?;
|
|
let reflect_resource = registration.data::<ReflectResource>().ok_or_else(|| {
|
|
SceneSpawnError::UnregisteredResource {
|
|
type_name: component_info.name().to_string(),
|
|
}
|
|
})?;
|
|
reflect_resource.copy(&self.world, world);
|
|
}
|
|
|
|
for archetype in self.world.archetypes().iter() {
|
|
for scene_entity in archetype.entities() {
|
|
let entity = *instance_info
|
|
.entity_map
|
|
.entry(scene_entity.entity())
|
|
.or_insert_with(|| world.spawn_empty().id());
|
|
for component_id in archetype.components() {
|
|
let component_info = self
|
|
.world
|
|
.components()
|
|
.get_info(component_id)
|
|
.expect("component_ids in archetypes should have ComponentInfo");
|
|
|
|
let reflect_component = type_registry
|
|
.get(component_info.type_id().unwrap())
|
|
.ok_or_else(|| SceneSpawnError::UnregisteredType {
|
|
type_name: component_info.name().to_string(),
|
|
})
|
|
.and_then(|registration| {
|
|
registration.data::<ReflectComponent>().ok_or_else(|| {
|
|
SceneSpawnError::UnregisteredComponent {
|
|
type_name: component_info.name().to_string(),
|
|
}
|
|
})
|
|
})?;
|
|
reflect_component.copy(&self.world, world, scene_entity.entity(), entity);
|
|
}
|
|
}
|
|
}
|
|
|
|
for registration in type_registry.iter() {
|
|
if let Some(map_entities_reflect) = registration.data::<ReflectMapEntities>() {
|
|
map_entities_reflect.map_all_entities(world, &mut instance_info.entity_map);
|
|
}
|
|
}
|
|
|
|
Ok(instance_info)
|
|
}
|
|
}
|