move dynamic plugin loading to its own optional crate (#544)
move dynamic plugin loading to its own crate
This commit is contained in:
		
							parent
							
								
									8e876463ec
								
							
						
					
					
						commit
						4c753e2588
					
				@ -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" }
 | 
			
		||||
 | 
			
		||||
@ -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"] }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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,
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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" }
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										23
									
								
								crates/bevy_dynamic_plugin/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								crates/bevy_dynamic_plugin/Cargo.toml
									
									
									
									
									
										Normal 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" }
 | 
			
		||||
							
								
								
									
										3
									
								
								crates/bevy_dynamic_plugin/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								crates/bevy_dynamic_plugin/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
			
		||||
mod loader;
 | 
			
		||||
 | 
			
		||||
pub use loader::*;
 | 
			
		||||
							
								
								
									
										27
									
								
								crates/bevy_dynamic_plugin/src/loader.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								crates/bevy_dynamic_plugin/src/loader.rs
									
									
									
									
									
										Normal 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
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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" }
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
@ -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::*;
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user