Skip to content

Commit c4692d1

Browse files
inodentryItsDoot
authored andcommitted
bevy_dynamic_plugin: make it possible to handle loading errors (bevyengine#6437)
# Objective Currently, `bevy_dynamic_plugin` simply panics on error. This makes it impossible to handle failures in applications that use this feature. For example, I'd like to build an optional expansion for my game, that may not be distributed to all users. I want to use `bevy_dynamic_plugin` for loading it. I want my game to try to load it on startup, but continue without it if it cannot be loaded. ## Solution - Make the `dynamically_load_plugin` function return a `Result`, so it can gracefully return loading errors. - Create an error enum type, to provide useful information about the kind of error. This adds `thiserror` to the dependencies of `bevy_dynamic_plugin`, but that dependency is already used in other parts of bevy (such as `bevy_asset`), so not a big deal. I chose not to change the behavior of the builder method in the App extension trait. I kept it as panicking. There is no clean way (that I'm aware of) to make a builder-style API that has fallible methods. So it is either a panic or a warning. I feel the panic is more appropriate. --- ## Changelog ### Changed - `bevy_dynamic_plugin::dynamically_load_plugin` now returns `Result` instead of panicking, to allow for error handling
1 parent 1461fa1 commit c4692d1

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

crates/bevy_dynamic_plugin/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ bevy_app = { path = "../bevy_app", version = "0.9.0-dev" }
1616

1717
# other
1818
libloading = { version = "0.7" }
19+
thiserror = "1.0"

crates/bevy_dynamic_plugin/src/loader.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
use libloading::{Library, Symbol};
2+
use thiserror::Error;
23

34
use bevy_app::{App, CreatePlugin, Plugin};
45

6+
/// Errors that can occur when loading a dynamic plugin
7+
#[derive(Debug, Error)]
8+
pub enum DynamicPluginLoadError {
9+
#[error("cannot load library for dynamic plugin: {0}")]
10+
Library(libloading::Error),
11+
#[error("dynamic library does not contain a valid Bevy dynamic plugin")]
12+
Plugin(libloading::Error),
13+
}
14+
515
/// Dynamically links a plugin at the given path. The plugin must export a function with the
616
/// [`CreatePlugin`] signature named `_bevy_create_plugin`.
717
///
@@ -10,11 +20,15 @@ use bevy_app::{App, CreatePlugin, Plugin};
1020
/// The specified plugin must be linked against the exact same libbevy.so as this program.
1121
/// In addition the `_bevy_create_plugin` symbol must not be manually created, but instead created
1222
/// by deriving `DynamicPlugin` on a unit struct implementing [`Plugin`].
13-
pub unsafe fn dynamically_load_plugin(path: &str) -> (Library, Box<dyn Plugin>) {
14-
let lib = Library::new(path).unwrap();
15-
let func: Symbol<CreatePlugin> = lib.get(b"_bevy_create_plugin").unwrap();
23+
pub unsafe fn dynamically_load_plugin(
24+
path: &str,
25+
) -> Result<(Library, Box<dyn Plugin>), DynamicPluginLoadError> {
26+
let lib = Library::new(path).map_err(DynamicPluginLoadError::Library)?;
27+
let func: Symbol<CreatePlugin> = lib
28+
.get(b"_bevy_create_plugin")
29+
.map_err(DynamicPluginLoadError::Plugin)?;
1630
let plugin = Box::from_raw(func());
17-
(lib, plugin)
31+
Ok((lib, plugin))
1832
}
1933

2034
pub trait DynamicPluginExt {
@@ -26,7 +40,7 @@ pub trait DynamicPluginExt {
2640

2741
impl DynamicPluginExt for App {
2842
unsafe fn load_plugin(&mut self, path: &str) -> &mut Self {
29-
let (lib, plugin) = dynamically_load_plugin(path);
43+
let (lib, plugin) = dynamically_load_plugin(path).unwrap();
3044
std::mem::forget(lib); // Ensure that the library is not automatically unloaded
3145
plugin.build(self);
3246
self

0 commit comments

Comments
 (0)