smash_bgm_property/
lib.rs

1//! # bgm_property_lib
2//!
3//! bgm_property_lib is a library for reading and writing `bgm_property.bin` files from Super Smash Bros. Ultimate.
4use std::{
5    fs,
6    io::{Cursor, Read, Seek, Write},
7    path::Path,
8};
9
10use binrw::{binrw, BinReaderExt, BinResult, BinWrite};
11pub use hash40::Hash40;
12
13#[cfg(feature = "serde")]
14use serde::{Deserialize, Serialize};
15
16/// The container type for BGM stream properties.
17#[binrw]
18#[brw(magic = b"PMGB", little)]
19#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
20#[derive(Debug)]
21pub struct BgmPropertyFile {
22    #[br(temp)]
23    #[bw(calc = entries.len() as u32)]
24    entry_count: u32,
25
26    #[br(count = entry_count)]
27    pub entries: Vec<BgmPropertyEntry>,
28}
29
30impl BgmPropertyFile {
31    /// Reads the data from the given reader.
32    pub fn read<R: Read + Seek>(reader: &mut R) -> BinResult<Self> {
33        let bgm_property = reader.read_le::<Self>()?;
34
35        Ok(bgm_property)
36    }
37
38    /// Reads the data from the given file path.
39    pub fn from_file<P: AsRef<Path>>(path: P) -> BinResult<Self> {
40        let mut file = Cursor::new(fs::read(path)?);
41        let bgm_property = file.read_le::<Self>()?;
42
43        Ok(bgm_property)
44    }
45
46    /// Writes the data to the given writer.
47    pub fn write<W: Write + Seek>(&self, writer: &mut W) -> BinResult<()> {
48        self.write_le(writer)
49    }
50
51    /// Writes the data to the given file path.
52    pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> BinResult<()> {
53        let mut cursor = Cursor::new(Vec::new());
54
55        self.write_le(&mut cursor)?;
56        fs::write(path, cursor.get_mut())?;
57
58        Ok(())
59    }
60}
61
62/// A group of timing properties for a BGM stream.
63#[binrw]
64#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
65#[derive(Debug)]
66pub struct BgmPropertyEntry {
67    /// Hashed name of the BGM stream.
68    pub stream_name: Hash40,
69
70    /// Beginning of the BGM stream's loop measured in milliseconds.
71    pub loop_start_ms: u32,
72
73    /// Beginning of the BGM stream's loop measured in samples.
74    pub loop_start_sample: u32,
75
76    /// End of the BGM stream's loop measured in milliseconds.
77    pub loop_end_ms: u32,
78
79    /// End of the BGM stream's loop measured in samples.
80    pub loop_end_sample: u32,
81
82    /// Duration of the BGM stream measured in milliseconds.
83    pub duration_ms: u32,
84
85    /// Duration of the BGM stream measured in samples.
86    #[brw(pad_after = 4)]
87    pub duration_sample: u32,
88}