-
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from Snowiiii/plugins-feature
Make a way to disable extism
- Loading branch information
Showing
5 changed files
with
58 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
#[cfg(feature = "plugins")] | ||
use extism::{convert::Msgpack, host_fn, FromBytes, ToBytes}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// Used to keep track of things created by plugins. | ||
/// This can include things like events, commands, etc. | ||
#[derive(Hash, PartialEq, Eq, ToBytes, FromBytes, Serialize, Deserialize)] | ||
#[derive(Hash, PartialEq, Eq, ToBytes, FromBytes, serde::Serialize, serde::Deserialize)] | ||
#[cfg(feature = "plugins")] | ||
#[encoding(Msgpack)] // TODO: Switch to protocal buffers for smaller size | ||
struct Identifier { | ||
namespace: String, | ||
path: String, | ||
} | ||
|
||
#[cfg(feature = "plugins")] | ||
host_fn!(new(namespace: String, path: String) -> Result<Identifier, _> { | ||
Ok(Identifier { namespace, path }) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,65 @@ | ||
mod api; | ||
|
||
use std::path::Path; | ||
|
||
#[cfg(feature = "plugins")] | ||
use extism::{Manifest, Plugin, Wasm}; | ||
|
||
pub const PLUGIN_DIR: &str = "plugins"; | ||
|
||
pub struct PumpkinPlugin { | ||
#[cfg(feature = "plugins")] | ||
_plugin: Plugin, | ||
} | ||
|
||
pub struct PluginLoader { | ||
plugins: Vec<Plugin>, | ||
plugins: Vec<PumpkinPlugin>, | ||
} | ||
|
||
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); | ||
#[cfg(feature = "plugins")] | ||
{ | ||
use std::path::Path; | ||
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"); | ||
let pumpkin_plugin = PumpkinPlugin { _plugin: plugin }; | ||
plugins.push(pumpkin_plugin); | ||
} | ||
} | ||
|
||
Self { plugins } | ||
} | ||
|
||
Self { plugins } | ||
#[cfg(not(feature = "plugins"))] | ||
Self { | ||
plugins: Vec::new(), | ||
} | ||
} | ||
|
||
pub fn plugins(&mut self) -> &mut Vec<Plugin> { | ||
pub fn plugins(&mut self) -> &mut Vec<PumpkinPlugin> { | ||
&mut self.plugins | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters