From 7d5b29638918c67f05ee4353b9770a31000e098a Mon Sep 17 00:00:00 2001 From: mintlu8 Date: Fri, 24 Nov 2023 16:02:25 +0800 Subject: [PATCH] Make app and reflect opt-out for hierarchy. --- crates/bevy_hierarchy/Cargo.toml | 15 +++++++------- .../bevy_hierarchy/src/components/children.rs | 9 +++++---- .../bevy_hierarchy/src/components/parent.rs | 9 +++++---- crates/bevy_hierarchy/src/lib.rs | 11 ++++++---- .../src/valid_parent_check_plugin.rs | 20 +++++++++---------- 5 files changed, 35 insertions(+), 29 deletions(-) diff --git a/crates/bevy_hierarchy/Cargo.toml b/crates/bevy_hierarchy/Cargo.toml index 03b7dadf64ba9..47d492f3118c2 100644 --- a/crates/bevy_hierarchy/Cargo.toml +++ b/crates/bevy_hierarchy/Cargo.toml @@ -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 diff --git a/crates/bevy_hierarchy/src/components/children.rs b/crates/bevy_hierarchy/src/components/children.rs index 73a7aad7fbc76..b2186a1251a79 100644 --- a/crates/bevy_hierarchy/src/components/children.rs +++ b/crates/bevy_hierarchy/src/components/children.rs @@ -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; @@ -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 { diff --git a/crates/bevy_hierarchy/src/components/parent.rs b/crates/bevy_hierarchy/src/components/parent.rs index e9616391afb08..bffd3c28f3646 100644 --- a/crates/bevy_hierarchy/src/components/parent.rs +++ b/crates/bevy_hierarchy/src/components/parent.rs @@ -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. @@ -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 { diff --git a/crates/bevy_hierarchy/src/lib.rs b/crates/bevy_hierarchy/src/lib.rs index f156086b1a926..15d9d2fa425d6 100644 --- a/crates/bevy_hierarchy/src/lib.rs +++ b/crates/bevy_hierarchy/src/lib.rs @@ -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::() diff --git a/crates/bevy_hierarchy/src/valid_parent_check_plugin.rs b/crates/bevy_hierarchy/src/valid_parent_check_plugin.rs index bcabb3d957d73..764a4f6eea9aa 100644 --- a/crates/bevy_hierarchy/src/valid_parent_check_plugin.rs +++ b/crates/bevy_hierarchy/src/valid_parent_check_plugin.rs @@ -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`]. /// /// This resource is added by [`ValidParentCheckPlugin`]. @@ -45,6 +43,7 @@ impl Default for ReportHierarchyIssue { } } +#[cfg(feature = "bevy_app")] /// System to print a warning for each [`Entity`] with a `T` component /// which parent hasn't a `T` component. /// @@ -55,7 +54,7 @@ impl Default for ReportHierarchyIssue { /// (See B0004 explanation linked in warning message) pub fn check_hierarchy_component_has_valid_parent( parent_query: Query< - (Entity, &Parent, Option<&Name>), + (Entity, &Parent, Option<&bevy_core::Name>), (With, Or<(Changed, Added)>), >, component_query: Query<(), With>, @@ -65,7 +64,7 @@ pub fn check_hierarchy_component_has_valid_parent( 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::()), @@ -94,10 +93,11 @@ impl Default for ValidParentCheckPlugin { } } -impl Plugin for ValidParentCheckPlugin { - fn build(&self, app: &mut App) { +#[cfg(feature = "bevy_app")] +impl bevy_app::Plugin for ValidParentCheckPlugin { + fn build(&self, app: &mut bevy_app::App) { app.init_resource::>().add_systems( - Last, + bevy_app::Last, check_hierarchy_component_has_valid_parent:: .run_if(resource_equals(ReportHierarchyIssue::::new(true))), );