Refactor context handling and redraw requests
Remove unsafe pointer shenanigans and give Lua instance ownership over `Context`. Event handlers now directly mutate Context instead of using a shared app state which is then hooked up to Context via unsafe pointers. The removal of a shared state means stages (previously called "modes") are now responsible for keeping their own internal state. Additionally, clean up draw request handling and module hierarchy.
This commit is contained in:
1 parent
337e876571
commit
cc6de120f2
27 files changed
+803
-876
No files matched your search
@@ -0,0 +1,51 @@
|
||||
use crate::{pob::Context, util::change_working_directory};
|
||||
use mlua::{Function, IntoLuaMulti, Lua, MultiValue, Result as LuaResult, Value};
|
||||
use std::env;
|
||||
|
||||
pub fn protected_call(l: &Lua, (func, args): (Function, MultiValue)) -> LuaResult<MultiValue> {
|
||||
match func.call::<MultiValue>(args) {
|
||||
// callers expect first return value to be Nil on success
|
||||
Ok(return_values) => Ok(std::iter::once(Value::Nil).chain(return_values).collect()),
|
||||
// otherwise it is set to error message.
|
||||
Err(err) => err.to_string().into_lua_multi(l),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_module(l: &Lua, (name, args): (String, MultiValue)) -> LuaResult<MultiValue> {
|
||||
let script_dir = {
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
ctx.script_dir.to_owned()
|
||||
};
|
||||
let mut module_path = script_dir.join(name);
|
||||
if module_path.extension().is_none() {
|
||||
module_path.set_extension("lua");
|
||||
}
|
||||
|
||||
let current_dir = env::current_dir()?;
|
||||
change_working_directory(script_dir.as_path())?;
|
||||
let result = l.load(module_path).call::<MultiValue>(args);
|
||||
change_working_directory(current_dir)?;
|
||||
result
|
||||
}
|
||||
|
||||
pub fn protected_load_module(l: &Lua, (name, args): (String, MultiValue)) -> LuaResult<MultiValue> {
|
||||
let script_dir = {
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
ctx.script_dir.to_owned()
|
||||
};
|
||||
let mut module_path = script_dir.join(name);
|
||||
if module_path.extension().is_none() {
|
||||
module_path.set_extension("lua");
|
||||
}
|
||||
|
||||
let current_dir = env::current_dir()?;
|
||||
change_working_directory(script_dir.as_path())?;
|
||||
let result = match l.load(module_path).call::<MultiValue>(args) {
|
||||
// on success, callers expect a Nil followed by return values
|
||||
Ok(res) => Ok(std::iter::once(Value::Nil).chain(res).collect()),
|
||||
// otherwise it is set to error message.
|
||||
Err(err) => err.to_string().into_lua_multi(l),
|
||||
};
|
||||
change_working_directory(current_dir)?;
|
||||
result
|
||||
}
|
||||
Reference in new issue
Block a user