smash_csb/
lib.rs

1//! # csb_lib
2//!
3//! csb_lib is a library for reading and writing `commonsoundtable.csb` 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 common fighter sounds.
17#[binrw]
18#[brw(magic = b"CSB\0", little)]
19#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
20#[derive(Debug)]
21pub struct CsbFile {
22    #[br(temp)]
23    #[bw(calc = 1u32)]
24    unk1: u32,
25
26    #[br(temp)]
27    #[bw(calc = entries.get(0).map(|entry| entry.sound_table.len()).unwrap_or(0) as u16)]
28    sound_num: u16,
29
30    #[br(temp)]
31    #[bw(calc = entries.len() as u16)]
32    fighter_num: u16,
33
34    #[br(args { inner: (sound_num,) }, count = fighter_num)]
35    pub entries: Vec<CsbEntry>,
36}
37
38impl CsbFile {
39    /// Reads the data from the given reader.
40    pub fn read<R: Read + Seek>(reader: &mut R) -> BinResult<Self> {
41        let csb = reader.read_le::<Self>()?;
42
43        Ok(csb)
44    }
45
46    /// Reads the data from the given file path.
47    pub fn from_file<P: AsRef<Path>>(path: P) -> BinResult<Self> {
48        let mut file = Cursor::new(fs::read(path)?);
49        let csb = file.read_le::<Self>()?;
50
51        Ok(csb)
52    }
53
54    /// Writes the data to the given writer.
55    pub fn write<W: Write + Seek>(&self, writer: &mut W) -> BinResult<()> {
56        self.write_le(writer)
57    }
58
59    /// Writes the data to the given file path.
60    pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> BinResult<()> {
61        let mut cursor = Cursor::new(Vec::new());
62
63        self.write_le(&mut cursor)?;
64        fs::write(path, cursor.get_mut())?;
65
66        Ok(())
67    }
68}
69
70/// A collection of common sound labels for a particular fighter.
71#[binrw]
72#[br(import(sound_num: u16))]
73#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
74#[derive(Debug)]
75pub struct CsbEntry {
76    /// Hashed name of the fighter.
77    pub fighter_kind: Hash40,
78
79    /// Collection of common sound labels associated with the fighter.
80    #[br(count = sound_num)]
81    pub sound_table: Vec<Hash40>,
82}