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

@@ -1,7 +1,7 @@
PLUGIN_NAME=example_c
CC_FLAGS = $(DIM_CC_FLAGS)
CC_FLAGS = $(DIM_CC_FLAGS) -fPIC
SOURCES=$(wildcard src/*.c)
OBJECTS=$(patsubst src/%.c,$(OBJECT_DIR)/$(PLUGIN_NAME)/%.o,$(SOURCES))
@@ -12,8 +12,8 @@ build: $(PLUGIN_DIR)/$(PLUGIN_NAME).dim
$(PLUGIN_DIR)/$(PLUGIN_NAME).dim: $(OBJECTS)
@mkdir -p $(dir $@)
$(DIM_CC) -o $@ $^ $(CC_FLAGS)
$(DIM_CC) -o $@ $^ -rdynamic -shared $(CC_FLAGS)
$(OBJECT_DIR)/$(PLUGIN_NAME)/%.o: src/%.c
@mkdir -p $(dir $@)
$(DIM_CC) -c -o $@ $< -fPIC -pie
$(DIM_CC) -c -o $@ $< -pie $(CC_FLAGS)

View File

@@ -2,9 +2,9 @@
#include <stdlib.h>
#include <assert.h>
#include "../../../include/plug.h"
#include "dim_sdk.h"
PLUG_INFO("Example plugin", "0.0.0", "GPLv3")
PLUG_INFO("Example plugin", "0.0.1", "GPLv3")
typedef struct plug_t {
char* some_data;
@@ -16,7 +16,6 @@ plug_t* p = {0};
void plug_init() {
p = malloc(sizeof(plug_t));
assert(p != NULL && "Buy more ram KEKW");
p->some_data = "hi :3";
p->count = 0;
printf("Hello from plugin");
@@ -31,7 +30,7 @@ void plug_post_reload(void *state) {
}
void plug_poll(char *buf, size_t len) {
snprintf(buf, len, "%s (%d)", p->some_data, p->count++);
snprintf(buf, len, "Hello from C! (%d)", p->count++);
}
void plug_free() {

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() {
}