Refactor
This commit is contained in:
parent
4b94fa645c
commit
2128b8ea1e
4 changed files with 36 additions and 16 deletions
|
@ -1,6 +1,5 @@
|
|||
#[repr(C)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HotloadPlugin {
|
||||
pub struct HotloadPluginInfo {
|
||||
// The plugin's name
|
||||
pub name: String,
|
||||
|
||||
|
|
|
@ -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"),
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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<HotloadPluginInfo, String> {
|
||||
let get_info_fn: libloading::Symbol<extern "C" fn() -> *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<Plugin>,
|
||||
}
|
||||
|
||||
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<extern "C" fn() -> *const HotloadPlugin> =
|
||||
lib.get(b"register").unwrap();
|
||||
register_func()
|
||||
Plugin::new(lib)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue