@@ -68,6 +68,7 @@ pub struct App {
68
68
/// A container of [`Stage`]s set to be run in a linear order.
69
69
pub schedule : Schedule ,
70
70
sub_apps : HashMap < AppLabelId , SubApp > ,
71
+ plugin_registry : Vec < Box < dyn Plugin > > ,
71
72
}
72
73
73
74
impl Debug for App {
@@ -131,6 +132,7 @@ impl App {
131
132
schedule : Default :: default ( ) ,
132
133
runner : Box :: new ( run_once) ,
133
134
sub_apps : HashMap :: default ( ) ,
135
+ plugin_registry : Vec :: default ( ) ,
134
136
}
135
137
}
136
138
@@ -825,11 +827,59 @@ impl App {
825
827
where
826
828
T : Plugin ,
827
829
{
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 {
828
835
debug ! ( "added plugin: {}" , plugin. name( ) ) ;
829
836
plugin. build ( self ) ;
837
+ self . plugin_registry . push ( plugin) ;
830
838
self
831
839
}
832
840
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
+
833
883
/// Adds a group of [`Plugin`]s.
834
884
///
835
885
/// [`Plugin`]s can be grouped into a set by using a [`PluginGroup`].
0 commit comments