Make a way to disable extism

- Made a `plugins` feature in `pumpkin-plugin`
- Made a `plugins` feature in `pumpkin`, which enables the `plugins` feature in `pumpkin-plugin`
- Made the feature default
This commit is contained in:
lukas0008 2024-09-07 21:01:05 +02:00
parent 01a71d65ba
commit f3728a169a
5 changed files with 58 additions and 37 deletions

View File

@ -33,6 +33,6 @@ tokio = { version = "1.40", features = [
"sync", "sync",
] } ] }
rayon = "1.10.0" rayon = "1.10.0"
uuid = { version = "1.10.0", features = ["serde", "v3"] } uuid = { version = "1.10.0", features = ["serde", "v3", "v4"] }
derive_more = { version = "1.0.0", features = ["full"] } derive_more = { version = "1.0.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }

View File

@ -3,7 +3,11 @@ name = "pumpkin-plugin"
version.workspace = true version.workspace = true
edition.workspace = true edition.workspace = true
[features]
default = []
plugins = ["dep:extism"]
[dependencies] [dependencies]
serde.workspace = true serde.workspace = true
log.workspace = true log.workspace = true
extism = "1.6.0" extism = { version = "1.6.0", optional = true }

View File

@ -1,15 +1,17 @@
#[cfg(feature = "plugins")]
use extism::{convert::Msgpack, host_fn, FromBytes, ToBytes}; use extism::{convert::Msgpack, host_fn, FromBytes, ToBytes};
use serde::{Deserialize, Serialize};
/// Used to keep track of things created by plugins. /// Used to keep track of things created by plugins.
/// This can include things like events, commands, etc. /// 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 #[encoding(Msgpack)] // TODO: Switch to protocal buffers for smaller size
struct Identifier { struct Identifier {
namespace: String, namespace: String,
path: String, path: String,
} }
#[cfg(feature = "plugins")]
host_fn!(new(namespace: String, path: String) -> Result<Identifier, _> { host_fn!(new(namespace: String, path: String) -> Result<Identifier, _> {
Ok(Identifier { namespace, path }) Ok(Identifier { namespace, path })
}); });

View File

@ -1,17 +1,24 @@
mod api; mod api;
use std::path::Path; #[cfg(feature = "plugins")]
use extism::{Manifest, Plugin, Wasm}; use extism::{Manifest, Plugin, Wasm};
pub const PLUGIN_DIR: &str = "plugins"; pub const PLUGIN_DIR: &str = "plugins";
pub struct PumpkinPlugin {
#[cfg(feature = "plugins")]
_plugin: Plugin,
}
pub struct PluginLoader { pub struct PluginLoader {
plugins: Vec<Plugin>, plugins: Vec<PumpkinPlugin>,
} }
impl PluginLoader { impl PluginLoader {
pub fn load() -> Self { pub fn load() -> Self {
#[cfg(feature = "plugins")]
{
use std::path::Path;
let plugin_dir = Path::new(PLUGIN_DIR); let plugin_dir = Path::new(PLUGIN_DIR);
if !plugin_dir.exists() || !plugin_dir.is_dir() { if !plugin_dir.exists() || !plugin_dir.is_dir() {
log::info!("Creating plugins dir..."); log::info!("Creating plugins dir...");
@ -38,14 +45,21 @@ impl PluginLoader {
plugin plugin
.call::<(), ()>("on_enable", ()) .call::<(), ()>("on_enable", ())
.expect("Failed to call on_enable funcation"); .expect("Failed to call on_enable funcation");
plugins.push(plugin); let pumpkin_plugin = PumpkinPlugin { _plugin: plugin };
plugins.push(pumpkin_plugin);
} }
} }
Self { plugins } Self { plugins }
} }
pub fn plugins(&mut self) -> &mut Vec<Plugin> { #[cfg(not(feature = "plugins"))]
Self {
plugins: Vec::new(),
}
}
pub fn plugins(&mut self) -> &mut Vec<PumpkinPlugin> {
&mut self.plugins &mut self.plugins
} }
} }

View File

@ -5,7 +5,8 @@ description = "Empowering everyone to host fast and efficient Minecraft servers.
edition = "2021" edition = "2021"
[features] [features]
default = [] default = ["plugins"]
plugins = ["pumpkin-plugin/plugins"]
[dependencies] [dependencies]
# pumpkin # pumpkin