@@ -7,8 +7,10 @@ use bevy_app::{App, CreatePlugin, Plugin};
7
7
/// Errors that can occur when loading a dynamic plugin
8
8
#[ derive( Debug , Error ) ]
9
9
pub enum DynamicPluginLoadError {
10
+ /// An error occurred when loading a dynamic library.
10
11
#[ error( "cannot load library for dynamic plugin: {0}" ) ]
11
12
Library ( #[ source] libloading:: Error ) ,
13
+ /// An error occurred when loading a library without a valid Bevy plugin.
12
14
#[ error( "dynamic library does not contain a valid Bevy dynamic plugin" ) ]
13
15
Plugin ( #[ source] libloading:: Error ) ,
14
16
}
@@ -18,22 +20,22 @@ pub enum DynamicPluginLoadError {
18
20
///
19
21
/// # Safety
20
22
///
21
- /// The specified plugin must be linked against the exact same libbevy.so as this program.
23
+ /// The specified plugin must be linked against the exact same ` libbevy.so` as this program.
22
24
/// In addition the `_bevy_create_plugin` symbol must not be manually created, but instead created
23
25
/// by deriving `DynamicPlugin` on a unit struct implementing [`Plugin`].
24
26
///
25
- /// Dynamically loading plugins is orchestrated through dynamic linking. When linking against foreign
26
- /// code, initialization routines may be run (as well as termination routines when the program exits).
27
- /// The caller of this function is responsible for ensuring these routines are sound. For more
28
- /// information, please see the safety section of [`libloading::Library::new`].
27
+ /// Dynamically loading plugins is orchestrated through dynamic linking. When linking against
28
+ /// foreign code, initialization routines may be run (as well as termination routines when the
29
+ /// program exits). The caller of this function is responsible for ensuring these routines are
30
+ /// sound. For more information, please see the safety section of [`libloading::Library::new`].
29
31
pub unsafe fn dynamically_load_plugin < P : AsRef < OsStr > > (
30
32
path : P ,
31
33
) -> Result < ( Library , Box < dyn Plugin > ) , DynamicPluginLoadError > {
32
34
// SAFETY: Caller must follow the safety requirements of Library::new.
33
35
let lib = unsafe { Library :: new ( path) . map_err ( DynamicPluginLoadError :: Library ) ? } ;
34
36
35
- // SAFETY: Loaded plugins are not allowed to specify `_bevy_create_plugin` symbol manually, but must
36
- // instead automatically generate it through `DynamicPlugin`.
37
+ // SAFETY: Loaded plugins are not allowed to specify `_bevy_create_plugin` symbol manually, but
38
+ // must instead automatically generate it through `DynamicPlugin`.
37
39
let func: Symbol < CreatePlugin > = unsafe {
38
40
lib. get ( b"_bevy_create_plugin" )
39
41
. map_err ( DynamicPluginLoadError :: Plugin ) ?
@@ -46,10 +48,15 @@ pub unsafe fn dynamically_load_plugin<P: AsRef<OsStr>>(
46
48
Ok ( ( lib, plugin) )
47
49
}
48
50
51
+ /// An extension trait for [`App`] that allows loading dynamic plugins.
49
52
pub trait DynamicPluginExt {
53
+ /// Dynamically links a plugin at the given path, registering the plugin.
54
+ ///
55
+ /// For more details, see [`dynamically_load_plugin`].
56
+ ///
50
57
/// # Safety
51
58
///
52
- /// Same as [`dynamically_load_plugin`].
59
+ /// See [`dynamically_load_plugin`]'s safety section .
53
60
unsafe fn load_plugin < P : AsRef < OsStr > > ( & mut self , path : P ) -> & mut Self ;
54
61
}
55
62
0 commit comments