2025-03-16 17:43:29 -04:00
use binrw ::{ BinRead , BinWrite , binrw } ;
2025-03-17 17:12:40 -04:00
/// Required to implement for specializations of `IpcSegment`. These should be read/writeable, however for client packets you can leave calc_size() unimplemented.
pub trait ReadWriteIpcSegment :
2025-03-16 17:43:29 -04:00
for < ' a > BinRead < Args < ' a > = ( ) > + for < ' a > BinWrite < Args < ' a > = ( ) > + std ::fmt ::Debug + 'static
{
2025-03-16 18:15:19 -04:00
/// Calculate the size of this Ipc segment *including* the 16 byte header.
2025-03-17 17:12:40 -04:00
/// When implementing this, please use the size seen in retail instead of guessing.
2025-03-25 18:15:41 -04:00
fn calc_size ( & self ) -> u32 ;
2025-03-16 17:43:29 -04:00
}
2025-03-17 16:58:48 -04:00
/// An IPC packet segment.
/// When implementing a new connection type, `OpCode` and `Data` can be used to specialize this type:
/// ```
/// # use binrw::binrw;
/// # use kawari::packet::IpcSegment;
/// #
/// # #[binrw]
/// # #[brw(repr = u16)]
/// # #[derive(Clone, PartialEq, Debug)]
/// # pub enum ClientLobbyIpcType {
/// # Dummy = 0x1,
/// # }
/// #
/// # #[binrw]
/// # #[br(import(magic: &ClientLobbyIpcType))]
/// # #[derive(Debug, Clone)]
/// # pub enum ClientLobbyIpcData {
/// # Dummy()
/// # }
/// #
/// pub type ClientLobbyIpcSegment = IpcSegment<ClientLobbyIpcType, ClientLobbyIpcData>;
/// ```
2025-03-16 17:43:29 -04:00
#[ binrw ]
#[ derive(Debug, Clone) ]
pub struct IpcSegment < OpCode , Data >
where
for < ' a > OpCode : BinRead < Args < ' a > = ( ) > + ' a + std ::fmt ::Debug ,
for < ' a > OpCode : BinWrite < Args < ' a > = ( ) > + ' a + std ::fmt ::Debug ,
for < ' a > Data : BinRead < Args < ' a > = ( & ' a OpCode , ) > + ' a + std ::fmt ::Debug ,
for < ' a > Data : BinWrite < Args < ' a > = ( ) > + ' a + std ::fmt ::Debug ,
{
2025-03-17 16:58:48 -04:00
/// Unknown purpose, but usually 20.
2025-03-16 17:43:29 -04:00
pub unk1 : u8 ,
2025-03-17 16:58:48 -04:00
/// Unknown purpose, but usually 0.
2025-03-16 17:43:29 -04:00
pub unk2 : u8 ,
2025-03-17 16:58:48 -04:00
/// The opcode for this segment.
2025-03-16 17:43:29 -04:00
pub op_code : OpCode ,
#[ brw(pad_before = 2) ] // empty
2025-03-17 16:58:48 -04:00
/// Unknown purpose, but safe to keep 0.
2025-03-16 17:43:29 -04:00
pub server_id : u16 ,
2025-03-17 16:58:48 -04:00
/// The timestamp of this packet in seconds since UNIX epoch.
2025-03-16 17:43:29 -04:00
pub timestamp : u32 ,
2025-03-17 16:58:48 -04:00
/// The data associated with the opcode.
2025-03-16 17:43:29 -04:00
#[ brw(pad_before = 4) ]
#[ br(args(&op_code)) ]
pub data : Data ,
}