1
Fork 0
mirror of https://github.com/redstrate/Kawari.git synced 2025-06-30 19:57:46 +00:00

Log, don't panic for Lua commands

It will also try to fallback to a Rust command - if available.

See #39
This commit is contained in:
Joshua Goins 2025-06-22 08:27:21 -04:00
parent cdcce88c5f
commit 6b0487f30f

View file

@ -507,44 +507,43 @@ async fn client_loop(
let lua = lua.lock().unwrap(); let lua = lua.lock().unwrap();
let state = lua.app_data_ref::<ExtraLuaState>().unwrap(); let state = lua.app_data_ref::<ExtraLuaState>().unwrap();
// If a Lua command exists, try using that first
if let Some(command_script) = if let Some(command_script) =
state.command_scripts.get(command_name) state.command_scripts.get(command_name)
{ {
handled = true; handled = true;
lua.scope(|scope| {
let connection_data = scope
.create_userdata_ref_mut(&mut lua_player)
.unwrap();
let config = get_config();
let file_name = format!( let file_name = format!(
"{}/{}", "{}/{}",
&config.world.scripts_location, command_script &config.world.scripts_location, command_script
); );
let mut run_script = || -> mlua::Result<()> {
lua.scope(|scope| {
let connection_data = scope
.create_userdata_ref_mut(&mut lua_player)?;
lua.load( lua.load(
std::fs::read(&file_name) std::fs::read(&file_name)
.expect("Failed to locate scripts directory!"), .expect("Failed to locate scripts directory!"),
) )
.set_name("@".to_string() + &file_name) .set_name("@".to_string() + &file_name)
.exec() .exec()?;
.unwrap();
let required_rank = lua.globals().get("required_rank"); let required_rank = lua.globals().get("required_rank");
if let Err(error) = required_rank { if let Err(error) = required_rank {
tracing::info!("Script is missing required_rank! Unable to run command, sending error to user. Additional information: {}", error); tracing::info!("Script is missing required_rank! Unable to run command, sending error to user. Additional information: {}", error);
let func: Function = let func: Function =
lua.globals().get("onCommandRequiredRankMissingError").unwrap(); lua.globals().get("onCommandRequiredRankMissingError")?;
func.call::<()>((error.to_string(), connection_data)).unwrap(); func.call::<()>((error.to_string(), connection_data))?;
return Ok(()); return Ok(());
} }
/* Reset state for future commands. Without this it'll stay set to the last value /* Reset state for future commands. Without this it'll stay set to the last value
* and allow other commands that omit required_rank to run, which is undesirable. */ * and allow other commands that omit required_rank to run, which is undesirable. */
lua.globals().set("required_rank", mlua::Value::Nil).unwrap(); lua.globals().set("required_rank", mlua::Value::Nil)?;
if connection.player_data.gm_rank as u8 >= required_rank.unwrap() { if connection.player_data.gm_rank as u8 >= required_rank? {
let mut func_args = ""; let mut func_args = "";
if parts.len() > 1 { if parts.len() > 1 {
func_args = &chat_message.message[command_name.len() + 2..]; func_args = &chat_message.message[command_name.len() + 2..];
@ -553,20 +552,24 @@ async fn client_loop(
tracing::info!("No additional args passed to Lua command {}.", command_name); tracing::info!("No additional args passed to Lua command {}.", command_name);
} }
let func: Function = let func: Function =
lua.globals().get("onCommand").unwrap(); lua.globals().get("onCommand")?;
func.call::<()>((func_args, connection_data)). func.call::<()>((func_args, connection_data))?;
unwrap();
Ok(()) Ok(())
} else { } else {
tracing::info!("User with account_id {} tried to invoke GM command {} with insufficient privileges!", tracing::info!("User with account_id {} tried to invoke GM command {} with insufficient privileges!",
connection.player_data.account_id, command_name); connection.player_data.account_id, command_name);
let func: Function = let func: Function =
lua.globals().get("onCommandRequiredRankInsufficientError").unwrap(); lua.globals().get("onCommandRequiredRankInsufficientError")?;
func.call::<()>(connection_data).unwrap(); func.call::<()>(connection_data)?;
Ok(()) Ok(())
} }
}) })
.unwrap(); };
if let Err(err) = run_script() {
tracing::warn!("Lua error in {file_name}: {:?}", err);
handled = false;
}
} else { } else {
tracing::info!("Unknown command {command_name}"); tracing::info!("Unknown command {command_name}");
@ -582,6 +585,7 @@ async fn client_loop(
} }
} }
// Fallback to Rust implemented commands
if !handled { if !handled {
ChatHandler::handle_chat_message( ChatHandler::handle_chat_message(
&mut connection, &mut connection,