smash_fnv/
lib.rs

1//! # fnv_lib
2//!
3//! fnv_lib is a library for reading and writing `sound_volume_fighter_num_table.fnv` files from Super Smash Bros. for Nintendo 3DS and Wii U and Super Smash Bros. Ultimate. Not to be confused with [Fowler–Noll–Vo](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function).
4use std::{
5    fs,
6    io::{Cursor, Read, Seek, Write},
7    path::Path,
8};
9
10use binrw::{binrw, BinReaderExt, BinResult, BinWrite};
11
12#[cfg(feature = "serde")]
13use serde::{Deserialize, Serialize};
14
15/// The container type for groups of audio gain parameters according to the number of fighters in-game.
16#[binrw]
17#[brw(magic = b"FNV\0", little)]
18#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
19#[derive(Debug)]
20pub struct FnvFile {
21    #[br(temp)]
22    #[bw(calc = 1u32)]
23    unk1: u32,
24
25    #[br(temp)]
26    #[bw(calc = entries.len() as u32)]
27    entry_count: u32,
28
29    #[br(count = entry_count)]
30    pub entries: Vec<FnvEntry>,
31}
32
33impl FnvFile {
34    /// Reads the data from the given reader.
35    pub fn read<R: Read + Seek>(reader: &mut R) -> BinResult<Self> {
36        let fnv = reader.read_le::<Self>()?;
37
38        Ok(fnv)
39    }
40
41    /// Reads the data from the given file path.
42    pub fn from_file<P: AsRef<Path>>(path: P) -> BinResult<Self> {
43        let mut file = Cursor::new(fs::read(path)?);
44        let fnv = file.read_le::<Self>()?;
45
46        Ok(fnv)
47    }
48
49    /// Writes the data to the given writer.
50    pub fn write<W: Write + Seek>(&self, writer: &mut W) -> BinResult<()> {
51        self.write_le(writer)
52    }
53
54    /// Writes the data to the given file path.
55    pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> BinResult<()> {
56        let mut cursor = Cursor::new(Vec::new());
57
58        self.write_le(&mut cursor)?;
59        fs::write(path, cursor.get_mut())?;
60
61        Ok(())
62    }
63}
64
65/// A group of audio gain parameters according to the number of fighters in-game.
66#[binrw]
67#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
68#[derive(Debug)]
69pub struct FnvEntry {
70    pub fighter_num: u32,
71    pub volume: Volume,
72}
73
74/// A group of audio gain parameters for different classes of sounds.
75#[binrw]
76#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
77#[derive(Debug)]
78pub struct Volume {
79    pub other: f32,
80    pub sound_attr: f32,
81    pub se_fighter_step: f32,
82    pub se_fighter_landing: f32,
83    pub se_collision_step: f32,
84    pub se_collision_landing: f32,
85    pub se_stage: f32,
86    pub bgm: f32,
87}