rpfm_lib/files/dat/mod.rs
1//---------------------------------------------------------------------------//
2// Copyright (c) 2017-2026 Ismael Gutiérrez González. All rights reserved.
3//
4// This file is part of the Rusted PackFile Manager (RPFM) project,
5// which can be found here: https://github.com/Frodo45127/rpfm.
6//
7// This file is licensed under the MIT license, which can be found here:
8// https://github.com/Frodo45127/rpfm/blob/master/LICENSE.
9//---------------------------------------------------------------------------//
10
11//! DAT audio configuration file format support.
12//!
13//! DAT files (`.dat`) are audio configuration files used in Total War games to define
14//! sound events, parameters, and enumerations. These files work in conjunction with
15//! Wwise sound banks to configure audio playback behavior.
16//!
17//! # File Format
18//!
19//! DAT files are binary files containing six data blocks:
20//! - **Event 0**: Event parameters (name-value pairs with float values)
21//! - **Block 1**: Event enumerations (name with list of enumeration values)
22//! - **Block 2**: Event enumerations (name with list of enumeration values)
23//! - **Voice Events**: Voice event enumerations (name with list of voice event values)
24//! - **Block 4**: Event enumeration list (simple string list)
25//! - **Block 5**: Event enumeration list (simple string list)
26//!
27//! # Purpose
28//!
29//! DAT files define audio event configurations including:
30//! - Sound event parameters and their default values
31//! - Available enumeration values for event properties
32//! - Groupings and categorizations of audio events
33//!
34//! # Usage
35//!
36//! ```rust,ignore
37//! use rpfm_lib::files::dat::Dat;
38//! use rpfm_lib::files::Decodeable;
39//!
40//! // Decode from binary data
41//! let dat = Dat::decode(&mut data, &None)?;
42//!
43//! // Access event parameters
44//! for (event_name, value) in dat.event_0() {
45//! println!("Parameter '{}' = {}", event_name, value);
46//! }
47//!
48//! // Access voice events
49//! for (voice_event, values) in dat.voice_events() {
50//! println!("Voice event '{}' has {} values", voice_event, values.len());
51//! }
52//! ```
53//!
54//! # File Location
55//!
56//! These files are typically found at:
57//! - `sound/*.dat`
58//! - `audio/wwisedata/*.dat`
59
60use getset::*;
61use serde_derive::{Serialize, Deserialize};
62
63use crate::binary::{ReadBytes, WriteBytes};
64use crate::error::Result;
65use crate::files::{Decodeable, EncodeableExtraData, Encodeable};
66use crate::utils::*;
67
68use super::DecodeableExtraData;
69
70pub use crate::files::detection::dat::EXTENSION;
71
72//#[cfg(test)] mod test_dat;
73
74//---------------------------------------------------------------------------//
75// Enum & Structs
76//---------------------------------------------------------------------------//
77
78/// Represents a DAT audio configuration file decoded in memory.
79///
80/// Contains six blocks of audio event configuration data including parameters,
81/// enumerations, and event lists used to configure Wwise sound playback.
82///
83/// # Structure
84///
85/// The file is organized into six sequential blocks, each serving a different purpose
86/// in the audio configuration system. The exact semantic meaning of each block may
87/// vary between Total War games.
88///
89/// # Fields
90///
91/// * `event_0` - Event parameters with float values (e.g., volume, pitch defaults)
92/// * `block_1` - Event enumerations with multiple values (e.g., sound categories)
93/// * `block_2` - Event enumerations with multiple values (e.g., sound categories)
94/// * `voice_events` - Voice event enumerations with associated values
95/// * `block_4` - Simple event enumeration list
96/// * `block_5` - Simple event enumeration list
97///
98/// # Examples
99///
100/// ```rust,ignore
101/// let dat = Dat::decode(&mut data, &None)?;
102///
103/// // Iterate through event parameters
104/// for (name, value) in dat.event_0() {
105/// println!("Parameter: {} = {}", name, value);
106/// }
107///
108/// // Iterate through voice events
109/// for (voice_event, voice_values) in dat.voice_events() {
110/// for value in voice_values {
111/// println!(" {} -> {}", voice_event, value);
112/// }
113/// }
114/// ```
115#[derive(Default, PartialEq, Clone, Debug, Getters, MutGetters, Setters, Serialize, Deserialize)]
116#[getset(get = "pub", get_mut = "pub", set = "pub")]
117pub struct Dat {
118 /// Event parameters with floating-point values.
119 ///
120 /// Maps event parameter names to their default float values.
121 /// Typically used for parameters like volume, pitch, or other numeric settings.
122 event_0: Vec<(String, f32)>,
123
124 /// Event enumerations with string lists.
125 ///
126 /// Maps enumeration names to lists of possible string values.
127 /// Used for categorical audio properties.
128 block_1: Vec<(String, Vec<String>)>,
129
130 /// Event enumerations with string lists.
131 ///
132 /// Maps enumeration names to lists of possible string values.
133 /// Similar to block_1, potentially for different enumeration categories.
134 block_2: Vec<(String, Vec<String>)>,
135
136 /// Voice event enumerations with string lists.
137 ///
138 /// Maps voice event names to lists of possible voice event values.
139 /// Used for categorizing and organizing voice-related audio events.
140 voice_events: Vec<(String, Vec<String>)>,
141
142 /// Simple event enumeration list.
143 ///
144 /// List of event enumeration names without associated values.
145 /// FIXME: Possible wrong implementation.
146 block_4: Vec<String>,
147
148 /// Simple event enumeration list.
149 ///
150 /// List of event enumeration names without associated values.
151 /// Similar to block_4, potentially for a different category.
152 block_5: Vec<String>,
153}
154
155//---------------------------------------------------------------------------//
156// Implementation of Dat
157//---------------------------------------------------------------------------//
158
159impl Decodeable for Dat {
160
161 fn decode<R: ReadBytes>(data: &mut R, _extra_data: &Option<DecodeableExtraData>) -> Result<Self> {
162 let mut decoded = Self::default();
163 let data_len = data.len()?;
164
165 for _ in 0..data.read_u32()? {
166 let string_size = data.read_u32()?;
167 let event_name = data.read_string_u8(string_size as usize)?;
168 let event_value = data.read_f32()?;
169 decoded.event_0.push((event_name, event_value));
170 }
171
172 for _ in 0..data.read_u32()? {
173 let string_size = data.read_u32()?;
174 let event_enum = data.read_string_u8(string_size as usize)?;
175
176 let mut entries = vec![];
177 for _ in 0..data.read_u32()? {
178 let string_size = data.read_u32()?;
179 let event_enum = data.read_string_u8(string_size as usize)?;
180 entries.push(event_enum);
181 }
182 decoded.block_1.push((event_enum, entries));
183 }
184
185 for _ in 0..data.read_u32()? {
186 let string_size = data.read_u32()?;
187 let event_enum = data.read_string_u8(string_size as usize)?;
188
189 let mut entries = vec![];
190 for _ in 0..data.read_u32()? {
191 let string_size = data.read_u32()?;
192 let event_enum = data.read_string_u8(string_size as usize)?;
193 entries.push(event_enum);
194 }
195 decoded.block_2.push((event_enum, entries));
196 }
197
198 for _ in 0..data.read_u32()? {
199 let string_size = data.read_u32()?;
200 let event_enum = data.read_string_u8(string_size as usize)?;
201
202 let mut entries = vec![];
203 for _ in 0..data.read_u32()? {
204 let string_size = data.read_u32()?;
205 let event_enum = data.read_string_u8(string_size as usize)?;
206 entries.push(event_enum);
207 }
208 decoded.voice_events.push((event_enum, entries));
209 }
210
211 for _ in 0..data.read_u32()? {
212 let string_size = data.read_u32()?;
213 let event_enum = data.read_string_u8(string_size as usize)?;
214 decoded.block_4.push(event_enum);
215 }
216
217 for _ in 0..data.read_u32()? {
218 let string_size = data.read_u32()?;
219 let event_enum = data.read_string_u8(string_size as usize)?;
220 decoded.block_5.push(event_enum);
221 }
222
223 check_size_mismatch(data.stream_position()? as usize, data_len as usize)?;
224 Ok(decoded)
225 }
226}
227
228impl Encodeable for Dat {
229
230 fn encode<W: WriteBytes>(&mut self, buffer: &mut W, _extra_data: &Option<EncodeableExtraData>) -> Result<()> {
231 buffer.write_u32(self.event_0.len() as u32)?;
232 for entry in self.event_0() {
233 buffer.write_u32(entry.0.len() as u32)?;
234 buffer.write_string_u8(&entry.0)?;
235 buffer.write_f32(entry.1)?;
236 }
237
238 buffer.write_u32(self.block_1.len() as u32)?;
239 for entry in self.block_1() {
240 buffer.write_u32(entry.0.len() as u32)?;
241 buffer.write_string_u8(&entry.0)?;
242
243 buffer.write_u32(entry.1.len() as u32)?;
244 for subentry in &entry.1 {
245 buffer.write_u32(subentry.len() as u32)?;
246 buffer.write_string_u8(subentry)?;
247 }
248 }
249
250 buffer.write_u32(self.block_2.len() as u32)?;
251 for entry in self.block_2() {
252 buffer.write_u32(entry.0.len() as u32)?;
253 buffer.write_string_u8(&entry.0)?;
254
255 buffer.write_u32(entry.1.len() as u32)?;
256 for subentry in &entry.1 {
257 buffer.write_u32(subentry.len() as u32)?;
258 buffer.write_string_u8(subentry)?;
259 }
260 }
261
262 buffer.write_u32(self.voice_events.len() as u32)?;
263 for entry in self.voice_events() {
264 buffer.write_u32(entry.0.len() as u32)?;
265 buffer.write_string_u8(&entry.0)?;
266
267 buffer.write_u32(entry.1.len() as u32)?;
268 for subentry in &entry.1 {
269 buffer.write_u32(subentry.len() as u32)?;
270 buffer.write_string_u8(subentry)?;
271 }
272 }
273
274 buffer.write_u32(self.block_4.len() as u32)?;
275 for entry in self.block_4() {
276 buffer.write_u32(entry.len() as u32)?;
277 buffer.write_string_u8(entry)?;
278 }
279
280 buffer.write_u32(self.block_5.len() as u32)?;
281 for entry in self.block_5() {
282 buffer.write_u32(entry.len() as u32)?;
283 buffer.write_string_u8(entry)?;
284 }
285
286 Ok(())
287 }
288}