Created
March 9, 2024 01:13
-
-
Save thebluefish/29c4c568c434c9c9c9b4079af3db40ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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