1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use libloading::{Library, Symbol};
use bevy_app::{App, CreatePlugin, 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)
}
pub trait DynamicPluginExt {
unsafe fn load_plugin(&mut self, path: &str) -> &mut Self;
}
impl DynamicPluginExt for App {
unsafe fn load_plugin(&mut self, path: &str) -> &mut Self {
let (lib, plugin) = dynamically_load_plugin(path);
std::mem::forget(lib);
plugin.build(self);
self
}
}