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] - can get the settings of a plugin from the app #6372

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 49 additions & 0 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub struct App {
/// A container of [`Stage`]s set to be run in a linear order.
pub schedule: Schedule,
sub_apps: HashMap<AppLabelId, SubApp>,
plugin_registry: Vec<Box<dyn Plugin>>,
}

impl Debug for App {
Expand Down Expand Up @@ -131,6 +132,7 @@ impl App {
schedule: Default::default(),
runner: Box::new(run_once),
sub_apps: HashMap::default(),
plugin_registry: Vec::default(),
}
}

Expand Down Expand Up @@ -825,11 +827,58 @@ impl App {
where
T: Plugin,
{
self.add_boxed_plugin(Box::new(plugin))
}

/// 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 {
debug!("added plugin: {}", plugin.name());
plugin.build(self);
self.plugin_registry.push(plugin);
self
}

/// Checks if a [`Plugin`] has already been added.
///
/// This can be used by plugins to check if a plugin dependency they have has already been
mockersf marked this conversation as resolved.
Show resolved Hide resolved
/// added.
pub fn is_plugin_added<T>(&self) -> bool
where
T: Plugin,
{
self.plugin_registry
.iter()
.find(|p| p.downcast_ref::<T>().is_some())
.is_some()
}

/// Returns the plugin of a given type that have been added.
mockersf marked this conversation as resolved.
Show resolved Hide resolved
///
/// This can be used by plugins to get the settings of already added plugins.
mockersf marked this conversation as resolved.
Show resolved Hide resolved
///
/// ```rust
/// # use bevy_app::prelude::*;
/// # #[derive(Default)]
/// # struct ImagePlugin {
/// # default_sampler: bool,
/// # }
/// # impl Plugin for ImagePlugin {
/// # fn build(&self, app: &mut App) {}
/// # }
/// # let mut app = App::new();
/// # app.add_plugin(ImagePlugin::default());
/// let default_sampler = app.get_added_plugins::<ImagePlugin>()[0].default_sampler;
/// ```
pub fn get_added_plugins<T>(&self) -> Vec<&T>
where
T: Plugin,
{
self.plugin_registry
.iter()
.filter_map(|p| p.downcast_ref())
.collect()
}

/// Adds a group of [`Plugin`]s.
///
/// [`Plugin`]s can be grouped into a set by using a [`PluginGroup`].
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_app/src/plugin_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ impl PluginGroupBuilder {

/// Consumes the [`PluginGroupBuilder`] and [builds](Plugin::build) the contained [`Plugin`]s
/// in the order specified.
pub fn finish(self, app: &mut App) {
pub fn finish(mut self, app: &mut App) {
for ty in &self.order {
if let Some(entry) = self.plugins.get(ty) {
if let Some(entry) = self.plugins.remove(ty) {
if entry.enabled {
debug!("added plugin: {}", entry.plugin.name());
entry.plugin.build(app);
app.add_boxed_plugin(entry.plugin);
}
}
}
Expand Down