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

Make bevy_app and reflect opt-out for bevy_hierarchy. #10721

Merged
merged 2 commits into from
Nov 25, 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
15 changes: 8 additions & 7 deletions crates/bevy_hierarchy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ license = "MIT OR Apache-2.0"
keywords = ["bevy"]

[features]
default = ["bevy_app"]
trace = []
bevy_app = ["reflect", "dep:bevy_app", "bevy_core", "bevy_log"]
reflect = ["bevy_ecs/bevy_reflect", "bevy_reflect"]

[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.12.0" }
bevy_core = { path = "../bevy_core", version = "0.12.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.12.0", features = [
"bevy_reflect",
] }
bevy_log = { path = "../bevy_log", version = "0.12.0" }
bevy_app = { path = "../bevy_app", version = "0.12.0", optional = true }
bevy_core = { path = "../bevy_core", version = "0.12.0", optional = true }
bevy_ecs = { path = "../bevy_ecs", version = "0.12.0", default-features = false }
bevy_log = { path = "../bevy_log", version = "0.12.0", optional = true }
bevy_reflect = { path = "../bevy_reflect", version = "0.12.0", features = [
"bevy",
] }
], optional = true }
bevy_utils = { path = "../bevy_utils", version = "0.12.0" }

# other
Expand Down
9 changes: 5 additions & 4 deletions crates/bevy_hierarchy/src/components/children.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[cfg(feature = "reflect")]
use bevy_ecs::reflect::{ReflectComponent, ReflectMapEntities};
use bevy_ecs::{
component::Component,
entity::{Entity, EntityMapper, MapEntities},
prelude::FromWorld,
reflect::{ReflectComponent, ReflectMapEntities},
world::World,
};
use bevy_reflect::Reflect;
use core::slice;
use smallvec::SmallVec;
use std::ops::Deref;
Expand All @@ -23,8 +23,9 @@ use std::ops::Deref;
/// [`Query`]: bevy_ecs::system::Query
/// [`Parent`]: crate::components::parent::Parent
/// [`BuildChildren::with_children`]: crate::child_builder::BuildChildren::with_children
#[derive(Component, Debug, Reflect)]
#[reflect(Component, MapEntities)]
#[derive(Component, Debug)]
#[cfg_attr(feature = "reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "reflect", reflect(Component, MapEntities))]
pub struct Children(pub(crate) SmallVec<[Entity; 8]>);

impl MapEntities for Children {
Expand Down
9 changes: 5 additions & 4 deletions crates/bevy_hierarchy/src/components/parent.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#[cfg(feature = "reflect")]
use bevy_ecs::reflect::{ReflectComponent, ReflectMapEntities};
use bevy_ecs::{
component::Component,
entity::{Entity, EntityMapper, MapEntities},
reflect::{ReflectComponent, ReflectMapEntities},
world::{FromWorld, World},
};
use bevy_reflect::Reflect;
use std::ops::Deref;

/// Holds a reference to the parent entity of this entity.
Expand All @@ -20,8 +20,9 @@ use std::ops::Deref;
/// [`Query`]: bevy_ecs::system::Query
/// [`Children`]: super::children::Children
/// [`BuildChildren::with_children`]: crate::child_builder::BuildChildren::with_children
#[derive(Component, Debug, Eq, PartialEq, Reflect)]
#[reflect(Component, MapEntities, PartialEq)]
#[derive(Component, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "reflect", reflect(Component, MapEntities, PartialEq))]
pub struct Parent(pub(crate) Entity);

impl Parent {
Expand Down
11 changes: 7 additions & 4 deletions crates/bevy_hierarchy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ pub use query_extension::*;
#[doc(hidden)]
pub mod prelude {
#[doc(hidden)]
pub use crate::{
child_builder::*, components::*, hierarchy::*, query_extension::*, HierarchyPlugin,
ValidParentCheckPlugin,
};
pub use crate::{child_builder::*, components::*, hierarchy::*, query_extension::*};

#[doc(hidden)]
#[cfg(feature = "bevy_app")]
pub use crate::{HierarchyPlugin, ValidParentCheckPlugin};
}

#[cfg(feature = "bevy_app")]
use bevy_app::prelude::*;

/// The base plugin for handling [`Parent`] and [`Children`] components
#[derive(Default)]
pub struct HierarchyPlugin;

#[cfg(feature = "bevy_app")]
impl Plugin for HierarchyPlugin {
fn build(&self, app: &mut App) {
app.register_type::<Children>()
Expand Down
20 changes: 10 additions & 10 deletions crates/bevy_hierarchy/src/valid_parent_check_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::marker::PhantomData;

use bevy_app::{App, Last, Plugin};
use bevy_core::Name;
#[cfg(feature = "bevy_app")]
use crate::Parent;
use bevy_ecs::prelude::*;
use bevy_log::warn;
#[cfg(feature = "bevy_app")]
use bevy_utils::{get_short_name, HashSet};

use crate::Parent;

/// When enabled, runs [`check_hierarchy_component_has_valid_parent<T>`].
///
/// This resource is added by [`ValidParentCheckPlugin<T>`].
Expand Down Expand Up @@ -45,6 +43,7 @@ impl<T> Default for ReportHierarchyIssue<T> {
}
}

#[cfg(feature = "bevy_app")]
/// System to print a warning for each [`Entity`] with a `T` component
/// which parent hasn't a `T` component.
///
Expand All @@ -55,7 +54,7 @@ impl<T> Default for ReportHierarchyIssue<T> {
/// (See B0004 explanation linked in warning message)
pub fn check_hierarchy_component_has_valid_parent<T: Component>(
parent_query: Query<
(Entity, &Parent, Option<&Name>),
(Entity, &Parent, Option<&bevy_core::Name>),
(With<T>, Or<(Changed<Parent>, Added<T>)>),
>,
component_query: Query<(), With<T>>,
Expand All @@ -65,7 +64,7 @@ pub fn check_hierarchy_component_has_valid_parent<T: Component>(
let parent = parent.get();
if !component_query.contains(parent) && !already_diagnosed.contains(&entity) {
already_diagnosed.insert(entity);
warn!(
bevy_log::warn!(
"warning[B0004]: {name} with the {ty_name} component has a parent without {ty_name}.\n\
This will cause inconsistent behaviors! See https://bevyengine.org/learn/errors/#b0004",
ty_name = get_short_name(std::any::type_name::<T>()),
Expand Down Expand Up @@ -94,10 +93,11 @@ impl<T: Component> Default for ValidParentCheckPlugin<T> {
}
}

impl<T: Component> Plugin for ValidParentCheckPlugin<T> {
fn build(&self, app: &mut App) {
#[cfg(feature = "bevy_app")]
impl<T: Component> bevy_app::Plugin for ValidParentCheckPlugin<T> {
fn build(&self, app: &mut bevy_app::App) {
app.init_resource::<ReportHierarchyIssue<T>>().add_systems(
Last,
bevy_app::Last,
check_hierarchy_component_has_valid_parent::<T>
.run_if(resource_equals(ReportHierarchyIssue::<T>::new(true))),
);
Expand Down
Loading