mirror of
https://github.com/eigenein/wotbreplay-parser.git
synced 2026-10-21 10:27:01 +02:00
Break: read replay packets
This commit is contained in:
1 parent
615537ce45
commit
fb5f52038d
16 files changed
+232
-119
No files matched your search
@@ -15,12 +15,14 @@ default = []
|
|||||||
meta = ["dep:serde_json"]
|
meta = ["dep:serde_json"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
bytes = "1.4.0"
|
||||||
byteorder = "1.4.3"
|
byteorder = "1.4.3"
|
||||||
prost = "0.11.3"
|
prost = "0.11.3"
|
||||||
serde = { version = "1.0.148", features = ["derive"] }
|
serde = { version = "1.0.148", features = ["derive"] }
|
||||||
serde_bytes = "0.11.7"
|
serde_bytes = "0.11.7"
|
||||||
serde-pickle = "1.1.1"
|
serde-pickle = "1.1.1"
|
||||||
serde_json = { version = "1.0.96", optional = true }
|
serde_json = { version = "1.0.96", optional = true }
|
||||||
|
serde_with = { version = "2.3.3", features = ["hex"] }
|
||||||
thiserror = "1.0.37"
|
thiserror = "1.0.37"
|
||||||
zip = { version = "0.6.3", default-features = false, features = ["deflate"] }
|
zip = { version = "0.6.3", default-features = false, features = ["deflate"] }
|
||||||
|
|
||||||
|
|||||||
@@ -23,4 +23,7 @@ pub enum Error {
|
|||||||
|
|
||||||
#[error("invalid magic: {0:#X}, expected: {1:#X}")]
|
#[error("invalid magic: {0:#X}, expected: {1:#X}")]
|
||||||
InvalidMagic(u32, u32),
|
InvalidMagic(u32, u32),
|
||||||
|
|
||||||
|
#[error("failed to read a packet length, got {0} bytes")]
|
||||||
|
PacketLengthError(usize),
|
||||||
}
|
}
|
||||||
+2
-3
@@ -12,9 +12,8 @@
|
|||||||
)]
|
)]
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
mod models;
|
pub mod models;
|
||||||
pub mod prelude;
|
pub mod replay;
|
||||||
mod replay;
|
|
||||||
mod result;
|
mod result;
|
||||||
|
|
||||||
pub use self::error::Error;
|
pub use self::error::Error;
|
||||||
|
|||||||
+4
-10
@@ -1,10 +1,4 @@
|
|||||||
mod battle_results;
|
pub mod battle_results;
|
||||||
mod battle_results_dat;
|
pub mod battle_results_dat;
|
||||||
mod data;
|
pub mod data;
|
||||||
mod meta;
|
pub mod meta;
|
||||||
|
|
||||||
pub use self::battle_results::*;
|
|
||||||
pub use self::battle_results_dat::*;
|
|
||||||
pub use self::data::*;
|
|
||||||
#[cfg(feature = "meta")]
|
|
||||||
pub use self::meta::*;
|
|
||||||
@@ -5,7 +5,7 @@ use serde::Deserialize;
|
|||||||
use serde_bytes::ByteBuf;
|
use serde_bytes::ByteBuf;
|
||||||
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::models::BattleResults;
|
use crate::models::battle_results::BattleResults;
|
||||||
use crate::result::Result;
|
use crate::result::Result;
|
||||||
|
|
||||||
/// Represents un-pickled `battle_results.dat`.
|
/// Represents un-pickled `battle_results.dat`.
|
||||||
|
|||||||
+51
-89
@@ -1,122 +1,84 @@
|
|||||||
//! `data.wotreplay` models.
|
//! `data.wotreplay` models.
|
||||||
|
|
||||||
use std::collections::HashMap;
|
pub mod entity_method;
|
||||||
|
pub mod packet;
|
||||||
|
pub mod payload;
|
||||||
|
pub mod replay_header;
|
||||||
|
pub mod summary;
|
||||||
|
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
use byteorder::{LittleEndian, ReadBytesExt};
|
use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use serde::{Deserialize, Serialize};
|
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::result::Result;
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
|
|
||||||
/// `data.wotreplay` root structure.
|
/// `data.wotreplay` root structure.
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct Data {
|
pub struct Data {
|
||||||
|
/// For example: `9.8.5_apple`.
|
||||||
pub client_version: String,
|
pub client_version: String,
|
||||||
pub author_nickname: String,
|
|
||||||
pub arena_unique_id: u64,
|
|
||||||
pub arena_type_id: u32,
|
|
||||||
pub summary: Summary,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
pub replay_header: Option<ReplayHeader>,
|
||||||
pub struct Summary {
|
|
||||||
#[serde(rename = "playersBattleCategoriesIds")]
|
|
||||||
pub players_battle_categories_ids: HashMap<u32, (u8, u32)>,
|
|
||||||
|
|
||||||
#[serde(rename = "battleLevel")]
|
pub other_packets: Vec<Packet>,
|
||||||
pub battle_level: u8,
|
|
||||||
|
|
||||||
#[serde(rename = "battleCategoryId")]
|
|
||||||
pub battle_category_id: u8,
|
|
||||||
|
|
||||||
#[serde(rename = "mouseEnabled")]
|
|
||||||
pub is_mouse_enabled: bool,
|
|
||||||
|
|
||||||
#[serde(rename = "mmType")]
|
|
||||||
pub matchmaker_type: u8,
|
|
||||||
|
|
||||||
#[serde(rename = "camouflageSlot")]
|
|
||||||
pub camouflage_slot: u8,
|
|
||||||
|
|
||||||
#[serde(rename = "avgMmr")]
|
|
||||||
pub average_mmr: Vec<f64>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Data {
|
impl Data {
|
||||||
pub fn from_reader(mut reader: impl Read) -> Result<Self> {
|
pub fn from_reader(mut reader: impl Read) -> Result<Self> {
|
||||||
Self::assert_magic(reader.read_u32::<LittleEndian>()?, 0x12345678)?;
|
assert_magic(reader.read_u32::<LittleEndian>()?, 0x12345678)?;
|
||||||
reader.read_u64::<LittleEndian>()?; // TODO
|
|
||||||
read_bytes(&mut reader)?; // TODO
|
// No idea what it is:
|
||||||
|
reader.read_u64::<LittleEndian>()?;
|
||||||
|
|
||||||
|
// Some sort of client hash, e.g.: `6CF2A9EFA5C52D6F6CE43A6D4A699C05`:
|
||||||
|
read_bytes(&mut reader)?;
|
||||||
|
|
||||||
let client_version = read_string(&mut reader)?;
|
let client_version = read_string(&mut reader)?;
|
||||||
reader.read_exact(&mut [0; 49])?; // TODO
|
|
||||||
let author_nickname = read_string(&mut reader)?;
|
// Some extra byte, no idea:
|
||||||
let arena_unique_id = reader.read_u64::<LittleEndian>()?;
|
reader.read_u8()?;
|
||||||
let arena_type_id = reader.read_u32::<LittleEndian>()?;
|
|
||||||
let summary = {
|
let mut this = Self::new(client_version);
|
||||||
Self::assert_magic(reader.read_u8()?, 0xFF)?;
|
|
||||||
let pickled_length = reader.read_u16::<LittleEndian>()?;
|
loop {
|
||||||
Self::assert_magic(reader.read_u8()?, 0x00)?;
|
let Some(packet) = Packet::from_reader(&mut reader)? else { break };
|
||||||
read_pickled(&mut reader, pickled_length as usize)?
|
match packet.payload {
|
||||||
};
|
Payload::ReplayHeader(header) => {
|
||||||
|
this.replay_header = Some(header);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
this.other_packets.push(packet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let this = Self {
|
|
||||||
client_version,
|
|
||||||
author_nickname,
|
|
||||||
arena_unique_id,
|
|
||||||
arena_type_id,
|
|
||||||
summary,
|
|
||||||
};
|
|
||||||
Ok(this)
|
Ok(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
pub const fn new(client_version: String) -> Self {
|
||||||
fn assert_magic<T: Into<u32> + PartialEq>(actual: T, expected: T) -> Result {
|
Self {
|
||||||
|
client_version,
|
||||||
|
replay_header: None,
|
||||||
|
other_packets: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn assert_magic<T: Into<u32> + PartialEq>(actual: T, expected: T) -> Result {
|
||||||
if actual == expected {
|
if actual == expected {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(Error::InvalidMagic(actual.into(), expected.into()))
|
Err(Error::InvalidMagic(actual.into(), expected.into()))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct Packet {
|
|
||||||
clock: f32,
|
|
||||||
payload: Payload,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Packet {
|
|
||||||
fn from_reader(reader: &mut impl Read) -> Result<Self> {
|
|
||||||
let length = reader.read_u32::<LittleEndian>()?;
|
|
||||||
let type_ = reader.read_u32::<LittleEndian>()?;
|
|
||||||
let clock = reader.read_f32::<LittleEndian>()?;
|
|
||||||
let payload = Payload::from_reader(reader, type_, length)?;
|
|
||||||
let this = Self { clock, payload };
|
|
||||||
Ok(this)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum Payload {
|
|
||||||
EntityProperty,
|
|
||||||
EntityMethod,
|
|
||||||
Other,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Payload {
|
|
||||||
fn from_reader(reader: &mut impl Read, type_: u32, length: u32) -> Result<Self> {
|
|
||||||
let this = match type_ {
|
|
||||||
_ => {
|
|
||||||
let mut buffer = Vec::new();
|
|
||||||
buffer.resize(length as usize, 0);
|
|
||||||
reader.read_exact(&mut buffer)?;
|
|
||||||
Self::Other
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(this)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
use crate::result::Result;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub enum EntityMethod {
|
||||||
|
/// TODO: team composition.
|
||||||
|
Subtype2F,
|
||||||
|
|
||||||
|
/// Default variant when subtype is not known.
|
||||||
|
Unknown { sub_type: u32, payload: Vec<u8> },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EntityMethod {
|
||||||
|
pub fn new(payload: Vec<u8>) -> Result<Self> {
|
||||||
|
let mut reader = payload.as_slice();
|
||||||
|
|
||||||
|
reader.read_u32::<LittleEndian>()?;
|
||||||
|
let sub_type = reader.read_u32::<LittleEndian>()?;
|
||||||
|
|
||||||
|
let this = match sub_type {
|
||||||
|
0x2F => Self::Subtype2F,
|
||||||
|
_ => Self::Unknown { sub_type, payload },
|
||||||
|
};
|
||||||
|
Ok(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
use crate::models::data::payload::Payload;
|
||||||
|
use crate::result::Result;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub struct Packet {
|
||||||
|
pub clock: f32,
|
||||||
|
pub payload: Payload,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Packet {
|
||||||
|
pub fn from_reader(reader: &mut impl Read) -> Result<Option<Self>> {
|
||||||
|
let Ok(length) = reader.read_u32::<LittleEndian>() else { return Ok(None) };
|
||||||
|
let type_ = reader.read_u32::<LittleEndian>()?;
|
||||||
|
let clock = reader.read_f32::<LittleEndian>()?;
|
||||||
|
let payload = Self::read_payload(reader, length as usize)?;
|
||||||
|
let payload = Payload::new(type_, payload)?;
|
||||||
|
let this = Self { clock, payload };
|
||||||
|
Ok(Some(this))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_payload(reader: &mut impl Read, length: usize) -> Result<Vec<u8>> {
|
||||||
|
let mut buffer = Vec::new();
|
||||||
|
buffer.resize(length, 0);
|
||||||
|
reader.read_exact(&mut buffer)?;
|
||||||
|
Ok(buffer)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
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 {
|
||||||
|
ReplayHeader(ReplayHeader),
|
||||||
|
EntityMethod(EntityMethod),
|
||||||
|
|
||||||
|
/// Default payload when type is not known.
|
||||||
|
Unknown(#[serde_as(as = "serde_with::hex::Hex")] Vec<u8>),
|
||||||
|
}
|
||||||
|
|
||||||
|
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)?
|
||||||
|
};
|
||||||
|
Self::ReplayHeader(ReplayHeader {
|
||||||
|
summary,
|
||||||
|
author_nickname,
|
||||||
|
arena_unique_id,
|
||||||
|
arena_type_id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
8 => Self::EntityMethod(EntityMethod::new(payload)?),
|
||||||
|
|
||||||
|
_ => Self::Unknown(payload),
|
||||||
|
};
|
||||||
|
Ok(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
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,
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Summary {
|
||||||
|
#[serde(rename = "playersBattleCategoriesIds")]
|
||||||
|
pub players_battle_categories_ids: HashMap<u32, (u8, u32)>,
|
||||||
|
|
||||||
|
#[serde(rename = "battleLevel")]
|
||||||
|
pub battle_level: u8,
|
||||||
|
|
||||||
|
#[serde(rename = "battleCategoryId")]
|
||||||
|
pub battle_category_id: u8,
|
||||||
|
|
||||||
|
#[serde(rename = "mouseEnabled")]
|
||||||
|
pub is_mouse_enabled: bool,
|
||||||
|
|
||||||
|
#[serde(rename = "mmType")]
|
||||||
|
pub matchmaker_type: u8,
|
||||||
|
|
||||||
|
#[serde(rename = "camouflageSlot")]
|
||||||
|
pub camouflage_slot: u8,
|
||||||
|
|
||||||
|
#[serde(rename = "avgMmr")]
|
||||||
|
pub average_mmr: Vec<f64>,
|
||||||
|
}
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
pub use crate::models::*;
|
|
||||||
pub use crate::replay::Replay;
|
|
||||||
+4
-2
@@ -2,9 +2,11 @@ use std::io::{Read, Seek};
|
|||||||
|
|
||||||
use zip::ZipArchive;
|
use zip::ZipArchive;
|
||||||
|
|
||||||
|
use crate::models::battle_results::BattleResults;
|
||||||
|
use crate::models::battle_results_dat::BattleResultsDat;
|
||||||
|
use crate::models::data::Data;
|
||||||
#[cfg(feature = "meta")]
|
#[cfg(feature = "meta")]
|
||||||
use crate::models::Meta;
|
use crate::models::meta::Meta;
|
||||||
use crate::models::{BattleResults, BattleResultsDat, Data};
|
|
||||||
use crate::result::Result;
|
use crate::result::Result;
|
||||||
|
|
||||||
/// Opened (but not yet parsed) replay.
|
/// Opened (but not yet parsed) replay.
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use wotbreplay_parser::prelude::*;
|
use wotbreplay_parser::models::battle_results::BattleResults;
|
||||||
|
use wotbreplay_parser::replay::Replay;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn player_results_ok() -> Result<()> {
|
fn player_results_ok() -> Result<()> {
|
||||||
|
|||||||
+13
-10
@@ -1,7 +1,7 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use wotbreplay_parser::prelude::*;
|
use wotbreplay_parser::replay::Replay;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn data_ok() -> Result<()> {
|
fn data_ok() -> Result<()> {
|
||||||
@@ -9,15 +9,18 @@ fn data_ok() -> Result<()> {
|
|||||||
let data = replay.read_data()?;
|
let data = replay.read_data()?;
|
||||||
|
|
||||||
assert_eq!(data.client_version, "9.4.0_apple");
|
assert_eq!(data.client_version, "9.4.0_apple");
|
||||||
assert_eq!(data.author_nickname, "zeekrab");
|
assert_eq!(data.other_packets.len(), 44720);
|
||||||
assert_eq!(data.arena_unique_id, 1661909200500084);
|
|
||||||
assert_eq!(data.arena_type_id, 65567);
|
let header = data.replay_header.unwrap();
|
||||||
assert_eq!(data.summary.battle_level, 8);
|
assert_eq!(header.author_nickname, "zeekrab");
|
||||||
assert_eq!(data.summary.battle_category_id, 13);
|
assert_eq!(header.arena_unique_id, 1661909200500084);
|
||||||
assert!(data.summary.is_mouse_enabled);
|
assert_eq!(header.arena_type_id, 65567);
|
||||||
assert_eq!(data.summary.matchmaker_type, 2);
|
assert_eq!(header.summary.battle_level, 8);
|
||||||
assert_eq!(data.summary.camouflage_slot, 1);
|
assert_eq!(header.summary.battle_category_id, 13);
|
||||||
assert_eq!(data.summary.average_mmr, [-25.396859946846963, 77.58806455880404]);
|
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, [-25.396859946846963, 77.58806455880404]);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use wotbreplay_parser::prelude::*;
|
use wotbreplay_parser::replay::Replay;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn meta_ok() -> Result<()> {
|
fn meta_ok() -> Result<()> {
|
||||||
|
|||||||
Reference in new issue
Block a user