mirror of
https://github.com/eigenein/wotbreplay-parser.git
synced 2026-10-21 02:17:02 +02:00
Break: read some of data.wotreplay
This commit is contained in:
1 parent
8e96f4847b
commit
615537ce45
10 files changed
+200
-41
No files matched your search
@@ -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"
|
||||
|
||||
+16
-10
@@ -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),
|
||||
}
|
||||
@@ -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::*;
|
||||
@@ -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> {
|
||||
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")]
|
||||
|
||||
@@ -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<Self> {
|
||||
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<BattleResults> for BattleResultsDat {
|
||||
|
||||
/// Decodes the battle results from the internal buffer.
|
||||
fn try_into(self) -> Result<BattleResults> {
|
||||
BattleResults::decode(self.buffer.as_ref()).map_err(Error::DecodeFailed)
|
||||
Ok(BattleResults::decode(self.buffer.as_ref())?)
|
||||
}
|
||||
}
|
||||
@@ -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<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>,
|
||||
}
|
||||
|
||||
impl Data {
|
||||
pub fn from_reader(mut reader: impl Read) -> Result<Self> {
|
||||
Self::assert_magic(reader.read_u32::<LittleEndian>()?, 0x12345678)?;
|
||||
reader.read_u64::<LittleEndian>()?; // 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::<LittleEndian>()?;
|
||||
let arena_type_id = reader.read_u32::<LittleEndian>()?;
|
||||
let summary = {
|
||||
Self::assert_magic(reader.read_u8()?, 0xFF)?;
|
||||
let pickled_length = reader.read_u16::<LittleEndian>()?;
|
||||
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<T: Into<u32> + 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<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]
|
||||
fn read_bytes(reader: &mut impl Read) -> Result<Vec<u8>> {
|
||||
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<String> {
|
||||
Ok(String::from_utf8(read_bytes(reader)?)?)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_pickled<T: DeserializeOwned>(reader: &mut impl Read, length: usize) -> Result<T> {
|
||||
let mut buffer = Vec::new();
|
||||
buffer.resize(length, 0);
|
||||
reader.read_exact(&mut buffer)?;
|
||||
Ok(serde_pickle::from_slice(&buffer, Default::default())?)
|
||||
}
|
||||
+1
-2
@@ -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<Self> {
|
||||
serde_json::from_reader(reader).map_err(Error::JsonDecodeFailed)
|
||||
Ok(serde_json::from_reader(reader)?)
|
||||
}
|
||||
}
|
||||
+8
-14
@@ -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<R>(pub(crate) ZipArchive<R>);
|
||||
impl<R: Read + Seek> Replay<R> {
|
||||
/// Opens a replay from the reader which contains a `*.wotbreplay`.
|
||||
pub fn open(reader: R) -> Result<Self> {
|
||||
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<R: Read + Seek> Replay<R> {
|
||||
|
||||
/// Reads and parses the included `battle_results.dat`.
|
||||
pub fn read_battle_results_dat(&mut self) -> Result<BattleResultsDat> {
|
||||
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> {
|
||||
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> {
|
||||
Data::from_reader(self.0.by_name("data.wotreplay")?)
|
||||
}
|
||||
}
|
||||
+3
-10
@@ -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")?)?
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
Reference in new issue
Block a user