From 2128b8ea1e8480559c6556185b2dd746826f5c65 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Fri, 28 Jan 2022 22:48:29 +0100 Subject: [PATCH] Refactor --- crates/hotload_plugin/src/lib.rs | 3 +-- plugins/hotload_toto1/src/lib.rs | 6 +++--- src/main.rs | 6 ++---- src/plugin_manager.rs | 37 ++++++++++++++++++++++++++------ 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/crates/hotload_plugin/src/lib.rs b/crates/hotload_plugin/src/lib.rs index 6952330..9958e8e 100644 --- a/crates/hotload_plugin/src/lib.rs +++ b/crates/hotload_plugin/src/lib.rs @@ -1,6 +1,5 @@ -#[repr(C)] #[derive(Debug, Clone)] -pub struct HotloadPlugin { +pub struct HotloadPluginInfo { // The plugin's name pub name: String, diff --git a/plugins/hotload_toto1/src/lib.rs b/plugins/hotload_toto1/src/lib.rs index e9be413..9bf0861 100644 --- a/plugins/hotload_toto1/src/lib.rs +++ b/plugins/hotload_toto1/src/lib.rs @@ -1,8 +1,8 @@ -use hotload_plugin::HotloadPlugin; +use hotload_plugin::HotloadPluginInfo; #[no_mangle] -pub extern "C" fn register() -> *const HotloadPlugin { - let plugin = Box::new(HotloadPlugin { +pub extern fn plugin_info() -> *const HotloadPluginInfo { + let plugin = Box::new(HotloadPluginInfo { name: String::from("test_lib"), version: String::from("0.1.0"), author: String::from("mrdev023"), diff --git a/src/main.rs b/src/main.rs index 1ae7cb0..eb8580b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,10 +3,8 @@ mod plugin_manager; fn main() { let plugin_manager = plugin_manager::PluginManager::new(); - unsafe { - for plugin in &plugin_manager.plugins { - println!("Plugin {:?}", (*(*plugin))); - } + for plugin in &plugin_manager.plugins { + println!("Plugin {:?}", plugin.get_info().unwrap()); } println!("Plugins number : {}", plugin_manager.plugins.len()); diff --git a/src/plugin_manager.rs b/src/plugin_manager.rs index e57a05f..acf3f4e 100644 --- a/src/plugin_manager.rs +++ b/src/plugin_manager.rs @@ -1,10 +1,35 @@ use glob::glob; -use hotload_plugin::HotloadPlugin; +use hotload_plugin::HotloadPluginInfo; use std::env::current_exe; use std::path::PathBuf; +pub struct Plugin { + library: libloading::Library, +} + +impl Plugin { + pub fn new(library: libloading::Library) -> Self { + Self { library } + } + + pub fn get_info(&self) -> Result { + let get_info_fn: libloading::Symbol *const HotloadPluginInfo> = unsafe { + self.library + .get(b"plugin_info\0") + .map_err(|e| format!("{}", e))? + }; + + let info = get_info_fn(); + + if info.is_null() { + return Err(String::from("get_info function returned null")); + } + Ok(unsafe { (*info).clone() }) + } +} + pub struct PluginManager { - pub plugins: Vec<*const HotloadPlugin>, + pub plugins: Vec, } impl PluginManager { @@ -22,13 +47,11 @@ impl PluginManager { PluginManager { plugins } } - fn load_library(lib_file: PathBuf) -> *const HotloadPlugin { - println!("Loading library {:?}", lib_file); + fn load_library(lib_file: PathBuf) -> Plugin { + println!("Loading library {:?}", lib_file.file_name().unwrap()); unsafe { let lib = libloading::Library::new(lib_file).unwrap(); - let register_func: libloading::Symbol *const HotloadPlugin> = - lib.get(b"register").unwrap(); - register_func() + Plugin::new(lib) } } }