move dynamic plugin loading to its own optional crate (#544)

move dynamic plugin loading to its own crate
This commit is contained in:
EthanYidong 2020-10-01 13:04:06 -07:00 committed by GitHub
parent 8e876463ec
commit 4c753e2588
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 63 additions and 42 deletions

View file

@ -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" }

View file

@ -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"] }

View file

@ -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<T>(&mut self, plugin: T) -> &mut Self
where
T: Plugin,

View file

@ -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<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)
}
}

View file

@ -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" }

View file

@ -0,0 +1,23 @@
[package]
name = "bevy_dynamic_plugin"
version = "0.2.1"
authors = [
"Bevy Contributors <bevyengine@gmail.com>",
"Carter Anderson <mcanders1@gmail.com>",
]
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" }

View file

@ -0,0 +1,3 @@
mod loader;
pub use loader::*;

View file

@ -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
}
}

View file

@ -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" }

View file

@ -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;

View file

@ -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::*;

View file

@ -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