Added PluginContext

This commit is contained in:
2024-06-20 23:43:16 +03:00
parent 43a07debde
commit 19aee664c2
25 changed files with 172 additions and 83 deletions

View File

@@ -0,0 +1,25 @@
use std::{ffi::{c_char, CStr}, path::PathBuf};
#[repr(C)]
pub struct ContextRaw {
pub config_dir: *const c_char
}
#[derive(Debug, Clone)]
pub struct Context {
pub config_dir: PathBuf
}
impl Context {
pub fn new(cr: *const ContextRaw) -> Self {
let config_dir = unsafe {
let v = CStr::from_ptr((*cr).config_dir);
PathBuf::from(v.to_string_lossy().to_string())
};
Self {
config_dir
}
}
}

View File

@@ -4,14 +4,15 @@
mod magic;
mod c_buffer;
mod plugin_info;
mod context;
pub use c_buffer::*;
pub use plugin_info::*;
pub use anyhow::Result;
pub use context::*;
pub trait DimPlugin {
fn init(&mut self);
fn init(&mut self, ctx: Context);
fn pre_reload(&mut self);
fn post_reload(&mut self);
fn poll(&mut self, f: &mut CBuffer) -> Result<()>;
@@ -23,3 +24,4 @@ pub trait DimPlugin {

View File

@@ -15,10 +15,12 @@ macro_rules! plugin_info {
}
#[no_mangle]
unsafe extern "C" fn plug_init() {
unsafe extern "C" fn plug_init(ctx: *const $crate::ContextRaw) {
PLUG = std::alloc::alloc(std::alloc::Layout::new::<$typ>()) as *mut $typ;
*PLUG = Plug::new();
(&mut *PLUG).init();
let ctx = $crate::Context::new(ctx);
(&mut *PLUG).init(ctx);
}
#[no_mangle]