Refactor
This commit is contained in:
parent
4b94fa645c
commit
2128b8ea1e
4 changed files with 36 additions and 16 deletions
|
@ -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…
Add table
Add a link
Reference in a new issue