updated plugin init process

This commit is contained in:
michael-bailey 2022-04-05 06:55:29 +01:00
parent cac3a161ef
commit d58a088fe8
1 changed files with 18 additions and 8 deletions

View File

@ -2,18 +2,19 @@ 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 crate::plugin::plugin::{Plugin, PluginDetailsFn, TestPluginFn};
use tokio::sync::mpsc::Sender;
use crate::plugin::plugin::{GetPluginFn, Plugin};
use crate::plugin::plugin_details::PluginDetails;
/// # PluginManager
///
/// This struct handles the loading and unloading of plugins in the server
///
/// ## Attributes
/// - plugins: a hash_map of all loaded plugins
/// - plugins: A [HashMap] of all loaded plugins
pub struct PluginManager {
plugins: HashMap<String, Arc<dyn Plugin>>
plugins: HashMap<String, Arc<dyn Plugin>>,
}
impl PluginManager {
@ -31,14 +32,23 @@ impl PluginManager {
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());
unsafe {
let lib = Library::new(child.path()).unwrap();
let get_details = lib.get::<PluginDetailsFn>("details".as_ref()).unwrap();
let details = get_details();
println!("[PluginManager]: got details: {}", details);
let plugin = lib.get::<GetPluginFn>("get_plugin".as_ref()).unwrap();
let plugin: Arc<dyn Plugin> = plugin();
plugin.init();
tokio::spawn(async {
loop {
let cont = plugin.clone().run().await;
}
()
});
println!("[PluginManager]: got details: {}", plugin.details());
};
}
}