Added PluginContext
This commit is contained in:
25
sdk/rust/dim_sdk/src/context.rs
Normal file
25
sdk/rust/dim_sdk/src/context.rs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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]
|
||||
Reference in New Issue
Block a user