mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-04-05 23:36:23 +00:00
more lua api stuff
This commit is contained in:
@@ -1,5 +1,31 @@
|
||||
use super::Backend;
|
||||
use super::{Backend, ServerBoundPluginEvent};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc::Sender;
|
||||
use mlua::prelude::*;
|
||||
use mlua::{UserData, UserDataMethods};
|
||||
|
||||
struct Context {
|
||||
tx: Arc<Sender<ServerBoundPluginEvent>>,
|
||||
}
|
||||
|
||||
impl Context {
|
||||
fn new(tx: Arc<Sender<ServerBoundPluginEvent>>) -> Self {
|
||||
Self {
|
||||
tx,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UserData for Context {
|
||||
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
methods.add_method("RegisterEventHandler", |_, me, (event_name, handler_name): (String, String)| {
|
||||
debug!("Event handler registered: {} (EVENT) = {} (LUA)", event_name, handler_name);
|
||||
// TODO: Figure out how to handle these errors (?)
|
||||
let _ = me.tx.blocking_send(ServerBoundPluginEvent::RegisterEventHandler((event_name, handler_name)));
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BackendLua {
|
||||
lua: Lua,
|
||||
@@ -21,17 +47,24 @@ impl Backend for BackendLua {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_api(&mut self) -> anyhow::Result<()> {
|
||||
fn load_api(&mut self, tx: Arc<Sender<ServerBoundPluginEvent>>) -> anyhow::Result<()> {
|
||||
let print_fn = self.lua.create_function(|_lua, (msg,): (String,)| {
|
||||
info!("[LUA] {}", msg);
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
let api = self.lua.create_table()?;
|
||||
// let register_event_handler_fn = self.lua.create_function(|_lua, (name, handler_name): (String, String)| {
|
||||
// tx.blocking_send(ServerBoundPluginEvent::RegisterEventHandler((name, handler_name)));
|
||||
// Ok(())
|
||||
// })?;
|
||||
|
||||
api.set("print", print_fn)?;
|
||||
// let api = self.lua.create_table()?;
|
||||
let api = Context::new(tx);
|
||||
// api.set("", thing)?;
|
||||
|
||||
self.lua.globals().set("MP", api)?;
|
||||
let globals = self.lua.globals();
|
||||
globals.set("MP", api)?;
|
||||
globals.set("print", print_fn)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user