Improve ergonomics of calling Lua callbacks

This commit is contained in:
meehl committed 2026-08-29 11:28:30 +02:00
1 parent 77e1d615e0
commit c297eeaca6
6 files changed
+139 -131

No files matched your search

+30 -52
View File
@@ -1,19 +1,17 @@
pub use crate::pob::api::callback::{call_callback, get_callback};
use crate::{
pob::Context,
pob::api::{
callback::{get_custom_callback, set_custom_callback, set_main_object},
use crate::pob::{
api::{
callback::{call_callback, get_custom_callback, set_custom_callback, set_main_object},
clipboard::{copy, paste},
compression::{deflate, inflate},
console::{console_clear, console_execute, console_print_table, console_printf},
general::{exit, get_time, open_url, render_init, restart, strip_escapes, take_screenshot},
image_handle::register_image_handle_api,
input::{get_cursor_pos, is_key_down},
input::{get_cursor_pos, is_key_down, set_cursor_pos, show_cursor},
lua::{load_module, protected_call, protected_load_module},
paths::{
get_runtime_path, get_script_path, get_user_path, get_work_dir, make_dir, remove_dir,
set_work_dir,
},
rendering::PoBString,
search_handle::new_search_handle,
subscript::{abort_subscript, is_subscript_running, launch_subscript},
window::{
@@ -21,14 +19,15 @@ use crate::{
set_foreground, set_window_title,
},
},
subscript::NativeMultiValue,
};
use mlua::{IntoLuaMulti, Lua, MultiValue, Result as LuaResult, Variadic};
use std::time::{SystemTime, UNIX_EPOCH};
use mlua::{Lua, MultiValue, Result as LuaResult};
mod callback;
mod clipboard;
mod compression;
mod console;
mod general;
mod image_handle;
mod input;
mod lua;
@@ -69,8 +68,6 @@ pub fn register_globals(lua: &Lua) -> LuaResult<()> {
globals.set("Restart", lua.create_function(restart)?)?;
globals.set("OpenURL", lua.create_function(open_url)?)?;
globals.set("RenderInit", lua.create_function(render_init)?)?;
let take_screenshot = |_: &Lua, ()| -> LuaResult<()> { Ok(()) }; // stub
globals.set("TakeScreenshot", lua.create_function(take_screenshot)?)?;
// compression
@@ -89,6 +86,8 @@ pub fn register_globals(lua: &Lua) -> LuaResult<()> {
// input
globals.set("GetCursorPos", lua.create_function(get_cursor_pos)?)?;
globals.set("SetCursorPos", lua.create_function(set_cursor_pos)?)?;
globals.set("ShowCursor", lua.create_function(show_cursor)?)?;
globals.set("IsKeyDown", lua.create_function(is_key_down)?)?;
// window
@@ -121,53 +120,32 @@ pub fn register_globals(lua: &Lua) -> LuaResult<()> {
)?;
globals.set("AbortSubScript", lua.create_function(abort_subscript)?)?;
// NOTE: not used by PoB
let set_cursor_pos = |_: &Lua, ()| -> LuaResult<()> { unimplemented!() };
let show_cursor = |_: &Lua, ()| -> LuaResult<()> { unimplemented!() };
globals.set("SetCursorPos", lua.create_function(set_cursor_pos)?)?;
globals.set("ShowCursor", lua.create_function(show_cursor)?)?;
// rendering
rendering::register_globals(lua)?;
Ok(())
}
fn get_time(_l: &Lua, _: ()) -> LuaResult<u128> {
Ok(SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis())
// Registers Lua callback functions that can be called from Rust
macro_rules! define_callbacks {
($($fn_name:ident => $lua_name:literal ($($arg:ident : $arg_ty:ty),*) -> $ret_ty:ty;)*) => {
$(
pub fn $fn_name(lua: &Lua, $($arg: $arg_ty),*) -> LuaResult<$ret_ty> {
call_callback(lua, $lua_name, ($($arg,)*))
}
)*
};
}
fn strip_escapes(_: &Lua, text: String) -> LuaResult<String> {
Ok(PoBString(&text).strip_escapes())
}
fn exit(l: &Lua, exit_msg: Option<String>) -> LuaResult<()> {
if let Some(exit_msg) = exit_msg {
println!("{exit_msg}");
}
let mut ctx = l.app_data_mut::<Context>().unwrap();
ctx.should_exit = true;
Ok(())
}
fn restart(l: &Lua, _: ()) -> LuaResult<()> {
let mut ctx = l.app_data_mut::<Context>().unwrap();
ctx.needs_restart = true;
Ok(())
}
fn open_url(l: &Lua, url: String) -> LuaResult<MultiValue> {
match open::that(url) {
Ok(()) => ().into_lua_multi(l),
Err(_) => "Unable to open url!".into_lua_multi(l),
}
}
fn render_init(l: &Lua, features: Variadic<String>) -> LuaResult<()> {
let mut ctx = l.app_data_mut::<Context>().unwrap();
ctx.is_dpi_aware = features.iter().any(|feat| feat == "DPI_AWARE");
Ok(())
define_callbacks! {
on_init => "OnInit"() -> ();
on_exit => "OnExit"() -> ();
on_frame => "OnFrame"() -> ();
can_exit => "CanExit"() -> bool;
on_key_down => "OnKeyDown"(key: &str, double_click: bool) -> ();
on_key_up => "OnKeyUp"(key: &str) -> ();
on_char => "OnChar"(ch: char) -> ();
on_sub_call => "OnSubCall"(name: String, args: NativeMultiValue) -> MultiValue;
on_sub_finished => "OnSubFinished"(id: u64, return_values: NativeMultiValue) -> ();
on_sub_error => "OnSubError"(id: u64, error_msg: String) -> ();
}