Files
rustdesk/src/plugins.rs

106 lines
2.7 KiB
Rust
Raw Normal View History

2023-04-08 18:17:13 +08:00
use std::{
collections::HashMap,
ffi::CStr,
path::Path,
sync::{Arc, RwLock},
};
2023-04-08 17:46:47 +08:00
use hbb_common::anyhow::{anyhow, Error};
use lazy_static::lazy_static;
2023-04-08 18:17:13 +08:00
use libloading::Library;
2023-04-08 17:46:47 +08:00
lazy_static! {
pub static ref PLUGIN_REGISTRAR: Arc<PluginRegistar<PluginImpl>> =
Arc::new(PluginRegistar::<PluginImpl>::default());
}
2023-04-08 18:17:13 +08:00
// API needed to be implemented by plugins.
pub type PluginInitFunc = fn() -> i32;
// API needed to be implemented by plugins.
pub type PluginDisposeFunc = fn() -> i32;
2023-04-08 17:46:47 +08:00
pub trait Plugin {
// Return: the unique ID which identifies this plugin.
fn plugin_id(&self) -> String;
// Return: the name which is human-readable.
fn plugin_name(&self) -> String;
2023-04-08 18:17:13 +08:00
// Return: the virtual table of the plugin.
fn plugin_vt(&self) -> &RustDeskPluginTable;
}
#[repr(C)]
#[derive(Default, Clone)]
pub struct RustDeskPluginTable {
pub init: Option<PluginInitFunc>,
pub dispose: Option<PluginDisposeFunc>,
2023-04-08 17:46:47 +08:00
}
#[derive(Default, Clone)]
pub struct PluginImpl {
2023-04-08 18:17:13 +08:00
vt: RustDeskPluginTable,
2023-04-08 17:46:47 +08:00
id: String,
name: String,
}
impl Plugin for PluginImpl {
fn plugin_id(&self) -> String {
self.id.to_owned()
}
fn plugin_name(&self) -> String {
self.name.to_owned()
}
2023-04-08 18:17:13 +08:00
fn plugin_vt(&self) -> &RustDeskPluginTable {
&self.vt
}
2023-04-08 17:46:47 +08:00
}
#[derive(Default, Clone)]
pub struct PluginRegistar<P: Plugin> {
2023-04-08 18:17:13 +08:00
plugins: Arc<RwLock<HashMap<String, P>>>,
2023-04-08 17:46:47 +08:00
}
impl<P: Plugin> PluginRegistar<P> {
pub fn load_plugin(&self, path: *const i8) -> i32 {
let p = unsafe { CStr::from_ptr(path) };
2023-04-08 18:17:13 +08:00
let lib_path = p.to_str().unwrap_or("").to_owned();
let lib = unsafe { libloading::Library::new(lib_path.as_str()) };
match lib {
Ok(lib) => match lib.try_into() {
Ok(plugin) => {
PLUGIN_REGISTRAR
.plugins
.write()
.unwrap()
.insert(lib_path, plugin);
return 0;
}
Err(err) => {
eprintln!("Load plugin failed: {}", err);
}
},
Err(err) => {
eprintln!("Load plugin failed: {}", err);
}
}
-1
2023-04-08 17:46:47 +08:00
}
pub fn unload_plugin(&self, path: *const i8) -> i32 {
let p = unsafe { CStr::from_ptr(path) };
2023-04-08 18:17:13 +08:00
let lib_path = p.to_str().unwrap_or("").to_owned();
match PLUGIN_REGISTRAR.plugins.write().unwrap().remove(&lib_path) {
Some(_) => 0,
None => -1,
}
2023-04-08 17:46:47 +08:00
}
}
2023-04-08 18:17:13 +08:00
impl TryFrom<Library> for PluginImpl {
2023-04-08 17:46:47 +08:00
type Error = Error;
2023-04-08 18:17:13 +08:00
fn try_from(library: Library) -> Result<Self, Self::Error> {
todo!()
2023-04-08 17:46:47 +08:00
}
}