wow_dbc/tbc_tables/
emotes_text_sound.rs

1use crate::{
2    DbcTable, Indexable,
3};
4use crate::header::{
5    DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::tbc_tables::chr_races::ChrRacesKey;
8use crate::tbc_tables::emotes_text::EmotesTextKey;
9use crate::tbc_tables::sound_entries::SoundEntriesKey;
10use std::io::Write;
11
12#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub struct EmotesTextSound {
14    pub rows: Vec<EmotesTextSoundRow>,
15}
16
17impl DbcTable for EmotesTextSound {
18    type Row = EmotesTextSoundRow;
19
20    const FILENAME: &'static str = "EmotesTextSound.dbc";
21
22    fn rows(&self) -> &[Self::Row] { &self.rows }
23    fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }
24
25    fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
26        let mut header = [0_u8; HEADER_SIZE];
27        b.read_exact(&mut header)?;
28        let header = parse_header(&header)?;
29
30        if header.record_size != 20 {
31            return Err(crate::DbcError::InvalidHeader(
32                crate::InvalidHeaderError::RecordSize {
33                    expected: 20,
34                    actual: header.record_size,
35                },
36            ));
37        }
38
39        if header.field_count != 5 {
40            return Err(crate::DbcError::InvalidHeader(
41                crate::InvalidHeaderError::FieldCount {
42                    expected: 5,
43                    actual: header.field_count,
44                },
45            ));
46        }
47
48        let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
49        b.read_exact(&mut r)?;
50
51        let mut rows = Vec::with_capacity(header.record_count as usize);
52
53        for mut chunk in r.chunks(header.record_size as usize) {
54            let chunk = &mut chunk;
55
56            // id: primary_key (EmotesTextSound) int32
57            let id = EmotesTextSoundKey::new(crate::util::read_i32_le(chunk)?);
58
59            // emotes_text_id: foreign_key (EmotesText) int32
60            let emotes_text_id = EmotesTextKey::new(crate::util::read_i32_le(chunk)?.into());
61
62            // race_id: foreign_key (ChrRaces) int32
63            let race_id = ChrRacesKey::new(crate::util::read_i32_le(chunk)?.into());
64
65            // sex_id: int32
66            let sex_id = crate::util::read_i32_le(chunk)?;
67
68            // sound_id: foreign_key (SoundEntries) int32
69            let sound_id = SoundEntriesKey::new(crate::util::read_i32_le(chunk)?.into());
70
71
72            rows.push(EmotesTextSoundRow {
73                id,
74                emotes_text_id,
75                race_id,
76                sex_id,
77                sound_id,
78            });
79        }
80
81        Ok(EmotesTextSound { rows, })
82    }
83
84    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
85        let header = DbcHeader {
86            record_count: self.rows.len() as u32,
87            field_count: 5,
88            record_size: 20,
89            string_block_size: 1,
90        };
91
92        b.write_all(&header.write_header())?;
93
94        for row in &self.rows {
95            // id: primary_key (EmotesTextSound) int32
96            b.write_all(&row.id.id.to_le_bytes())?;
97
98            // emotes_text_id: foreign_key (EmotesText) int32
99            b.write_all(&(row.emotes_text_id.id as i32).to_le_bytes())?;
100
101            // race_id: foreign_key (ChrRaces) int32
102            b.write_all(&(row.race_id.id as i32).to_le_bytes())?;
103
104            // sex_id: int32
105            b.write_all(&row.sex_id.to_le_bytes())?;
106
107            // sound_id: foreign_key (SoundEntries) int32
108            b.write_all(&(row.sound_id.id as i32).to_le_bytes())?;
109
110        }
111
112        b.write_all(&[0_u8])?;
113
114        Ok(())
115    }
116
117}
118
119impl Indexable for EmotesTextSound {
120    type PrimaryKey = EmotesTextSoundKey;
121    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
122        let key = key.try_into().ok()?;
123        self.rows.iter().find(|a| a.id.id == key.id)
124    }
125
126    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
127        let key = key.try_into().ok()?;
128        self.rows.iter_mut().find(|a| a.id.id == key.id)
129    }
130}
131
132#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
133pub struct EmotesTextSoundKey {
134    pub id: i32
135}
136
137impl EmotesTextSoundKey {
138    pub const fn new(id: i32) -> Self {
139        Self { id }
140    }
141
142}
143
144impl From<u8> for EmotesTextSoundKey {
145    fn from(v: u8) -> Self {
146        Self::new(v.into())
147    }
148}
149
150impl From<u16> for EmotesTextSoundKey {
151    fn from(v: u16) -> Self {
152        Self::new(v.into())
153    }
154}
155
156impl From<i8> for EmotesTextSoundKey {
157    fn from(v: i8) -> Self {
158        Self::new(v.into())
159    }
160}
161
162impl From<i16> for EmotesTextSoundKey {
163    fn from(v: i16) -> Self {
164        Self::new(v.into())
165    }
166}
167
168impl From<i32> for EmotesTextSoundKey {
169    fn from(v: i32) -> Self {
170        Self::new(v)
171    }
172}
173
174impl TryFrom<u32> for EmotesTextSoundKey {
175    type Error = u32;
176    fn try_from(v: u32) -> Result<Self, Self::Error> {
177        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
178    }
179}
180
181impl TryFrom<usize> for EmotesTextSoundKey {
182    type Error = usize;
183    fn try_from(v: usize) -> Result<Self, Self::Error> {
184        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
185    }
186}
187
188impl TryFrom<u64> for EmotesTextSoundKey {
189    type Error = u64;
190    fn try_from(v: u64) -> Result<Self, Self::Error> {
191        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
192    }
193}
194
195impl TryFrom<i64> for EmotesTextSoundKey {
196    type Error = i64;
197    fn try_from(v: i64) -> Result<Self, Self::Error> {
198        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
199    }
200}
201
202impl TryFrom<isize> for EmotesTextSoundKey {
203    type Error = isize;
204    fn try_from(v: isize) -> Result<Self, Self::Error> {
205        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
206    }
207}
208
209#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
210pub struct EmotesTextSoundRow {
211    pub id: EmotesTextSoundKey,
212    pub emotes_text_id: EmotesTextKey,
213    pub race_id: ChrRacesKey,
214    pub sex_id: i32,
215    pub sound_id: SoundEntriesKey,
216}
217