fixed cloning issues with plugin manager

This commit is contained in:
michael-bailey 2022-04-06 22:49:12 +01:00
parent 3ada0ce5a8
commit f2be134720
1 changed files with 26 additions and 20 deletions

View File

@ -1,12 +1,11 @@
use crate::plugin::plugin::{GetPluginFn, Plugin};
use libloading::Library;
use std::collections::HashMap;
use std::io::Error;
use std::sync::Arc;
use libloading::Library;
use mlua::Value::Thread;
use tokio::fs::{create_dir, File, read_dir};
use tokio::sync::mpsc::Sender;
use crate::plugin::plugin::{GetPluginFn, Plugin};
use crate::plugin::plugin_details::PluginDetails;
use tokio::fs::{create_dir, read_dir};
/// # PluginManager
/// This struct handles the loading and unloading of plugins in the server
@ -14,38 +13,45 @@ use crate::plugin::plugin_details::PluginDetails;
/// ## Attributes
/// - plugins: A [HashMap] of all loaded plugins
pub struct PluginManager {
#[allow(dead_code)]
plugins: HashMap<String, Arc<dyn Plugin>>,
}
impl PluginManager {
pub fn new() -> Arc<Self>{
return Arc::new(Self {
plugins: HashMap::new()
pub fn new() -> Arc<Self> {
Arc::new(Self {
plugins: HashMap::new(),
})
}
pub async fn load(&self) -> Result<(), Error> {
println!("[PluginManager]: loading plugins");
println!("[PluginManager]: from: {}", std::env::current_dir().unwrap().to_string_lossy());
println!(
"[PluginManager]: from: {}",
std::env::current_dir().unwrap().to_string_lossy()
);
if let Ok( mut plugins) = read_dir("./plugins").await {
if let Ok(mut plugins) = read_dir("./plugins").await {
while let Some(child) = plugins.next_entry().await? {
let metadata = child.metadata().await?;
if metadata.is_file() && child.path().extension().unwrap() == "dylib" {
println!("[PluginManager]: Library at:{}", child.path().to_string_lossy());
println!(
"[PluginManager]: Library at:{}",
child.path().to_string_lossy()
);
unsafe {
let lib = Library::new(child.path()).unwrap();
let plugin = lib.get::<GetPluginFn>("get_plugin".as_ref()).unwrap();
let plugin: Arc<dyn Plugin> = plugin();
let plugin_fn = lib.get::<GetPluginFn>("get_plugin".as_ref()).unwrap();
let plugin: Arc<dyn Plugin> = plugin_fn();
plugin.init();
tokio::spawn(async {
loop {
let cont = plugin.clone().run().await;
let cont = plugin.clone();
tokio::spawn(async move {
loop {
cont.run().await;
}
()
});
println!("[PluginManager]: got details: {}", plugin.details());
@ -57,4 +63,4 @@ impl PluginManager {
}
Ok(())
}
}
}