diff --git a/src/bin/kawari-world.rs b/src/bin/kawari-world.rs index c0de4ce..082469d 100644 --- a/src/bin/kawari-world.rs +++ b/src/bin/kawari-world.rs @@ -295,25 +295,23 @@ async fn client_loop( } 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(), }, + ..Default::default() }) .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(), }, + ..Default::default() }) .await; @@ -915,8 +913,6 @@ async fn client_loop( { connection .send_segment(PacketSegment { - source_actor: 0, - target_actor: 0, segment_type: SegmentType::KawariIpc, data: SegmentData::KawariIpc { data: CustomIpcSegment { @@ -931,6 +927,7 @@ async fn client_loop( }, }, }, + ..Default::default() }) .await; } @@ -944,8 +941,6 @@ async fn client_loop( { connection .send_segment(PacketSegment { - source_actor: 0, - target_actor: 0, segment_type: SegmentType::KawariIpc, data: SegmentData::KawariIpc { data: CustomIpcSegment { @@ -957,6 +952,7 @@ async fn client_loop( data: CustomIpcData::ActorIdFound { actor_id }, }, }, + ..Default::default() }) .await; } @@ -968,8 +964,6 @@ async fn client_loop( { connection .send_segment(PacketSegment { - source_actor: 0, - target_actor: 0, segment_type: SegmentType::KawariIpc, data: SegmentData::KawariIpc { data: CustomIpcSegment { @@ -983,6 +977,7 @@ async fn client_loop( }, }, }, + ..Default::default() }) .await; } @@ -1016,8 +1011,6 @@ async fn client_loop( ConnectionType::None, CompressionType::Uncompressed, &[PacketSegment { - source_actor: 0, - target_actor: 0, segment_type: SegmentType::KawariIpc, data: SegmentData::KawariIpc { data: CustomIpcSegment { @@ -1031,6 +1024,7 @@ async fn client_loop( }, }, }, + ..Default::default() }], ) .await; @@ -1047,8 +1041,6 @@ async fn client_loop( ConnectionType::None, CompressionType::Uncompressed, &[PacketSegment { - source_actor: 0, - target_actor: 0, segment_type: SegmentType::KawariIpc, data: SegmentData::KawariIpc { data: CustomIpcSegment { @@ -1062,6 +1054,7 @@ async fn client_loop( }, }, }, + ..Default::default() }], ) .await; diff --git a/src/lobby/connection.rs b/src/lobby/connection.rs index ba32ff8..4a5d519 100644 --- a/src/lobby/connection.rs +++ b/src/lobby/connection.rs @@ -74,10 +74,9 @@ impl LobbyConnection { blowfish.encrypt(&mut data); self.send_segment(PacketSegment { - source_actor: 0, - target_actor: 0, segment_type: SegmentType::SecurityInitialize, data: SegmentData::SecurityInitialize { data }, + ..Default::default() }) .await; } @@ -102,10 +101,9 @@ impl LobbyConnection { }; self.send_segment(PacketSegment { - source_actor: 0, - target_actor: 0, segment_type: SegmentType::Ipc, data: SegmentData::Ipc { data: ipc }, + ..Default::default() }) .await; } @@ -146,10 +144,9 @@ impl LobbyConnection { }; let response_packet = PacketSegment { - source_actor: 0, - target_actor: 0, segment_type: SegmentType::Ipc, data: SegmentData::Ipc { data: ipc }, + ..Default::default() }; packets.push(response_packet); } @@ -168,10 +165,9 @@ impl LobbyConnection { }; let response_packet = PacketSegment { - source_actor: 0, - target_actor: 0, segment_type: SegmentType::Ipc, data: SegmentData::Ipc { data: ipc }, + ..Default::default() }; packets.push(response_packet); } @@ -271,10 +267,9 @@ impl LobbyConnection { }; self.send_segment(PacketSegment { - source_actor: 0, - target_actor: 0, segment_type: SegmentType::Ipc, data: SegmentData::Ipc { data: ipc }, + ..Default::default() }) .await; } @@ -304,10 +299,9 @@ impl LobbyConnection { }; self.send_segment(PacketSegment { - source_actor: 0, - target_actor: 0, segment_type: SegmentType::Ipc, data: SegmentData::Ipc { data: ipc }, + ..Default::default() }) .await; } @@ -332,10 +326,9 @@ impl LobbyConnection { }; self.send_segment(PacketSegment { - source_actor: 0, - target_actor: 0, segment_type: SegmentType::Ipc, data: SegmentData::Ipc { data: ipc }, + ..Default::default() }) .await; } @@ -393,10 +386,9 @@ impl LobbyConnection { }; self.send_segment(PacketSegment { - source_actor: 0x0, - target_actor: 0x0, segment_type: SegmentType::Ipc, data: SegmentData::Ipc { data: ipc }, + ..Default::default() }) .await; } else { @@ -416,10 +408,9 @@ impl LobbyConnection { }; let response_packet = PacketSegment { - source_actor: 0x0, - target_actor: 0x0, segment_type: SegmentType::Ipc, data: SegmentData::Ipc { data: ipc }, + ..Default::default() }; self.send_segment(response_packet).await; } @@ -489,10 +480,9 @@ impl LobbyConnection { }; self.send_segment(PacketSegment { - source_actor: 0x0, - target_actor: 0x0, segment_type: SegmentType::Ipc, data: SegmentData::Ipc { data: ipc }, + ..Default::default() }) .await; } @@ -542,10 +532,9 @@ impl LobbyConnection { }; self.send_segment(PacketSegment { - source_actor: 0x0, - target_actor: 0x0, segment_type: SegmentType::Ipc, data: SegmentData::Ipc { data: ipc }, + ..Default::default() }) .await; } @@ -578,10 +567,9 @@ pub async fn send_custom_world_packet(segment: CustomIpcSegment) -> Option = PacketSegment { - source_actor: 0, - target_actor: 0, segment_type: SegmentType::KawariIpc, data: SegmentData::KawariIpc { data: segment }, + ..Default::default() }; send_packet( diff --git a/src/packet/parsing.rs b/src/packet/parsing.rs index 9dddacc..c97cb22 100644 --- a/src/packet/parsing.rs +++ b/src/packet/parsing.rs @@ -26,8 +26,10 @@ pub enum ConnectionType { #[binrw] #[brw(repr = u16)] -#[derive(Debug, PartialEq, Copy, Clone)] +#[derive(Debug, PartialEq, Copy, Clone, Default)] pub enum SegmentType { + #[default] + None = 0x0, Setup = 0x1, Initialize = 0x2, // Also known as "UPLAYER" @@ -46,6 +48,8 @@ pub enum SegmentType { #[brw(import(kind: &SegmentType, size: u32, encryption_key: Option<&[u8]>))] #[derive(Debug, Clone)] pub enum SegmentData { + #[br(pre_assert(*kind == SegmentType::None))] + None(), #[br(pre_assert(*kind == SegmentType::Setup))] Setup { #[brw(pad_before = 4)] // empty @@ -95,6 +99,12 @@ pub enum SegmentData { KawariIpc { data: CustomIpcSegment }, } +impl Default for SegmentData { + fn default() -> Self { + Self::None() + } +} + #[binrw] #[derive(Debug)] pub struct PacketHeader { @@ -124,11 +134,23 @@ pub struct PacketSegment { pub data: SegmentData, } +impl Default for PacketSegment { + fn default() -> Self { + Self { + source_actor: 0, + target_actor: 0, + segment_type: SegmentType::default(), + data: SegmentData::default(), + } + } +} + impl PacketSegment { pub fn calc_size(&self) -> u32 { let header = std::mem::size_of::() * 4; header as u32 + match &self.data { + SegmentData::None() => 16, SegmentData::SecuritySetup { .. } => 616, SegmentData::SecurityInitialize { .. } => 640, SegmentData::Ipc { data } => data.calc_size(), @@ -231,10 +253,9 @@ pub async fn send_keep_alive( timestamp: u32, ) { let response_packet: PacketSegment = PacketSegment { - source_actor: 0, - target_actor: 0, segment_type: SegmentType::KeepAliveResponse, data: SegmentData::KeepAliveResponse { id, timestamp }, + ..Default::default() }; send_packet( socket, @@ -297,10 +318,9 @@ mod tests { let mut cursor = Cursor::new(Vec::new()); let packet_segment: PacketSegment = PacketSegment { - source_actor: 0, - target_actor: 0, - segment_type: SegmentType::Setup, + segment_type: SegmentType::None, data: packet.clone(), + ..Default::default() }; packet_segment.write_le(&mut cursor).unwrap(); diff --git a/src/world/connection.rs b/src/world/connection.rs index e60d257..372fa22 100644 --- a/src/world/connection.rs +++ b/src/world/connection.rs @@ -195,25 +195,23 @@ impl ZoneConnection { // 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(), }, + ..Default::default() }) .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(), }, + ..Default::default() }) .await; }