diff --git a/Cargo.toml b/Cargo.toml index f517ee8f22..9d55b8471f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,12 +18,12 @@ exclude = ["assets/**/*", "tools/**/*", ".github/**/*", "crates/**/*"] [features] default = [ "bevy_audio", + "bevy_dynamic_plugin", "bevy_gilrs", "bevy_gltf", "bevy_wgpu", "bevy_winit", "render", - "dynamic_plugins", "png", "hdr", "mp3", @@ -31,11 +31,7 @@ default = [ ] profiler = ["bevy_ecs/profiler", "bevy_diagnostic/profiler"] wgpu_trace = ["bevy_wgpu/trace"] -dynamic_plugins = [ - "bevy_core/dynamic_plugins", - "bevy_app/dynamic_plugins", - "bevy_type_registry/dynamic_plugins", -] + # Rendering support render = ["bevy_pbr", "bevy_render", "bevy_sprite", "bevy_text", "bevy_ui"] # 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" } bevy_gltf = { path = "crates/bevy_gltf", optional = true, version = "0.2.1" } bevy_pbr = { path = "crates/bevy_pbr", optional = true, version = "0.2.1" } bevy_render = { path = "crates/bevy_render", optional = true, version = "0.2.1" } +bevy_dynamic_plugin = { path = "crates/bevy_dynamic_plugin", optional = true, version = "0.2.1" } bevy_sprite = { path = "crates/bevy_sprite", optional = true, version = "0.2.1" } bevy_text = { path = "crates/bevy_text", optional = true, version = "0.2.1" } bevy_ui = { path = "crates/bevy_ui", optional = true, version = "0.2.1" } diff --git a/crates/bevy_app/Cargo.toml b/crates/bevy_app/Cargo.toml index 2b7e6443de..6f0fa28fee 100644 --- a/crates/bevy_app/Cargo.toml +++ b/crates/bevy_app/Cargo.toml @@ -12,9 +12,6 @@ repository = "https://github.com/bevyengine/bevy" license = "MIT" keywords = ["bevy"] -[features] -dynamic_plugins = ["libloading"] - [dependencies] # bevy bevy_derive = { path = "../bevy_derive", version = "0.2.1" } @@ -22,7 +19,6 @@ bevy_ecs = { path = "../bevy_ecs", version = "0.2.1" } bevy_math = { path = "../bevy_math", version = "0.2.1" } # other -libloading = { version = "0.6", optional = true } log = { version = "0.4", features = ["release_max_level_info"] } serde = { version = "1.0", features = ["derive"] } diff --git a/crates/bevy_app/src/app_builder.rs b/crates/bevy_app/src/app_builder.rs index 548b898286..6672c87726 100644 --- a/crates/bevy_app/src/app_builder.rs +++ b/crates/bevy_app/src/app_builder.rs @@ -1,5 +1,3 @@ -#[cfg(feature = "dynamic_plugins")] -use crate::plugin::dynamically_load_plugin; use crate::{ app::{App, AppExit}, event::Events, @@ -244,14 +242,6 @@ impl AppBuilder { self } - #[cfg(feature = "dynamic_plugins")] - pub fn load_plugin(&mut self, path: &str) -> &mut Self { - let (_lib, plugin) = dynamically_load_plugin(path); - log::debug!("loaded plugin: {}", plugin.name()); - plugin.build(self); - self - } - pub fn add_plugin(&mut self, plugin: T) -> &mut Self where T: Plugin, diff --git a/crates/bevy_app/src/plugin.rs b/crates/bevy_app/src/plugin.rs index d97a7be21a..cbb49239ec 100644 --- a/crates/bevy_app/src/plugin.rs +++ b/crates/bevy_app/src/plugin.rs @@ -1,6 +1,4 @@ use crate::AppBuilder; -#[cfg(feature = "dynamic_plugins")] -use libloading::{Library, Symbol}; use std::any::Any; /// A collection of Bevy App logic and configuration @@ -14,15 +12,3 @@ pub trait Plugin: Any + Send + Sync { } pub type CreatePlugin = unsafe fn() -> *mut dyn Plugin; - -#[cfg(feature = "dynamic_plugins")] -/// Dynamically links a plugin a the given path. The plugin must export the [CreatePlugin] function. -pub fn dynamically_load_plugin(path: &str) -> (Library, Box) { - let lib = Library::new(path).unwrap(); - - unsafe { - let func: Symbol = lib.get(b"_create_plugin").unwrap(); - let plugin = Box::from_raw(func()); - (lib, plugin) - } -} diff --git a/crates/bevy_core/Cargo.toml b/crates/bevy_core/Cargo.toml index 23eeff107e..121680d339 100644 --- a/crates/bevy_core/Cargo.toml +++ b/crates/bevy_core/Cargo.toml @@ -12,11 +12,6 @@ repository = "https://github.com/bevyengine/bevy" license = "MIT" keywords = ["bevy"] -[features] -dynamic_plugins = [ - "bevy_app/dynamic_plugins", - "bevy_type_registry/dynamic_plugins", -] [dependencies] bevy_app = { path = "../bevy_app", version = "0.2.1" } diff --git a/crates/bevy_dynamic_plugin/Cargo.toml b/crates/bevy_dynamic_plugin/Cargo.toml new file mode 100644 index 0000000000..b46ee9676e --- /dev/null +++ b/crates/bevy_dynamic_plugin/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "bevy_dynamic_plugin" +version = "0.2.1" +authors = [ + "Bevy Contributors ", + "Carter Anderson ", +] +edition = "2018" +description = "Provides dynamic plugin loading capabilities for non-wasm platforms" +homepage = "https://bevyengine.org" +repository = "https://github.com/bevyengine/bevy" +license = "MIT" +keywords = ["bevy"] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +# bevy +bevy_app = { path = "../bevy_app", version = "0.2.1" } + +# other +log = { version = "0.4", features = ["release_max_level_info"] } +libloading = { version = "0.6" } diff --git a/crates/bevy_dynamic_plugin/src/lib.rs b/crates/bevy_dynamic_plugin/src/lib.rs new file mode 100644 index 0000000000..4ff6f92606 --- /dev/null +++ b/crates/bevy_dynamic_plugin/src/lib.rs @@ -0,0 +1,3 @@ +mod loader; + +pub use loader::*; diff --git a/crates/bevy_dynamic_plugin/src/loader.rs b/crates/bevy_dynamic_plugin/src/loader.rs new file mode 100644 index 0000000000..7d9670dfea --- /dev/null +++ b/crates/bevy_dynamic_plugin/src/loader.rs @@ -0,0 +1,27 @@ +use libloading::{Library, Symbol}; + +use bevy_app::{AppBuilder, CreatePlugin, Plugin}; + +/// Dynamically links a plugin a the given path. The plugin must export the [CreatePlugin] function. +pub fn dynamically_load_plugin(path: &str) -> (Library, Box) { + let lib = Library::new(path).unwrap(); + + unsafe { + let func: Symbol = lib.get(b"_create_plugin").unwrap(); + let plugin = Box::from_raw(func()); + (lib, plugin) + } +} + +pub trait DynamicPluginExt { + fn load_plugin(&mut self, path: &str) -> &mut Self; +} + +impl DynamicPluginExt for AppBuilder { + fn load_plugin(&mut self, path: &str) -> &mut Self { + let (_lib, plugin) = dynamically_load_plugin(path); + log::debug!("loaded plugin: {}", plugin.name()); + plugin.build(self); + self + } +} diff --git a/crates/bevy_type_registry/Cargo.toml b/crates/bevy_type_registry/Cargo.toml index 9e7d47441d..900854080b 100644 --- a/crates/bevy_type_registry/Cargo.toml +++ b/crates/bevy_type_registry/Cargo.toml @@ -12,9 +12,6 @@ repository = "https://github.com/bevyengine/bevy" license = "MIT" keywords = ["bevy"] -[features] -dynamic_plugins = ["bevy_app/dynamic_plugins"] - [dependencies] # bevy bevy_app = { path = "../bevy_app", version = "0.2.1" } diff --git a/src/lib.rs b/src/lib.rs index 5b8c092afb..35c9ae3b75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -81,3 +81,6 @@ pub use bevy_winit as winit; #[cfg(feature = "bevy_wgpu")] pub use bevy_wgpu as wgpu; + +#[cfg(feature = "bevy_dynamic_plugin")] +pub use bevy_dynamic_plugin as dynamic_plugin; diff --git a/src/prelude.rs b/src/prelude.rs index 76a63b94a3..7c252ffec2 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -21,3 +21,6 @@ pub use crate::text::prelude::*; #[cfg(feature = "bevy_ui")] pub use crate::ui::prelude::*; + +#[cfg(feature = "bevy_dynamic_plugin")] +pub use crate::dynamic_plugin::*; diff --git a/tools/publish.sh b/tools/publish.sh index 9ce3e2365a..220c1d5b49 100644 --- a/tools/publish.sh +++ b/tools/publish.sh @@ -8,6 +8,7 @@ crates=( bevy_ecs/hecs bevy_ecs bevy_app + bevy_dynamic_plugin bevy_property/bevy_property_derive bevy_property bevy_type_registry