1
Fork 0
mirror of https://github.com/redstrate/Kawari.git synced 2025-05-06 04:37:46 +00:00

Implement Default trait for PacketSegment

This reduces a *ton* of places where I had to set
source_actor/target_actor to zero because it wasn't relevant.
This commit is contained in:
Joshua Goins 2025-05-02 00:28:05 -04:00
parent fb69689f2a
commit 0f98bfde52
4 changed files with 47 additions and 48 deletions

View file

@ -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;

View file

@ -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<Custo
};
let segment: PacketSegment<CustomIpcSegment> = PacketSegment {
source_actor: 0,
target_actor: 0,
segment_type: SegmentType::KawariIpc,
data: SegmentData::KawariIpc { data: segment },
..Default::default()
};
send_packet(

View file

@ -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<T: ReadWriteIpcSegment> {
#[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<T: ReadWriteIpcSegment> {
KawariIpc { data: CustomIpcSegment },
}
impl<T: ReadWriteIpcSegment> Default for SegmentData<T> {
fn default() -> Self {
Self::None()
}
}
#[binrw]
#[derive(Debug)]
pub struct PacketHeader {
@ -124,11 +134,23 @@ pub struct PacketSegment<T: ReadWriteIpcSegment> {
pub data: SegmentData<T>,
}
impl<T: ReadWriteIpcSegment> Default for PacketSegment<T> {
fn default() -> Self {
Self {
source_actor: 0,
target_actor: 0,
segment_type: SegmentType::default(),
data: SegmentData::default(),
}
}
}
impl<T: ReadWriteIpcSegment> PacketSegment<T> {
pub fn calc_size(&self) -> u32 {
let header = std::mem::size_of::<u32>() * 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<T: ReadWriteIpcSegment>(
timestamp: u32,
) {
let response_packet: PacketSegment<T> = 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<ClientLobbyIpcSegment> = 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();

View file

@ -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;
}