add: init/dispose hook when load/unload plugins
This commit is contained in:
parent
a0a0079e1c
commit
e3c828a6f1
@ -4,7 +4,10 @@ use std::{
|
|||||||
sync::{Arc, RwLock},
|
sync::{Arc, RwLock},
|
||||||
};
|
};
|
||||||
|
|
||||||
use hbb_common::{anyhow::Error, log::debug};
|
use hbb_common::{
|
||||||
|
anyhow::Error,
|
||||||
|
log::{debug, error},
|
||||||
|
};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use libloading::{Library, Symbol};
|
use libloading::{Library, Symbol};
|
||||||
|
|
||||||
@ -82,6 +85,18 @@ impl<P: Plugin> PluginRegistar<P> {
|
|||||||
match lib {
|
match lib {
|
||||||
Ok(lib) => match lib.try_into() {
|
Ok(lib) => match lib.try_into() {
|
||||||
Ok(plugin) => {
|
Ok(plugin) => {
|
||||||
|
let plugin: PluginImpl = plugin;
|
||||||
|
// try to initialize this plugin
|
||||||
|
if let Some(init) = plugin.plugin_vt().init {
|
||||||
|
let init_ret = init();
|
||||||
|
if init_ret != 0 {
|
||||||
|
error!(
|
||||||
|
"Error when initializing the plugin {} with error code {}.",
|
||||||
|
plugin.name, init_ret
|
||||||
|
);
|
||||||
|
return init_ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
PLUGIN_REGISTRAR
|
PLUGIN_REGISTRAR
|
||||||
.plugins
|
.plugins
|
||||||
.write()
|
.write()
|
||||||
@ -104,7 +119,12 @@ impl<P: Plugin> PluginRegistar<P> {
|
|||||||
let p = unsafe { CStr::from_ptr(path) };
|
let p = unsafe { CStr::from_ptr(path) };
|
||||||
let lib_path = p.to_str().unwrap_or("").to_owned();
|
let lib_path = p.to_str().unwrap_or("").to_owned();
|
||||||
match PLUGIN_REGISTRAR.plugins.write().unwrap().remove(&lib_path) {
|
match PLUGIN_REGISTRAR.plugins.write().unwrap().remove(&lib_path) {
|
||||||
Some(_) => 0,
|
Some(plugin) => {
|
||||||
|
if let Some(dispose) = plugin.plugin_vt().dispose {
|
||||||
|
return dispose();
|
||||||
|
}
|
||||||
|
0
|
||||||
|
}
|
||||||
None => -1,
|
None => -1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user