mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-04-06 07:46:06 +00:00
more lua api stuff
This commit is contained in:
@@ -1,21 +1,34 @@
|
||||
pub mod backend_lua;
|
||||
|
||||
use std::sync::Arc;
|
||||
use tokio::runtime::Runtime;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::mpsc::{self, Sender, Receiver};
|
||||
|
||||
pub trait Backend {
|
||||
/// NOTE: Send is required as the backend is constructed on the main thread and sent over.
|
||||
/// Even if we construct it inside the runtime however, because of tokio, we would
|
||||
// still have to require Send as the runtime might run on different threads (?)
|
||||
pub trait Backend: Send {
|
||||
fn load(&mut self, code: String) -> anyhow::Result<()>;
|
||||
fn load_api(&mut self) -> anyhow::Result<()> { Ok(()) }
|
||||
fn load_api(&mut self, tx: Arc<Sender<ServerBoundPluginEvent>>) -> anyhow::Result<()> { Ok(()) }
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Argument {
|
||||
Number(f32),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum PluginBoundPluginEvent {
|
||||
|
||||
CallEventHandler((String, Vec<Argument>))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ServerBoundPluginEvent {
|
||||
PluginLoaded,
|
||||
|
||||
/// Arguments: (event name, handler function name)
|
||||
RegisterEventHandler((String, String)),
|
||||
}
|
||||
|
||||
pub struct Plugin {
|
||||
@@ -25,27 +38,33 @@ pub struct Plugin {
|
||||
}
|
||||
|
||||
impl Plugin {
|
||||
pub fn new(backend: Box<dyn Backend>) -> Self {
|
||||
pub fn new(mut backend: Box<dyn Backend>, src: String) -> anyhow::Result<Self> {
|
||||
let runtime = Runtime::new().expect("Failed to create a tokio Runtime!");
|
||||
let (pb_tx, mut pb_rx) = mpsc::channel(1_000);
|
||||
let (sb_tx, sb_rx) = mpsc::channel(1_000);
|
||||
runtime.spawn(async move {
|
||||
if sb_tx.send(ServerBoundPluginEvent::PluginLoaded).await.is_err() {
|
||||
error!("Plugin communication channels somehow already closed!");
|
||||
return;
|
||||
let sb_tx = Arc::new(sb_tx);
|
||||
runtime.spawn_blocking(move || {
|
||||
if backend.load_api(sb_tx.clone()).is_ok() {
|
||||
if backend.load(src).is_ok() {
|
||||
if sb_tx.blocking_send(ServerBoundPluginEvent::PluginLoaded).is_err() {
|
||||
error!("Plugin communication channels somehow already closed!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
loop {
|
||||
if let Some(message) = pb_rx.recv().await {
|
||||
if let Some(message) = pb_rx.blocking_recv() {
|
||||
debug!("Received message: {:?}", message);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
Self {
|
||||
Ok(Self {
|
||||
runtime,
|
||||
tx: pb_tx,
|
||||
rx: sb_rx,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user