Skip to content

Instantly share code, notes, and snippets.

@thebluefish
Created March 9, 2024 01:13
Show Gist options
  • Save thebluefish/29c4c568c434c9c9c9b4079af3db40ce to your computer and use it in GitHub Desktop.
Save thebluefish/29c4c568c434c9c9c9b4079af3db40ce to your computer and use it in GitHub Desktop.
use bevy::app::prelude::*;
use dyn_clone::DynClone;
pub trait AppExt {
fn add_all_plugins(&mut self);
}
impl AppExt for App {
fn add_all_plugins(&mut self) {
for plugin in inventory::iter::<AutoPlugin> {
self.add_boxed_plugin(plugin.0.clone_box().upcast_plugin()).unwrap();
}
}
}
pub trait DynPlugin: Plugin + DynClone {
fn clone_box(&self) -> Box<dyn DynPlugin>;
fn upcast_plugin(self: Box<Self>) -> Box<dyn Plugin>;
}
impl<T: Plugin + Clone> DynPlugin for T {
fn clone_box(&self) -> Box<dyn DynPlugin> {
dyn_clone::clone_box(&*self)
}
fn upcast_plugin(self: Box<Self>) -> Box<dyn Plugin> {
self
}
}
pub struct AutoPlugin(Box<dyn DynPlugin>);
impl AutoPlugin {
pub fn from_plugin<P: Plugin + Clone>(plugin: P) -> Self {
AutoPlugin(Box::new(plugin))
}
}
inventory::collect!(AutoPlugin);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment