From 4d6c69772be7d8dc72e9f0c64ad7832b84de47e2 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Thu, 11 Dec 2025 23:19:56 +0100 Subject: [PATCH] Improved plugin loader --- src/plugin/loader.rs | 27 +++++++++++++-------------- src/plugin/mod.rs | 26 ++++++++++++++++++++++---- src/plugin/types.rs | 9 +++++++++ 3 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 src/plugin/types.rs diff --git a/src/plugin/loader.rs b/src/plugin/loader.rs index 96acdb4..b321ac8 100644 --- a/src/plugin/loader.rs +++ b/src/plugin/loader.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, fs, - io::Read, + io::{BufRead, BufReader, Read}, net::{TcpListener, TcpStream}, path::Path, process::Command, @@ -27,14 +27,12 @@ pub struct PluginHandshake { } pub struct PluginLoader { - plugins: Vec, plugin_clients: Arc>>, } impl PluginLoader { pub fn new() -> Self { Self { - plugins: Vec::new(), plugin_clients: Arc::new(Mutex::new(HashMap::new())), } } @@ -58,15 +56,15 @@ impl PluginLoader { .is_none() {} LOGGER.info(format!("Loaded {}", plugin_json.id)); - Plugin( - self.plugin_clients - .lock() - .unwrap() - .get(&plugin_json.id) - .unwrap() - .try_clone() - .unwrap(), - ) + let a = self + .plugin_clients + .lock() + .unwrap() + .get(&plugin_json.id) + .unwrap() + .try_clone() + .unwrap(); + Plugin(a.try_clone().unwrap(), BufReader::new(a)) } pub fn start_server(&self) { @@ -75,9 +73,10 @@ impl PluginLoader { std::thread::spawn(move || { for stream in listener.incoming() { - let mut stream = stream.unwrap(); + let stream = stream.unwrap(); + let mut reader = BufReader::new(&stream); let mut s = String::new(); - stream.read_to_string(&mut s).unwrap(); + reader.read_line(&mut s).unwrap(); let plugin_handshake: PluginHandshake = serde_json::from_str(&s).unwrap(); plugin_clients .lock() diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index 98f998a..6c083a2 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -1,7 +1,25 @@ -use std::net::TcpStream; - pub mod loader; +pub mod types; -pub struct Plugin(TcpStream); +use std::{ + io::{BufRead, BufReader, Read, Write}, + net::TcpStream, +}; -impl Plugin {} +use crate::plugin::types::{LoaderMessage, PluginMessage}; + +pub struct Plugin(TcpStream, BufReader); + +impl Plugin { + pub fn send(&mut self, m: LoaderMessage) { + self.0 + .write(serde_json::to_string(&m).unwrap().as_bytes()) + .unwrap(); + } + + pub fn read(&mut self) -> PluginMessage { + let mut buf = String::new(); + self.1.read_line(&mut buf).unwrap(); + serde_json::from_str(&buf).unwrap() + } +} diff --git a/src/plugin/types.rs b/src/plugin/types.rs new file mode 100644 index 0000000..d4ae513 --- /dev/null +++ b/src/plugin/types.rs @@ -0,0 +1,9 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "type", content = "params", rename_all = "snake_case")] +pub enum LoaderMessage {} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "type", content = "params", rename_all = "snake_case")] +pub enum PluginMessage {}