mirror of
https://github.com/redstrate/Kawari.git
synced 2025-06-30 11:47:45 +00:00
parent
6b0487f30f
commit
a6e270d5e3
2 changed files with 61 additions and 37 deletions
|
@ -573,15 +573,21 @@ async fn client_loop(
|
||||||
} else {
|
} else {
|
||||||
tracing::info!("Unknown command {command_name}");
|
tracing::info!("Unknown command {command_name}");
|
||||||
|
|
||||||
lua.scope(|scope| {
|
let mut call_func = || {
|
||||||
let connection_data = scope
|
lua.scope(|scope| {
|
||||||
.create_userdata_ref_mut(&mut lua_player)
|
let connection_data = scope
|
||||||
.unwrap();
|
.create_userdata_ref_mut(&mut lua_player)?;;
|
||||||
let func: Function =
|
let func: Function =
|
||||||
lua.globals().get("onUnknownCommandError").unwrap();
|
lua.globals().get("onUnknownCommandError")?;
|
||||||
func.call::<()>((command_name, connection_data)).unwrap();
|
func.call::<()>((command_name, connection_data))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}).unwrap();
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(err) = call_func() {
|
||||||
|
tracing::warn!("Lua error in Global.lua: {:?}", err);
|
||||||
|
handled = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ use crate::{common::ObjectTypeId, config::get_config};
|
||||||
use super::{LuaPlayer, Zone};
|
use super::{LuaPlayer, Zone};
|
||||||
|
|
||||||
pub struct Event {
|
pub struct Event {
|
||||||
|
file_name: String,
|
||||||
lua: Lua,
|
lua: Lua,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,70 +15,87 @@ impl Event {
|
||||||
|
|
||||||
let config = get_config();
|
let config = get_config();
|
||||||
let file_name = format!("{}/{}", &config.world.scripts_location, path);
|
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)
|
.set_name("@".to_string() + &file_name)
|
||||||
.exec()
|
.exec()
|
||||||
.unwrap();
|
{
|
||||||
|
tracing::warn!("Syntax error in {}: {:?}", file_name, err);
|
||||||
|
return Self { file_name, lua };
|
||||||
|
}
|
||||||
|
|
||||||
lua.globals().set("EVENT_ID", id).unwrap();
|
lua.globals().set("EVENT_ID", id).unwrap();
|
||||||
|
|
||||||
Self { lua }
|
Self { file_name, lua }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn enter_territory(&mut self, player: &mut LuaPlayer, zone: &Zone) {
|
pub fn enter_territory(&mut self, player: &mut LuaPlayer, zone: &Zone) {
|
||||||
self.lua
|
let mut run_script = || {
|
||||||
.scope(|scope| {
|
self.lua.scope(|scope| {
|
||||||
let player = scope.create_userdata_ref_mut(player).unwrap();
|
let player = scope.create_userdata_ref_mut(player)?;
|
||||||
let zone = scope.create_userdata_ref(zone).unwrap();
|
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(())
|
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) {
|
pub fn scene_finished(&mut self, player: &mut LuaPlayer, scene: u16) {
|
||||||
self.lua
|
let mut run_script = || {
|
||||||
.scope(|scope| {
|
self.lua.scope(|scope| {
|
||||||
let player = scope.create_userdata_ref_mut(player).unwrap();
|
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(())
|
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) {
|
pub fn talk(&mut self, target_id: ObjectTypeId, player: &mut LuaPlayer) {
|
||||||
self.lua
|
let mut run_script = || {
|
||||||
.scope(|scope| {
|
self.lua.scope(|scope| {
|
||||||
let player = scope.create_userdata_ref_mut(player).unwrap();
|
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(())
|
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) {
|
pub fn finish(&mut self, scene: u16, results: &[u32], player: &mut LuaPlayer) {
|
||||||
self.lua
|
let mut run_script = || {
|
||||||
.scope(|scope| {
|
self.lua.scope(|scope| {
|
||||||
let player = scope.create_userdata_ref_mut(player).unwrap();
|
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(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.unwrap();
|
};
|
||||||
|
if let Err(err) = run_script() {
|
||||||
|
tracing::warn!("Syntax error in {}: {:?}", self.file_name, err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue