From 4b94fa645c24b5aafcfb64b6e029bc623c8ed935 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Fri, 28 Jan 2022 22:06:47 +0100 Subject: [PATCH] Add common struct --- Cargo.lock | 8 ++++++++ Cargo.toml | 2 +- crates/hotload_plugin/Cargo.lock | 7 +++++++ crates/hotload_plugin/Cargo.toml | 8 ++++++++ crates/hotload_plugin/src/lib.rs | 12 ++++++++++++ plugins/hotload_toto1/Cargo.lock | 7 +++++++ plugins/hotload_toto1/Cargo.toml | 2 +- plugins/hotload_toto1/src/lib.rs | 15 +++++++++------ src/main.rs | 6 ++++++ src/plugin_manager.rs | 23 ++++++----------------- 10 files changed, 65 insertions(+), 25 deletions(-) create mode 100644 crates/hotload_plugin/Cargo.lock create mode 100644 crates/hotload_plugin/Cargo.toml create mode 100644 crates/hotload_plugin/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 3ee3144..ff51627 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,13 +19,21 @@ name = "hotload" version = "0.1.0" dependencies = [ "glob", + "hotload_plugin", "hotload_toto1", "libloading", ] +[[package]] +name = "hotload_plugin" +version = "0.1.0" + [[package]] name = "hotload_toto1" version = "0.1.0" +dependencies = [ + "hotload_plugin", +] [[package]] name = "libloading" diff --git a/Cargo.toml b/Cargo.toml index 26a58a1..930e175 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,5 @@ edition = "2021" [dependencies] libloading = "0.7" glob = "0.3" -# hotload_plugin = { path = "./crates/hotload_plugin" } +hotload_plugin = { path = "./crates/hotload_plugin" } hotload_toto1 = { path = "./plugins/hotload_toto1" } diff --git a/crates/hotload_plugin/Cargo.lock b/crates/hotload_plugin/Cargo.lock new file mode 100644 index 0000000..f410574 --- /dev/null +++ b/crates/hotload_plugin/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "hotload_plugin" +version = "0.1.0" diff --git a/crates/hotload_plugin/Cargo.toml b/crates/hotload_plugin/Cargo.toml new file mode 100644 index 0000000..2177365 --- /dev/null +++ b/crates/hotload_plugin/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "hotload_plugin" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/crates/hotload_plugin/src/lib.rs b/crates/hotload_plugin/src/lib.rs new file mode 100644 index 0000000..6952330 --- /dev/null +++ b/crates/hotload_plugin/src/lib.rs @@ -0,0 +1,12 @@ +#[repr(C)] +#[derive(Debug, Clone)] +pub struct HotloadPlugin { + // The plugin's name + pub name: String, + + // The plugin's version + pub version: String, + + // The plugin's author + pub author: String, +} diff --git a/plugins/hotload_toto1/Cargo.lock b/plugins/hotload_toto1/Cargo.lock index f333e14..6edfafc 100644 --- a/plugins/hotload_toto1/Cargo.lock +++ b/plugins/hotload_toto1/Cargo.lock @@ -2,6 +2,13 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "hotload_plugin" +version = "0.1.0" + [[package]] name = "hotload_toto1" version = "0.1.0" +dependencies = [ + "hotload_plugin", +] diff --git a/plugins/hotload_toto1/Cargo.toml b/plugins/hotload_toto1/Cargo.toml index 5159015..5508914 100644 --- a/plugins/hotload_toto1/Cargo.toml +++ b/plugins/hotload_toto1/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -# hotload_plugin = { path = "../../crates/hotload_plugin" } +hotload_plugin = { path = "../../crates/hotload_plugin" } [lib] crate-type = ["dylib"] diff --git a/plugins/hotload_toto1/src/lib.rs b/plugins/hotload_toto1/src/lib.rs index eebfb2c..e9be413 100644 --- a/plugins/hotload_toto1/src/lib.rs +++ b/plugins/hotload_toto1/src/lib.rs @@ -1,9 +1,12 @@ -#[no_mangle] -pub extern fn version() -> String { - "0.2.0".to_string() -} +use hotload_plugin::HotloadPlugin; #[no_mangle] -pub extern fn name() -> String { - "test name".to_string() +pub extern "C" fn register() -> *const HotloadPlugin { + let plugin = Box::new(HotloadPlugin { + name: String::from("test_lib"), + version: String::from("0.1.0"), + author: String::from("mrdev023"), + }); + + Box::into_raw(plugin) } diff --git a/src/main.rs b/src/main.rs index 555d51a..1ae7cb0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,5 +3,11 @@ mod plugin_manager; fn main() { let plugin_manager = plugin_manager::PluginManager::new(); + unsafe { + for plugin in &plugin_manager.plugins { + println!("Plugin {:?}", (*(*plugin))); + } + } + println!("Plugins number : {}", plugin_manager.plugins.len()); } diff --git a/src/plugin_manager.rs b/src/plugin_manager.rs index 3b47df2..e57a05f 100644 --- a/src/plugin_manager.rs +++ b/src/plugin_manager.rs @@ -1,15 +1,10 @@ use glob::glob; +use hotload_plugin::HotloadPlugin; use std::env::current_exe; use std::path::PathBuf; -#[derive(Debug, Clone)] -pub struct Plugin { - pub version: String, - pub name: String, -} - pub struct PluginManager { - pub plugins: Vec, + pub plugins: Vec<*const HotloadPlugin>, } impl PluginManager { @@ -21,25 +16,19 @@ impl PluginManager { let lib_folder = parent_path.join("*.so"); for lib_file in glob(lib_folder.to_str().unwrap()).unwrap() { let plugin = Self::load_library(lib_file.unwrap()); - println!("Loaded plugin {:?}", plugin); plugins.push(plugin); } PluginManager { plugins } } - fn load_library(lib_file: PathBuf) -> Plugin { + fn load_library(lib_file: PathBuf) -> *const HotloadPlugin { println!("Loading library {:?}", lib_file); unsafe { let lib = libloading::Library::new(lib_file).unwrap(); - let version_func: libloading::Symbol String> = - lib.get(b"version").unwrap(); - let name_func: libloading::Symbol String> = - lib.get(b"name").unwrap(); - Plugin { - version: version_func(), - name: name_func(), - } + let register_func: libloading::Symbol *const HotloadPlugin> = + lib.get(b"register").unwrap(); + register_func() } } }