Add pumpkin-plugin
mhh, Whats that?
This commit is contained in:
1 parent
8e9ea66787
commit
3b438872a8
9 files changed
+1725
-30
No files matched your search
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "pumpkin-plugin"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
|
||||
[dependencies]
|
||||
extism = "1.5.0"
|
||||
log.workspace = true
|
||||
@@ -0,0 +1,49 @@
|
||||
use std::path::Path;
|
||||
|
||||
use extism::{Manifest, Plugin, Wasm};
|
||||
|
||||
pub const PLUGIN_DIR: &str = "plugins";
|
||||
|
||||
pub struct PluginLoader {
|
||||
plugins: Vec<Plugin>,
|
||||
}
|
||||
|
||||
impl PluginLoader {
|
||||
pub fn load() -> Self {
|
||||
let plugin_dir = Path::new(PLUGIN_DIR);
|
||||
if !plugin_dir.exists() || !plugin_dir.is_dir() {
|
||||
log::info!("Creating plugins dir...");
|
||||
std::fs::create_dir(plugin_dir).expect("Failed to create Plugin dir");
|
||||
return Self { plugins: vec![] };
|
||||
}
|
||||
let files = std::fs::read_dir(plugin_dir).expect("Failed to read plugin dir");
|
||||
let mut plugins = Vec::new();
|
||||
for file in files {
|
||||
let file = file.expect("Failed to get Plugin file");
|
||||
let path = file.path();
|
||||
if path
|
||||
.extension()
|
||||
.expect("Failed to get Plugin file extension")
|
||||
== "wasm"
|
||||
{
|
||||
log::info!(
|
||||
"Loading Plugin {:?}",
|
||||
path.file_name().expect("Failed to get Plugin file name")
|
||||
);
|
||||
let wasm = Wasm::file(path);
|
||||
let manifest = Manifest::new([wasm]);
|
||||
let mut plugin = Plugin::new(&manifest, [], true).unwrap();
|
||||
plugin
|
||||
.call::<(), ()>("on_enable", ())
|
||||
.expect("Failed to call on_enable funcation");
|
||||
plugins.push(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
Self { plugins }
|
||||
}
|
||||
|
||||
pub fn plugins(&mut self) -> &mut Vec<Plugin> {
|
||||
&mut self.plugins
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user