Break: refactor the packet payloads

This commit is contained in:
eigenein committed 2023-04-30 19:32:35 +02:00
1 parent 9d601082d6
commit e48fe1dd0d
8 files changed
+70 -70

No files matched your search

+1
View File
@@ -8,3 +8,4 @@ reorder_impl_items = true
unstable_features = true
use_field_init_shorthand = true
version = "Two"
struct_lit_width = 35
+7 -30
View File
@@ -3,8 +3,8 @@
pub mod entity_method;
pub mod packet;
pub mod payload;
pub mod replay_header;
pub mod summary;
pub mod subtype_2f;
pub mod type_0;
use std::io::Read;
@@ -12,11 +12,7 @@ use byteorder::{LittleEndian, ReadBytesExt};
use serde::de::DeserializeOwned;
use serde::Serialize;
pub use self::entity_method::*;
pub use self::summary::*;
use crate::models::data::packet::Packet;
use crate::models::data::payload::Payload;
use crate::models::data::replay_header::ReplayHeader;
use crate::result::Result;
use crate::Error;
@@ -26,9 +22,7 @@ pub struct Data {
/// For example: `9.8.5_apple`.
pub client_version: String,
pub replay_header: Option<ReplayHeader>,
pub other_packets: Vec<Packet>,
pub packets: Vec<Packet>,
}
impl Data {
@@ -46,29 +40,12 @@ impl Data {
// Some extra byte, no idea:
reader.read_u8()?;
let mut this = Self::new(client_version);
loop {
let Some(packet) = Packet::from_reader(&mut reader)? else { break };
match packet.payload {
Payload::ReplayHeader(header) => {
this.replay_header = Some(*header);
}
_ => {
this.other_packets.push(packet);
}
}
let mut packets = Vec::new();
while let Some(packet) = Packet::from_reader(&mut reader)? {
packets.push(packet);
}
Ok(this)
}
pub const fn new(client_version: String) -> Self {
Self {
client_version,
replay_header: None,
other_packets: Vec::new(),
}
Ok(Self { client_version, packets })
}
}
+12 -4
View File
@@ -1,3 +1,5 @@
use std::io::Read;
use byteorder::{LittleEndian, ReadBytesExt};
use serde::Serialize;
use serde_with::serde_as;
@@ -7,10 +9,11 @@ use crate::result::Result;
#[serde_as]
#[derive(Debug, Serialize)]
pub enum EntityMethod {
/// TODO: team composition.
Subtype2F(#[serde_as(as = "serde_with::hex::Hex")] Vec<u8>),
Subtype2F {
// prefix: [u8; 9],
},
/// Default variant when subtype is not known.
/// Default variant when subtype is not known by the parser.
Unknown {
sub_type: u32,
@@ -28,7 +31,12 @@ impl EntityMethod {
let sub_type = reader.read_u32::<LittleEndian>()?;
let this = match sub_type {
0x2F => Self::Subtype2F(payload),
0x2F => {
// let mut prefix = [0; 9];
// reader.read_exact(&mut prefix)?;
Self::Subtype2F {}
}
_ => Self::Unknown { sub_type, payload },
};
Ok(this)
+17 -12
View File
@@ -4,17 +4,25 @@ 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::models::data::entity_method::EntityMethod;
use crate::models::data::type_0::Type0;
use crate::models::data::{assert_magic, read_pickled, read_string};
use crate::result::Result;
#[serde_as]
#[derive(Debug, Serialize)]
pub enum Payload {
ReplayHeader(Box<ReplayHeader>),
/// Looks like some sort of replay summary.
Type0 {
author_nickname: String,
arena_unique_id: u64,
arena_type_id: u32,
type_0: Box<Type0>,
},
EntityMethod(EntityMethod),
/// Default payload when type is not known.
/// Default payload when type is not known by the parser.
Unknown {
type_: u32,
@@ -34,26 +42,23 @@ impl Payload {
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 = {
let type_0 = {
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)?
};
Self::ReplayHeader(Box::new(ReplayHeader {
summary,
Self::Type0 {
type_0,
author_nickname,
arena_unique_id,
arena_type_id,
}))
}
}
8 => Self::EntityMethod(EntityMethod::new(payload)?),
_ => Self::Unknown {
raw: payload,
type_,
},
_ => Self::Unknown { raw: payload, type_ },
};
Ok(this)
}
-11
View File
@@ -1,11 +0,0 @@
use serde::Serialize;
use crate::models::data::Summary;
#[derive(Debug, Serialize)]
pub struct ReplayHeader {
pub summary: Summary,
pub author_nickname: String,
pub arena_unique_id: u64,
pub arena_type_id: u32,
}
+1
View File
@@ -0,0 +1 @@
pub struct Subtype2f {}
@@ -2,8 +2,9 @@ use std::collections::HashMap;
use serde::{Deserialize, Serialize};
/// Looks like some sort of replay summary.
#[derive(Debug, Serialize, Deserialize)]
pub struct Summary {
pub struct Type0 {
#[serde(rename(deserialize = "playersBattleCategoriesIds"))]
pub players_battle_categories_ids: HashMap<u32, (u8, u32)>,
+30 -12
View File
@@ -1,6 +1,7 @@
use std::fs::File;
use anyhow::Result;
use wotbreplay_parser::models::data::payload::Payload;
use wotbreplay_parser::replay::Replay;
#[test]
@@ -11,20 +12,37 @@ fn data_ok() -> Result<()> {
let data = replay.read_data()?;
assert_eq!(data.client_version, "9.8.5_apple");
assert_eq!(data.other_packets.len(), 60138);
assert_eq!(data.packets.len(), 60139);
let header = data.replay_header.unwrap();
assert_eq!(header.author_nickname, "helaas_pindakaas");
assert_eq!(header.arena_unique_id, 16114615898101270);
assert_eq!(header.arena_type_id, 43);
assert_eq!(header.summary.battle_level, 9);
assert_eq!(header.summary.battle_category_id, 13);
assert!(header.summary.is_mouse_enabled);
assert_eq!(header.summary.matchmaker_type, 2);
assert_eq!(header.summary.camouflage_slot, 1);
assert_eq!(header.summary.average_mmr, [Some(59.29457753400008), Some(99.34182121604681)]);
let (author_nickname, arena_unique_id, arena_type_id, type_0) = data
.packets
.iter()
.filter_map(|packet| {
if let Payload::Type0 {
author_nickname,
arena_unique_id,
arena_type_id,
type_0,
} = &packet.payload
{
Some((author_nickname, arena_unique_id, arena_type_id, type_0))
} else {
None
}
})
.next()
.unwrap();
assert_eq!(author_nickname, "helaas_pindakaas");
assert_eq!(*arena_unique_id, 16114615898101270);
assert_eq!(*arena_type_id, 43);
assert_eq!(type_0.battle_level, 9);
assert_eq!(type_0.battle_category_id, 13);
assert!(type_0.is_mouse_enabled);
assert_eq!(type_0.matchmaker_type, 2);
assert_eq!(type_0.camouflage_slot, 1);
assert_eq!(type_0.average_mmr, [Some(59.29457753400008), Some(99.34182121604681)]);
assert_eq!(
header.summary.account_database_ids,
type_0.account_database_ids,
[
517033644, 537867068, 597583401, 601893865, 576810156, 531053956, 599580824, 558025185,
526927603, 594778041, 505587490, 597422303, 531923111, 587617355