mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-04-05 23:36:23 +00:00
very early version of plugin system
This commit is contained in:
38
src/server/plugins/backend_lua.rs
Normal file
38
src/server/plugins/backend_lua.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use super::Backend;
|
||||
use mlua::prelude::*;
|
||||
|
||||
pub struct BackendLua {
|
||||
lua: Lua,
|
||||
}
|
||||
|
||||
impl BackendLua {
|
||||
pub fn new() -> Self {
|
||||
let lua = Lua::new();
|
||||
|
||||
Self {
|
||||
lua,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Backend for BackendLua {
|
||||
fn load(&mut self, code: String) -> anyhow::Result<()> {
|
||||
self.lua.load(code).exec().map_err(|e| { error!("[LUA] {:?}", e); e })?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_api(&mut self) -> anyhow::Result<()> {
|
||||
let print_fn = self.lua.create_function(|_lua, (msg,): (String,)| {
|
||||
info!("[LUA] {}", msg);
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
let api = self.lua.create_table()?;
|
||||
|
||||
api.set("print", print_fn)?;
|
||||
|
||||
self.lua.globals().set("MP", api)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user