Skip to content

Commit

Permalink
Make validity check enabled through a resource
Browse files Browse the repository at this point in the history
  • Loading branch information
nicopap committed Sep 1, 2022
1 parent 26b8f08 commit 69184e8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
33 changes: 31 additions & 2 deletions crates/bevy_hierarchy/src/valid_parent_check_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@ use std::marker::PhantomData;

use bevy_app::{App, CoreStage, Plugin};
use bevy_core::Name;
use bevy_ecs::prelude::*;
use bevy_ecs::{prelude::*, schedule::ShouldRun};
use bevy_log::warn;
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>`].
/// It is enabled on debug builds and disabled in release builds by default,
/// you can update this resource at runtime to change the default behavior.
#[derive(Resource)]
pub struct ReportHierarchyIssue<T> {
/// Whether to run [`check_hierarchy_component_has_valid_parent<T>`].
pub enabled: bool,
_comp: PhantomData<fn(T)>,
}
impl<T> Default for ReportHierarchyIssue<T> {
fn default() -> Self {
Self {
enabled: cfg!(debug_assertions),
_comp: PhantomData,
}
}
}

/// System to print a warning for each `Entity` with a `T` component
/// which parent hasn't a `T` component.
///
Expand Down Expand Up @@ -38,6 +58,14 @@ pub fn check_hierarchy_component_has_valid_parent<T: Component>(
}
}

/// Run criteria that only allows running when [`ReportHierarchyIssue<T>`] is enabled.
pub fn on_hierarchy_reports_enabled<T>(report: Res<ReportHierarchyIssue<T>>) -> ShouldRun
where
T: Component,
{
report.enabled.into()
}

/// Print a warning for each `Entity` with a `T` component
/// which parent hasn't a `T` component.
///
Expand All @@ -53,7 +81,8 @@ impl<T: Component> Plugin for ValidParentCheckPlugin<T> {
fn build(&self, app: &mut App) {
app.add_system_to_stage(
CoreStage::Last,
check_hierarchy_component_has_valid_parent::<T>,
check_hierarchy_component_has_valid_parent::<T>
.with_run_criteria(on_hierarchy_reports_enabled::<T>),
);
}
}
10 changes: 0 additions & 10 deletions crates/bevy_internal/src/default_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ impl PluginGroup for DefaultPlugins {
group.add(bevy_input::InputPlugin::default());
group.add(bevy_window::WindowPlugin::default());

#[cfg(debug_assertions)]
group.add(bevy_hierarchy::ValidParentCheckPlugin::<
crate::prelude::GlobalTransform,
>::default());

#[cfg(feature = "bevy_asset")]
group.add(bevy_asset::AssetPlugin::default());

Expand All @@ -55,11 +50,6 @@ impl PluginGroup for DefaultPlugins {
#[cfg(feature = "bevy_render")]
group.add(bevy_render::RenderPlugin::default());

#[cfg(all(feature = "bevy_render", debug_assertions))]
group.add(bevy_hierarchy::ValidParentCheckPlugin::<
crate::prelude::ComputedVisibility,
>::default());

#[cfg(feature = "bevy_core_pipeline")]
group.add(bevy_core_pipeline::CorePipelinePlugin::default());

Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod spatial_bundle;
pub mod texture;
pub mod view;

use bevy_hierarchy::ValidParentCheckPlugin;
pub use extract_param::Extract;

pub mod prelude {
Expand All @@ -34,6 +35,7 @@ pub mod prelude {
}

pub use once_cell;
use prelude::ComputedVisibility;

use crate::{
camera::CameraPlugin,
Expand Down Expand Up @@ -315,7 +317,8 @@ impl Plugin for RenderPlugin {
});
}

app.add_plugin(WindowRenderPlugin)
app.add_plugin(ValidParentCheckPlugin::<ComputedVisibility>::default())
.add_plugin(WindowRenderPlugin)
.add_plugin(CameraPlugin)
.add_plugin(ViewPlugin)
.add_plugin(MeshPlugin)
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod prelude {

use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use bevy_hierarchy::ValidParentCheckPlugin;
use prelude::{GlobalTransform, Transform};

/// A [`Bundle`] of the [`Transform`] and [`GlobalTransform`]
Expand Down Expand Up @@ -91,6 +92,7 @@ impl Plugin for TransformPlugin {
fn build(&self, app: &mut App) {
app.register_type::<Transform>()
.register_type::<GlobalTransform>()
.add_plugin(ValidParentCheckPlugin::<GlobalTransform>::default())
// add transform systems to startup so the first update is "correct"
.add_startup_system_to_stage(
StartupStage::PostStartup,
Expand Down

0 comments on commit 69184e8

Please sign in to comment.