Fixed rust, named the libraries sdk's, better examples

This commit is contained in:
2024-06-16 00:59:04 +03:00
parent 407faab33a
commit f55279b7ef
20 changed files with 229 additions and 226 deletions

View File

@@ -8,5 +8,5 @@ crate-type=["cdylib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dim_plugin_helper = {path="../../dim_plugin_helper"}
dim_sdk = {path="../../sdk/rust/"}
lazy_static = "1.4.0"

View File

@@ -4,12 +4,12 @@ PLUGIN_NAME=example_rust
build: $(PLUGIN_DIR)/$(PLUGIN_NAME).dim
$(PLUGIN_DIR)/$(PLUGIN_NAME).dim: $(OBJECT_DIR)/$(PLUGIN_NAME)/release/lib$(PLUGIN_NAME).so
cp $(OBJECT_DIR)/$(PLUGIN_NAME)/release/lib$(PLUGIN_NAME).so $(PLUGIN_DIR)/$(PLUGIN_NAME).dim
$(PLUGIN_DIR)/$(PLUGIN_NAME).dim: $(OBJECT_DIR)/$(PLUGIN_NAME)/debug/lib$(PLUGIN_NAME).so
cp $(OBJECT_DIR)/$(PLUGIN_NAME)/debug/lib$(PLUGIN_NAME).so $(PLUGIN_DIR)/$(PLUGIN_NAME).dim
$(OBJECT_DIR)/$(PLUGIN_NAME)/release/lib$(PLUGIN_NAME).so:
$(OBJECT_DIR)/$(PLUGIN_NAME)/debug/lib$(PLUGIN_NAME).so:
mkdir -p $(OBJECT_DIR)/$(PLUGIN_NAME)
cargo build --release --target-dir $(OBJECT_DIR)/$(PLUGIN_NAME)
cargo build --target-dir $(OBJECT_DIR)/$(PLUGIN_NAME)

View File

@@ -1,67 +1,48 @@
use std::ffi::c_void;
use std::ffi::CStr;
use std::ffi::CString;
use std::io::BufWriter;
use std::io::Write;
use dim_plugin_helper::{plugin_info, PluginInfo};
use std::fmt::Write;
use dim_sdk::{plugin_info, DimPlugin};
plugin_info!("Example rust project", "owo", "nyaaa");
plugin_info!(
Plug, // Your main global structs name that implements `DimPlugin`
"Example rust project", // Plugin name
"0.0.0", // Plugin Version (leave empty for none)
"GPLv3" // Plugin license (leave empty for none)
);
struct Plug {
pub some_data: String,
counter: usize,
}
impl Plug {
pub fn new() -> Self {
Self {
some_data: String::from("OwO")
counter: 0
}
}
}
static mut PLUG: *mut Plug = std::ptr::null_mut();
#[no_mangle]
extern "C" fn plug_get_info() -> *const c_void {
PLUGIN_INFO.get()
}
#[no_mangle]
extern "C" fn plug_init() {
unsafe {
PLUG = (&mut Plug::new()) as *mut Plug;
impl DimPlugin for Plug {
fn init(&mut self) {
// Initialise data, this will run once, it will not run again after reload
}
fn poll(&mut self, f: &mut dim_sdk::CBuffer) -> dim_sdk::Result<()> {
// Write to buffer the text you want to display, keep this short
write!(f, "Hello from rust! ({})", self.counter)?;
self.counter += 1;
Ok(())
}
fn pre_reload(&mut self) {
// Do stuff before reload, probably save important things because theres a good chance
// (especially on rust) that if you change the data layout it will die
}
fn post_reload(&mut self) {
// Do stuff after reloading plugin, state a.k.a this struct has the same data, will crash
// if the data layout changed
}
fn free(&mut self) {
// Yout probably dont need this but its for freeing things before the plugin gets unloaded
}
}
#[no_mangle]
extern "C" fn plug_pre_reload() -> *const () {
unsafe {
return PLUG as *const ();
}
}
#[no_mangle]
extern "C" fn plug_post_reload(state: *mut ()) {
unsafe {
PLUG = state as *mut Plug;
}
}
#[no_mangle]
extern "C" fn plug_poll(buf: *mut i8, len: usize) {
let mut buf = dim_plugin_helper::CBuffer::from_raw_parts_mut(buf, len);
// let mut buf = StringBuffer::from_raw_parts_mut(buf, len);
let data = unsafe {(*PLUG).some_data.clone()};
let _ = write!(buf, "{}", data);
}
#[no_mangle]
extern "C" fn plug_free() {
}