Skip to content

Commit 1fa37b5

Browse files
mockersfjames7132
authored andcommitted
can get the settings of a plugin from the app (bevyengine#6372)
# Objective - Make the settings of plugins readable during app building ## Solution - Added a vector of added plugins to the app. Their settings can be accessed as read only
1 parent f440ca5 commit 1fa37b5

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

crates/bevy_app/src/app.rs

+50
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub struct App {
6868
/// A container of [`Stage`]s set to be run in a linear order.
6969
pub schedule: Schedule,
7070
sub_apps: HashMap<AppLabelId, SubApp>,
71+
plugin_registry: Vec<Box<dyn Plugin>>,
7172
}
7273

7374
impl Debug for App {
@@ -131,6 +132,7 @@ impl App {
131132
schedule: Default::default(),
132133
runner: Box::new(run_once),
133134
sub_apps: HashMap::default(),
135+
plugin_registry: Vec::default(),
134136
}
135137
}
136138

@@ -825,11 +827,59 @@ impl App {
825827
where
826828
T: Plugin,
827829
{
830+
self.add_boxed_plugin(Box::new(plugin))
831+
}
832+
833+
/// Boxed variant of `add_plugin`, can be used from a [`PluginGroup`]
834+
pub(crate) fn add_boxed_plugin(&mut self, plugin: Box<dyn Plugin>) -> &mut Self {
828835
debug!("added plugin: {}", plugin.name());
829836
plugin.build(self);
837+
self.plugin_registry.push(plugin);
830838
self
831839
}
832840

841+
/// Checks if a [`Plugin`] has already been added.
842+
///
843+
/// This can be used by plugins to check if a plugin they depend upon has already been
844+
/// added.
845+
pub fn is_plugin_added<T>(&self) -> bool
846+
where
847+
T: Plugin,
848+
{
849+
self.plugin_registry
850+
.iter()
851+
.any(|p| p.downcast_ref::<T>().is_some())
852+
}
853+
854+
/// Returns a vector of references to any plugins of type `T` that have been added.
855+
///
856+
/// This can be used to read the settings of any already added plugins.
857+
/// This vector will be length zero if no plugins of that type have been added.
858+
/// If multiple copies of the same plugin are added to the [`App`], they will be listed in insertion order in this vector.
859+
///
860+
/// ```rust
861+
/// # use bevy_app::prelude::*;
862+
/// # #[derive(Default)]
863+
/// # struct ImagePlugin {
864+
/// # default_sampler: bool,
865+
/// # }
866+
/// # impl Plugin for ImagePlugin {
867+
/// # fn build(&self, app: &mut App) {}
868+
/// # }
869+
/// # let mut app = App::new();
870+
/// # app.add_plugin(ImagePlugin::default());
871+
/// let default_sampler = app.get_added_plugins::<ImagePlugin>()[0].default_sampler;
872+
/// ```
873+
pub fn get_added_plugins<T>(&self) -> Vec<&T>
874+
where
875+
T: Plugin,
876+
{
877+
self.plugin_registry
878+
.iter()
879+
.filter_map(|p| p.downcast_ref())
880+
.collect()
881+
}
882+
833883
/// Adds a group of [`Plugin`]s.
834884
///
835885
/// [`Plugin`]s can be grouped into a set by using a [`PluginGroup`].

crates/bevy_app/src/plugin_group.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ impl PluginGroupBuilder {
155155

156156
/// Consumes the [`PluginGroupBuilder`] and [builds](Plugin::build) the contained [`Plugin`]s
157157
/// in the order specified.
158-
pub fn finish(self, app: &mut App) {
158+
pub fn finish(mut self, app: &mut App) {
159159
for ty in &self.order {
160-
if let Some(entry) = self.plugins.get(ty) {
160+
if let Some(entry) = self.plugins.remove(ty) {
161161
if entry.enabled {
162162
debug!("added plugin: {}", entry.plugin.name());
163-
entry.plugin.build(app);
163+
app.add_boxed_plugin(entry.plugin);
164164
}
165165
}
166166
}

0 commit comments

Comments
 (0)