rpfm_lib/files/sound_bank/
mod.rs1use getset::*;
28use serde_derive::{Serialize, Deserialize};
29
30use crate::binary::{ReadBytes, WriteBytes};
31use crate::error::{Result, RLibError};
32use crate::files::{Decodeable, EncodeableExtraData, Encodeable};
33use crate::utils::check_size_mismatch;
34
35use self::sections::bkhd::Bkhd;
36use self::sections::hirc::Hirc;
37
38use super::DecodeableExtraData;
39
40pub use crate::files::detection::sound_bank::EXTENSION;
41
42const FNV_NO: &str = "none"; const FNV_BNK: &str = "bank";
45const FNV_LNG: &str = "language";
46const FNV_EVT: &str = "event";
47const FNV_BUS: &str = "bus";
48const FNV_SFX: &str = "sfx";
49const FNV_TRG: &str = "trigger";
50const FNV_GME: &str = "rtpc/game-variable";
51const FNV_VAR: &str = "variable"; const FNV_VAL: &str = "value"; const FNV_UNK: &str = "???";
54
55const FNV_ORDER: [&str; 10] = [
56 FNV_BNK, FNV_LNG, FNV_EVT, FNV_BUS, FNV_SFX, FNV_TRG, FNV_GME, FNV_VAR, FNV_VAL, FNV_UNK
57];
58
59const FNV_ORDER_JOIN: [&str; 3] = [
60 FNV_BNK, FNV_LNG, FNV_BUS
61];
62
63const SIGNATURE_AKBK: &str = "AKBK"; const SIGNATURE_BKHD: &str = "BKHD"; const SIGNATURE_HIRC: &str = "HIRC"; const SIGNATURE_DATA: &str = "DATA"; const SIGNATURE_FXPR: &str = "FXPR"; const SIGNATURE_ENVS: &str = "ENVS"; const SIGNATURE_STID: &str = "STID"; const SIGNATURE_STMG: &str = "STMG"; const SIGNATURE_DIDX: &str = "DIDX"; const SIGNATURE_PLAT: &str = "PLAT"; const SIGNATURE_INIT: &str = "INIT"; mod common;
77mod sections;
78
79#[cfg(test)] mod test_soundbank;
80
81#[derive(Default, PartialEq, Clone, Debug, Getters, MutGetters, Setters, Serialize, Deserialize)]
93#[getset(get = "pub", get_mut = "pub", set = "pub")]
94pub struct SoundBank {
95 sections: Vec<Section>,
97}
98
99#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
105pub enum Section {
106 BKHD(Bkhd),
108 HIRC(Hirc),
110}
111
112impl Decodeable for SoundBank {
117
118 fn decode<R: ReadBytes>(data: &mut R, _extra_data: &Option<DecodeableExtraData>) -> Result<Self> {
119 let mut decoded = Self::default();
120 let data_len = data.len()?;
121
122 while let Ok(section_signature) = data.read_string_u8(4) {
123 let section_size = data.read_u32()? as u64;
124 dbg!(§ion_signature);
128 dbg!(§ion_size);
129 decoded.sections.push(match &*section_signature {
130 SIGNATURE_AKBK => return Err(RLibError::SoundBankUnsupportedSectionFound(SIGNATURE_AKBK.to_string())),
131
132 SIGNATURE_BKHD => Section::BKHD(Bkhd::read(data, section_size as usize)?),
134 SIGNATURE_HIRC => {
135 let header = match decoded.sections.first() {
136 Some(Section::BKHD(section)) => section,
137 _ => return Err(RLibError::SoundBankBKHDNotFound),
138 };
139
140 Section::HIRC(Hirc::read(data, *header.version())?)
141 },
142 SIGNATURE_DATA => return Err(RLibError::SoundBankUnsupportedSectionFound(SIGNATURE_DATA.to_string())),
143 SIGNATURE_FXPR => return Err(RLibError::SoundBankUnsupportedSectionFound(SIGNATURE_FXPR.to_string())),
144 SIGNATURE_ENVS => return Err(RLibError::SoundBankUnsupportedSectionFound(SIGNATURE_ENVS.to_string())),
145 SIGNATURE_STID => return Err(RLibError::SoundBankUnsupportedSectionFound(SIGNATURE_STID.to_string())),
146 SIGNATURE_STMG => return Err(RLibError::SoundBankUnsupportedSectionFound(SIGNATURE_STMG.to_string())),
147 SIGNATURE_DIDX => return Err(RLibError::SoundBankUnsupportedSectionFound(SIGNATURE_DIDX.to_string())),
148 SIGNATURE_PLAT => return Err(RLibError::SoundBankUnsupportedSectionFound(SIGNATURE_PLAT.to_string())),
149 SIGNATURE_INIT => return Err(RLibError::SoundBankUnsupportedSectionFound(SIGNATURE_INIT.to_string())),
150 _ => return Err(RLibError::SoundBankUnsupportedSectionFound(section_signature)),
151 });
152 }
153
154 check_size_mismatch(data.stream_position()? as usize, data_len as usize)?;
155
156 Ok(decoded)
157 }
158}
159
160impl Encodeable for SoundBank {
161
162 fn encode<W: WriteBytes>(&mut self, buffer: &mut W, _extra_data: &Option<EncodeableExtraData>) -> Result<()> {
163 let header = match self.sections.first() {
164 Some(Section::BKHD(section)) => section,
165 _ => return Err(RLibError::SoundBankBKHDNotFound),
166 };
167
168 for section in self.sections() {
169 match section {
170 Section::BKHD(data) => data.write(buffer)?,
171 Section::HIRC(data) => data.write(buffer, *header.version())?,
172 }
173 }
174
175 Ok(())
176 }
177}