# Objective
- Followup to #7184.
- ~Deprecate `TypeUuid` and remove its internal references.~ No longer
part of this PR.
- Use `TypePath` for the type registry, and (de)serialisation instead of
`std::any::type_name`.
- Allow accessing type path information behind proxies.
## Solution
- Introduce methods on `TypeInfo` and friends for dynamically querying
type path. These methods supersede the old `type_name` methods.
- Remove `Reflect::type_name` in favor of `DynamicTypePath::type_path`
and `TypeInfo::type_path_table`.
- Switch all uses of `std::any::type_name` in reflection, non-debugging
contexts to use `TypePath`.
---
## Changelog
- Added `TypePathTable` for dynamically accessing methods on `TypePath`
through `TypeInfo` and the type registry.
- Removed `type_name` from all `TypeInfo`-like structs.
- Added `type_path` and `type_path_table` methods to all `TypeInfo`-like
structs.
- Removed `Reflect::type_name` in favor of
`DynamicTypePath::reflect_type_path` and `TypeInfo::type_path`.
- Changed the signature of all `DynamicTypePath` methods to return
strings with a static lifetime.
## Migration Guide
- Rely on `TypePath` instead of `std::any::type_name` for all stability
guarantees and for use in all reflection contexts, this is used through
with one of the following APIs:
- `TypePath::type_path` if you have a concrete type and not a value.
- `DynamicTypePath::reflect_type_path` if you have an `dyn Reflect`
value without a concrete type.
- `TypeInfo::type_path` for use through the registry or if you want to
work with the represented type of a `DynamicFoo`.
- Remove `type_name` from manual `Reflect` implementations.
- Use `type_path` and `type_path_table` in place of `type_name` on
`TypeInfo`-like structs.
- Use `get_with_type_path(_mut)` over `get_with_type_name(_mut)`.
## Note to reviewers
I think if anything we were a little overzealous in merging #7184 and we
should take that extra care here.
In my mind, this is the "point of no return" for `TypePath` and while I
think we all agree on the design, we should carefully consider if the
finer details and current implementations are actually how we want them
moving forward.
For example [this incorrect `TypePath` implementation for
`String`](3fea3c6c0b/crates/bevy_reflect/src/impls/std.rs (L90))
(note that `String` is in the default Rust prelude) snuck in completely
under the radar.
134 lines
5.3 KiB
Rust
134 lines
5.3 KiB
Rust
use crate::{DynamicScene, InstanceInfo, SceneSpawnError};
|
|
use bevy_asset::Asset;
|
|
use bevy_ecs::{
|
|
reflect::{AppTypeRegistry, ReflectComponent, ReflectMapEntities, ReflectResource},
|
|
world::World,
|
|
};
|
|
use bevy_reflect::TypePath;
|
|
use bevy_utils::HashMap;
|
|
|
|
/// 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(Asset, TypePath, Debug)]
|
|
pub struct Scene {
|
|
/// The world of the scene, containing its entities and resources.
|
|
pub world: World,
|
|
}
|
|
|
|
impl Scene {
|
|
/// Creates a new scene with the given world.
|
|
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 = HashMap::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: HashMap::default(),
|
|
};
|
|
|
|
let type_registry = type_registry.read();
|
|
|
|
// Resources archetype
|
|
for (component_id, resource_data) in self.world.storages().resources.iter() {
|
|
if !resource_data.is_present() {
|
|
continue;
|
|
}
|
|
|
|
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 {
|
|
std_type_name: component_info.name().to_string(),
|
|
})?;
|
|
let reflect_resource = registration.data::<ReflectResource>().ok_or_else(|| {
|
|
SceneSpawnError::UnregisteredResource {
|
|
type_path: registration.type_info().type_path().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 {
|
|
std_type_name: component_info.name().to_string(),
|
|
})
|
|
.and_then(|registration| {
|
|
registration.data::<ReflectComponent>().ok_or_else(|| {
|
|
SceneSpawnError::UnregisteredComponent {
|
|
type_path: registration.type_info().type_path().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)
|
|
}
|
|
}
|