From de5e6cfb94ec865f2d8e3aa9d2e877b52121bf1c Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Fri, 2 May 2025 23:38:44 -0400 Subject: [PATCH] Use Default trait in more segment construction --- src/bin/kawari-world.rs | 30 +++-------- src/ipc/kawari/mod.rs | 13 +++++ src/lobby/connection.rs | 103 ++++++++------------------------------ src/world/chat_handler.rs | 24 +++------ src/world/connection.rs | 8 +-- src/world/lua.rs | 4 +- 6 files changed, 48 insertions(+), 134 deletions(-) diff --git a/src/bin/kawari-world.rs b/src/bin/kawari-world.rs index 5937a70..88ccf62 100644 --- a/src/bin/kawari-world.rs +++ b/src/bin/kawari-world.rs @@ -913,15 +913,12 @@ async fn client_loop( segment_type: SegmentType::KawariIpc, data: SegmentData::KawariIpc { data: CustomIpcSegment { - unk1: 0, - unk2: 0, op_code: CustomIpcType::CharacterCreated, - option: 0, - timestamp: 0, data: CustomIpcData::CharacterCreated { actor_id, content_id, }, + ..Default::default() }, }, ..Default::default() @@ -941,12 +938,9 @@ async fn client_loop( segment_type: SegmentType::KawariIpc, data: SegmentData::KawariIpc { data: CustomIpcSegment { - unk1: 0, - unk2: 0, op_code: CustomIpcType::ActorIdFound, - option: 0, - timestamp: 0, data: CustomIpcData::ActorIdFound { actor_id }, + ..Default::default() }, }, ..Default::default() @@ -964,14 +958,11 @@ async fn client_loop( segment_type: SegmentType::KawariIpc, data: SegmentData::KawariIpc { data: CustomIpcSegment { - unk1: 0, - unk2: 0, op_code: CustomIpcType::NameIsAvailableResponse, - option: 0, - timestamp: 0, data: CustomIpcData::NameIsAvailableResponse { free: is_name_free, }, + ..Default::default() }, }, ..Default::default() @@ -1011,14 +1002,11 @@ async fn client_loop( segment_type: SegmentType::KawariIpc, data: SegmentData::KawariIpc { data: CustomIpcSegment { - unk1: 0, - unk2: 0, op_code: CustomIpcType::RequestCharacterListRepsonse, - option: 0, - timestamp: 0, data: CustomIpcData::RequestCharacterListRepsonse { characters }, + ..Default::default() }, }, ..Default::default() @@ -1041,14 +1029,11 @@ async fn client_loop( segment_type: SegmentType::KawariIpc, data: SegmentData::KawariIpc { data: CustomIpcSegment { - unk1: 0, - unk2: 0, op_code: CustomIpcType::CharacterDeleted, - option: 0, - timestamp: 0, data: CustomIpcData::CharacterDeleted { deleted: 1, }, + ..Default::default() }, }, ..Default::default() @@ -1078,14 +1063,11 @@ async fn client_loop( segment_type: SegmentType::KawariIpc, data: SegmentData::KawariIpc { data: CustomIpcSegment { - unk1: 0, - unk2: 0, op_code: CustomIpcType::CharacterRemade, - option: 0, - timestamp: 0, data: CustomIpcData::CharacterRemade { content_id: *content_id, }, + ..Default::default() }, }, ..Default::default() diff --git a/src/ipc/kawari/mod.rs b/src/ipc/kawari/mod.rs index 28b2cda..b20c898 100644 --- a/src/ipc/kawari/mod.rs +++ b/src/ipc/kawari/mod.rs @@ -29,6 +29,19 @@ impl ReadWriteIpcSegment for CustomIpcSegment { } } +impl Default for CustomIpcSegment { + fn default() -> Self { + Self { + unk1: 0, + unk2: 0, + op_code: CustomIpcType::GetActorId, + option: 0, + timestamp: 0, + data: CustomIpcData::GetActorId { content_id: 0 }, + } + } +} + #[binrw] #[brw(repr = u16)] #[derive(Default, Clone, PartialEq, Debug)] diff --git a/src/lobby/connection.rs b/src/lobby/connection.rs index da0d774..1379faf 100644 --- a/src/lobby/connection.rs +++ b/src/lobby/connection.rs @@ -88,12 +88,10 @@ impl LobbyConnection { }); let ipc = ServerLobbyIpcSegment { - unk1: 0, - unk2: 0, op_code: ServerLobbyIpcType::LoginReply, - option: 0, timestamp: timestamp_secs(), data: service_account_list, + ..Default::default() }; self.send_segment(PacketSegment { @@ -113,10 +111,8 @@ impl LobbyConnection { let mut servers = [Server { id: config.world.world_id, - index: 0, - flags: 0, - icon: 0, name: self.world_name.clone(), + ..Default::default() }] .to_vec(); // add any empty boys @@ -125,18 +121,16 @@ impl LobbyConnection { let lobby_server_list = ServerLobbyIpcData::DistWorldInfo(DistWorldInfo { sequence: 0, unk1: 1, - offset: 0, num_servers: 1, servers, + ..Default::default() }); let ipc = ServerLobbyIpcSegment { - unk1: 0, - unk2: 0, op_code: ServerLobbyIpcType::DistWorldInfo, - option: 0, timestamp: timestamp_secs(), data: lobby_server_list, + ..Default::default() }; let response_packet = PacketSegment { @@ -152,12 +146,10 @@ impl LobbyConnection { let lobby_retainer_list = ServerLobbyIpcData::DistRetainerInfo { unk1: 1 }; let ipc = ServerLobbyIpcSegment { - unk1: 0, - unk2: 0, op_code: ServerLobbyIpcType::DistRetainerInfo, - option: 0, timestamp: timestamp_secs(), data: lobby_retainer_list, + ..Default::default() }; let response_packet = PacketSegment { @@ -180,14 +172,11 @@ impl LobbyConnection { // now send them the character list { let charlist_request = CustomIpcSegment { - unk1: 0, - unk2: 0, op_code: CustomIpcType::RequestCharacterList, - option: 0, - timestamp: 0, data: CustomIpcData::RequestCharacterList { service_account_id: self.selected_service_account.unwrap(), }, + ..Default::default() }; let name_response = send_custom_world_packet(charlist_request) @@ -214,14 +203,7 @@ impl LobbyConnection { sequence, counter: (i * 4) + 1, // TODO: why the + 1 here? num_in_packet: characters_in_packet.len() as u8, - unk1: 0, - unk2: 0, - unk3: 0, unk4: 128, - unk5: [0; 7], - unk6: 0, - veteran_rank: 0, - unk7: 0, days_subscribed: 30, remaining_days: 30, days_to_next_rank: 0, @@ -229,37 +211,23 @@ impl LobbyConnection { max_characters_on_world: 8, entitled_expansion: 5, characters: characters_in_packet, + ..Default::default() } } else { ServiceLoginReply { sequence, counter: i * 4, num_in_packet: characters_in_packet.len() as u8, - unk1: 0, - unk2: 0, - unk3: 0, - unk4: 0, - unk5: [0; 7], - unk6: 0, - veteran_rank: 0, - unk7: 0, - days_subscribed: 0, - remaining_days: 0, - days_to_next_rank: 0, - max_characters_on_world: 0, - unk8: 0, - entitled_expansion: 0, characters: characters_in_packet, + ..Default::default() } }; let ipc = ServerLobbyIpcSegment { - unk1: 0, - unk2: 0, op_code: ServerLobbyIpcType::ServiceLoginReply, - option: 0, timestamp: timestamp_secs(), data: ServerLobbyIpcData::ServiceLoginReply(lobby_character_list), + ..Default::default() }; self.send_segment(PacketSegment { @@ -286,12 +254,10 @@ impl LobbyConnection { }; let ipc = ServerLobbyIpcSegment { - unk1: 0, - unk2: 0, op_code: ServerLobbyIpcType::GameLoginReply, - option: 0, timestamp: timestamp_secs(), data: enter_world, + ..Default::default() }; self.send_segment(PacketSegment { @@ -313,12 +279,10 @@ impl LobbyConnection { }; let ipc = ServerLobbyIpcSegment { - unk1: 0, - unk2: 0, op_code: ServerLobbyIpcType::NackReply, - option: 0, timestamp: timestamp_secs(), data: lobby_error, + ..Default::default() }; self.send_segment(PacketSegment { @@ -339,14 +303,11 @@ impl LobbyConnection { // check with the world server if the name is available let name_request = CustomIpcSegment { - unk1: 0, - unk2: 0, op_code: CustomIpcType::CheckNameIsAvailable, - option: 0, - timestamp: 0, data: CustomIpcData::CheckNameIsAvailable { name: character_action.name.clone(), }, + ..Default::default() }; let name_response = send_custom_world_packet(name_request) @@ -362,11 +323,7 @@ impl LobbyConnection { self.stored_character_creation_name = character_action.name.clone(); let ipc = ServerLobbyIpcSegment { - unk1: 0, - unk2: 0, op_code: ServerLobbyIpcType::CharaMakeReply, - option: 0, - timestamp: 0, data: ServerLobbyIpcData::CharaMakeReply { sequence: character_action.sequence + 1, unk1: 0x1, @@ -379,6 +336,7 @@ impl LobbyConnection { ..Default::default() }, }, + ..Default::default() }; self.send_segment(PacketSegment { @@ -389,11 +347,7 @@ impl LobbyConnection { .await; } else { let ipc = ServerLobbyIpcSegment { - unk1: 0, - unk2: 0, op_code: ServerLobbyIpcType::NackReply, - option: 0, - timestamp: 0, data: ServerLobbyIpcData::NackReply { sequence: character_action.sequence, error: 0x00000bdb, @@ -401,6 +355,7 @@ impl LobbyConnection { value: 0, unk1: 0, }, + ..Default::default() }; let response_packet = PacketSegment { @@ -420,16 +375,13 @@ impl LobbyConnection { // tell the world server to create this character { let ipc_segment = CustomIpcSegment { - unk1: 0, - unk2: 0, op_code: CustomIpcType::RequestCreateCharacter, - option: 0, - timestamp: 0, data: CustomIpcData::RequestCreateCharacter { service_account_id: self.selected_service_account.unwrap(), name: self.stored_character_creation_name.clone(), // TODO: worth double-checking, but AFAIK we have to store it this way? chara_make_json: character_action.json.clone(), }, + ..Default::default() }; let response_segment = send_custom_world_packet(ipc_segment).await.unwrap(); @@ -452,11 +404,7 @@ impl LobbyConnection { // a slightly different character created packet now { let ipc = ServerLobbyIpcSegment { - unk1: 0, - unk2: 0, op_code: ServerLobbyIpcType::CharaMakeReply, - option: 0, - timestamp: 0, data: ServerLobbyIpcData::CharaMakeReply { sequence: character_action.sequence + 1, unk1: 0x1, @@ -471,6 +419,7 @@ impl LobbyConnection { ..Default::default() }, }, + ..Default::default() }; self.send_segment(PacketSegment { @@ -486,14 +435,11 @@ impl LobbyConnection { // tell the world server to yeet this guy { let ipc_segment = CustomIpcSegment { - unk1: 0, - unk2: 0, op_code: CustomIpcType::DeleteCharacter, - option: 0, - timestamp: 0, data: CustomIpcData::DeleteCharacter { content_id: character_action.content_id, }, + ..Default::default() }; let _ = send_custom_world_packet(ipc_segment).await.unwrap(); @@ -504,11 +450,7 @@ impl LobbyConnection { // send a confirmation that the deletion was successful { let ipc = ServerLobbyIpcSegment { - unk1: 0, - unk2: 0, op_code: ServerLobbyIpcType::CharaMakeReply, - option: 0, - timestamp: 0, data: ServerLobbyIpcData::CharaMakeReply { sequence: character_action.sequence + 1, unk1: 0x1, @@ -523,6 +465,7 @@ impl LobbyConnection { ..Default::default() }, }, + ..Default::default() }; self.send_segment(PacketSegment { @@ -539,15 +482,12 @@ impl LobbyConnection { // tell the world server to turn this guy into a catgirl { let ipc_segment = CustomIpcSegment { - unk1: 0, - unk2: 0, op_code: CustomIpcType::RemakeCharacter, - option: 0, - timestamp: 0, data: CustomIpcData::RemakeCharacter { content_id: character_action.content_id, chara_make_json: character_action.json.clone(), }, + ..Default::default() }; let _ = send_custom_world_packet(ipc_segment).await.unwrap(); @@ -558,11 +498,7 @@ impl LobbyConnection { // send a confirmation that the remakewas successful { let ipc = ServerLobbyIpcSegment { - unk1: 0, - unk2: 0, op_code: ServerLobbyIpcType::CharaMakeReply, - option: 0, - timestamp: 0, data: ServerLobbyIpcData::CharaMakeReply { sequence: character_action.sequence + 1, unk1: 0x1, @@ -577,6 +513,7 @@ impl LobbyConnection { ..Default::default() }, }, + ..Default::default() }; self.send_segment(PacketSegment { diff --git a/src/world/chat_handler.rs b/src/world/chat_handler.rs index e4f141c..64fc805 100644 --- a/src/world/chat_handler.rs +++ b/src/world/chat_handler.rs @@ -129,10 +129,7 @@ impl ChatHandler { // zone in { let ipc = ServerZoneIpcSegment { - unk1: 20, - unk2: 0, op_code: ServerZoneIpcType::ActorControl, - option: 0, timestamp: timestamp_secs(), data: ServerZoneIpcData::ActorControl(ActorControl { category: ActorControlCategory::ZoneIn { @@ -140,6 +137,7 @@ impl ChatHandler { raise_anim: 0x0, }, }), + ..Default::default() }; connection @@ -154,10 +152,7 @@ impl ChatHandler { } "!spawnnpc" => { let ipc = ServerZoneIpcSegment { - unk1: 20, - unk2: 0, op_code: ServerZoneIpcType::NpcSpawn, - option: 0, timestamp: timestamp_secs(), data: ServerZoneIpcData::NpcSpawn(NpcSpawn { common: CommonSpawn { @@ -192,6 +187,7 @@ impl ChatHandler { }, ..Default::default() }), + ..Default::default() }; connection @@ -209,10 +205,7 @@ impl ChatHandler { // spawn a tiny mandragora { let ipc = ServerZoneIpcSegment { - unk1: 20, - unk2: 0, op_code: ServerZoneIpcType::NpcSpawn, - option: 0, timestamp: timestamp_secs(), data: ServerZoneIpcData::NpcSpawn(NpcSpawn { aggression_mode: 1, @@ -233,6 +226,7 @@ impl ChatHandler { }, ..Default::default() }), + ..Default::default() }; connection @@ -258,10 +252,7 @@ impl ChatHandler { // Load the game script for this event on the client { let ipc = ServerZoneIpcSegment { - unk1: 20, - unk2: 0, op_code: ServerZoneIpcType::EventStart, - option: 0, timestamp: timestamp_secs(), data: ServerZoneIpcData::EventStart(EventStart { target_id: ObjectTypeId { @@ -273,6 +264,7 @@ impl ChatHandler { flags: 0, event_arg: 182, // zone? }), + ..Default::default() }; connection @@ -288,16 +280,14 @@ impl ChatHandler { // set our status icon to viewing cutscene { let ipc = ServerZoneIpcSegment { - unk1: 20, - unk2: 0, op_code: ServerZoneIpcType::ActorControl, - option: 0, timestamp: timestamp_secs(), data: ServerZoneIpcData::ActorControl(ActorControl { category: ActorControlCategory::SetStatusIcon { icon: OnlineStatus::ViewingCutscene, }, }), + ..Default::default() }; connection @@ -333,15 +323,13 @@ impl ChatHandler { common.spawn_index = connection.get_free_spawn_index(); let ipc = ServerZoneIpcSegment { - unk1: 20, - unk2: 0, op_code: ServerZoneIpcType::NpcSpawn, - option: 0, timestamp: timestamp_secs(), data: ServerZoneIpcData::NpcSpawn(NpcSpawn { common, ..Default::default() }), + ..Default::default() }; connection diff --git a/src/world/connection.rs b/src/world/connection.rs index 38b1c49..800a295 100644 --- a/src/world/connection.rs +++ b/src/world/connection.rs @@ -282,15 +282,13 @@ impl ZoneConnection { common.spawn_index = actor.spawn_index as u8; let ipc = ServerZoneIpcSegment { - unk1: 20, - unk2: 0, op_code: ServerZoneIpcType::NpcSpawn, - option: 0, timestamp: timestamp_secs(), data: ServerZoneIpcData::NpcSpawn(NpcSpawn { common, ..Default::default() }), + ..Default::default() }; self.send_segment(PacketSegment { @@ -309,15 +307,13 @@ impl ZoneConnection { tracing::info!("Removing actor {actor_id} {}!", actor.spawn_index); let ipc = ServerZoneIpcSegment { - unk1: 20, - unk2: 0, op_code: ServerZoneIpcType::Delete, - option: 0, timestamp: timestamp_secs(), data: ServerZoneIpcData::Delete { spawn_index: actor.spawn_index as u8, actor_id, }, + ..Default::default() }; self.send_segment(PacketSegment { diff --git a/src/world/lua.rs b/src/world/lua.rs index 66bc2a3..d47c171 100644 --- a/src/world/lua.rs +++ b/src/world/lua.rs @@ -55,10 +55,7 @@ impl LuaPlayer { fn play_scene(&mut self, event_id: u32, scene: u16, scene_flags: u32, param: u8) { let ipc = ServerZoneIpcSegment { - unk1: 20, - unk2: 0, op_code: ServerZoneIpcType::EventScene, - option: 0, timestamp: timestamp_secs(), data: ServerZoneIpcData::EventScene(EventScene { actor_id: ObjectTypeId { @@ -71,6 +68,7 @@ impl LuaPlayer { unk2: param, ..Default::default() }), + ..Default::default() }; self.queue_segment(PacketSegment {