From a6e270d5e3249b8a5480b5cfd9f04e2cabe84b06 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sun, 22 Jun 2025 08:35:03 -0400 Subject: [PATCH] Log, don't panic for syntax errors in event scripting Fixes #39 --- src/bin/kawari-world.rs | 24 ++++++++----- src/world/event.rs | 74 +++++++++++++++++++++++++---------------- 2 files changed, 61 insertions(+), 37 deletions(-) diff --git a/src/bin/kawari-world.rs b/src/bin/kawari-world.rs index 6c721fa..504b644 100644 --- a/src/bin/kawari-world.rs +++ b/src/bin/kawari-world.rs @@ -573,15 +573,21 @@ async fn client_loop( } else { tracing::info!("Unknown command {command_name}"); - lua.scope(|scope| { - let connection_data = scope - .create_userdata_ref_mut(&mut lua_player) - .unwrap(); - let func: Function = - lua.globals().get("onUnknownCommandError").unwrap(); - func.call::<()>((command_name, connection_data)).unwrap(); - Ok(()) - }).unwrap(); + let mut call_func = || { + lua.scope(|scope| { + let connection_data = scope + .create_userdata_ref_mut(&mut lua_player)?;; + let func: Function = + lua.globals().get("onUnknownCommandError")?; + func.call::<()>((command_name, connection_data))?; + Ok(()) + }) + }; + + if let Err(err) = call_func() { + tracing::warn!("Lua error in Global.lua: {:?}", err); + handled = false; + } } } diff --git a/src/world/event.rs b/src/world/event.rs index 8d2a9a0..913116b 100644 --- a/src/world/event.rs +++ b/src/world/event.rs @@ -5,6 +5,7 @@ use crate::{common::ObjectTypeId, config::get_config}; use super::{LuaPlayer, Zone}; pub struct Event { + file_name: String, lua: Lua, } @@ -14,70 +15,87 @@ impl Event { let config = get_config(); let file_name = format!("{}/{}", &config.world.scripts_location, path); - lua.load(std::fs::read(&file_name).expect("Failed to locate scripts directory!")) + + if let Err(err) = lua + .load(std::fs::read(&file_name).expect("Failed to locate scripts directory!")) .set_name("@".to_string() + &file_name) .exec() - .unwrap(); + { + tracing::warn!("Syntax error in {}: {:?}", file_name, err); + return Self { file_name, lua }; + } lua.globals().set("EVENT_ID", id).unwrap(); - Self { lua } + Self { file_name, lua } } pub fn enter_territory(&mut self, player: &mut LuaPlayer, zone: &Zone) { - self.lua - .scope(|scope| { - let player = scope.create_userdata_ref_mut(player).unwrap(); - let zone = scope.create_userdata_ref(zone).unwrap(); + let mut run_script = || { + self.lua.scope(|scope| { + let player = scope.create_userdata_ref_mut(player)?; + let zone = scope.create_userdata_ref(zone)?; - let func: Function = self.lua.globals().get("onEnterTerritory").unwrap(); + let func: Function = self.lua.globals().get("onEnterTerritory")?; - func.call::<()>((player, zone)).unwrap(); + func.call::<()>((player, zone))?; Ok(()) }) - .unwrap(); + }; + if let Err(err) = run_script() { + tracing::warn!("Syntax error in {}: {:?}", self.file_name, err); + } } pub fn scene_finished(&mut self, player: &mut LuaPlayer, scene: u16) { - self.lua - .scope(|scope| { - let player = scope.create_userdata_ref_mut(player).unwrap(); + let mut run_script = || { + self.lua.scope(|scope| { + let player = scope.create_userdata_ref_mut(player)?; - let func: Function = self.lua.globals().get("onSceneFinished").unwrap(); + let func: Function = self.lua.globals().get("onSceneFinished")?; - func.call::<()>((player, scene)).unwrap(); + func.call::<()>((player, scene))?; Ok(()) }) - .unwrap(); + }; + if let Err(err) = run_script() { + tracing::warn!("Syntax error in {}: {:?}", self.file_name, err); + } } pub fn talk(&mut self, target_id: ObjectTypeId, player: &mut LuaPlayer) { - self.lua - .scope(|scope| { - let player = scope.create_userdata_ref_mut(player).unwrap(); + let mut run_script = || { + self.lua.scope(|scope| { + let player = scope.create_userdata_ref_mut(player)?; - let func: Function = self.lua.globals().get("onTalk").unwrap(); + let func: Function = self.lua.globals().get("onTalk")?; - func.call::<()>((target_id, player)).unwrap(); + func.call::<()>((target_id, player))?; Ok(()) }) - .unwrap(); + }; + if let Err(err) = run_script() { + tracing::warn!("Syntax error in {}: {:?}", self.file_name, err); + } } pub fn finish(&mut self, scene: u16, results: &[u32], player: &mut LuaPlayer) { - self.lua - .scope(|scope| { - let player = scope.create_userdata_ref_mut(player).unwrap(); + let mut run_script = || { + self.lua.scope(|scope| { + let player = scope.create_userdata_ref_mut(player)?; - let func: Function = self.lua.globals().get("onReturn").unwrap(); + let func: Function = self.lua.globals().get("onReturn")?; - func.call::<()>((scene, results, player)).unwrap(); + func.call::<()>((scene, results, player))?; Ok(()) }) - .unwrap(); + }; + if let Err(err) = run_script() { + tracing::warn!("Syntax error in {}: {:?}", self.file_name, err); + } } }