smash_bgm_property/
lib.rs1use 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#[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 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 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 pub fn write<W: Write + Seek>(&self, writer: &mut W) -> BinResult<()> {
48 self.write_le(writer)
49 }
50
51 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#[binrw]
64#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
65#[derive(Debug)]
66pub struct BgmPropertyEntry {
67 pub stream_name: Hash40,
69
70 pub loop_start_ms: u32,
72
73 pub loop_start_sample: u32,
75
76 pub loop_end_ms: u32,
78
79 pub loop_end_sample: u32,
81
82 pub duration_ms: u32,
84
85 #[brw(pad_after = 4)]
87 pub duration_sample: u32,
88}