Files
wotbreplay-parser/src/models/data/payload.rs
T

60 lines
1.8 KiB
Rust
Raw Normal View History

2023-04-29 22:17:12 +02:00
use std::io::Read;
use byteorder::{LittleEndian, ReadBytesExt};
use serde::Serialize;
use serde_with::serde_as;
use crate::models::data::replay_header::ReplayHeader;
use crate::models::data::{assert_magic, read_pickled, read_string, EntityMethod};
use crate::result::Result;
#[serde_as]
#[derive(Debug, Serialize)]
pub enum Payload {
2023-04-29 22:52:24 +02:00
ReplayHeader(Box<ReplayHeader>),
2023-04-29 22:17:12 +02:00
EntityMethod(EntityMethod),
/// Default payload when type is not known.
Unknown {
type_: u32,
/// Whole packet payload.
#[serde_as(as = "serde_with::hex::Hex")]
raw: Vec<u8>,
},
2023-04-29 22:17:12 +02:00
}
impl Payload {
pub fn new(type_: u32, payload: Vec<u8>) -> Result<Self> {
let this = match type_ {
0 => {
let mut reader = payload.as_slice();
reader.read_exact(&mut [0; 10])?;
let author_nickname = read_string(&mut reader)?;
let arena_unique_id = reader.read_u64::<LittleEndian>()?;
let arena_type_id = reader.read_u32::<LittleEndian>()?;
let summary = {
assert_magic(reader.read_u8()?, 0xFF)?;
let pickled_length = reader.read_u16::<LittleEndian>()?;
assert_magic(reader.read_u8()?, 0x00)?;
read_pickled(&mut reader, pickled_length as usize)?
};
2023-04-29 22:52:24 +02:00
Self::ReplayHeader(Box::new(ReplayHeader {
2023-04-29 22:17:12 +02:00
summary,
author_nickname,
arena_unique_id,
arena_type_id,
2023-04-29 22:52:24 +02:00
}))
2023-04-29 22:17:12 +02:00
}
8 => Self::EntityMethod(EntityMethod::new(payload)?),
_ => Self::Unknown {
raw: payload,
type_,
},
2023-04-29 22:17:12 +02:00
};
Ok(this)
}
}