|
1 | | -// TODO(tec27): build it |
| 1 | +use std::io::{Read, Seek, SeekFrom}; |
| 2 | + |
| 3 | +use byteorder::ReadBytesExt as _; |
| 4 | +use thiserror::Error; |
| 5 | + |
| 6 | +#[derive(Error, Debug)] |
| 7 | +pub enum BroodrepError { |
| 8 | + #[error(transparent)] |
| 9 | + IoError(#[from] std::io::Error), |
| 10 | + #[error("malformed header: {0}")] |
| 11 | + MalformedHeader(&'static str), |
| 12 | +} |
| 13 | + |
| 14 | +pub struct Replay<R: Read + Seek> { |
| 15 | + inner: R, |
| 16 | + format: ReplayFormat, |
| 17 | +} |
| 18 | + |
| 19 | +impl<R: Read + Seek> Replay<R> { |
| 20 | + pub fn new(mut reader: R) -> Result<Self, BroodrepError> { |
| 21 | + let format = Self::detect_format(&mut reader)?; |
| 22 | + Ok(Replay { |
| 23 | + inner: reader, |
| 24 | + format, |
| 25 | + }) |
| 26 | + } |
| 27 | + |
| 28 | + pub fn into_inner(self) -> R { |
| 29 | + self.inner |
| 30 | + } |
| 31 | + |
| 32 | + pub fn format(&self) -> ReplayFormat { |
| 33 | + self.format |
| 34 | + } |
| 35 | + |
| 36 | + fn detect_format(reader: &mut R) -> Result<ReplayFormat, BroodrepError> { |
| 37 | + // 1.21+ has `seRS`, before that it's `reRS` |
| 38 | + reader.seek(SeekFrom::Start(12))?; |
| 39 | + let mut magic = [0; 4]; |
| 40 | + reader.read(&mut magic)?; |
| 41 | + if magic == *b"seRS" { |
| 42 | + return Ok(ReplayFormat::Modern121); |
| 43 | + } |
| 44 | + if magic != *b"reRS" { |
| 45 | + return Err(BroodrepError::MalformedHeader("invalid magic bytes")); |
| 46 | + } |
| 47 | + |
| 48 | + // Check compression type, newer compression type indicates 1.18+ |
| 49 | + reader.seek(SeekFrom::Current(12))?; // offset 28 |
| 50 | + let byte = reader.read_u8()?; |
| 51 | + if byte == 0x78 { |
| 52 | + Ok(ReplayFormat::Modern) |
| 53 | + } else { |
| 54 | + // TODO(tec27): Make sure it's within the valid range |
| 55 | + Ok(ReplayFormat::Legacy) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// The format version of a replay. |
| 61 | +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
| 62 | +pub enum ReplayFormat { |
| 63 | + /// The replay was created with a version before 1.18. |
| 64 | + Legacy, |
| 65 | + /// The replay was created with a version between 1.18 and 1.21. |
| 66 | + Modern, |
| 67 | + /// The replay was created with version 1.21 or later. |
| 68 | + Modern121, |
| 69 | +} |
| 70 | + |
| 71 | +#[cfg(test)] |
| 72 | +mod tests { |
| 73 | + use std::io::Cursor; |
| 74 | + |
| 75 | + use super::*; |
| 76 | + |
| 77 | + const NOT_A_REPLAY: &[u8] = include_bytes!("../testdata/not_a_replay.rep"); |
| 78 | + const LEGACY: &[u8] = include_bytes!("../testdata/things.rep"); |
| 79 | + const LEGACY_EMPTY: &[u8] = include_bytes!("../testdata/empty.rep"); |
| 80 | + const SCR_OLD: &[u8] = include_bytes!("../testdata/scr_old.rep"); |
| 81 | + const SCR_121: &[u8] = include_bytes!("../testdata/scr_replay.rep"); |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_replay_format_invalid() { |
| 85 | + let mut cursor = Cursor::new(NOT_A_REPLAY); |
| 86 | + assert!(matches!( |
| 87 | + Replay::new(&mut cursor), |
| 88 | + Err(BroodrepError::MalformedHeader(_)) |
| 89 | + )); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn test_replay_format_legacy() { |
| 94 | + let mut cursor = Cursor::new(LEGACY); |
| 95 | + let replay = Replay::new(&mut cursor).unwrap(); |
| 96 | + assert_eq!(replay.format, ReplayFormat::Legacy); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_replay_format_legacy_empty() { |
| 101 | + let mut cursor = Cursor::new(LEGACY_EMPTY); |
| 102 | + let replay = Replay::new(&mut cursor).unwrap(); |
| 103 | + assert_eq!(replay.format, ReplayFormat::Legacy); |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn test_replay_format_scr_old() { |
| 108 | + let mut cursor = Cursor::new(SCR_OLD); |
| 109 | + let replay = Replay::new(&mut cursor).unwrap(); |
| 110 | + assert_eq!(replay.format, ReplayFormat::Modern); |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn test_replay_format_scr_121() { |
| 115 | + let mut cursor = Cursor::new(SCR_121); |
| 116 | + let replay = Replay::new(&mut cursor).unwrap(); |
| 117 | + assert_eq!(replay.format, ReplayFormat::Modern121); |
| 118 | + } |
| 119 | +} |
0 commit comments