1
Fork 0
mirror of https://github.com/redstrate/Kawari.git synced 2025-06-30 11:47:45 +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,66 +507,69 @@ 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 file_name = format!(
let connection_data = scope "{}/{}",
.create_userdata_ref_mut(&mut lua_player) &config.world.scripts_location, command_script
.unwrap(); );
let config = get_config(); let mut run_script = || -> mlua::Result<()> {
lua.scope(|scope| {
let connection_data = scope
.create_userdata_ref_mut(&mut lua_player)?;
let file_name = format!( lua.load(
"{}/{}", std::fs::read(&file_name)
&config.world.scripts_location, command_script .expect("Failed to locate scripts directory!"),
); )
lua.load( .set_name("@".to_string() + &file_name)
std::fs::read(&file_name) .exec()?;
.expect("Failed to locate scripts directory!"),
)
.set_name("@".to_string() + &file_name)
.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
* and allow other commands that omit required_rank to run, which is undesirable. */
lua.globals().set("required_rank", mlua::Value::Nil).unwrap();
if connection.player_data.gm_rank as u8 >= required_rank.unwrap() {
let mut func_args = "";
if parts.len() > 1 {
func_args = &chat_message.message[command_name.len() + 2..];
tracing::info!("Args passed to Lua command {}: {}", command_name, func_args);
} else {
tracing::info!("No additional args passed to Lua command {}.", command_name);
} }
let func: Function =
lua.globals().get("onCommand").unwrap(); /* Reset state for future commands. Without this it'll stay set to the last value
func.call::<()>((func_args, connection_data)). * and allow other commands that omit required_rank to run, which is undesirable. */
unwrap(); lua.globals().set("required_rank", mlua::Value::Nil)?;
Ok(())
} else { if connection.player_data.gm_rank as u8 >= required_rank? {
tracing::info!("User with account_id {} tried to invoke GM command {} with insufficient privileges!", let mut func_args = "";
connection.player_data.account_id, command_name); if parts.len() > 1 {
let func: Function = func_args = &chat_message.message[command_name.len() + 2..];
lua.globals().get("onCommandRequiredRankInsufficientError").unwrap(); tracing::info!("Args passed to Lua command {}: {}", command_name, func_args);
func.call::<()>(connection_data).unwrap(); } else {
Ok(()) tracing::info!("No additional args passed to Lua command {}.", command_name);
} }
}) let func: Function =
.unwrap(); lua.globals().get("onCommand")?;
func.call::<()>((func_args, connection_data))?;
Ok(())
} else {
tracing::info!("User with account_id {} tried to invoke GM command {} with insufficient privileges!",
connection.player_data.account_id, command_name);
let func: Function =
lua.globals().get("onCommandRequiredRankInsufficientError")?;
func.call::<()>(connection_data)?;
Ok(())
}
})
};
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,