use crate::{App, AppError, Plugin};
use bevy_utils::{tracing::debug, tracing::warn, HashMap};
use std::any::TypeId;
pub trait PluginGroup: Sized {
fn build(self) -> PluginGroupBuilder;
fn name() -> String {
std::any::type_name::<Self>().to_string()
}
fn set<T: Plugin>(self, plugin: T) -> PluginGroupBuilder {
self.build().set(plugin)
}
}
struct PluginEntry {
plugin: Box<dyn Plugin>,
enabled: bool,
}
impl PluginGroup for PluginGroupBuilder {
fn build(self) -> PluginGroupBuilder {
self
}
}
pub struct PluginGroupBuilder {
group_name: String,
plugins: HashMap<TypeId, PluginEntry>,
order: Vec<TypeId>,
}
impl PluginGroupBuilder {
pub fn start<PG: PluginGroup>() -> Self {
Self {
group_name: PG::name(),
plugins: Default::default(),
order: Default::default(),
}
}
fn index_of<Target: Plugin>(&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>()
),
}
}
fn upsert_plugin_state<T: Plugin>(&mut self, plugin: T, added_at_index: usize) {
if let Some(entry) = self.plugins.insert(
TypeId::of::<T>(),
PluginEntry {
plugin: Box::new(plugin),
enabled: true,
},
) {
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);
}
}
}
pub fn set<T: Plugin>(mut self, plugin: T) -> Self {
let entry = self.plugins.get_mut(&TypeId::of::<T>()).unwrap_or_else(|| {
panic!(
"{} does not exist in this PluginGroup",
std::any::type_name::<T>(),
)
});
entry.plugin = Box::new(plugin);
self
}
#[allow(clippy::should_implement_trait)]
pub fn add<T: Plugin>(mut self, plugin: T) -> Self {
let target_index = self.order.len();
self.order.push(TypeId::of::<T>());
self.upsert_plugin_state(plugin, target_index);
self
}
pub fn add_before<Target: Plugin, T: Plugin>(mut self, plugin: T) -> Self {
let target_index = self.index_of::<Target>();
self.order.insert(target_index, TypeId::of::<T>());
self.upsert_plugin_state(plugin, target_index);
self
}
pub fn add_after<Target: Plugin, T: Plugin>(mut self, plugin: T) -> Self {
let target_index = self.index_of::<Target>() + 1;
self.order.insert(target_index, TypeId::of::<T>());
self.upsert_plugin_state(plugin, target_index);
self
}
pub fn enable<T: Plugin>(mut self) -> Self {
let mut plugin_entry = self
.plugins
.get_mut(&TypeId::of::<T>())
.expect("Cannot enable a plugin that does not exist.");
plugin_entry.enabled = true;
self
}
pub fn disable<T: Plugin>(mut self) -> Self {
let mut plugin_entry = self
.plugins
.get_mut(&TypeId::of::<T>())
.expect("Cannot disable a plugin that does not exist.");
plugin_entry.enabled = false;
self
}
pub fn finish(mut self, app: &mut App) {
for ty in &self.order {
if let Some(entry) = self.plugins.remove(ty) {
if entry.enabled {
debug!("added plugin: {}", entry.plugin.name());
if let Err(AppError::DuplicatePlugin { plugin_name }) =
app.add_boxed_plugin(entry.plugin)
{
panic!(
"Error adding plugin {} in group {}: plugin was already added in application",
plugin_name,
self.group_name
);
}
}
}
}
}
}
#[doc(hidden)]
pub struct NoopPluginGroup;
impl PluginGroup for NoopPluginGroup {
fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::start::<Self>()
}
}
#[cfg(test)]
mod tests {
use super::PluginGroupBuilder;
use crate::{App, NoopPluginGroup, 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 group = PluginGroupBuilder::start::<NoopPluginGroup>()
.add(PluginA)
.add(PluginB)
.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 group = PluginGroupBuilder::start::<NoopPluginGroup>()
.add(PluginA)
.add(PluginB)
.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 group = PluginGroupBuilder::start::<NoopPluginGroup>()
.add(PluginA)
.add(PluginB)
.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 group = PluginGroupBuilder::start::<NoopPluginGroup>()
.add(PluginA)
.add(PluginB)
.add(PluginC)
.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 group = PluginGroupBuilder::start::<NoopPluginGroup>()
.add(PluginA)
.add(PluginB)
.add(PluginC)
.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 group = PluginGroupBuilder::start::<NoopPluginGroup>()
.add(PluginA)
.add(PluginB)
.add(PluginC)
.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>(),
]
);
}
}