From 903a2b283f37f96fa1b3ae7ee011d060e1869cc9 Mon Sep 17 00:00:00 2001 From: andy Date: Thu, 26 Dec 2019 18:47:00 -0400 Subject: [PATCH 1/4] initial chat packets. wip --- src/ship/ship.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 0272bdb..795c12e 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -38,6 +38,7 @@ pub enum RecvShipPacket { CharData(CharData), Message(Message), DirectMessage(DirectMessage), + PlayerChat(PlayerChat), } impl RecvServerPacket for RecvShipPacket { @@ -48,6 +49,7 @@ impl RecvServerPacket for RecvShipPacket { 0x61 => Ok(RecvShipPacket::CharData(CharData::from_bytes(data)?)), 0x60 => Ok(RecvShipPacket::Message(Message::from_bytes(data)?)), 0x62 => Ok(RecvShipPacket::DirectMessage(DirectMessage::from_bytes(data)?)), + 0x06 => Ok(RecvShipPacket::PlayerChat(PlayerChat::from_bytes(data)?)), _ => Err(PacketParseError::WrongPacketForServerType(u16::from_le_bytes([data[2], data[3]]), data.to_vec())) } } @@ -64,6 +66,7 @@ pub enum SendShipPacket { AddToLobby(AddToLobby), Message(Message), DirectMessage(DirectMessage), + PlayerChat(PlayerChat), } impl SendServerPacket for SendShipPacket { @@ -78,6 +81,7 @@ impl SendServerPacket for SendShipPacket { SendShipPacket::AddToLobby(pkt) => pkt.as_bytes(), SendShipPacket::Message(pkt) => pkt.as_bytes(), SendShipPacket::DirectMessage(pkt) => pkt.as_bytes(), + SendShipPacket::PlayerChat(pkt) => pkt.as_bytes(), } } } @@ -311,6 +315,24 @@ impl ShipServerState { (client.client_id, SendShipPacket::DirectMessage(cmsg.clone())) }).collect::>().into_iter()) } + + fn player_chat(&mut self, id: ClientId, msg: &PlayerChat) -> Box> { + let mut cmsg = PlayerChat::new(0, 0, String::new()); + let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id)).unwrap(); + cmsg.padding = 0x00010000; + cmsg.guildcard = client.user.guildcard.unwrap(); + cmsg.message.push_str(msg.message.as_str()); + let mut mlen = (cmsg.message.len() * 2) + 0x12; + while mlen & 0x07 != 0 { + cmsg.message.push('\0'); + mlen += 1; + } + + Box::new(self.client_location.get_area_by_user(id).clients().iter() + .map(move |client| { + (client.client_id, SendShipPacket::PlayerChat(cmsg.clone())) + }).collect::>().into_iter()) + } } @@ -354,6 +376,10 @@ impl ServerState for ShipServerState { RecvShipPacket::DirectMessage(msg) => { self.direct_message(id, msg) }, + + RecvShipPacket::PlayerChat(msg) => { + self.player_chat(id, msg) + }, }) } } From bbd7aeff2adc342f63b4565ce77d5c1ce206862a Mon Sep 17 00:00:00 2001 From: andy Date: Mon, 30 Dec 2019 20:31:51 -0400 Subject: [PATCH 2/4] clean up --- src/ship/ship.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 795c12e..6e7002f 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -317,11 +317,8 @@ impl ShipServerState { } fn player_chat(&mut self, id: ClientId, msg: &PlayerChat) -> Box> { - let mut cmsg = PlayerChat::new(0, 0, String::new()); let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id)).unwrap(); - cmsg.padding = 0x00010000; - cmsg.guildcard = client.user.guildcard.unwrap(); - cmsg.message.push_str(msg.message.as_str()); + let mut cmsg = PlayerChat::new(0x00010000, client.user.guildcard.unwrap(), msg.message.clone()); let mut mlen = (cmsg.message.len() * 2) + 0x12; while mlen & 0x07 != 0 { cmsg.message.push('\0'); From e0da49d0482bede1ded76b77f1e1d3daaf5e674c Mon Sep 17 00:00:00 2001 From: andy Date: Tue, 31 Dec 2019 00:03:56 -0400 Subject: [PATCH 3/4] moved padding logic to libpso, changed return type from Box to Result --- src/ship/ship.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 6e7002f..59231e6 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -316,19 +316,14 @@ impl ShipServerState { }).collect::>().into_iter()) } - fn player_chat(&mut self, id: ClientId, msg: &PlayerChat) -> Box> { - let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id)).unwrap(); - let mut cmsg = PlayerChat::new(0x00010000, client.user.guildcard.unwrap(), msg.message.clone()); - let mut mlen = (cmsg.message.len() * 2) + 0x12; - while mlen & 0x07 != 0 { - cmsg.message.push('\0'); - mlen += 1; - } - - Box::new(self.client_location.get_area_by_user(id).clients().iter() + fn player_chat(&mut self, id: ClientId, msg: &PlayerChat) -> Result>, ShipError> { + let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?; + let cmsg = PlayerChat::new(0x00010000, client.user.guildcard.unwrap(), msg.message.clone()); + + Ok(Box::new(self.client_location.get_area_by_user(id).clients().iter() .map(move |client| { (client.client_id, SendShipPacket::PlayerChat(cmsg.clone())) - }).collect::>().into_iter()) + }).collect::>().into_iter())) } } @@ -375,7 +370,7 @@ impl ServerState for ShipServerState { }, RecvShipPacket::PlayerChat(msg) => { - self.player_chat(id, msg) + Box::new(self.player_chat(id, msg)?.into_iter()) }, }) } From de199c4d805918d1a41d13cd2d556c6570b90372 Mon Sep 17 00:00:00 2001 From: andy Date: Thu, 2 Jan 2020 23:25:33 -0400 Subject: [PATCH 4/4] remove manual padding. handled in libpso now --- src/ship/ship.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 59231e6..847ba34 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -318,7 +318,7 @@ impl ShipServerState { fn player_chat(&mut self, id: ClientId, msg: &PlayerChat) -> Result>, ShipError> { let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?; - let cmsg = PlayerChat::new(0x00010000, client.user.guildcard.unwrap(), msg.message.clone()); + let cmsg = PlayerChat::new(client.user.guildcard.unwrap(), msg.message.clone()); Ok(Box::new(self.client_location.get_area_by_user(id).clients().iter() .map(move |client| {