Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move AppTypeRegistry to bevy_ecs #8901

Merged
merged 2 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ bevy_utils::define_label!(
AppLabelId,
);

/// The [`Resource`] that stores the [`App`]'s [`TypeRegistry`](bevy_reflect::TypeRegistry).
#[cfg(feature = "bevy_reflect")]
#[derive(Resource, Clone, bevy_derive::Deref, bevy_derive::DerefMut, Default)]
pub struct AppTypeRegistry(pub bevy_reflect::TypeRegistryArc);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we default a world to include this resource (under bevy_reflect feature)? Or is it still the responsibility of the user/bevy_app to initialize it?

I'm guessing the latter, but just want to clarify.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an interesting proposition. I like the idea. Though I'm curious of the implications with regard to Scenes for example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid automatically inserting resources whenever possible :)


pub(crate) enum AppError {
DuplicatePlugin { plugin_name: String },
}
Expand Down
3 changes: 0 additions & 3 deletions crates/bevy_app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ pub use schedule_runner::*;

#[allow(missing_docs)]
pub mod prelude {
#[cfg(feature = "bevy_reflect")]
#[doc(hidden)]
pub use crate::AppTypeRegistry;
#[doc(hidden)]
pub use crate::{
app::App,
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use crate::{
update_asset_storage_system, Asset, AssetEvents, AssetLoader, AssetServer, Handle, HandleId,
LoadAssets, RefChange, ReflectAsset, ReflectHandle,
};
use bevy_app::{App, AppTypeRegistry};
use bevy_app::App;
use bevy_ecs::prelude::*;
use bevy_ecs::reflect::AppTypeRegistry;
use bevy_reflect::{FromReflect, GetTypeRegistration, Reflect};
use bevy_utils::HashMap;
use crossbeam_channel::Sender;
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_asset/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ impl<A: Asset> FromType<Handle<A>> for ReflectHandle {
mod tests {
use std::any::TypeId;

use bevy_app::{App, AppTypeRegistry};
use bevy_app::App;
use bevy_ecs::reflect::AppTypeRegistry;
use bevy_reflect::{FromReflect, Reflect, ReflectMut, TypeUuid};

use crate::{AddAsset, AssetPlugin, HandleUntyped, ReflectAsset};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub use bevy_ptr as ptr;
pub mod prelude {
#[doc(hidden)]
#[cfg(feature = "bevy_reflect")]
pub use crate::reflect::{ReflectComponent, ReflectResource};
pub use crate::reflect::{AppTypeRegistry, ReflectComponent, ReflectResource};
#[doc(hidden)]
pub use crate::{
bundle::Bundle,
Expand Down
27 changes: 26 additions & 1 deletion crates/bevy_ecs/src/reflect/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//! Types that enable reflection support.

use crate::entity::Entity;
use std::ops::{Deref, DerefMut};

use crate as bevy_ecs;
use crate::{entity::Entity, system::Resource};
use bevy_reflect::{
impl_from_reflect_value, impl_reflect_value, ReflectDeserialize, ReflectSerialize,
TypeRegistryArc,
};

mod component;
Expand All @@ -13,5 +17,26 @@ pub use component::{ReflectComponent, ReflectComponentFns};
pub use map_entities::ReflectMapEntities;
pub use resource::{ReflectResource, ReflectResourceFns};

/// A [`Resource`] storing [`TypeRegistry`](bevy_reflect::TypeRegistry) for
/// type registrations relevant to a whole app.
#[derive(Resource, Clone, Default)]
pub struct AppTypeRegistry(pub TypeRegistryArc);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could consider calling this WorldTypeRegistry since we're moving it to ECS and it should exist in the world. But we can also hold off on that for a future PR.

Copy link
Contributor Author

@nicopap nicopap Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to include it in this PR. Changing the name is way more breaking than changing the crate it's defined in, as people will usually import it through prelude::*. Basically most users wouldn't need to change a line with this. While renaming it would require a bit more work. I think it's the kind of change that deserves a bit more scrutiny, even if I'm for it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!


impl Deref for AppTypeRegistry {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QQ: These won't mess with cloning right? Does this still work?

fn system(registry: Res<AppTypeRegistry>) {
  let registry2: AppTypeRegistry = registry.into_inner().clone();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was implementing Deref and DerefMut through the bevy_derive::Deref{,Mut} macro before. So it should reproduce the same behavior. The macro basically writes this code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I missed that part. Awesome, sounds good!

type Target = TypeRegistryArc;

#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for AppTypeRegistry {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl_reflect_value!((in bevy_ecs) Entity(Hash, PartialEq, Serialize, Deserialize));
impl_from_reflect_value!(Entity);
6 changes: 2 additions & 4 deletions crates/bevy_scene/src/dynamic_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::any::TypeId;

use crate::{DynamicSceneBuilder, Scene, SceneSpawnError};
use anyhow::Result;
use bevy_app::AppTypeRegistry;
use bevy_ecs::{
entity::{Entity, EntityMap},
reflect::{ReflectComponent, ReflectMapEntities},
reflect::{AppTypeRegistry, ReflectComponent, ReflectMapEntities},
world::World,
};
use bevy_reflect::{Reflect, TypePath, TypeRegistryArc, TypeUuid};
Expand Down Expand Up @@ -185,8 +184,7 @@ where

#[cfg(test)]
mod tests {
use bevy_app::AppTypeRegistry;
use bevy_ecs::{entity::EntityMap, system::Command, world::World};
use bevy_ecs::{entity::EntityMap, reflect::AppTypeRegistry, system::Command, world::World};
use bevy_hierarchy::{AddChild, Parent};

use crate::dynamic_scene_builder::DynamicSceneBuilder;
Expand Down
13 changes: 6 additions & 7 deletions crates/bevy_scene/src/dynamic_scene_builder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::{DynamicEntity, DynamicScene};
use bevy_app::AppTypeRegistry;
use bevy_ecs::component::ComponentId;
use bevy_ecs::{
prelude::Entity,
reflect::{ReflectComponent, ReflectResource},
reflect::{AppTypeRegistry, ReflectComponent, ReflectResource},
world::World,
};
use bevy_reflect::Reflect;
Expand All @@ -21,7 +20,7 @@ use std::collections::BTreeMap;
/// # Example
/// ```
/// # use bevy_scene::DynamicSceneBuilder;
/// # use bevy_app::AppTypeRegistry;
/// # use bevy_ecs::reflect::AppTypeRegistry;
/// # use bevy_ecs::{
/// # component::Component, prelude::Entity, query::With, reflect::ReflectComponent, world::World,
/// # };
Expand Down Expand Up @@ -101,7 +100,7 @@ impl<'w> DynamicSceneBuilder<'w> {
/// Extracting entities can be used to extract entities from a query:
/// ```
/// # use bevy_scene::DynamicSceneBuilder;
/// # use bevy_app::AppTypeRegistry;
/// # use bevy_ecs::reflect::AppTypeRegistry;
/// # use bevy_ecs::{
/// # component::Component, prelude::Entity, query::With, reflect::ReflectComponent, world::World,
/// # };
Expand Down Expand Up @@ -162,7 +161,7 @@ impl<'w> DynamicSceneBuilder<'w> {
/// Re-extracting a resource that was already extracted will have no effect.
/// ```
/// # use bevy_scene::DynamicSceneBuilder;
/// # use bevy_app::AppTypeRegistry;
/// # use bevy_ecs::reflect::AppTypeRegistry;
/// # use bevy_ecs::prelude::{ReflectResource, Resource, World};
/// # use bevy_reflect::Reflect;
/// #[derive(Resource, Default, Reflect)]
Expand Down Expand Up @@ -204,10 +203,10 @@ impl<'w> DynamicSceneBuilder<'w> {

#[cfg(test)]
mod tests {
use bevy_app::AppTypeRegistry;
use bevy_ecs::{
component::Component, prelude::Entity, prelude::Resource, query::With,
reflect::ReflectComponent, reflect::ReflectResource, world::World,
reflect::AppTypeRegistry, reflect::ReflectComponent, reflect::ReflectResource,
world::World,
};

use bevy_reflect::Reflect;
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_scene/src/scene.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use bevy_app::AppTypeRegistry;
use bevy_ecs::{
entity::EntityMap,
reflect::{ReflectComponent, ReflectMapEntities, ReflectResource},
reflect::{AppTypeRegistry, ReflectComponent, ReflectMapEntities, ReflectResource},
world::World,
};
use bevy_reflect::{TypePath, TypeUuid};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_scene/src/scene_loader.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[cfg(feature = "serialize")]
use crate::serde::SceneDeserializer;
use anyhow::{anyhow, Result};
use bevy_app::AppTypeRegistry;
use bevy_asset::{AssetLoader, LoadContext, LoadedAsset};
use bevy_ecs::reflect::AppTypeRegistry;
use bevy_ecs::world::{FromWorld, World};
use bevy_reflect::TypeRegistryArc;
use bevy_utils::BoxedFuture;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{DynamicScene, Scene};
use bevy_app::AppTypeRegistry;
use bevy_asset::{AssetEvent, Assets, Handle};
use bevy_ecs::{
entity::{Entity, EntityMap},
event::{Events, ManualEventReader},
reflect::AppTypeRegistry,
system::{Command, Resource},
world::{Mut, World},
};
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_scene/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,10 @@ impl<'a, 'de> Visitor<'de> for SceneMapVisitor<'a> {
mod tests {
use crate::serde::{SceneDeserializer, SceneSerializer};
use crate::{DynamicScene, DynamicSceneBuilder};
use bevy_app::AppTypeRegistry;
use bevy_ecs::entity::{Entity, EntityMap, EntityMapper, MapEntities};
use bevy_ecs::prelude::{Component, ReflectComponent, ReflectResource, Resource, World};
use bevy_ecs::query::{With, Without};
use bevy_ecs::reflect::ReflectMapEntities;
use bevy_ecs::reflect::{AppTypeRegistry, ReflectMapEntities};
use bevy_ecs::world::FromWorld;
use bevy_reflect::{FromReflect, Reflect, ReflectSerialize};
use bincode::Options;
Expand Down