From 615537ce4540997cc7b5db0efd0c69e775634f61 Mon Sep 17 00:00:00 2001 From: Pavel Perestoronin Date: Sat, 29 Apr 2023 19:05:40 +0200 Subject: [PATCH] Break: read some of `data.wotreplay` --- Cargo.toml | 1 + src/error.rs | 26 +++--- src/models.rs | 2 + src/models/battle_results.rs | 5 +- src/models/battle_results_dat.rs | 4 +- src/models/data.rs | 142 +++++++++++++++++++++++++++++++ src/models/meta.rs | 3 +- src/replay.rs | 22 ++--- tests/battle_results.rs | 13 +-- tests/data.rs | 23 +++++ 10 files changed, 200 insertions(+), 41 deletions(-) create mode 100644 src/models/data.rs create mode 100644 tests/data.rs diff --git a/Cargo.toml b/Cargo.toml index d0e2967..d86061a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ default = [] meta = ["dep:serde_json"] [dependencies] +byteorder = "1.4.3" prost = "0.11.3" serde = { version = "1.0.148", features = ["derive"] } serde_bytes = "0.11.7" diff --git a/src/error.rs b/src/error.rs index 40d9f04..05a7696 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,19 +2,25 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum Error { - #[error("failed to open the replay as a ZIP-archive")] - OpenArchiveFailed(#[source] zip::result::ZipError), + #[error("ZIP error")] + ZipError(#[from] zip::result::ZipError), - #[error("failed to open the entry in the replay archive")] - OpenEntryFailed(#[source] zip::result::ZipError), + #[error("Protocol Buffers error")] + DecodeFailed(#[from] prost::DecodeError), - #[error("failed to decode the Protocol Buffers encoded data")] - DecodeFailed(#[source] prost::DecodeError), + #[error("Pickle error")] + UnpickleFailed(#[from] serde_pickle::Error), - #[error("failed to un-pickle")] - UnpickleFailed(#[source] serde_pickle::Error), + #[error("I/O error")] + IoError(#[from] std::io::Error), + + #[error("UTF-8 error")] + StringDecodeError(#[from] std::string::FromUtf8Error), #[cfg(feature = "meta")] - #[error("failed to decode JSON")] - JsonDecodeFailed(#[source] serde_json::Error), + #[error("JSON error")] + JsonDecodeFailed(#[from] serde_json::Error), + + #[error("invalid magic: {0:#X}, expected: {1:#X}")] + InvalidMagic(u32, u32), } diff --git a/src/models.rs b/src/models.rs index 6afdb02..fcd45f3 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,8 +1,10 @@ mod battle_results; mod battle_results_dat; +mod data; mod meta; pub use self::battle_results::*; pub use self::battle_results_dat::*; +pub use self::data::*; #[cfg(feature = "meta")] pub use self::meta::*; diff --git a/src/models/battle_results.rs b/src/models/battle_results.rs index e04c9d5..ccab513 100644 --- a/src/models/battle_results.rs +++ b/src/models/battle_results.rs @@ -2,7 +2,6 @@ use prost::bytes::Buf; use prost::Message; use serde::Serialize; -use crate::error::Error; use crate::result::Result; impl BattleResults { @@ -10,7 +9,7 @@ impl BattleResults { /// /// Note, that this does NOT parse `battle_results.dat` itself, but the un-pickled tuple element. pub fn from_buffer(buffer: impl Buf) -> Result { - Self::decode(buffer).map_err(Error::DecodeFailed) + Ok(Self::decode(buffer)?) } } @@ -24,7 +23,7 @@ pub struct BattleResults { pub timestamp: i64, #[prost(enumeration = "TeamNumber", tag = "3")] - pub winning_team: i32, + pub winner_team_number: i32, /// Replay's author results. #[prost(message, required, tag = "8")] diff --git a/src/models/battle_results_dat.rs b/src/models/battle_results_dat.rs index 05ba0ea..4801759 100644 --- a/src/models/battle_results_dat.rs +++ b/src/models/battle_results_dat.rs @@ -33,7 +33,7 @@ impl BattleResultsDat { /// It can be used either on the `battle_results.dat` for a replay archive, /// or directly on `**/DAVAProject/battle_results/*/*_full.dat`. pub fn from_reader(reader: impl Read) -> Result { - serde_pickle::from_reader(reader, Default::default()).map_err(Error::UnpickleFailed) + Ok(serde_pickle::from_reader(reader, Default::default())?) } } @@ -42,6 +42,6 @@ impl TryInto for BattleResultsDat { /// Decodes the battle results from the internal buffer. fn try_into(self) -> Result { - BattleResults::decode(self.buffer.as_ref()).map_err(Error::DecodeFailed) + Ok(BattleResults::decode(self.buffer.as_ref())?) } } diff --git a/src/models/data.rs b/src/models/data.rs new file mode 100644 index 0000000..62d5f84 --- /dev/null +++ b/src/models/data.rs @@ -0,0 +1,142 @@ +//! `data.wotreplay` models. + +use std::collections::HashMap; +use std::io::Read; + +use byteorder::{LittleEndian, ReadBytesExt}; +use serde::de::DeserializeOwned; +use serde::{Deserialize, Serialize}; + +use crate::result::Result; +use crate::Error; + +/// `data.wotreplay` root structure. +#[derive(Debug, Serialize)] +pub struct Data { + 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 struct Summary { + #[serde(rename = "playersBattleCategoriesIds")] + pub players_battle_categories_ids: HashMap, + + #[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, +} + +impl Data { + pub fn from_reader(mut reader: impl Read) -> Result { + Self::assert_magic(reader.read_u32::()?, 0x12345678)?; + reader.read_u64::()?; // TODO + read_bytes(&mut reader)?; // TODO + let client_version = read_string(&mut reader)?; + reader.read_exact(&mut [0; 49])?; // TODO + let author_nickname = read_string(&mut reader)?; + let arena_unique_id = reader.read_u64::()?; + let arena_type_id = reader.read_u32::()?; + let summary = { + Self::assert_magic(reader.read_u8()?, 0xFF)?; + let pickled_length = reader.read_u16::()?; + Self::assert_magic(reader.read_u8()?, 0x00)?; + read_pickled(&mut reader, pickled_length as usize)? + }; + + let this = Self { + client_version, + author_nickname, + arena_unique_id, + arena_type_id, + summary, + }; + Ok(this) + } + + #[inline] + fn assert_magic + PartialEq>(actual: T, expected: T) -> Result { + if actual == expected { + Ok(()) + } else { + 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 { + let length = reader.read_u32::()?; + let type_ = reader.read_u32::()?; + let clock = reader.read_f32::()?; + 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 { + 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] +fn read_bytes(reader: &mut impl Read) -> Result> { + let length = reader.read_u8()? as usize; + let mut buffer = Vec::new(); + buffer.resize(length, 0); + reader.read_exact(&mut buffer)?; + Ok(buffer) +} + +#[inline] +fn read_string(reader: &mut impl Read) -> Result { + Ok(String::from_utf8(read_bytes(reader)?)?) +} + +#[inline] +fn read_pickled(reader: &mut impl Read, length: usize) -> Result { + let mut buffer = Vec::new(); + buffer.resize(length, 0); + reader.read_exact(&mut buffer)?; + Ok(serde_pickle::from_slice(&buffer, Default::default())?) +} diff --git a/src/models/meta.rs b/src/models/meta.rs index dbca09b..9d33c6a 100644 --- a/src/models/meta.rs +++ b/src/models/meta.rs @@ -4,7 +4,6 @@ use std::io::Read; use serde::Deserialize; -use crate::error::Error; use crate::result::Result; /// The model for `meta.json`. @@ -25,6 +24,6 @@ pub struct Meta { impl Meta { pub fn from_reader(reader: impl Read) -> Result { - serde_json::from_reader(reader).map_err(Error::JsonDecodeFailed) + Ok(serde_json::from_reader(reader)?) } } diff --git a/src/replay.rs b/src/replay.rs index c78f83e..5b0cc0a 100644 --- a/src/replay.rs +++ b/src/replay.rs @@ -2,10 +2,9 @@ use std::io::{Read, Seek}; use zip::ZipArchive; -use crate::error::Error; #[cfg(feature = "meta")] use crate::models::Meta; -use crate::models::{BattleResults, BattleResultsDat}; +use crate::models::{BattleResults, BattleResultsDat, Data}; use crate::result::Result; /// Opened (but not yet parsed) replay. @@ -14,9 +13,7 @@ pub struct Replay(pub(crate) ZipArchive); impl Replay { /// Opens a replay from the reader which contains a `*.wotbreplay`. pub fn open(reader: R) -> Result { - ZipArchive::new(reader) - .map_err(Error::OpenArchiveFailed) - .map(Self) + Ok(Self(ZipArchive::new(reader)?)) } /// Reads and parses the battle results from the replay. @@ -26,19 +23,16 @@ impl Replay { /// Reads and parses the included `battle_results.dat`. pub fn read_battle_results_dat(&mut self) -> Result { - let pickled_battle_results = self - .0 - .by_name("battle_results.dat") - .map_err(Error::OpenEntryFailed)?; + let pickled_battle_results = self.0.by_name("battle_results.dat")?; BattleResultsDat::from_reader(pickled_battle_results) } #[cfg(feature = "meta")] pub fn read_meta(&mut self) -> Result { - Meta::from_reader( - self.0 - .by_name("meta.json") - .map_err(Error::OpenEntryFailed)?, - ) + Meta::from_reader(self.0.by_name("meta.json")?) + } + + pub fn read_data(&mut self) -> Result { + Data::from_reader(self.0.by_name("data.wotreplay")?) } } diff --git a/tests/battle_results.rs b/tests/battle_results.rs index 671af31..067e366 100644 --- a/tests/battle_results.rs +++ b/tests/battle_results.rs @@ -10,6 +10,9 @@ fn player_results_ok() -> Result<()> { assert_eq!(battle_results_dat.arena_unique_id, 1661909200500084); let battle_results: BattleResults = battle_results_dat.try_into()?; + assert_eq!(battle_results.winner_team_number, 2); + assert_eq!(battle_results.author.team_number, 2); + let snak_the_ripper_info = &battle_results.player_results[3].info; assert_eq!(snak_the_ripper_info.base_xp, 929); assert_eq!(snak_the_ripper_info.damage_dealt, 2584); @@ -108,16 +111,6 @@ fn victory_points_ok() -> Result<()> { Ok(()) } -#[test] -fn winning_team_ok() -> Result<()> { - let battle_results = Replay::open(File::open("tests/replays/winning_team.wotbreplay")?)? - .read_battle_results()?; - - assert_eq!(battle_results.winning_team(), TeamNumber::One); - - Ok(()) -} - #[test] fn author_results_ok() -> Result<()> { let battle_results = Replay::open(File::open("tests/replays/author_results.wotbreplay")?)? diff --git a/tests/data.rs b/tests/data.rs new file mode 100644 index 0000000..88280de --- /dev/null +++ b/tests/data.rs @@ -0,0 +1,23 @@ +use std::fs::File; + +use anyhow::Result; +use wotbreplay_parser::prelude::*; + +#[test] +fn data_ok() -> Result<()> { + let mut replay = Replay::open(File::open("tests/replays/player_results.wotbreplay")?)?; + let data = replay.read_data()?; + + assert_eq!(data.client_version, "9.4.0_apple"); + assert_eq!(data.author_nickname, "zeekrab"); + assert_eq!(data.arena_unique_id, 1661909200500084); + assert_eq!(data.arena_type_id, 65567); + assert_eq!(data.summary.battle_level, 8); + assert_eq!(data.summary.battle_category_id, 13); + assert!(data.summary.is_mouse_enabled); + assert_eq!(data.summary.matchmaker_type, 2); + assert_eq!(data.summary.camouflage_slot, 1); + assert_eq!(data.summary.average_mmr, [-25.396859946846963, 77.58806455880404]); + + Ok(()) +}