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

plugin_group! macro #11460

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions crates/bevy_a11y/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub enum AccessibilitySystem {
}

/// Plugin managing non-GUI aspects of integrating with accessibility APIs.
#[derive(Default)]
pub struct AccessibilityPlugin;

impl Plugin for AccessibilityPlugin {
Expand Down
89 changes: 89 additions & 0 deletions crates/bevy_app/src/plugin_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,95 @@ use crate::{App, AppError, Plugin};
use bevy_utils::{tracing::debug, tracing::warn, HashMap};
use std::any::TypeId;

/// A macro for generating a well-documented [`PluginGroup`] from a list of [`Plugin`] paths.
///
/// Every plugin must implement the [`Default`] trait.
///
/// # Example
///
/// ```
/// # use bevy_app::*;
/// # mod velocity {
/// # use bevy_app::*;
/// # #[derive(Default)] pub struct VelocityPlugin;
/// # impl Plugin for VelocityPlugin { fn build(&self, _: &mut App) {} }
/// # }
/// # mod collision { pub mod capsule {
/// # use bevy_app::*;
/// # #[derive(Default)] pub struct CapsuleCollisionPlugin;
/// # impl Plugin for CapsuleCollisionPlugin { fn build(&self, _: &mut App) {} }
/// # } }
/// # #[derive(Default)] pub struct TickratePlugin;
/// # impl Plugin for TickratePlugin { fn build(&self, _: &mut App) {} }
/// # mod features {
/// # use bevy_app::*;
/// # #[derive(Default)] pub struct ForcePlugin;
/// # impl Plugin for ForcePlugin { fn build(&self, _: &mut App) {} }
/// # }
/// # mod web {
/// # use bevy_app::*;
/// # #[derive(Default)] pub struct WebCompatibilityPlugin;
/// # impl Plugin for WebCompatibilityPlugin { fn build(&self, _: &mut App) {} }
/// # }
/// plugin_group!(
/// PhysicsPlugins {
/// // Due to local ambiguity issues, you have to
/// // use a semicolon before the plugin's name.
doonv marked this conversation as resolved.
Show resolved Hide resolved
/// :TickratePlugin,
/// // Due to local ambiguity issues, you have to
/// // use 3 semicolons for the last part of the path.
doonv marked this conversation as resolved.
Show resolved Hide resolved
/// collision::capsule:::CapsuleCollisionPlugin,
/// velocity:::VelocityPlugin,
/// #[cfg(feature = "external_forces")]
/// features:::ForcePlugin,
/// // You can add any attribute you want like this, but it won't be documented.
/// #[custom(cfg(target_arch = "wasm32"))]
/// web:::WebCompatibilityPlugin
/// }
/// );
/// ```
#[macro_export]
macro_rules! plugin_group {
(
doonv marked this conversation as resolved.
Show resolved Hide resolved
$(#[$group_meta:meta])*
$group:ident {
$(
$(#[cfg(feature = $feature:literal)])?
$(#[custom($plugin_meta:meta)])*
doonv marked this conversation as resolved.
Show resolved Hide resolved
$($plugin_path:ident::)* : $plugin_name:ident
doonv marked this conversation as resolved.
Show resolved Hide resolved
),*$(,)?
}
$($(#[doc = $post_doc:literal])+)?
) => {
$(#[$group_meta])*
///
$(#[doc = concat!(
"* [`", stringify!($plugin_name), "`](" $(, stringify!($plugin_path), "::")*, stringify!($plugin_name), ")"
$(, " - with feature `", $feature, "`")?
)])*
$(
///
$(#[doc = $post_doc])+
)?
pub struct $group;
impl PluginGroup for $group {
fn build(self) -> PluginGroupBuilder {
let mut group = PluginGroupBuilder::start::<Self>();
Copy link
Member

Choose a reason for hiding this comment

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

This will fail to compile if PluginGroup and PluginGroupBuilder are not imported. Using $crate will fix this.

Additionally, #[allow(unused_mut)] will allow an empty plugin_group!(MyPlugins {}) to build without warnings.

Suggested change
impl PluginGroup for $group {
fn build(self) -> PluginGroupBuilder {
let mut group = PluginGroupBuilder::start::<Self>();
impl $crate::PluginGroup for $group {
fn build(self) -> $crate::PluginGroupBuilder {
#[allow(unused_mut)]
let mut group = $crate::PluginGroupBuilder::start::<Self>();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why would you want to create a plugin group with no plugins?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think there is any long-term use of having an empty plugin group, but I could see someone testing the macro empty and being confused why rustc is raising an warning on code they can't see. Additionally the macro doesn't prevent you from creating an empty group, so it's bound to happen at some point.

I don't think the extra #[allow(...)] annotation will be confusing for future reviewers, but feel free to add a comment explaining why it's there. It can't hurt :)


$(
$(#[cfg(feature = $feature)])?
$(#[$plugin_meta])*
{
group = group.add(<$($plugin_path::)*$plugin_name>::default());
}
doonv marked this conversation as resolved.
Show resolved Hide resolved
)*

group
}
}
};
}

/// Combines multiple [`Plugin`]s into a single unit.
pub trait PluginGroup: Sized {
/// Configures the [`Plugin`]s that are to be added.
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_gizmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ use std::{any::TypeId, mem};
const LINE_SHADER_HANDLE: Handle<Shader> = Handle::weak_from_u128(7414812689238026784);

/// A [`Plugin`] that provides an immediate mode drawing api for visual debugging.
#[derive(Default)]
pub struct GizmoPlugin;

impl Plugin for GizmoPlugin {
Expand Down
218 changes: 67 additions & 151 deletions crates/bevy_internal/src/default_plugins.rs
Original file line number Diff line number Diff line change
@@ -1,144 +1,69 @@
use bevy_app::{Plugin, PluginGroup, PluginGroupBuilder};

/// This plugin group will add all the default plugins for a *Bevy* application:
/// * [`LogPlugin`](crate::log::LogPlugin)
/// * [`TaskPoolPlugin`](crate::core::TaskPoolPlugin)
/// * [`TypeRegistrationPlugin`](crate::core::TypeRegistrationPlugin)
/// * [`FrameCountPlugin`](crate::core::FrameCountPlugin)
/// * [`TimePlugin`](crate::time::TimePlugin)
/// * [`TransformPlugin`](crate::transform::TransformPlugin)
/// * [`HierarchyPlugin`](crate::hierarchy::HierarchyPlugin)
/// * [`DiagnosticsPlugin`](crate::diagnostic::DiagnosticsPlugin)
/// * [`InputPlugin`](crate::input::InputPlugin)
/// * [`WindowPlugin`](crate::window::WindowPlugin)
/// * [`AssetPlugin`](crate::asset::AssetPlugin) - with feature `bevy_asset`
/// * [`ScenePlugin`](crate::scene::ScenePlugin) - with feature `bevy_scene`
/// * [`WinitPlugin`](crate::winit::WinitPlugin) - with feature `bevy_winit`
/// * [`RenderPlugin`](crate::render::RenderPlugin) - with feature `bevy_render`
/// * [`ImagePlugin`](crate::render::texture::ImagePlugin) - with feature `bevy_render`
/// * [`PipelinedRenderingPlugin`](crate::render::pipelined_rendering::PipelinedRenderingPlugin) - with feature `bevy_render` when not targeting `wasm32`
/// * [`CorePipelinePlugin`](crate::core_pipeline::CorePipelinePlugin) - with feature `bevy_core_pipeline`
/// * [`SpritePlugin`](crate::sprite::SpritePlugin) - with feature `bevy_sprite`
/// * [`TextPlugin`](crate::text::TextPlugin) - with feature `bevy_text`
/// * [`UiPlugin`](crate::ui::UiPlugin) - with feature `bevy_ui`
/// * [`PbrPlugin`](crate::pbr::PbrPlugin) - with feature `bevy_pbr`
/// * [`GltfPlugin`](crate::gltf::GltfPlugin) - with feature `bevy_gltf`
/// * [`AudioPlugin`](crate::audio::AudioPlugin) - with feature `bevy_audio`
/// * [`GilrsPlugin`](crate::gilrs::GilrsPlugin) - with feature `bevy_gilrs`
/// * [`AnimationPlugin`](crate::animation::AnimationPlugin) - with feature `bevy_animation`
///
/// [`DefaultPlugins`] obeys *Cargo* *feature* flags. Users may exert control over this plugin group
/// by disabling `default-features` in their `Cargo.toml` and enabling only those features
/// that they wish to use.
///
/// [`DefaultPlugins`] contains all the plugins typically required to build
/// a *Bevy* application which includes a *window* and presentation components.
/// For *headless* cases – without a *window* or presentation, see [`MinimalPlugins`].
pub struct DefaultPlugins;

impl PluginGroup for DefaultPlugins {
fn build(self) -> PluginGroupBuilder {
let mut group = PluginGroupBuilder::start::<Self>();
group = group
.add(bevy_log::LogPlugin::default())
.add(bevy_core::TaskPoolPlugin::default())
.add(bevy_core::TypeRegistrationPlugin)
.add(bevy_core::FrameCountPlugin)
.add(bevy_time::TimePlugin)
.add(bevy_transform::TransformPlugin)
.add(bevy_hierarchy::HierarchyPlugin)
.add(bevy_diagnostic::DiagnosticsPlugin)
.add(bevy_input::InputPlugin)
.add(bevy_window::WindowPlugin::default())
.add(bevy_a11y::AccessibilityPlugin);

#![allow(dead_code)]
use bevy_app::{plugin_group, Plugin, PluginGroup, PluginGroupBuilder};

plugin_group!(
/// This plugin group will add all the default plugins for a *Bevy* application:
DefaultPlugins {
bevy_log:::LogPlugin,
bevy_core:::TaskPoolPlugin,
bevy_core:::TypeRegistrationPlugin,
bevy_core:::FrameCountPlugin,
bevy_time:::TimePlugin,
bevy_transform:::TransformPlugin,
bevy_hierarchy:::HierarchyPlugin,
bevy_diagnostic:::DiagnosticsPlugin,
bevy_input:::InputPlugin,
bevy_window:::WindowPlugin,
bevy_a11y:::AccessibilityPlugin,
#[cfg(feature = "bevy_asset")]
{
group = group.add(bevy_asset::AssetPlugin::default());
}

bevy_asset:::AssetPlugin,
#[cfg(feature = "bevy_scene")]
{
group = group.add(bevy_scene::ScenePlugin);
}

bevy_scene:::ScenePlugin,
#[cfg(feature = "bevy_winit")]
{
group = group.add(bevy_winit::WinitPlugin::default());
}

bevy_winit:::WinitPlugin,
#[cfg(feature = "bevy_render")]
{
group = group
.add(bevy_render::RenderPlugin::default())
// NOTE: Load this after renderer initialization so that it knows about the supported
// compressed texture formats
.add(bevy_render::texture::ImagePlugin::default());

#[cfg(all(not(target_arch = "wasm32"), feature = "multi-threaded"))]
{
group = group.add(bevy_render::pipelined_rendering::PipelinedRenderingPlugin);
}
}

bevy_render:::RenderPlugin,
// NOTE: Load this after renderer initialization so that it knows about the supported
// compressed texture formats
#[cfg(feature = "bevy_render")]
bevy_render::texture:::ImagePlugin,
#[cfg(feature = "bevy_render")]
#[custom(cfg(all(not(target_arch = "wasm32"), feature = "multi-threaded")))]
bevy_render::pipelined_rendering:::PipelinedRenderingPlugin,
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice if it would document not targeting wasm32

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 would be nice, but it would be quite difficult to implement this with the macro.

Copy link
Contributor Author

@doonv doonv Jan 26, 2024

Choose a reason for hiding this comment

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

The original DefaultPlugins documentation didn't mention this requirement. So I don't think that's necessary.

Copy link
Member

Choose a reason for hiding this comment

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

It did.

/// * [`PipelinedRenderingPlugin`](crate::render::pipelined_rendering::PipelinedRenderingPlugin) - with feature `bevy_render` when not targeting `wasm32`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah mb

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

bevy_core_pipeline:::CorePipelinePlugin,
#[cfg(feature = "bevy_sprite")]
{
group = group.add(bevy_sprite::SpritePlugin);
}

bevy_sprite:::SpritePlugin,
#[cfg(feature = "bevy_text")]
{
group = group.add(bevy_text::TextPlugin);
}

bevy_text:::TextPlugin,
#[cfg(feature = "bevy_ui")]
{
group = group.add(bevy_ui::UiPlugin);
}

#[cfg(feature = "bevy_pbr")]
{
group = group.add(bevy_pbr::PbrPlugin::default());
}

bevy_ui:::UiPlugin,
// NOTE: Load this after renderer initialization so that it knows about the supported
// compressed texture formats
#[cfg(feature = "bevy_pbr")]
bevy_pbr:::PbrPlugin,
#[cfg(feature = "bevy_gltf")]
{
group = group.add(bevy_gltf::GltfPlugin::default());
}

bevy_gltf:::GltfPlugin,
#[cfg(feature = "bevy_audio")]
{
group = group.add(bevy_audio::AudioPlugin::default());
}

bevy_audio:::AudioPlugin,
#[cfg(feature = "bevy_gilrs")]
{
group = group.add(bevy_gilrs::GilrsPlugin);
}

bevy_gilrs:::GilrsPlugin,
#[cfg(feature = "bevy_animation")]
{
group = group.add(bevy_animation::AnimationPlugin);
}

bevy_animation:::AnimationPlugin,
#[cfg(feature = "bevy_gizmos")]
{
group = group.add(bevy_gizmos::GizmoPlugin);
}

group = group.add(IgnoreAmbiguitiesPlugin);

group
bevy_gizmos:::GizmoPlugin,
:IgnoreAmbiguitiesPlugin,
doonv marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// [`DefaultPlugins`] obeys *Cargo feature* flags. Users may exert control over this plugin group
/// by disabling `default-features` in their `Cargo.toml` and enabling only those features
/// that they wish to use.
///
/// [`DefaultPlugins`] contains all the plugins typically required to build
/// a *Bevy* application which includes a *window* and presentation components.
/// For *headless* cases – without a *window* or presentation, see [`MinimalPlugins`].
);

#[derive(Default)]
struct IgnoreAmbiguitiesPlugin;

impl Plugin for IgnoreAmbiguitiesPlugin {
Expand Down Expand Up @@ -179,30 +104,21 @@ impl Plugin for IgnoreAmbiguitiesPlugin {
}
}

/// This plugin group will add the minimal plugins for a *Bevy* application:
/// * [`TaskPoolPlugin`](crate::core::TaskPoolPlugin)
/// * [`TypeRegistrationPlugin`](crate::core::TypeRegistrationPlugin)
/// * [`FrameCountPlugin`](crate::core::FrameCountPlugin)
/// * [`TimePlugin`](crate::time::TimePlugin)
/// * [`ScheduleRunnerPlugin`](crate::app::ScheduleRunnerPlugin)
///
/// This group of plugins is intended for use for minimal, *headless* programs –
/// see the [*Bevy* *headless* example](https://github.com/bevyengine/bevy/blob/main/examples/app/headless.rs)
/// – and includes a [schedule runner (`ScheduleRunnerPlugin`)](crate::app::ScheduleRunnerPlugin)
/// to provide functionality that would otherwise be driven by a windowed application's
/// *event loop* or *message loop*.
///
/// Windowed applications that wish to use a reduced set of plugins should consider the
/// [`DefaultPlugins`] plugin group which can be controlled with *Cargo* *feature* flags.
pub struct MinimalPlugins;

impl PluginGroup for MinimalPlugins {
fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::start::<Self>()
.add(bevy_core::TaskPoolPlugin::default())
.add(bevy_core::TypeRegistrationPlugin)
.add(bevy_core::FrameCountPlugin)
.add(bevy_time::TimePlugin)
.add(bevy_app::ScheduleRunnerPlugin::default())
plugin_group!(
/// This plugin group will add the minimal plugins for a *Bevy* application:
MinimalPlugins {
bevy_core:::TaskPoolPlugin,
bevy_core:::TypeRegistrationPlugin,
bevy_core:::FrameCountPlugin,
bevy_time:::TimePlugin,
bevy_app:::ScheduleRunnerPlugin,
}
}
/// This group of plugins is intended for use for minimal, *headless* programs –
/// see the [*Bevy* *headless* example](https://github.com/bevyengine/bevy/blob/main/examples/app/headless.rs)
/// – and includes a [schedule runner (`ScheduleRunnerPlugin`)](crate::app::ScheduleRunnerPlugin)
/// to provide functionality that would otherwise be driven by a windowed application's
/// *event loop* or *message loop*.
///
/// Windowed applications that wish to use a reduced set of plugins should consider the
/// [`DefaultPlugins`] plugin group which can be controlled with *Cargo* *feature* flags.
);