Improved plugin loader

This commit is contained in:
2025-12-11 23:19:56 +01:00
parent 5e2e819a94
commit 4d6c69772b
3 changed files with 44 additions and 18 deletions
+8 -9
View File
@@ -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>,
plugin_clients: Arc<Mutex<HashMap<String, TcpStream>>>,
}
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
let a = self
.plugin_clients
.lock()
.unwrap()
.get(&plugin_json.id)
.unwrap()
.try_clone()
.unwrap(),
)
.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()
+22 -4
View File
@@ -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<TcpStream>);
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()
}
}
+9
View File
@@ -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 {}