plugrs_host/
lib.rs

1mod plugin_loader;
2
3pub use plugin_loader::PluginLoader;
4use std::path::PathBuf;
5
6/// 获取平台特定的动态库文件名
7pub fn plugin_path(name: &str) -> PathBuf {
8    let file_name = match std::env::consts::OS {
9        "windows" => format!("{}.dll", name),
10        "macos" => format!("lib{}.dylib", name),
11        _ => format!("lib{}.so", name),
12    };
13    // 使用 target/debug 目录
14    PathBuf::from("target/debug").join(file_name)
15}
16
17/// 插件管理器
18pub struct PluginManager {
19    plugins: Vec<Box<dyn plugrs_interface::Plugin>>,
20}
21
22impl PluginManager {
23    pub fn new() -> Self {
24        Self {
25            plugins: Vec::new(),
26        }
27    }
28
29    /// 加载插件
30    pub fn load_plugin(&mut self, path: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
31        // println!("尝试加载插件: {:?}", path);
32        let loader = PluginLoader::new(path)?;
33        let plugin = loader.load_plugin()?;
34        self.plugins.push(plugin);
35        Ok(())
36    }
37
38    /// 执行所有已加载的插件
39    pub fn execute_all(&self) -> Vec<i32> {
40        self.plugins
41            .iter()
42            .map(|plugin| {
43                // println!("执行插件: {}", plugin.name());
44                plugin.execute()
45            })
46            .collect()
47    }
48
49    /// 获取已加载的插件数量
50    pub fn plugin_count(&self) -> usize {
51        self.plugins.len()
52    }
53}
54
55impl Default for PluginManager {
56    fn default() -> Self {
57        Self::new()
58    }
59}