diff --git a/src/packet/login.rs b/src/packet/login.rs index f381991..48d7689 100644 --- a/src/packet/login.rs +++ b/src/packet/login.rs @@ -1,7 +1,7 @@ use chrono::{DateTime, Utc}; use psopacket::{pso_packet, PSOPacketData}; -use crate::{PSOPacket, PacketParseError, PSOPacketData}; +use crate::{PSOPacket, PacketParseError, PSOPacketData, utf8_to_utf16_array}; use crate::character::character::SelectScreenCharacter; @@ -484,7 +484,7 @@ impl Timestamp { } -#[derive(Debug)] +#[derive(Debug, PSOPacketData, PartialEq, Clone)] pub struct ShipListEntry { pub menu: u32, pub item: u32, @@ -492,91 +492,26 @@ pub struct ShipListEntry { pub name: [u16; 0x11], } -impl PSOPacketData for ShipListEntry { - fn from_bytes(cursor: &mut R) -> Result { - let mut buf4 = [0u8; 4]; - cursor.read(&mut buf4).map_err(|_| PacketParseError::ReadError)?; - let menu = u32::from_le_bytes(buf4); - cursor.read(&mut buf4).map_err(|_| PacketParseError::ReadError)?; - let item = u32::from_le_bytes(buf4); - - let mut buf2 = [0u8; 2]; - cursor.read(&mut buf2).map_err(|_| PacketParseError::ReadError)?; - let flags = u16::from_le_bytes(buf2); - - let mut name = [0u8; 0x11 * 2]; - cursor.read(&mut name).map_err(|_| PacketParseError::ReadError)?; - Ok(ShipListEntry { - menu: menu, - item: item, - flags: flags, - name: unsafe { std::mem::transmute(name)} - }) - } - - fn as_bytes(&self) -> Vec { - let mut bytes = Vec::new(); - bytes.extend_from_slice(&u32::to_le_bytes(self.menu)); - bytes.extend_from_slice(&u32::to_le_bytes(self.item)); - bytes.extend_from_slice(&u16::to_le_bytes(self.flags)); - bytes.extend_from_slice(&unsafe { std::mem::transmute::<[u16; 0x11], [u8; 0x11*2]>(self.name) }); - bytes - } -} - - +#[pso_packet(0xA0)] pub struct ShipList { + baseship: ShipListEntry, pub ships: Vec, } -impl PSOPacket for ShipList { - fn from_bytes(_data: &[u8]) -> Result { - unimplemented!(); - } - - fn as_bytes(&self) -> Vec { - let mut buf: Vec = Vec::new(); - buf.extend_from_slice(&u32::to_le_bytes(self.ships.len() as u32)); - buf.extend_from_slice(&u32::to_le_bytes(0)); - buf.extend_from_slice(&u32::to_le_bytes(0)); - buf.extend_from_slice(&u16::to_le_bytes(0)); - let filler_name: [u16; 0x11] = ['S' as u16,0, 'h' as u16,0, 'i' as u16,0, 'p' as u16 ,0,0,0,0,0,0,0,0,0,0]; - for char in filler_name.iter() { - buf.extend_from_slice(&u16::to_le_bytes(*char)); - } - - for ship in self.ships.iter() { - buf.extend_from_slice(&u32::to_le_bytes(ship.menu)); - buf.extend_from_slice(&u32::to_le_bytes(ship.item)); - buf.extend_from_slice(&u16::to_le_bytes(ship.flags)); - for char in ship.name.iter() { - buf.extend_from_slice(&u16::to_le_bytes(*char)); - } +impl ShipList { + pub fn new(ships: Vec) -> ShipList { + ShipList { + baseship: ShipListEntry { + menu: ships[0].menu, + item: 0, + flags: 0, + name: utf8_to_utf16_array!("Ship", 0x11), + }, + ships: ships, } - - while buf.len() % 4 != 0 { - buf.push(0); - } - - let pkt_len = (buf.len() + 4) as u16; - let mut prebuf: Vec = Vec::new(); - prebuf.extend_from_slice(&u16::to_le_bytes(pkt_len)); - prebuf.extend_from_slice(&u16::to_le_bytes(0xA0)); - prebuf.append(&mut buf); - prebuf - } -} - -impl std::fmt::Debug for ShipList { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "packet ShipList {{\n").unwrap(); - write!(f, " flag: {:?}\n", 0).unwrap(); - write!(f, " ships: {:?}]\n", self.ships).unwrap(); - write!(f, "}}") } } - #[pso_packet(0x10)] pub struct MenuSelect { pub menu: u32,