Rust rewrite
This commit is contained in:
19
dim_plugins/example_c/Makefile
Normal file
19
dim_plugins/example_c/Makefile
Normal file
@@ -0,0 +1,19 @@
|
||||
PLUGIN_NAME=example_c
|
||||
|
||||
|
||||
CC_FLAGS = $(DIM_CC_FLAGS)
|
||||
|
||||
SOURCES=$(wildcard src/*.c)
|
||||
OBJECTS=$(patsubst src/%.c,$(OBJECT_DIR)/$(PLUGIN_NAME)/%.o,$(SOURCES))
|
||||
|
||||
$(info $(SOURCES))
|
||||
|
||||
build: $(PLUGIN_DIR)/$(PLUGIN_NAME).dim
|
||||
|
||||
$(PLUGIN_DIR)/$(PLUGIN_NAME).dim: $(OBJECTS)
|
||||
@mkdir -p $(dir $@)
|
||||
$(DIM_CC) -o $@ $^ $(CC_FLAGS)
|
||||
|
||||
$(OBJECT_DIR)/$(PLUGIN_NAME)/%.o: src/%.c
|
||||
@mkdir -p $(dir $@)
|
||||
$(DIM_CC) -c -o $@ $< -fPIC -pie
|
||||
39
dim_plugins/example_c/src/main.c
Normal file
39
dim_plugins/example_c/src/main.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "../../../include/plug.h"
|
||||
|
||||
PLUG_INFO("Example plugin", "0.0.0", "GPLv3")
|
||||
|
||||
typedef struct plug_t {
|
||||
char* some_data;
|
||||
int count;
|
||||
} plug_t;
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
void* plug_pre_reload() {
|
||||
return p;
|
||||
}
|
||||
|
||||
void plug_post_reload(void *state) {
|
||||
p = state;
|
||||
}
|
||||
|
||||
void plug_poll(char *buf, size_t len) {
|
||||
snprintf(buf, len, "%s (%d)", p->some_data, p->count++);
|
||||
}
|
||||
|
||||
void plug_free() {
|
||||
free(p);
|
||||
}
|
||||
12
dim_plugins/example_rust/Cargo.toml
Normal file
12
dim_plugins/example_rust/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "example_rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
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"}
|
||||
lazy_static = "1.4.0"
|
||||
15
dim_plugins/example_rust/Makefile
Normal file
15
dim_plugins/example_rust/Makefile
Normal file
@@ -0,0 +1,15 @@
|
||||
# Must match package name in Cargo.toml
|
||||
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
|
||||
|
||||
$(OBJECT_DIR)/$(PLUGIN_NAME)/release/lib$(PLUGIN_NAME).so:
|
||||
mkdir -p $(OBJECT_DIR)/$(PLUGIN_NAME)
|
||||
cargo build --release --target-dir $(OBJECT_DIR)/$(PLUGIN_NAME)
|
||||
|
||||
|
||||
|
||||
67
dim_plugins/example_rust/src/lib.rs
Normal file
67
dim_plugins/example_rust/src/lib.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
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};
|
||||
|
||||
plugin_info!("Example rust project", "owo", "nyaaa");
|
||||
|
||||
struct Plug {
|
||||
pub some_data: String,
|
||||
}
|
||||
|
||||
impl Plug {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
some_data: String::from("OwO")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
#[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() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user