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

[Merged by Bors] - Unique plugin #6411

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 81 additions & 3 deletions crates/bevy_app/src/app.rs
Expand Up @@ -28,6 +28,10 @@ bevy_utils::define_label!(
#[derive(Resource, Clone, Deref, DerefMut, Default)]
pub struct AppTypeRegistry(pub bevy_reflect::TypeRegistryArc);

pub(crate) enum AppError {
DuplicatePlugin { plugin_name: String },
}

#[allow(clippy::needless_doctest_main)]
/// A container of app logic and data.
///
Expand Down Expand Up @@ -823,19 +827,43 @@ impl App {
/// # }
/// App::new().add_plugin(bevy_log::LogPlugin::default());
/// ```
///
/// # Panics
///
/// Panics if the plugin was already added to the application.
pub fn add_plugin<T>(&mut self, plugin: T) -> &mut Self
where
T: Plugin,
{
self.add_boxed_plugin(Box::new(plugin))
match self.add_boxed_plugin(Box::new(plugin)) {
Ok(app) => app,
Err(AppError::DuplicatePlugin { plugin_name }) => panic!(
"Error adding plugin {}: : plugin was already added in application",
plugin_name
),
}
}

/// Boxed variant of `add_plugin`, can be used from a [`PluginGroup`]
pub(crate) fn add_boxed_plugin(&mut self, plugin: Box<dyn Plugin>) -> &mut Self {
pub(crate) fn add_boxed_plugin(
&mut self,
plugin: Box<dyn Plugin>,
) -> Result<&mut Self, AppError> {
debug!("added plugin: {}", plugin.name());
if plugin.is_unique()
&& self
.plugin_registry
.iter()
.map(|plugin| plugin.name())
.any(|name| name == plugin.name())
mockersf marked this conversation as resolved.
Show resolved Hide resolved
{
Err(AppError::DuplicatePlugin {
plugin_name: plugin.name().to_string(),
})?;
}
plugin.build(self);
self.plugin_registry.push(plugin);
self
Ok(self)
}

/// Checks if a [`Plugin`] has already been added.
Expand Down Expand Up @@ -898,6 +926,10 @@ impl App {
/// App::new()
/// .add_plugins(MinimalPlugins);
/// ```
///
/// # Panics
///
/// Panics if one of the plugin in the group was already added to the application.
pub fn add_plugins<T: PluginGroup>(&mut self, group: T) -> &mut Self {
let builder = group.build();
builder.finish(self);
Expand Down Expand Up @@ -1031,3 +1063,49 @@ fn run_once(mut app: App) {
/// frame is over.
#[derive(Debug, Clone, Default)]
pub struct AppExit;

#[cfg(test)]
mod tests {
use crate::{App, Plugin};

struct PluginA;
impl Plugin for PluginA {
fn build(&self, _app: &mut crate::App) {}
}
struct PluginB;
impl Plugin for PluginB {
fn build(&self, _app: &mut crate::App) {}
}
struct PluginC<T>(T);
impl<T: Send + Sync + 'static> Plugin for PluginC<T> {
fn build(&self, _app: &mut crate::App) {}
}
struct PluginD;
impl Plugin for PluginD {
fn build(&self, _app: &mut crate::App) {}
fn is_unique(&self) -> bool {
false
}
}

#[test]
fn can_add_two_plugins() {
App::new().add_plugin(PluginA).add_plugin(PluginB);
}

#[test]
#[should_panic]
fn cant_add_twice_the_same_plugin() {
App::new().add_plugin(PluginA).add_plugin(PluginA);
}

#[test]
fn can_add_twice_the_same_plugin_with_different_type_param() {
App::new().add_plugin(PluginC(0)).add_plugin(PluginC(true));
}

#[test]
fn can_add_twice_the_same_plugin_not_unique() {
App::new().add_plugin(PluginD).add_plugin(PluginD);
}
}
10 changes: 9 additions & 1 deletion crates/bevy_app/src/plugin.rs
Expand Up @@ -6,14 +6,22 @@ use std::any::Any;
/// A collection of Bevy app logic and configuration.
///
/// Plugins configure an [`App`]. When an [`App`] registers a plugin,
/// the plugin's [`Plugin::build`] function is run.
/// the plugin's [`Plugin::build`] function is run. By default, a plugin
/// can only be added once to an [`App`]. If the plugin may need to be
/// added twice or more, the function [`is_unique`](Plugin::is_unique)
/// should be overriden to return `false`.
mockersf marked this conversation as resolved.
Show resolved Hide resolved
pub trait Plugin: Downcast + Any + Send + Sync {
/// Configures the [`App`] to which this plugin is added.
fn build(&self, app: &mut App);
/// Configures a name for the [`Plugin`] which is primarily used for debugging.
fn name(&self) -> &str {
std::any::type_name::<Self>()
}
/// If the plugin can be meaningfully instantiated several times in an [`App`](crate::App),
/// override this method to return `false`.
fn is_unique(&self) -> bool {
true
mockersf marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl_downcast!(Plugin);
Expand Down
47 changes: 36 additions & 11 deletions crates/bevy_app/src/plugin_group.rs
@@ -1,11 +1,15 @@
use crate::{App, Plugin};
use crate::{App, AppError, Plugin};
use bevy_utils::{tracing::debug, tracing::warn, HashMap};
use std::any::TypeId;

/// Combines multiple [`Plugin`]s into a single unit.
pub trait PluginGroup: Sized {
/// Configures the [`Plugin`]s that are to be added.
fn build(self) -> PluginGroupBuilder;
/// Configures a name for the [`PluginGroup`] which is primarily used for debugging.
fn name() -> &'static str {
std::any::type_name::<Self>()
}
/// Sets the value of the given [`Plugin`], if it exists
fn set<T: Plugin>(self, plugin: T) -> PluginGroupBuilder {
self.build().set(plugin)
Expand All @@ -27,13 +31,22 @@ impl PluginGroup for PluginGroupBuilder {
/// Provides a build ordering to ensure that [`Plugin`]s which produce/require a [`Resource`](bevy_ecs::system::Resource)
/// are built before/after dependent/depending [`Plugin`]s. [`Plugin`]s inside the group
/// can be disabled, enabled or reordered.
#[derive(Default)]
pub struct PluginGroupBuilder {
group_name: String,
mockersf marked this conversation as resolved.
Show resolved Hide resolved
plugins: HashMap<TypeId, PluginEntry>,
order: Vec<TypeId>,
}

impl PluginGroupBuilder {
/// Start a new builder for the [`PluginGroup`].
pub fn start<PG: PluginGroup>() -> Self {
Self {
group_name: PG::name().to_string(),
plugins: Default::default(),
order: Default::default(),
}
}

/// Finds the index of a target [`Plugin`]. Panics if the target's [`TypeId`] is not found.
fn index_of<Target: Plugin>(&self) -> usize {
let index = self
Expand Down Expand Up @@ -155,12 +168,24 @@ impl PluginGroupBuilder {

/// Consumes the [`PluginGroupBuilder`] and [builds](Plugin::build) the contained [`Plugin`]s
/// in the order specified.
///
/// # Panics
///
/// Panics if one of the plugin in the group was already added to the application.
pub fn finish(mut self, app: &mut App) {
for ty in &self.order {
if let Some(entry) = self.plugins.remove(ty) {
if entry.enabled {
debug!("added plugin: {}", entry.plugin.name());
app.add_boxed_plugin(entry.plugin);
if let Err(AppError::DuplicatePlugin { plugin_name }) =
app.add_boxed_plugin(entry.plugin)
{
panic!(
"Error adding plugin {} in group {}: plugin was already added in application",
plugin_name,
self.group_name
);
}
}
}
}
Expand All @@ -181,14 +206,14 @@ pub struct NoopPluginGroup;

impl PluginGroup for NoopPluginGroup {
fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::default()
PluginGroupBuilder::start::<Self>()
}
}

#[cfg(test)]
mod tests {
use super::PluginGroupBuilder;
use crate::{App, Plugin};
use crate::{App, NoopPluginGroup, Plugin};

struct PluginA;
impl Plugin for PluginA {
Expand All @@ -207,7 +232,7 @@ mod tests {

#[test]
fn basic_ordering() {
let group = PluginGroupBuilder::default()
let group = PluginGroupBuilder::start::<NoopPluginGroup>()
.add(PluginA)
.add(PluginB)
.add(PluginC);
Expand All @@ -224,7 +249,7 @@ mod tests {

#[test]
fn add_after() {
let group = PluginGroupBuilder::default()
let group = PluginGroupBuilder::start::<NoopPluginGroup>()
.add(PluginA)
.add(PluginB)
.add_after::<PluginA, PluginC>(PluginC);
Expand All @@ -241,7 +266,7 @@ mod tests {

#[test]
fn add_before() {
let group = PluginGroupBuilder::default()
let group = PluginGroupBuilder::start::<NoopPluginGroup>()
.add(PluginA)
.add(PluginB)
.add_before::<PluginB, PluginC>(PluginC);
Expand All @@ -258,7 +283,7 @@ mod tests {

#[test]
fn readd() {
let group = PluginGroupBuilder::default()
let group = PluginGroupBuilder::start::<NoopPluginGroup>()
.add(PluginA)
.add(PluginB)
.add(PluginC)
Expand All @@ -276,7 +301,7 @@ mod tests {

#[test]
fn readd_after() {
let group = PluginGroupBuilder::default()
let group = PluginGroupBuilder::start::<NoopPluginGroup>()
.add(PluginA)
.add(PluginB)
.add(PluginC)
Expand All @@ -294,7 +319,7 @@ mod tests {

#[test]
fn readd_before() {
let group = PluginGroupBuilder::default()
let group = PluginGroupBuilder::start::<NoopPluginGroup>()
.add(PluginA)
.add(PluginB)
.add(PluginC)
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_internal/src/default_plugins.rs
Expand Up @@ -26,7 +26,7 @@ pub struct DefaultPlugins;

impl PluginGroup for DefaultPlugins {
fn build(self) -> PluginGroupBuilder {
let mut group = PluginGroupBuilder::default();
let mut group = PluginGroupBuilder::start::<Self>();
group = group
.add(bevy_log::LogPlugin::default())
.add(bevy_core::CorePlugin::default())
Expand Down Expand Up @@ -127,7 +127,7 @@ pub struct MinimalPlugins;

impl PluginGroup for MinimalPlugins {
fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::default()
PluginGroupBuilder::start::<Self>()
.add(bevy_core::CorePlugin::default())
.add(bevy_time::TimePlugin::default())
.add(bevy_app::ScheduleRunnerPlugin::default())
Expand Down
2 changes: 1 addition & 1 deletion examples/app/plugin_group.rs
Expand Up @@ -26,7 +26,7 @@ pub struct HelloWorldPlugins;

impl PluginGroup for HelloWorldPlugins {
fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::default()
PluginGroupBuilder::start::<Self>()
.add(PrintHelloPlugin)
.add(PrintWorldPlugin)
}
Expand Down