Skip to content

Commit

Permalink
move dynamic plugin loading to its own optional crate (bevyengine#544)
Browse files Browse the repository at this point in the history
move dynamic plugin loading to its own crate
  • Loading branch information
EthanYidong authored and mrk-its committed Oct 6, 2020
1 parent 1023f6c commit cbeaec1
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 42 deletions.
9 changes: 3 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,20 @@ 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",
"x11",
]
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)
Expand Down Expand Up @@ -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" }
Expand Down
4 changes: 0 additions & 4 deletions crates/bevy_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ 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" }
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"] }

Expand Down
10 changes: 0 additions & 10 deletions crates/bevy_app/src/app_builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(feature = "dynamic_plugins")]
use crate::plugin::dynamically_load_plugin;
use crate::{
app::{App, AppExit},
event::Events,
Expand Down Expand Up @@ -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<T>(&mut self, plugin: T) -> &mut Self
where
T: Plugin,
Expand Down
14 changes: 0 additions & 14 deletions crates/bevy_app/src/plugin.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<dyn Plugin>) {
let lib = Library::new(path).unwrap();

unsafe {
let func: Symbol<CreatePlugin> = lib.get(b"_create_plugin").unwrap();
let plugin = Box::from_raw(func());
(lib, plugin)
}
}
5 changes: 0 additions & 5 deletions crates/bevy_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
23 changes: 23 additions & 0 deletions crates/bevy_dynamic_plugin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "bevy_dynamic_plugin"
version = "0.2.1"
authors = [
"Bevy Contributors <[email protected]>",
"Carter Anderson <[email protected]>",
]
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" }
3 changes: 3 additions & 0 deletions crates/bevy_dynamic_plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod loader;

pub use loader::*;
27 changes: 27 additions & 0 deletions crates/bevy_dynamic_plugin/src/loader.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Plugin>) {
let lib = Library::new(path).unwrap();

unsafe {
let func: Symbol<CreatePlugin> = 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
}
}
3 changes: 0 additions & 3 deletions crates/bevy_type_registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 3 additions & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
1 change: 1 addition & 0 deletions tools/publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cbeaec1

Please sign in to comment.