wotbreplay_parser/
replay.rs

1use std::io::{Read, Seek};
2
3use zip::ZipArchive;
4
5use crate::models::battle_results::BattleResults;
6use crate::models::battle_results_dat::BattleResultsDat;
7use crate::models::data::Data;
8#[cfg(feature = "meta")]
9use crate::models::meta::Meta;
10use crate::result::Result;
11
12/// World of Tanks Blitz replay.
13///
14/// # Replay structure
15///
16/// `*.wotbreplay` is a ZIP-archive containing:
17///
18/// - `battle_results.dat`
19/// - `meta.json`
20/// - `data.wotreplay`
21pub struct Replay<R>(pub(crate) ZipArchive<R>);
22
23impl<R: Read + Seek> Replay<R> {
24    /// Open a replay from the reader which contains a `*.wotbreplay`.
25    pub fn open(reader: R) -> Result<Self> {
26        Ok(Self(ZipArchive::new(reader)?))
27    }
28
29    /// Read and parse the full battle results structure from `battle_results.dat`.
30    pub fn read_battle_results(&mut self) -> Result<BattleResults> {
31        self.read_battle_results_dat()?.try_into()
32    }
33
34    /// Read and parse the root 2-tuple from `battle_results.dat`.
35    pub fn read_battle_results_dat(&mut self) -> Result<BattleResultsDat> {
36        let pickled_battle_results = self.0.by_name("battle_results.dat")?;
37        BattleResultsDat::from_reader(pickled_battle_results)
38    }
39
40    /// Read and parse the included `meta.json`.
41    #[cfg(feature = "meta")]
42    pub fn read_meta(&mut self) -> Result<Meta> {
43        Meta::from_reader(self.0.by_name("meta.json")?)
44    }
45
46    /// Read and parse the included `data.wotreplay`.
47    pub fn read_data(&mut self) -> Result<Data> {
48        Data::from_reader(self.0.by_name("data.wotreplay")?)
49    }
50}