From 878867c49008e8541041569025e2ddf87ea17b99 Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 8 Sep 2019 23:54:15 -0700 Subject: [PATCH] character preview/guildcarddata packets --- src/login/character.rs | 69 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 9 deletions(-) diff --git a/src/login/character.rs b/src/login/character.rs index 7e1385f..19db09f 100644 --- a/src/login/character.rs +++ b/src/login/character.rs @@ -2,6 +2,7 @@ use std::net; use rand::{Rng, RngCore}; use bcrypt::{DEFAULT_COST, hash, verify}; +use crc::{crc32, Hasher32}; use libpso::packet::login::*; use libpso::{PacketParseError, PSOPacket}; @@ -33,6 +34,8 @@ pub enum RecvCharacterPacket { RequestSettings(RequestSettings), CharSelect(CharSelect), Checksum(Checksum), + GuildcardDataRequest(GuildcardDataRequest), + GuildcardDataChunkRequest(GuildcardDataChunkRequest), } impl RecvServerPacket for RecvCharacterPacket { @@ -42,6 +45,8 @@ impl RecvServerPacket for RecvCharacterPacket { 0xE0 => Ok(RecvCharacterPacket::RequestSettings(RequestSettings::from_bytes(data)?)), 0xE3 => Ok(RecvCharacterPacket::CharSelect(CharSelect::from_bytes(data)?)), 0x1E8 => Ok(RecvCharacterPacket::Checksum(Checksum::from_bytes(data)?)), + 0x3E8 => Ok(RecvCharacterPacket::GuildcardDataRequest(GuildcardDataRequest::from_bytes(data)?)), + 0x3DC => Ok(RecvCharacterPacket::GuildcardDataChunkRequest(GuildcardDataChunkRequest::from_bytes(data)?)), _ => Err(PacketParseError::WrongPacketForServerType) } } @@ -55,6 +60,9 @@ pub enum SendCharacterPacket { SendKeyAndTeamSettings(SendKeyAndTeamSettings), CharAck(CharAck), ChecksumAck(ChecksumAck), + CharacterPreview(CharacterPreview), + GuildcardDataHeader(GuildcardDataHeader), + GuildcardDataChunk(GuildcardDataChunk) } impl SendServerPacket for SendCharacterPacket { @@ -65,6 +73,9 @@ impl SendServerPacket for SendCharacterPacket { SendCharacterPacket::SendKeyAndTeamSettings(pkt) => pkt.as_bytes(), SendCharacterPacket::CharAck(pkt) => pkt.as_bytes(), SendCharacterPacket::ChecksumAck(pkt) => pkt.as_bytes(), + SendCharacterPacket::CharacterPreview(pkt) => pkt.as_bytes(), + SendCharacterPacket::GuildcardDataHeader(pkt) => pkt.as_bytes(), + SendCharacterPacket::GuildcardDataChunk(pkt) => pkt.as_bytes(), //SendLoginPacket::RedirectClient(pkt) => pkt.as_bytes(), } } @@ -74,7 +85,9 @@ pub struct CharacterServerState { //shared_state: SharedLoginState, data_access: DA, user: Option, - characters: Option<[Option; 4]> + characters: Option<[Option; 4]>, + guildcard_data_buffer: Option>, + } impl CharacterServerState { @@ -84,6 +97,7 @@ impl CharacterServerState { data_access: data_access, user: None, characters: None, + guildcard_data_buffer: None, } } @@ -117,13 +131,17 @@ impl CharacterServerState { } fn char_select(&mut self, select: &CharSelect) -> Vec { - /*if self.characters.is_none() { + if self.characters.is_none() { self.characters = Some(self.data_access.get_characters_by_user(self.user.as_ref().unwrap())); } let chars = self.characters.as_ref().unwrap(); - if let Some(ref char) = chars[select.slot as usize] { - Vec::new() + if let Some(char) = &chars[select.slot as usize] { + vec![SendCharacterPacket::CharacterPreview(CharacterPreview { + flag: 0, + slot: select.slot, + character: char.character.as_select_screen(), + })] } else { vec![SendCharacterPacket::CharAck(CharAck { @@ -131,8 +149,7 @@ impl CharacterServerState { slot: select.slot, code: 2, })] - }*/ - Vec::new() + } } fn validate_checksum(&mut self) -> Vec { @@ -141,6 +158,34 @@ impl CharacterServerState { ack: 1, })] } + + fn guildcard_data_header(&mut self) -> Vec { + let guildcard_data = self.data_access.get_guild_card_data_by_user(self.user.as_ref().unwrap()); + + let bytes = guildcard_data.guildcard.as_bytes(); + let mut crc = crc32::Digest::new(crc32::IEEE); + crc.write(&bytes[..]); + self.guildcard_data_buffer = Some(bytes.to_vec()); + + vec![SendCharacterPacket::GuildcardDataHeader(GuildcardDataHeader::new(bytes.len(), crc.sum32()))] + } + + fn guildcard_data_chunk(&mut self, chunk: u32, again: u32) -> Vec { + if again != 0 { + let start = chunk as usize * GUILD_CARD_CHUNK_SIZE; + let len = std::cmp::min(GUILD_CARD_CHUNK_SIZE, self.guildcard_data_buffer.as_ref().unwrap().len() as usize - start); + let end = start + len; + + let mut buf = [0u8; GUILD_CARD_CHUNK_SIZE as usize]; + buf[..len as usize].copy_from_slice(&self.guildcard_data_buffer.as_ref().unwrap()[start..end]); + + vec![SendCharacterPacket::GuildcardDataChunk(GuildcardDataChunk::new(chunk, buf, len))] + } else { + Vec::new() + } + + } + } impl ServerState for CharacterServerState { @@ -161,7 +206,7 @@ impl ServerState for CharacterServerState { Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, server_key)))) ] } - + fn handle(&mut self, id: ClientId, pkt: &RecvCharacterPacket) -> Box> { match pkt { RecvCharacterPacket::Login(login) => { @@ -172,9 +217,15 @@ impl ServerState for CharacterServerState { }, RecvCharacterPacket::CharSelect(sel) => { Box::new(self.char_select(sel).into_iter().map(move |pkt| (id, pkt))) - } - RecvCharacterPacket::Checksum(checksum) => { + }, + RecvCharacterPacket::Checksum(_checksum) => { Box::new(self.validate_checksum().into_iter().map(move |pkt| (id, pkt))) + }, + RecvCharacterPacket::GuildcardDataRequest(_request) => { + Box::new(self.guildcard_data_header().into_iter().map(move |pkt| (id, pkt))) + }, + RecvCharacterPacket::GuildcardDataChunkRequest(request) => { + Box::new(self.guildcard_data_chunk(request.chunk, request.again).into_iter().map(move |pkt| (id, pkt))) } } }