wow_vanilla_dbc/tables/
vocal_ui_sounds.rs

1use crate::header::{HEADER_SIZE, DbcHeader};
2use crate::header;
3use crate::DbcTable;
4use std::io::Write;
5use crate::Indexable;
6use crate::tables::chr_races::*;
7use crate::tables::sound_entries::*;
8
9#[derive(Debug, Clone, PartialEq)]
10pub struct VocalUISounds {
11    pub rows: Vec<VocalUISoundsRow>,
12}
13
14impl DbcTable for VocalUISounds {
15    type Row = VocalUISoundsRow;
16
17    fn filename() -> &'static str { "VocalUISounds.dbc" }
18
19    fn rows(&self) -> &[Self::Row] { &self.rows }
20    fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }
21
22    fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
23        let mut header = [0_u8; HEADER_SIZE];
24        b.read_exact(&mut header)?;
25        let header = header::parse_header(&header)?;
26
27        if header.record_size != 28 {
28            return Err(crate::DbcError::InvalidHeader(
29                crate::InvalidHeaderError::RecordSize {
30                    expected: 28,
31                    actual: header.record_size,
32                },
33            ));
34        }
35
36        if header.field_count != 7 {
37            return Err(crate::DbcError::InvalidHeader(
38                crate::InvalidHeaderError::FieldCount {
39                    expected: 28,
40                    actual: header.field_count,
41                },
42            ));
43        }
44
45        let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
46        b.read_exact(&mut r)?;
47
48        let mut rows = Vec::with_capacity(header.record_count as usize);
49
50        for mut chunk in r.chunks(header.record_size as usize) {
51            let chunk = &mut chunk;
52
53            // id: primary_key (VocalUISounds) uint32
54            let id = VocalUISoundsKey::new(crate::util::read_u32_le(chunk)?);
55
56            // vocal_ui_enum: int32
57            let vocal_ui_enum = crate::util::read_i32_le(chunk)?;
58
59            // race: foreign_key (ChrRaces) uint32
60            let race = ChrRacesKey::new(crate::util::read_u32_le(chunk)?.into());
61
62            // normal_male_sound: foreign_key (SoundEntries) uint32
63            let normal_male_sound = SoundEntriesKey::new(crate::util::read_u32_le(chunk)?.into());
64
65            // normal_female_sound: foreign_key (SoundEntries) uint32
66            let normal_female_sound = SoundEntriesKey::new(crate::util::read_u32_le(chunk)?.into());
67
68            // pissed_male_sound: foreign_key (SoundEntries) uint32
69            let pissed_male_sound = SoundEntriesKey::new(crate::util::read_u32_le(chunk)?.into());
70
71            // pissed_female_sound: foreign_key (SoundEntries) uint32
72            let pissed_female_sound = SoundEntriesKey::new(crate::util::read_u32_le(chunk)?.into());
73
74
75            rows.push(VocalUISoundsRow {
76                id,
77                vocal_ui_enum,
78                race,
79                normal_male_sound,
80                normal_female_sound,
81                pissed_male_sound,
82                pissed_female_sound,
83            });
84        }
85
86        Ok(VocalUISounds { rows, })
87    }
88
89    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
90        let header = DbcHeader {
91            record_count: self.rows.len() as u32,
92            field_count: 7,
93            record_size: 28,
94            string_block_size: 1,
95        };
96
97        b.write_all(&header.write_header())?;
98
99        for row in &self.rows {
100            // id: primary_key (VocalUISounds) uint32
101            b.write_all(&row.id.id.to_le_bytes())?;
102
103            // vocal_ui_enum: int32
104            b.write_all(&row.vocal_ui_enum.to_le_bytes())?;
105
106            // race: foreign_key (ChrRaces) uint32
107            b.write_all(&(row.race.id as u32).to_le_bytes())?;
108
109            // normal_male_sound: foreign_key (SoundEntries) uint32
110            b.write_all(&(row.normal_male_sound.id as u32).to_le_bytes())?;
111
112            // normal_female_sound: foreign_key (SoundEntries) uint32
113            b.write_all(&(row.normal_female_sound.id as u32).to_le_bytes())?;
114
115            // pissed_male_sound: foreign_key (SoundEntries) uint32
116            b.write_all(&(row.pissed_male_sound.id as u32).to_le_bytes())?;
117
118            // pissed_female_sound: foreign_key (SoundEntries) uint32
119            b.write_all(&(row.pissed_female_sound.id as u32).to_le_bytes())?;
120
121        }
122
123        b.write_all(&[0_u8])?;
124
125        Ok(())
126    }
127
128}
129
130impl Indexable for VocalUISounds {
131    type PrimaryKey = VocalUISoundsKey;
132    fn get(&self, key: &Self::PrimaryKey) -> Option<&Self::Row> {
133        self.rows.iter().find(|a| a.id.id == key.id)
134    }
135
136    fn get_mut(&mut self, key: &Self::PrimaryKey) -> Option<&mut Self::Row> {
137        self.rows.iter_mut().find(|a| a.id.id == key.id)
138    }
139
140}
141
142#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
143pub struct VocalUISoundsKey {
144    pub id: u32
145}
146
147impl VocalUISoundsKey {
148    pub const fn new(id: u32) -> Self {
149        Self { id }
150    }
151
152}
153
154#[derive(Debug, Clone, PartialEq)]
155pub struct VocalUISoundsRow {
156    pub id: VocalUISoundsKey,
157    pub vocal_ui_enum: i32,
158    pub race: ChrRacesKey,
159    pub normal_male_sound: SoundEntriesKey,
160    pub normal_female_sound: SoundEntriesKey,
161    pub pissed_male_sound: SoundEntriesKey,
162    pub pissed_female_sound: SoundEntriesKey,
163}
164