2020-10-01 20:04:06 +00:00
|
|
|
use libloading::{Library, Symbol};
|
|
|
|
|
2021-07-27 20:21:06 +00:00
|
|
|
use bevy_app::{App, CreatePlugin, Plugin};
|
2020-10-01 20:04:06 +00:00
|
|
|
|
2021-05-01 20:07:06 +00:00
|
|
|
/// Dynamically links a plugin a the given path. The plugin must export a function with the
|
|
|
|
/// [`CreatePlugin`] signature named `_bevy_create_plugin`.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The specified plugin must be linked against the exact same libbevy.so as this program.
|
|
|
|
/// In addition the `_bevy_create_plugin` symbol must not be manually created, but instead created
|
|
|
|
/// by deriving `DynamicPlugin` on a unit struct implementing [`Plugin`].
|
|
|
|
pub unsafe fn dynamically_load_plugin(path: &str) -> (Library, Box<dyn Plugin>) {
|
|
|
|
let lib = Library::new(path).unwrap();
|
|
|
|
let func: Symbol<CreatePlugin> = lib.get(b"_bevy_create_plugin").unwrap();
|
|
|
|
let plugin = Box::from_raw(func());
|
|
|
|
(lib, plugin)
|
2020-10-01 20:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait DynamicPluginExt {
|
2021-05-01 20:07:06 +00:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Same as [`dynamically_load_plugin`].
|
|
|
|
unsafe fn load_plugin(&mut self, path: &str) -> &mut Self;
|
2020-10-01 20:04:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 20:21:06 +00:00
|
|
|
impl DynamicPluginExt for App {
|
2021-05-01 20:07:06 +00:00
|
|
|
unsafe fn load_plugin(&mut self, path: &str) -> &mut Self {
|
|
|
|
let (lib, plugin) = dynamically_load_plugin(path);
|
|
|
|
std::mem::forget(lib); // Ensure that the library is not automatically unloaded
|
2020-10-01 20:04:06 +00:00
|
|
|
plugin.build(self);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|