2021-07-27 20:21:06 +00:00
|
|
|
use crate::{App, Plugin};
|
2022-05-09 13:06:22 +00:00
|
|
|
use bevy_utils::{tracing::debug, tracing::warn, 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-05-09 13:06:22 +00:00
|
|
|
/// are built before/after dependent/depending [`Plugin`]s. [`Plugin`]s inside the group
|
|
|
|
/// can be disabled, enabled or reordered.
|
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-05-09 13:06:22 +00:00
|
|
|
// Insert the new plugin as enabled, and removes its previous ordering if it was
|
|
|
|
// already present
|
|
|
|
fn upsert_plugin_state<T: Plugin>(&mut self, plugin: T, added_at_index: usize) {
|
|
|
|
if let Some(entry) = self.plugins.insert(
|
2020-10-29 20:04:28 +00:00
|
|
|
TypeId::of::<T>(),
|
|
|
|
PluginEntry {
|
|
|
|
plugin: Box::new(plugin),
|
|
|
|
enabled: true,
|
|
|
|
},
|
2022-05-09 13:06:22 +00:00
|
|
|
) {
|
|
|
|
if entry.enabled {
|
|
|
|
warn!(
|
|
|
|
"You are replacing plugin '{}' that was not disabled.",
|
|
|
|
entry.plugin.name()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if let Some(to_remove) = self
|
|
|
|
.order
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.find(|(i, ty)| *i != added_at_index && **ty == TypeId::of::<T>())
|
|
|
|
.map(|(i, _)| i)
|
|
|
|
{
|
|
|
|
self.order.remove(to_remove);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds the plugin [`Plugin`] at the end of this [`PluginGroupBuilder`]. If the plugin was
|
|
|
|
/// already in the group, it is removed from its previous place.
|
|
|
|
pub fn add<T: Plugin>(&mut self, plugin: T) -> &mut Self {
|
|
|
|
let target_index = self.order.len();
|
|
|
|
self.order.push(TypeId::of::<T>());
|
|
|
|
self.upsert_plugin_state(plugin, target_index);
|
2020-10-29 20:04:28 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-05-09 13:06:22 +00:00
|
|
|
/// Adds a [`Plugin`] in this [`PluginGroupBuilder`] before the plugin of type `Target`.
|
|
|
|
/// If the plugin was already the group, it is removed from its previous place. There must
|
|
|
|
/// be a plugin of type `Target` in the group or it will panic.
|
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>());
|
2022-05-09 13:06:22 +00:00
|
|
|
self.upsert_plugin_state(plugin, target_index);
|
2020-10-29 20:04:28 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-05-09 13:06:22 +00:00
|
|
|
/// Adds a [`Plugin`] in this [`PluginGroupBuilder`] after the plugin of type `Target`.
|
|
|
|
/// If the plugin was already the group, it is removed from its previous place. There must
|
|
|
|
/// be a plugin of type `Target` in the group or it will panic.
|
2020-10-29 20:04:28 +00:00
|
|
|
pub fn add_after<Target: Plugin, T: Plugin>(&mut self, plugin: T) -> &mut Self {
|
2022-05-09 13:06:22 +00:00
|
|
|
let target_index = self.index_of::<Target>() + 1;
|
|
|
|
self.order.insert(target_index, TypeId::of::<T>());
|
|
|
|
self.upsert_plugin_state(plugin, target_index);
|
2020-10-29 20:04:28 +00:00
|
|
|
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
|
2022-05-09 13:06:22 +00:00
|
|
|
/// opt back in to a [`Plugin`] after [disabling](Self::disable) it. If there are no plugins
|
|
|
|
/// of type `T` in this group, it will panic.
|
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-05-09 13:06:22 +00:00
|
|
|
/// Disables a [`Plugin`], preventing it from being added to the [`App`] with the rest of the
|
|
|
|
/// [`PluginGroup`]. The disabled [`Plugin`] keeps its place in the [`PluginGroup`], so it can
|
|
|
|
/// still be used for ordering with [`add_before`](Self::add_before) or
|
|
|
|
/// [`add_after`](Self::add_after), or it can be [re-enabled](Self::enable). If there are no
|
|
|
|
/// plugins of type `T` in this group, it will panic.
|
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-05-09 13:06:22 +00:00
|
|
|
/// Consumes the [`PluginGroupBuilder`] and [builds](Plugin::build) the contained [`Plugin`]s
|
|
|
|
/// in the order specified.
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-09 13:06:22 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::PluginGroupBuilder;
|
|
|
|
use crate::{App, Plugin};
|
|
|
|
|
|
|
|
struct PluginA;
|
|
|
|
impl Plugin for PluginA {
|
|
|
|
fn build(&self, _: &mut App) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PluginB;
|
|
|
|
impl Plugin for PluginB {
|
|
|
|
fn build(&self, _: &mut App) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PluginC;
|
|
|
|
impl Plugin for PluginC {
|
|
|
|
fn build(&self, _: &mut App) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic_ordering() {
|
|
|
|
let mut group = PluginGroupBuilder::default();
|
|
|
|
group.add(PluginA);
|
|
|
|
group.add(PluginB);
|
|
|
|
group.add(PluginC);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
group.order,
|
|
|
|
vec![
|
|
|
|
std::any::TypeId::of::<PluginA>(),
|
|
|
|
std::any::TypeId::of::<PluginB>(),
|
|
|
|
std::any::TypeId::of::<PluginC>(),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_after() {
|
|
|
|
let mut group = PluginGroupBuilder::default();
|
|
|
|
group.add(PluginA);
|
|
|
|
group.add(PluginB);
|
|
|
|
group.add_after::<PluginA, PluginC>(PluginC);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
group.order,
|
|
|
|
vec![
|
|
|
|
std::any::TypeId::of::<PluginA>(),
|
|
|
|
std::any::TypeId::of::<PluginC>(),
|
|
|
|
std::any::TypeId::of::<PluginB>(),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_before() {
|
|
|
|
let mut group = PluginGroupBuilder::default();
|
|
|
|
group.add(PluginA);
|
|
|
|
group.add(PluginB);
|
|
|
|
group.add_before::<PluginB, PluginC>(PluginC);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
group.order,
|
|
|
|
vec![
|
|
|
|
std::any::TypeId::of::<PluginA>(),
|
|
|
|
std::any::TypeId::of::<PluginC>(),
|
|
|
|
std::any::TypeId::of::<PluginB>(),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn readd() {
|
|
|
|
let mut group = PluginGroupBuilder::default();
|
|
|
|
group.add(PluginA);
|
|
|
|
group.add(PluginB);
|
|
|
|
group.add(PluginC);
|
|
|
|
group.add(PluginB);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
group.order,
|
|
|
|
vec![
|
|
|
|
std::any::TypeId::of::<PluginA>(),
|
|
|
|
std::any::TypeId::of::<PluginC>(),
|
|
|
|
std::any::TypeId::of::<PluginB>(),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn readd_after() {
|
|
|
|
let mut group = PluginGroupBuilder::default();
|
|
|
|
group.add(PluginA);
|
|
|
|
group.add(PluginB);
|
|
|
|
group.add(PluginC);
|
|
|
|
group.add_after::<PluginA, PluginC>(PluginC);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
group.order,
|
|
|
|
vec![
|
|
|
|
std::any::TypeId::of::<PluginA>(),
|
|
|
|
std::any::TypeId::of::<PluginC>(),
|
|
|
|
std::any::TypeId::of::<PluginB>(),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn readd_before() {
|
|
|
|
let mut group = PluginGroupBuilder::default();
|
|
|
|
group.add(PluginA);
|
|
|
|
group.add(PluginB);
|
|
|
|
group.add(PluginC);
|
|
|
|
group.add_before::<PluginB, PluginC>(PluginC);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
group.order,
|
|
|
|
vec![
|
|
|
|
std::any::TypeId::of::<PluginA>(),
|
|
|
|
std::any::TypeId::of::<PluginC>(),
|
|
|
|
std::any::TypeId::of::<PluginB>(),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|