101 lines
2.4 KiB
Rust
101 lines
2.4 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use self::plugin::Plugin;
|
|
|
|
|
|
mod plugin;
|
|
|
|
pub struct PlugMan {
|
|
plugins: Vec<Plugin>,
|
|
len_cap: usize,
|
|
}
|
|
|
|
|
|
impl PlugMan {
|
|
pub fn new(len_cap: usize) -> Self {
|
|
Self {
|
|
plugins: Vec::new(),
|
|
len_cap
|
|
}
|
|
}
|
|
|
|
pub fn load(&mut self, dir: PathBuf) -> Result<(), PluginError>{
|
|
let files = std::fs::read_dir(dir)?;
|
|
|
|
for file in files {
|
|
let entry = file?;
|
|
if entry.file_type()?.is_file() {
|
|
let plugin = Plugin::load(entry.path().to_path_buf())?;
|
|
println!("INFO: Loaded plugin {} {} licensed with {}",
|
|
plugin.name().clone(),
|
|
plugin.version().clone().unwrap_or("None".to_string()),
|
|
plugin.license().clone().unwrap_or("None".to_string())
|
|
);
|
|
self.plugins.push(plugin);
|
|
} else
|
|
if entry.file_type()?.is_dir() {
|
|
self.load(entry.path().to_path_buf())?;
|
|
}
|
|
}
|
|
println!("INFO: Loaded {} plugins", self.plugins.len());
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn init_plugins(&mut self) {
|
|
for plugin in &self.plugins {
|
|
plugin.init();
|
|
}
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub fn reload_plugins(&mut self) {
|
|
for plugin in &mut self.plugins {
|
|
if let Err(e) = plugin.reload() {
|
|
eprintln!("ERROR: Failed to reload plugin {:?}: {e}", plugin.name());
|
|
}
|
|
}
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub fn unload_plugins(&mut self) {
|
|
while let Some(plug) = self.plugins.pop() {
|
|
plug.free();
|
|
}
|
|
}
|
|
|
|
pub fn poll_plugins(&mut self) -> Vec<(String, String)> {
|
|
let mut answers = Vec::new();
|
|
let len = self.len_cap / self.plugins.len();
|
|
for plugin in &self.plugins {
|
|
if !plugin.enabled() {
|
|
continue;
|
|
}
|
|
match plugin.poll(len) {
|
|
Ok(v) => answers.push((plugin.name().clone(), v)),
|
|
Err(e) => eprintln!("Failed to poll plugin: {e}"),
|
|
}
|
|
|
|
}
|
|
answers
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum PluginError {
|
|
DlOpenError,
|
|
IoError
|
|
}
|
|
|
|
impl From<dlopen::Error> for PluginError {
|
|
fn from(_: dlopen::Error) -> Self {
|
|
Self::DlOpenError
|
|
}
|
|
}
|
|
|
|
impl From<std::io::Error> for PluginError {
|
|
fn from(_: std::io::Error) -> Self {
|
|
Self::IoError
|
|
}
|
|
}
|