smash_sli/
lib.rs

1//! # sli_lib
2//!
3//! sli_lib is a library for reading and writing `soundlabelinfo.sli` 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 sound entries.
17#[binrw]
18#[brw(magic = b"SLI\0", little)]
19#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
20#[derive(Debug)]
21pub struct SliFile {
22    #[br(temp)]
23    #[bw(calc = 1u32)]
24    unk1: u32,
25
26    #[br(temp)]
27    #[bw(calc = entries.len() as u32)]
28    entry_count: u32,
29
30    #[br(count = entry_count)]
31    #[bw(map = |e| {
32        let mut entries = e.clone();
33        entries.sort_unstable_by(|a, b| a.tone_name.cmp(&b.tone_name));
34        entries
35    })]
36    pub entries: Vec<SliEntry>,
37}
38
39impl SliFile {
40    /// Reads the data from the given reader.
41    pub fn read<R: Read + Seek>(reader: &mut R) -> BinResult<Self> {
42        let sli = reader.read_le::<Self>()?;
43
44        Ok(sli)
45    }
46
47    /// Reads the data from the given file path.
48    pub fn from_file<P: AsRef<Path>>(path: P) -> BinResult<Self> {
49        let mut file = Cursor::new(fs::read(path)?);
50        let sli = file.read_le::<Self>()?;
51
52        Ok(sli)
53    }
54
55    /// Writes the data to the given writer.
56    pub fn write<W: Write + Seek>(&self, writer: &mut W) -> BinResult<()> {
57        self.write_le(writer)
58    }
59
60    /// Writes the data to the given file path.
61    pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> BinResult<()> {
62        let mut cursor = Cursor::new(Vec::new());
63
64        self.write_le(&mut cursor)?;
65        fs::write(path, cursor.get_mut())?;
66
67        Ok(())
68    }
69}
70
71/// A group of sound identification parameters involved with sound lookups.
72#[binrw]
73#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
74#[derive(Debug, Clone)]
75pub struct SliEntry {
76    /// Hashed name of the sound.
77    pub tone_name: Hash40,
78
79    /// ID of the associated NUS3BANK file.
80    pub nus3bank_id: u32,
81
82    /// ID of the sound in the NUS3AUDIO, NUS3BANK, and TONELABEL files.
83    pub tone_id: u32,
84}