From caf70ea4696f40be65ee2ef65b649a5aaaec9bf2 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sun, 30 Mar 2025 19:13:24 -0400 Subject: [PATCH] Pass GameData into Zone, stop loading the same zone twice on login --- src/bin/kawari-world.rs | 11 +++++------ src/common/gamedata.rs | 2 +- src/packet/parsing.rs | 5 +---- src/world/connection.rs | 5 ++++- src/world/zone.rs | 9 +-------- 5 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/bin/kawari-world.rs b/src/bin/kawari-world.rs index 5d70d0f..e5fd1a2 100644 --- a/src/bin/kawari-world.rs +++ b/src/bin/kawari-world.rs @@ -213,11 +213,11 @@ async fn client_loop( let mut lua_player = LuaPlayer::default(); - let mut buf = [0; 2056]; + let mut buf = [0; 4096]; loop { tokio::select! { Ok(n) = connection.socket.read(&mut buf) => { - if n != 0 { + if n > 0 { let (segments, connection_type) = connection.parse_packet(&buf[..n]).await; for segment in &segments { match &segment.segment_type { @@ -318,9 +318,6 @@ async fn client_loop( .await; } - let zone_id = connection.player_data.zone_id; - connection.zone = Some(Zone::load(zone_id)); - // Player Setup { let ipc = ServerZoneIpcSegment { @@ -354,6 +351,7 @@ async fn client_loop( .await; } + let zone_id = connection.player_data.zone_id; connection.change_zone(zone_id).await; let lua = lua.lock().unwrap(); @@ -692,7 +690,8 @@ async fn client_loop( .unwrap(); // find the pop range on the other side - let new_zone = Zone::load(exit_box.territory_type); + let mut game_data = game_data.lock().unwrap(); + let new_zone = Zone::load(&mut game_data.game_data, exit_box.territory_type); let (destination_object, _) = new_zone .find_pop_range(exit_box.destination_instance_id) .unwrap(); diff --git a/src/common/gamedata.rs b/src/common/gamedata.rs index ae33f7f..211ad58 100644 --- a/src/common/gamedata.rs +++ b/src/common/gamedata.rs @@ -4,7 +4,7 @@ use crate::{common::Attributes, config::get_config}; /// Convenient methods built on top of Physis to access data relevant to the server pub struct GameData { - game_data: physis::gamedata::GameData, + pub game_data: physis::gamedata::GameData, } impl GameData { diff --git a/src/packet/parsing.rs b/src/packet/parsing.rs index 23bcc11..83f04db 100644 --- a/src/packet/parsing.rs +++ b/src/packet/parsing.rs @@ -168,10 +168,7 @@ pub async fn send_packet( let buffer = cursor.into_inner(); - socket - .write_all(&buffer) - .await - .expect("Failed to write packet!"); + socket.write_all(&buffer).await.unwrap(); } // temporary diff --git a/src/world/connection.rs b/src/world/connection.rs index 4e77c07..2c807e3 100644 --- a/src/world/connection.rs +++ b/src/world/connection.rs @@ -321,7 +321,10 @@ impl ZoneConnection { } pub async fn change_zone(&mut self, new_zone_id: u16) { - self.zone = Some(Zone::load(new_zone_id)); + { + let mut game_data = self.gamedata.lock().unwrap(); + self.zone = Some(Zone::load(&mut game_data.game_data, new_zone_id)); + } self.player_data.zone_id = new_zone_id; // Player Class Info diff --git a/src/world/zone.rs b/src/world/zone.rs index acb234f..18561f7 100644 --- a/src/world/zone.rs +++ b/src/world/zone.rs @@ -22,19 +22,12 @@ pub struct Zone { } impl Zone { - pub fn load(id: u16) -> Self { - let config = get_config(); - + pub fn load(game_data: &mut GameData, id: u16) -> Self { let mut zone = Self { id, ..Default::default() }; - let Some(mut game_data) = GameData::from_existing(Platform::Win32, &config.game_location) - else { - return zone; - }; - let Some(exh) = game_data.read_excel_sheet_header("TerritoryType") else { return zone; };