2019-11-10 21:43:02 -08:00
|
|
|
use std::io::{Seek, SeekFrom};
|
|
|
|
|
|
|
|
use psopacket::pso_message;
|
|
|
|
use crate::{PSOPacketData, PacketParseError};
|
|
|
|
|
|
|
|
|
|
|
|
pub trait PSOMessage {
|
|
|
|
const CMD: u8;
|
|
|
|
fn from_bytes<R: std::io::Read + std::io::Seek>(cur: &mut R) -> Result<Self, PacketParseError> where Self: Sized;
|
|
|
|
fn as_bytes(&self) -> Vec<u8>;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[pso_message(0x40)]
|
|
|
|
pub struct PlayerWalking {
|
|
|
|
x: f32,
|
|
|
|
y: f32,
|
|
|
|
z: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-16 23:13:36 -08:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum GameMessage {
|
2019-11-10 21:43:02 -08:00
|
|
|
PlayerWalking(PlayerWalking),
|
|
|
|
}
|
|
|
|
|
2019-11-16 23:13:36 -08:00
|
|
|
impl PSOPacketData for GameMessage {
|
2019-11-10 21:43:02 -08:00
|
|
|
fn from_bytes<R: std::io::Read + std::io::Seek>(mut cur: &mut R) -> Result<Self, PacketParseError> {
|
|
|
|
let mut byte = [0u8; 1];
|
|
|
|
cur.read(&mut byte);
|
|
|
|
cur.seek(SeekFrom::Current(-1)); // Cursor doesn't implement Peek?
|
|
|
|
match byte[0] {
|
2019-11-16 23:13:36 -08:00
|
|
|
PlayerWalking::CMD => Ok(GameMessage::PlayerWalking(PlayerWalking::from_bytes(&mut cur)?)),
|
2019-11-10 21:43:02 -08:00
|
|
|
_ => Err(PacketParseError::WrongPacketCommand),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn as_bytes(&self) -> Vec<u8> {
|
2019-11-16 23:13:36 -08:00
|
|
|
match self {
|
|
|
|
GameMessage::PlayerWalking(data) => data.as_bytes(),
|
|
|
|
}
|
2019-11-10 21:43:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|