2021-07-27 20:21:06 +00:00
|
|
|
use crate::{App, Plugin};
|
2020-11-13 01:23:57 +00:00
|
|
|
use bevy_utils::{tracing::debug, HashMap};
|
2020-10-29 20:04:28 +00:00
|
|
|
use std::any::TypeId;
|
|
|
|
|
2022-01-06 23:16:47 +00:00
|
|
|
/// Combines multiple [`Plugin`]s into a single unit.
|
2020-10-29 20:04:28 +00:00
|
|
|
pub trait PluginGroup {
|
2022-01-06 23:16:47 +00:00
|
|
|
/// Configures the [`Plugin`]s that are to be added.
|
2020-10-29 20:04:28 +00:00
|
|
|
fn build(&mut self, group: &mut PluginGroupBuilder);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PluginEntry {
|
|
|
|
plugin: Box<dyn Plugin>,
|
|
|
|
enabled: bool,
|
|
|
|
}
|
|
|
|
|
2022-01-06 23:16:47 +00:00
|
|
|
/// Facilitates the creation and configuration of a [`PluginGroup`].
|
2022-04-05 22:36:02 +00:00
|
|
|
/// Provides a build ordering to ensure that [`Plugin`]s which produce/require a [`Resource`](bevy_ecs::system::Resource)
|
2022-01-06 23:16:47 +00:00
|
|
|
/// are built before/after dependent/depending [`Plugin`]s.
|
2020-10-29 20:04:28 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct PluginGroupBuilder {
|
|
|
|
plugins: HashMap<TypeId, PluginEntry>,
|
|
|
|
order: Vec<TypeId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PluginGroupBuilder {
|
2022-05-02 13:20:54 +00:00
|
|
|
/// Finds the index of a target [`Plugin`]. Panics if the target's [`TypeId`] is not found.
|
|
|
|
fn index_of<Target: Plugin>(&mut self) -> usize {
|
|
|
|
let index = self
|
|
|
|
.order
|
|
|
|
.iter()
|
|
|
|
.position(|&ty| ty == TypeId::of::<Target>());
|
|
|
|
|
|
|
|
match index {
|
|
|
|
Some(i) => i,
|
|
|
|
None => panic!(
|
|
|
|
"Plugin does not exist in group: {}.",
|
|
|
|
std::any::type_name::<Target>()
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 23:16:47 +00:00
|
|
|
/// Appends a [`Plugin`] to the [`PluginGroupBuilder`].
|
2020-10-29 20:04:28 +00:00
|
|
|
pub fn add<T: Plugin>(&mut self, plugin: T) -> &mut Self {
|
|
|
|
self.order.push(TypeId::of::<T>());
|
|
|
|
self.plugins.insert(
|
|
|
|
TypeId::of::<T>(),
|
|
|
|
PluginEntry {
|
|
|
|
plugin: Box::new(plugin),
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-01-06 23:16:47 +00:00
|
|
|
/// Configures a [`Plugin`] to be built before another plugin.
|
2020-10-29 20:04:28 +00:00
|
|
|
pub fn add_before<Target: Plugin, T: Plugin>(&mut self, plugin: T) -> &mut Self {
|
2022-05-02 13:20:54 +00:00
|
|
|
let target_index = self.index_of::<Target>();
|
2020-10-29 20:04:28 +00:00
|
|
|
self.order.insert(target_index, TypeId::of::<T>());
|
|
|
|
self.plugins.insert(
|
|
|
|
TypeId::of::<T>(),
|
|
|
|
PluginEntry {
|
|
|
|
plugin: Box::new(plugin),
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-01-06 23:16:47 +00:00
|
|
|
/// Configures a [`Plugin`] to be built after another plugin.
|
2020-10-29 20:04:28 +00:00
|
|
|
pub fn add_after<Target: Plugin, T: Plugin>(&mut self, plugin: T) -> &mut Self {
|
2022-05-02 13:20:54 +00:00
|
|
|
let target_index = self.index_of::<Target>();
|
2020-10-29 20:04:28 +00:00
|
|
|
self.order.insert(target_index + 1, TypeId::of::<T>());
|
|
|
|
self.plugins.insert(
|
|
|
|
TypeId::of::<T>(),
|
|
|
|
PluginEntry {
|
|
|
|
plugin: Box::new(plugin),
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-04-05 22:36:02 +00:00
|
|
|
/// Enables a [`Plugin`].
|
2022-01-06 23:16:47 +00:00
|
|
|
///
|
|
|
|
/// [`Plugin`]s within a [`PluginGroup`] are enabled by default. This function is used to
|
|
|
|
/// opt back in to a [`Plugin`] after [disabling](Self::disable) it.
|
2020-10-29 20:04:28 +00:00
|
|
|
pub fn enable<T: Plugin>(&mut self) -> &mut Self {
|
|
|
|
let mut plugin_entry = self
|
|
|
|
.plugins
|
|
|
|
.get_mut(&TypeId::of::<T>())
|
2020-12-02 19:31:16 +00:00
|
|
|
.expect("Cannot enable a plugin that does not exist.");
|
2020-10-29 20:04:28 +00:00
|
|
|
plugin_entry.enabled = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-04-05 22:36:02 +00:00
|
|
|
/// Disables a [`Plugin`], preventing it from being added to the [`App`] with the rest of the [`PluginGroup`].
|
2020-10-29 20:04:28 +00:00
|
|
|
pub fn disable<T: Plugin>(&mut self) -> &mut Self {
|
|
|
|
let mut plugin_entry = self
|
|
|
|
.plugins
|
|
|
|
.get_mut(&TypeId::of::<T>())
|
2020-12-02 19:31:16 +00:00
|
|
|
.expect("Cannot disable a plugin that does not exist.");
|
2020-10-29 20:04:28 +00:00
|
|
|
plugin_entry.enabled = false;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-01-06 23:16:47 +00:00
|
|
|
/// Consumes the [`PluginGroupBuilder`] and [builds](Plugin::build) the contained [`Plugin`]s.
|
2021-07-27 20:21:06 +00:00
|
|
|
pub fn finish(self, app: &mut App) {
|
2020-10-29 20:04:28 +00:00
|
|
|
for ty in self.order.iter() {
|
|
|
|
if let Some(entry) = self.plugins.get(ty) {
|
|
|
|
if entry.enabled {
|
2020-11-13 01:23:57 +00:00
|
|
|
debug!("added plugin: {}", entry.plugin.name());
|
2020-10-29 20:04:28 +00:00
|
|
|
entry.plugin.build(app);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|