added plugin manager to server

This commit is contained in:
michael-bailey 2022-03-13 22:53:06 +00:00
parent be847e39b7
commit 83c8a6c2b7
2 changed files with 27 additions and 6 deletions

View File

@ -1,7 +1,10 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::io::Error;
use std::sync::Arc; use std::sync::Arc;
use libloading::Library; use libloading::Library;
use crate::plugin::Plugin::Plugin; use tokio::fs::{create_dir, File, read_dir};
use crate::plugin::plugin::{Plugin, PluginDetailsFn, TestPluginFn};
use crate::plugin::plugin_details::PluginDetails;
/// # PluginManager /// # PluginManager
/// ///
@ -20,11 +23,28 @@ impl PluginManager {
}) })
} }
pub async fn load(&self) { pub async fn load(&self) -> Result<(), Error> {
unsafe {
println!("[PluginManager]: loading plugins"); 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());
let lib = Library::new("./plugins")?;
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);
};
}
}
} else {
create_dir("./plugins").await?;
} }
Ok(())
} }
} }

View File

@ -120,6 +120,7 @@ impl Server {
// start client manager and network manager // start client manager and network manager
self.network_manager.clone().start(); self.network_manager.clone().start();
self.client_manager.clone().start(); self.client_manager.clone().start();
let _ = self.plugin_manager.clone().load().await;
// clone block items // clone block items
let server = self.clone(); let server = self.clone();