Skip to content

Commit cbeaec1

Browse files
EthanYidongmrk-its
authored andcommitted
move dynamic plugin loading to its own optional crate (bevyengine#544)
move dynamic plugin loading to its own crate
1 parent 1023f6c commit cbeaec1

File tree

12 files changed

+63
-42
lines changed

12 files changed

+63
-42
lines changed

Cargo.toml

+3-6
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,20 @@ exclude = ["assets/**/*", "tools/**/*", ".github/**/*", "crates/**/*"]
1818
[features]
1919
default = [
2020
"bevy_audio",
21+
"bevy_dynamic_plugin",
2122
"bevy_gilrs",
2223
"bevy_gltf",
2324
"bevy_wgpu",
2425
"bevy_winit",
2526
"render",
26-
"dynamic_plugins",
2727
"png",
2828
"hdr",
2929
"mp3",
3030
"x11",
3131
]
3232
profiler = ["bevy_ecs/profiler", "bevy_diagnostic/profiler"]
3333
wgpu_trace = ["bevy_wgpu/trace"]
34-
dynamic_plugins = [
35-
"bevy_core/dynamic_plugins",
36-
"bevy_app/dynamic_plugins",
37-
"bevy_type_registry/dynamic_plugins",
38-
]
34+
3935
# Rendering support
4036
render = ["bevy_pbr", "bevy_render", "bevy_sprite", "bevy_text", "bevy_ui"]
4137
# Image format support for texture loading (PNG and HDR are enabled by default)
@@ -79,6 +75,7 @@ bevy_audio = { path = "crates/bevy_audio", optional = true, version = "0.2.1" }
7975
bevy_gltf = { path = "crates/bevy_gltf", optional = true, version = "0.2.1" }
8076
bevy_pbr = { path = "crates/bevy_pbr", optional = true, version = "0.2.1" }
8177
bevy_render = { path = "crates/bevy_render", optional = true, version = "0.2.1" }
78+
bevy_dynamic_plugin = { path = "crates/bevy_dynamic_plugin", optional = true, version = "0.2.1" }
8279
bevy_sprite = { path = "crates/bevy_sprite", optional = true, version = "0.2.1" }
8380
bevy_text = { path = "crates/bevy_text", optional = true, version = "0.2.1" }
8481
bevy_ui = { path = "crates/bevy_ui", optional = true, version = "0.2.1" }

crates/bevy_app/Cargo.toml

-4
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@ repository = "https://github.com/bevyengine/bevy"
1212
license = "MIT"
1313
keywords = ["bevy"]
1414

15-
[features]
16-
dynamic_plugins = ["libloading"]
17-
1815
[dependencies]
1916
# bevy
2017
bevy_derive = { path = "../bevy_derive", version = "0.2.1" }
2118
bevy_ecs = { path = "../bevy_ecs", version = "0.2.1" }
2219
bevy_math = { path = "../bevy_math", version = "0.2.1" }
2320

2421
# other
25-
libloading = { version = "0.6", optional = true }
2622
log = { version = "0.4", features = ["release_max_level_info"] }
2723
serde = { version = "1.0", features = ["derive"] }
2824

crates/bevy_app/src/app_builder.rs

-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#[cfg(feature = "dynamic_plugins")]
2-
use crate::plugin::dynamically_load_plugin;
31
use crate::{
42
app::{App, AppExit},
53
event::Events,
@@ -244,14 +242,6 @@ impl AppBuilder {
244242
self
245243
}
246244

247-
#[cfg(feature = "dynamic_plugins")]
248-
pub fn load_plugin(&mut self, path: &str) -> &mut Self {
249-
let (_lib, plugin) = dynamically_load_plugin(path);
250-
log::debug!("loaded plugin: {}", plugin.name());
251-
plugin.build(self);
252-
self
253-
}
254-
255245
pub fn add_plugin<T>(&mut self, plugin: T) -> &mut Self
256246
where
257247
T: Plugin,

crates/bevy_app/src/plugin.rs

-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use crate::AppBuilder;
2-
#[cfg(feature = "dynamic_plugins")]
3-
use libloading::{Library, Symbol};
42
use std::any::Any;
53

64
/// A collection of Bevy App logic and configuration
@@ -14,15 +12,3 @@ pub trait Plugin: Any + Send + Sync {
1412
}
1513

1614
pub type CreatePlugin = unsafe fn() -> *mut dyn Plugin;
17-
18-
#[cfg(feature = "dynamic_plugins")]
19-
/// Dynamically links a plugin a the given path. The plugin must export the [CreatePlugin] function.
20-
pub fn dynamically_load_plugin(path: &str) -> (Library, Box<dyn Plugin>) {
21-
let lib = Library::new(path).unwrap();
22-
23-
unsafe {
24-
let func: Symbol<CreatePlugin> = lib.get(b"_create_plugin").unwrap();
25-
let plugin = Box::from_raw(func());
26-
(lib, plugin)
27-
}
28-
}

crates/bevy_core/Cargo.toml

-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ repository = "https://github.com/bevyengine/bevy"
1212
license = "MIT"
1313
keywords = ["bevy"]
1414

15-
[features]
16-
dynamic_plugins = [
17-
"bevy_app/dynamic_plugins",
18-
"bevy_type_registry/dynamic_plugins",
19-
]
2015

2116
[dependencies]
2217
bevy_app = { path = "../bevy_app", version = "0.2.1" }

crates/bevy_dynamic_plugin/Cargo.toml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "bevy_dynamic_plugin"
3+
version = "0.2.1"
4+
authors = [
5+
"Bevy Contributors <bevyengine@gmail.com>",
6+
"Carter Anderson <mcanders1@gmail.com>",
7+
]
8+
edition = "2018"
9+
description = "Provides dynamic plugin loading capabilities for non-wasm platforms"
10+
homepage = "https://bevyengine.org"
11+
repository = "https://github.com/bevyengine/bevy"
12+
license = "MIT"
13+
keywords = ["bevy"]
14+
15+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
16+
17+
[dependencies]
18+
# bevy
19+
bevy_app = { path = "../bevy_app", version = "0.2.1" }
20+
21+
# other
22+
log = { version = "0.4", features = ["release_max_level_info"] }
23+
libloading = { version = "0.6" }

crates/bevy_dynamic_plugin/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod loader;
2+
3+
pub use loader::*;
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use libloading::{Library, Symbol};
2+
3+
use bevy_app::{AppBuilder, CreatePlugin, Plugin};
4+
5+
/// Dynamically links a plugin a the given path. The plugin must export the [CreatePlugin] function.
6+
pub fn dynamically_load_plugin(path: &str) -> (Library, Box<dyn Plugin>) {
7+
let lib = Library::new(path).unwrap();
8+
9+
unsafe {
10+
let func: Symbol<CreatePlugin> = lib.get(b"_create_plugin").unwrap();
11+
let plugin = Box::from_raw(func());
12+
(lib, plugin)
13+
}
14+
}
15+
16+
pub trait DynamicPluginExt {
17+
fn load_plugin(&mut self, path: &str) -> &mut Self;
18+
}
19+
20+
impl DynamicPluginExt for AppBuilder {
21+
fn load_plugin(&mut self, path: &str) -> &mut Self {
22+
let (_lib, plugin) = dynamically_load_plugin(path);
23+
log::debug!("loaded plugin: {}", plugin.name());
24+
plugin.build(self);
25+
self
26+
}
27+
}

crates/bevy_type_registry/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ repository = "https://github.com/bevyengine/bevy"
1212
license = "MIT"
1313
keywords = ["bevy"]
1414

15-
[features]
16-
dynamic_plugins = ["bevy_app/dynamic_plugins"]
17-
1815
[dependencies]
1916
# bevy
2017
bevy_app = { path = "../bevy_app", version = "0.2.1" }

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,6 @@ pub use bevy_winit as winit;
8181

8282
#[cfg(feature = "bevy_wgpu")]
8383
pub use bevy_wgpu as wgpu;
84+
85+
#[cfg(feature = "bevy_dynamic_plugin")]
86+
pub use bevy_dynamic_plugin as dynamic_plugin;

src/prelude.rs

+3
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ pub use crate::text::prelude::*;
2121

2222
#[cfg(feature = "bevy_ui")]
2323
pub use crate::ui::prelude::*;
24+
25+
#[cfg(feature = "bevy_dynamic_plugin")]
26+
pub use crate::dynamic_plugin::*;

tools/publish.sh

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ crates=(
88
bevy_ecs/hecs
99
bevy_ecs
1010
bevy_app
11+
bevy_dynamic_plugin
1112
bevy_property/bevy_property_derive
1213
bevy_property
1314
bevy_type_registry

0 commit comments

Comments
 (0)