mirror of
https://github.com/eigenein/wotbreplay-parser.git
synced 2026-10-21 02:17:02 +02:00
Break: decode the players packet
This commit is contained in:
1 parent
e48fe1dd0d
commit
72165ae52c
5 files changed
+114
-16
No files matched your search
+19
-3
@@ -33,7 +33,7 @@ impl Data {
|
|||||||
reader.read_u64::<LittleEndian>()?;
|
reader.read_u64::<LittleEndian>()?;
|
||||||
|
|
||||||
// Some sort of client hash, e.g.: `6CF2A9EFA5C52D6F6CE43A6D4A699C05`:
|
// Some sort of client hash, e.g.: `6CF2A9EFA5C52D6F6CE43A6D4A699C05`:
|
||||||
read_bytes(&mut reader)?;
|
read_length_delimited(&mut reader)?;
|
||||||
|
|
||||||
let client_version = read_string(&mut reader)?;
|
let client_version = read_string(&mut reader)?;
|
||||||
|
|
||||||
@@ -59,7 +59,7 @@ fn assert_magic<T: Into<u32> + PartialEq>(actual: T, expected: T) -> Result {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn read_bytes(reader: &mut impl Read) -> Result<Vec<u8>> {
|
fn read_length_delimited(reader: &mut impl Read) -> Result<Vec<u8>> {
|
||||||
let length = reader.read_u8()? as usize;
|
let length = reader.read_u8()? as usize;
|
||||||
let mut buffer = Vec::new();
|
let mut buffer = Vec::new();
|
||||||
buffer.resize(length, 0);
|
buffer.resize(length, 0);
|
||||||
@@ -69,7 +69,7 @@ fn read_bytes(reader: &mut impl Read) -> Result<Vec<u8>> {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn read_string(reader: &mut impl Read) -> Result<String> {
|
fn read_string(reader: &mut impl Read) -> Result<String> {
|
||||||
Ok(String::from_utf8(read_bytes(reader)?)?)
|
Ok(String::from_utf8(read_length_delimited(reader)?)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -79,3 +79,19 @@ fn read_pickled<T: DeserializeOwned>(reader: &mut impl Read, length: usize) -> R
|
|||||||
reader.read_exact(&mut buffer)?;
|
reader.read_exact(&mut buffer)?;
|
||||||
Ok(serde_pickle::from_slice(&buffer, Default::default())?)
|
Ok(serde_pickle::from_slice(&buffer, Default::default())?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Read 2-byte length surrounded by the magical `0xFF` and `0x00`,
|
||||||
|
/// unless it's a simple 1-byte length.
|
||||||
|
///
|
||||||
|
/// P.S. Okay, Wargaming, I've no idea what you smoke, but I want some!
|
||||||
|
#[inline]
|
||||||
|
fn read_quirky_length(reader: &mut impl Read) -> Result<usize> {
|
||||||
|
match reader.read_u8()? {
|
||||||
|
0xFF => {
|
||||||
|
let length = reader.read_u16::<LittleEndian>()?;
|
||||||
|
assert_magic(reader.read_u8()?, 0x00)?;
|
||||||
|
Ok(length as usize)
|
||||||
|
}
|
||||||
|
length => Ok(length as usize),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,16 +1,23 @@
|
|||||||
use std::io::Read;
|
|
||||||
|
|
||||||
use byteorder::{LittleEndian, ReadBytesExt};
|
use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
|
use prost::encoding::decode_varint;
|
||||||
|
use prost::Message;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_with::serde_as;
|
use serde_with::serde_as;
|
||||||
|
|
||||||
|
use crate::models::data::read_quirky_length;
|
||||||
|
use crate::models::data::subtype_2f::Subtype2F;
|
||||||
use crate::result::Result;
|
use crate::result::Result;
|
||||||
|
|
||||||
#[serde_as]
|
#[serde_as]
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub enum EntityMethod {
|
pub enum EntityMethod {
|
||||||
Subtype2F {
|
Subtype2F {
|
||||||
// prefix: [u8; 9],
|
field_number: u64,
|
||||||
|
sub_type_2f: Subtype2F,
|
||||||
|
|
||||||
|
/// Original payload for investigation.
|
||||||
|
#[serde_as(as = "serde_with::hex::Hex")]
|
||||||
|
payload: Vec<u8>,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// Default variant when subtype is not known by the parser.
|
/// Default variant when subtype is not known by the parser.
|
||||||
@@ -32,9 +39,15 @@ impl EntityMethod {
|
|||||||
|
|
||||||
let this = match sub_type {
|
let this = match sub_type {
|
||||||
0x2F => {
|
0x2F => {
|
||||||
// let mut prefix = [0; 9];
|
let _inner_length = reader.read_u32::<LittleEndian>()?;
|
||||||
// reader.read_exact(&mut prefix)?;
|
|
||||||
Self::Subtype2F {}
|
let field_number = decode_varint(&mut reader)?;
|
||||||
|
let message_length = read_quirky_length(&mut reader)?;
|
||||||
|
Self::Subtype2F {
|
||||||
|
field_number,
|
||||||
|
sub_type_2f: Subtype2F::decode(&reader[..message_length])?,
|
||||||
|
payload,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => Self::Unknown { sub_type, payload },
|
_ => Self::Unknown { sub_type, payload },
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use serde_with::serde_as;
|
|||||||
|
|
||||||
use crate::models::data::entity_method::EntityMethod;
|
use crate::models::data::entity_method::EntityMethod;
|
||||||
use crate::models::data::type_0::Type0;
|
use crate::models::data::type_0::Type0;
|
||||||
use crate::models::data::{assert_magic, read_pickled, read_string};
|
use crate::models::data::{read_pickled, read_quirky_length, read_string};
|
||||||
use crate::result::Result;
|
use crate::result::Result;
|
||||||
|
|
||||||
#[serde_as]
|
#[serde_as]
|
||||||
@@ -43,10 +43,8 @@ impl Payload {
|
|||||||
let arena_unique_id = reader.read_u64::<LittleEndian>()?;
|
let arena_unique_id = reader.read_u64::<LittleEndian>()?;
|
||||||
let arena_type_id = reader.read_u32::<LittleEndian>()?;
|
let arena_type_id = reader.read_u32::<LittleEndian>()?;
|
||||||
let type_0 = {
|
let type_0 = {
|
||||||
assert_magic(reader.read_u8()?, 0xFF)?;
|
let pickled_length = read_quirky_length(&mut reader)?;
|
||||||
let pickled_length = reader.read_u16::<LittleEndian>()?;
|
read_pickled(&mut reader, pickled_length)?
|
||||||
assert_magic(reader.read_u8()?, 0x00)?;
|
|
||||||
read_pickled(&mut reader, pickled_length as usize)?
|
|
||||||
};
|
};
|
||||||
Self::Type0 {
|
Self::Type0 {
|
||||||
type_0,
|
type_0,
|
||||||
|
|||||||
@@ -1 +1,32 @@
|
|||||||
pub struct Subtype2f {}
|
use prost::Message;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
#[derive(Message, Serialize)]
|
||||||
|
pub struct Subtype2F {
|
||||||
|
#[prost(message, tag = "1")]
|
||||||
|
pub players: Option<Players>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Message, Serialize)]
|
||||||
|
pub struct Players {
|
||||||
|
#[prost(message, repeated, tag = "1")]
|
||||||
|
pub players: Vec<Player>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Message, Serialize)]
|
||||||
|
pub struct Player {
|
||||||
|
#[prost(string, required, tag = "3")]
|
||||||
|
pub nickname: String,
|
||||||
|
|
||||||
|
#[prost(uint32, required, tag = "4")]
|
||||||
|
pub team_number: u32,
|
||||||
|
|
||||||
|
#[prost(uint32, required, tag = "7")]
|
||||||
|
pub account_id: u32,
|
||||||
|
|
||||||
|
#[prost(string, optional, tag = "8")]
|
||||||
|
pub clan_tag: Option<String>,
|
||||||
|
|
||||||
|
#[prost(uint32, optional, tag = "11")]
|
||||||
|
pub platoon_number: Option<u32>,
|
||||||
|
}
|
||||||
+41
-1
@@ -1,11 +1,12 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use wotbreplay_parser::models::data::entity_method::EntityMethod;
|
||||||
use wotbreplay_parser::models::data::payload::Payload;
|
use wotbreplay_parser::models::data::payload::Payload;
|
||||||
use wotbreplay_parser::replay::Replay;
|
use wotbreplay_parser::replay::Replay;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn data_ok() -> Result<()> {
|
fn parse_ok() -> Result<()> {
|
||||||
let mut replay = Replay::open(File::open(
|
let mut replay = Replay::open(File::open(
|
||||||
"tests/replays/20230429_0126__helaas_pindakaas_A140_ASTRON_REX_105_16114615898101270.wotbreplay",
|
"tests/replays/20230429_0126__helaas_pindakaas_A140_ASTRON_REX_105_16114615898101270.wotbreplay",
|
||||||
)?)?;
|
)?)?;
|
||||||
@@ -14,6 +15,16 @@ fn data_ok() -> Result<()> {
|
|||||||
assert_eq!(data.client_version, "9.8.5_apple");
|
assert_eq!(data.client_version, "9.8.5_apple");
|
||||||
assert_eq!(data.packets.len(), 60139);
|
assert_eq!(data.packets.len(), 60139);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn type0_ok() -> Result<()> {
|
||||||
|
let mut replay = Replay::open(File::open(
|
||||||
|
"tests/replays/20230429_0126__helaas_pindakaas_A140_ASTRON_REX_105_16114615898101270.wotbreplay",
|
||||||
|
)?)?;
|
||||||
|
let data = replay.read_data()?;
|
||||||
|
|
||||||
let (author_nickname, arena_unique_id, arena_type_id, type_0) = data
|
let (author_nickname, arena_unique_id, arena_type_id, type_0) = data
|
||||||
.packets
|
.packets
|
||||||
.iter()
|
.iter()
|
||||||
@@ -51,3 +62,32 @@ fn data_ok() -> Result<()> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn subtype_2f_players_ok() -> Result<()> {
|
||||||
|
let mut replay = Replay::open(File::open(
|
||||||
|
"tests/replays/20230429_0126__helaas_pindakaas_A140_ASTRON_REX_105_16114615898101270.wotbreplay",
|
||||||
|
)?)?;
|
||||||
|
let data = replay.read_data()?;
|
||||||
|
|
||||||
|
let players = data
|
||||||
|
.packets
|
||||||
|
.iter()
|
||||||
|
.filter_map(|packet| match &packet.payload {
|
||||||
|
Payload::EntityMethod(EntityMethod::Subtype2F { sub_type_2f, .. }) => {
|
||||||
|
sub_type_2f.players.as_ref()
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.next()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(players.players.len(), 14);
|
||||||
|
assert_eq!(players.players[1].team_number, 1);
|
||||||
|
assert_eq!(players.players[1].nickname, "Igrok_WoT_blitz");
|
||||||
|
assert_eq!(players.players[1].account_id, 597583401);
|
||||||
|
assert_eq!(players.players[1].platoon_number, None);
|
||||||
|
assert_eq!(players.players[1].clan_tag, Some("F_ANG".to_string()));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Reference in new issue
Block a user