can get the settings of a plugin from the app (#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
This commit is contained in:
parent
54cf45c5b3
commit
7671ddea72
@ -68,6 +68,7 @@ pub struct App {
|
|||||||
/// A container of [`Stage`]s set to be run in a linear order.
|
/// A container of [`Stage`]s set to be run in a linear order.
|
||||||
pub schedule: Schedule,
|
pub schedule: Schedule,
|
||||||
sub_apps: HashMap<AppLabelId, SubApp>,
|
sub_apps: HashMap<AppLabelId, SubApp>,
|
||||||
|
plugin_registry: Vec<Box<dyn Plugin>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for App {
|
impl Debug for App {
|
||||||
@ -131,6 +132,7 @@ impl App {
|
|||||||
schedule: Default::default(),
|
schedule: Default::default(),
|
||||||
runner: Box::new(run_once),
|
runner: Box::new(run_once),
|
||||||
sub_apps: HashMap::default(),
|
sub_apps: HashMap::default(),
|
||||||
|
plugin_registry: Vec::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -825,11 +827,59 @@ impl App {
|
|||||||
where
|
where
|
||||||
T: Plugin,
|
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());
|
debug!("added plugin: {}", plugin.name());
|
||||||
plugin.build(self);
|
plugin.build(self);
|
||||||
|
self.plugin_registry.push(plugin);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checks if a [`Plugin`] has already been added.
|
||||||
|
///
|
||||||
|
/// This can be used by plugins to check if a plugin they depend upon has already been
|
||||||
|
/// added.
|
||||||
|
pub fn is_plugin_added<T>(&self) -> bool
|
||||||
|
where
|
||||||
|
T: Plugin,
|
||||||
|
{
|
||||||
|
self.plugin_registry
|
||||||
|
.iter()
|
||||||
|
.any(|p| p.downcast_ref::<T>().is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a vector of references to any plugins of type `T` that have been added.
|
||||||
|
///
|
||||||
|
/// This can be used to read the settings of any already added plugins.
|
||||||
|
/// This vector will be length zero if no plugins of that type have been added.
|
||||||
|
/// If multiple copies of the same plugin are added to the [`App`], they will be listed in insertion order in this vector.
|
||||||
|
///
|
||||||
|
/// ```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.
|
/// Adds a group of [`Plugin`]s.
|
||||||
///
|
///
|
||||||
/// [`Plugin`]s can be grouped into a set by using a [`PluginGroup`].
|
/// [`Plugin`]s can be grouped into a set by using a [`PluginGroup`].
|
||||||
|
@ -155,12 +155,12 @@ impl PluginGroupBuilder {
|
|||||||
|
|
||||||
/// Consumes the [`PluginGroupBuilder`] and [builds](Plugin::build) the contained [`Plugin`]s
|
/// Consumes the [`PluginGroupBuilder`] and [builds](Plugin::build) the contained [`Plugin`]s
|
||||||
/// in the order specified.
|
/// in the order specified.
|
||||||
pub fn finish(self, app: &mut App) {
|
pub fn finish(mut self, app: &mut App) {
|
||||||
for ty in &self.order {
|
for ty in &self.order {
|
||||||
if let Some(entry) = self.plugins.get(ty) {
|
if let Some(entry) = self.plugins.remove(ty) {
|
||||||
if entry.enabled {
|
if entry.enabled {
|
||||||
debug!("added plugin: {}", entry.plugin.name());
|
debug!("added plugin: {}", entry.plugin.name());
|
||||||
entry.plugin.build(app);
|
app.add_boxed_plugin(entry.plugin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user