From 54fd1b90b126930bed4159d3c40b57c9b642edca Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Fri, 2 May 2025 00:11:37 -0400 Subject: [PATCH] Move chat connection initialization out of WorldConnection I think this just causes more confusion, it's not really it's job to do this. --- src/bin/kawari-world.rs | 57 ++++++++++++++++++-- src/world/connection.rs | 112 ++++++++++------------------------------ 2 files changed, 80 insertions(+), 89 deletions(-) diff --git a/src/bin/kawari-world.rs b/src/bin/kawari-world.rs index 90f4a89..39af4a8 100644 --- a/src/bin/kawari-world.rs +++ b/src/bin/kawari-world.rs @@ -9,11 +9,12 @@ use kawari::common::{GameData, ObjectId, timestamp_secs}; use kawari::common::{Position, determine_initial_starting_zone}; use kawari::config::get_config; use kawari::oodle::OodleNetwork; -use kawari::opcodes::ServerZoneIpcType; +use kawari::opcodes::{ServerChatIpcType, ServerZoneIpcType}; use kawari::packet::{ CompressionType, ConnectionType, PacketSegment, PacketState, SegmentData, SegmentType, send_keep_alive, send_packet, }; +use kawari::world::chat::{ServerChatIpcData, ServerChatIpcSegment}; use kawari::world::ipc::{ ActionEffect, ActionResult, ClientZoneIpcData, EffectKind, GameMasterCommandType, GameMasterRank, OnlineStatus, ServerZoneIpcData, ServerZoneIpcSegment, SocialListRequestType, @@ -282,15 +283,63 @@ async fn client_loop( connection.player_data = database.find_player_data(actor_id); } - // collect actor data - connection.initialize(&connection_type, actor_id).await; - if connection_type == ConnectionType::Zone { + // collect actor data + connection.initialize(actor_id).await; + exit_position = Some(connection.player_data.position); exit_rotation = Some(connection.player_data.rotation); // tell the server we exist, now that we confirmed we are a legitimate connection connection.handle.send(ToServer::NewClient(client_handle.clone())).await; + } else if connection_type == ConnectionType::Chat { + // We have send THEM a keep alive + connection.send_chat_segment(PacketSegment { + source_actor: 0, + target_actor: 0, + segment_type: SegmentType::KeepAliveRequest, + data: SegmentData::KeepAliveRequest { + id: 0xE0037603u32, + timestamp: timestamp_secs(), + }, + }) + .await; + + // initialize connection + connection.send_chat_segment(PacketSegment { + source_actor: 0, + target_actor: 0, + segment_type: SegmentType::Initialize, + data: SegmentData::Initialize { + player_id: connection.player_data.actor_id, + timestamp: timestamp_secs(), + }, + }) + .await; + + // we need the actor id at this point! + assert!(connection.player_data.actor_id != 0); + + // send login reply + { + let ipc = ServerChatIpcSegment { + op_code: ServerChatIpcType::LoginReply, + timestamp: timestamp_secs(), + data: ServerChatIpcData::LoginReply { + timestamp: 0, + sid: 0, + }, + ..Default::default() + }; + + connection.send_chat_segment(PacketSegment { + source_actor: connection.player_data.actor_id, + target_actor: connection.player_data.actor_id, + segment_type: SegmentType::Ipc, + data: SegmentData::Ipc { data: ipc }, + }) + .await; + } } } SegmentData::Ipc { data } => { diff --git a/src/world/connection.rs b/src/world/connection.rs index 0fa2c3a..a51e757 100644 --- a/src/world/connection.rs +++ b/src/world/connection.rs @@ -12,16 +12,16 @@ use crate::{ OBFUSCATION_ENABLED_MODE, common::{GameData, ObjectId, Position, timestamp_secs}, config::get_config, - opcodes::{ServerChatIpcType, ServerZoneIpcType}, + opcodes::ServerZoneIpcType, packet::{ CompressionType, ConnectionType, PacketSegment, PacketState, SegmentData, SegmentType, parse_packet, send_packet, }, - world::chat::{ServerChatIpcData, ServerChatIpcSegment}, }; use super::{ Actor, CharacterData, Event, Inventory, Item, LuaPlayer, StatusEffects, WorldDatabase, Zone, + chat::ServerChatIpcSegment, inventory::Container, ipc::{ ActorControlSelf, ActorMove, ActorSetPos, ClientZoneIpcSegment, CommonSpawn, ContainerInfo, @@ -181,7 +181,7 @@ impl ZoneConnection { .await; } - pub async fn initialize(&mut self, connection_type: &ConnectionType, actor_id: u32) { + pub async fn initialize(&mut self, actor_id: u32) { // some still hardcoded values self.player_data.classjob_id = 1; self.player_data.level = 5; @@ -190,90 +190,32 @@ impl ZoneConnection { self.player_data.curr_mp = 10000; self.player_data.max_mp = 10000; - match connection_type { - ConnectionType::Zone => { - tracing::info!("Client {actor_id} is initializing zone session..."); + tracing::info!("Client {actor_id} is initializing zone session..."); - // We have send THEM a keep alive - { - self.send_segment(PacketSegment { - source_actor: 0, - target_actor: 0, - segment_type: SegmentType::KeepAliveRequest, - data: SegmentData::KeepAliveRequest { - id: 0xE0037603u32, - timestamp: timestamp_secs(), - }, - }) - .await; - } - - self.send_segment(PacketSegment { - source_actor: 0, - target_actor: 0, - segment_type: SegmentType::Initialize, - data: SegmentData::Initialize { - player_id: self.player_data.actor_id, - timestamp: timestamp_secs(), - }, - }) - .await; - } - ConnectionType::Chat => { - tracing::info!("Client {actor_id} is initializing chat session..."); - - // We have send THEM a keep alive - { - self.send_chat_segment(PacketSegment { - source_actor: 0, - target_actor: 0, - segment_type: SegmentType::KeepAliveRequest, - data: SegmentData::KeepAliveRequest { - id: 0xE0037603u32, - timestamp: timestamp_secs(), - }, - }) - .await; - } - - { - self.send_chat_segment(PacketSegment { - source_actor: 0, - target_actor: 0, - segment_type: SegmentType::Initialize, - data: SegmentData::Initialize { - player_id: self.player_data.actor_id, - timestamp: timestamp_secs(), - }, - }) - .await; - } - - // we need the actor id at this point! - assert!(self.player_data.actor_id != 0); - - { - let ipc = ServerChatIpcSegment { - op_code: ServerChatIpcType::LoginReply, - timestamp: timestamp_secs(), - data: ServerChatIpcData::LoginReply { - timestamp: 0, - sid: 0, - }, - ..Default::default() - }; - - self.send_chat_segment(PacketSegment { - source_actor: self.player_data.actor_id, - target_actor: self.player_data.actor_id, - segment_type: SegmentType::Ipc, - data: SegmentData::Ipc { data: ipc }, - }) - .await; - } - } - _ => panic!("The client is trying to initialize the wrong connection?!"), + // We have send THEM a keep alive + { + self.send_segment(PacketSegment { + source_actor: 0, + target_actor: 0, + segment_type: SegmentType::KeepAliveRequest, + data: SegmentData::KeepAliveRequest { + id: 0xE0037603u32, + timestamp: timestamp_secs(), + }, + }) + .await; } + + self.send_segment(PacketSegment { + source_actor: 0, + target_actor: 0, + segment_type: SegmentType::Initialize, + data: SegmentData::Initialize { + player_id: self.player_data.actor_id, + timestamp: timestamp_secs(), + }, + }) + .await; } pub async fn set_player_position(&mut self, position: Position) {