Browse Source

character preview/guildcarddata packets

pbs
jake 5 years ago
parent
commit
878867c490
  1. 69
      src/login/character.rs

69
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<DA: DataAccess> {
//shared_state: SharedLoginState<DA>,
data_access: DA,
user: Option<UserAccount>,
characters: Option<[Option<Character>; 4]>
characters: Option<[Option<Character>; 4]>,
guildcard_data_buffer: Option<Vec<u8>>,
}
impl<DA: DataAccess> CharacterServerState<DA> {
@ -84,6 +97,7 @@ impl<DA: DataAccess> CharacterServerState<DA> {
data_access: data_access,
user: None,
characters: None,
guildcard_data_buffer: None,
}
}
@ -117,13 +131,17 @@ impl<DA: DataAccess> CharacterServerState<DA> {
}
fn char_select(&mut self, select: &CharSelect) -> Vec<SendCharacterPacket> {
/*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<DA: DataAccess> CharacterServerState<DA> {
slot: select.slot,
code: 2,
})]
}*/
Vec::new()
}
}
fn validate_checksum(&mut self) -> Vec<SendCharacterPacket> {
@ -141,6 +158,34 @@ impl<DA: DataAccess> CharacterServerState<DA> {
ack: 1,
})]
}
fn guildcard_data_header(&mut self) -> Vec<SendCharacterPacket> {
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<SendCharacterPacket> {
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<DA: DataAccess> ServerState for CharacterServerState<DA> {
@ -161,7 +206,7 @@ impl<DA: DataAccess> ServerState for CharacterServerState<DA> {
Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, server_key))))
]
}
fn handle(&mut self, id: ClientId, pkt: &RecvCharacterPacket) -> Box<dyn Iterator<Item = (ClientId, SendCharacterPacket)>> {
match pkt {
RecvCharacterPacket::Login(login) => {
@ -172,9 +217,15 @@ impl<DA: DataAccess> ServerState for CharacterServerState<DA> {
},
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)))
}
}
}

Loading…
Cancel
Save