From ae3940c6b9d656706d4b174885ece813fda292a7 Mon Sep 17 00:00:00 2001 From: Flux Xu Date: Mon, 12 Feb 2024 22:41:58 -0500 Subject: [PATCH] Expose replay header. --- crates/w3replay/src/header.rs | 4 ++-- crates/w3replay/src/lib.rs | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/crates/w3replay/src/header.rs b/crates/w3replay/src/header.rs index 18753ddf..a6253f95 100644 --- a/crates/w3replay/src/header.rs +++ b/crates/w3replay/src/header.rs @@ -46,7 +46,7 @@ use flo_util::binary::*; use flo_util::dword_string::DwordString; use flo_util::{BinDecode, BinEncode}; -#[derive(Debug, BinEncode, BinDecode)] +#[derive(Debug, BinEncode, BinDecode, Clone)] pub struct Header { #[bin(eq = SIGNATURE)] _sig: [u8; 28], @@ -80,7 +80,7 @@ impl Header { } } -#[derive(Debug, BinEncode, BinDecode)] +#[derive(Debug, BinEncode, BinDecode, Clone)] pub struct GameVersion { #[bin(eq = b"W3XP")] pub product: DwordString, diff --git a/crates/w3replay/src/lib.rs b/crates/w3replay/src/lib.rs index 655f7e9f..a3abf480 100644 --- a/crates/w3replay/src/lib.rs +++ b/crates/w3replay/src/lib.rs @@ -21,7 +21,7 @@ pub use replay::*; #[derive(Debug)] pub struct W3Replay { - _header: Header, + header: Header, blocks: Blocks, } @@ -38,12 +38,17 @@ impl W3Replay> { let header = Header::decode(&mut buf_slice).map_err(|e| e.context("header"))?; Ok(W3Replay { blocks: Blocks::new(r, header.num_blocks as usize, len - Header::MIN_SIZE), - _header: header, + header, }) } + pub fn header(&self) -> &Header { + &self.header + } + pub fn inspect>(path: P) -> Result<(ReplayInfo, RecordIter>)> { let replay = Self::open(path)?; + let header = replay.header().clone(); let mut game = None; let mut players = vec![]; let mut slots = None; @@ -59,6 +64,7 @@ impl W3Replay> { } Ok(( ReplayInfo { + header, game: game.ok_or_else(|| Error::NoGameInfoRecord)?, players, slots: slots.ok_or_else(|| Error::NoSlotInfoRecord)?, @@ -77,7 +83,7 @@ where let header = Header::decode(&mut buf).map_err(|e| e.context("header"))?; Ok(W3Replay { blocks: Blocks::from_buf(buf, header.num_blocks as usize), - _header: header, + header, }) } } @@ -90,6 +96,7 @@ impl W3Replay { #[derive(Debug)] pub struct ReplayInfo { + pub header: Header, pub game: GameInfo, pub players: Vec, pub slots: SlotInfo,