wow_dbc/vanilla_tables/
emotes.rs

1use crate::{
2    DbcTable, Indexable,
3};
4use crate::header::{
5    DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::vanilla_tables::animation_data::AnimationDataKey;
8use crate::vanilla_tables::sound_entries::SoundEntriesKey;
9use std::io::Write;
10use wow_world_base::vanilla::{
11    EmoteFlags, EmoteSpecProc,
12};
13
14#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
15pub struct Emotes {
16    pub rows: Vec<EmotesRow>,
17}
18
19impl DbcTable for Emotes {
20    type Row = EmotesRow;
21
22    const FILENAME: &'static str = "Emotes.dbc";
23
24    fn rows(&self) -> &[Self::Row] { &self.rows }
25    fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }
26
27    fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
28        let mut header = [0_u8; HEADER_SIZE];
29        b.read_exact(&mut header)?;
30        let header = parse_header(&header)?;
31
32        if header.record_size != 28 {
33            return Err(crate::DbcError::InvalidHeader(
34                crate::InvalidHeaderError::RecordSize {
35                    expected: 28,
36                    actual: header.record_size,
37                },
38            ));
39        }
40
41        if header.field_count != 7 {
42            return Err(crate::DbcError::InvalidHeader(
43                crate::InvalidHeaderError::FieldCount {
44                    expected: 7,
45                    actual: header.field_count,
46                },
47            ));
48        }
49
50        let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
51        b.read_exact(&mut r)?;
52        let mut string_block = vec![0_u8; header.string_block_size as usize];
53        b.read_exact(&mut string_block)?;
54
55        let mut rows = Vec::with_capacity(header.record_count as usize);
56
57        for mut chunk in r.chunks(header.record_size as usize) {
58            let chunk = &mut chunk;
59
60            // id: primary_key (Emotes) uint32
61            let id = EmotesKey::new(crate::util::read_u32_le(chunk)?);
62
63            // emote_slash_command: string_ref
64            let emote_slash_command = {
65                let s = crate::util::get_string_as_vec(chunk, &string_block)?;
66                String::from_utf8(s)?
67            };
68
69            // animation_data: foreign_key (AnimationData) uint32
70            let animation_data = AnimationDataKey::new(crate::util::read_u32_le(chunk)?.into());
71
72            // flags: EmoteFlags
73            let flags = EmoteFlags::new(crate::util::read_i32_le(chunk)? as _);
74
75            // spec_proc: EmoteSpecProc
76            let spec_proc = crate::util::read_i32_le(chunk)?.try_into()?;
77
78            // emote_spec_proc_param: int32
79            let emote_spec_proc_param = crate::util::read_i32_le(chunk)?;
80
81            // event_sound_entry: foreign_key (SoundEntries) uint32
82            let event_sound_entry = SoundEntriesKey::new(crate::util::read_u32_le(chunk)?.into());
83
84
85            rows.push(EmotesRow {
86                id,
87                emote_slash_command,
88                animation_data,
89                flags,
90                spec_proc,
91                emote_spec_proc_param,
92                event_sound_entry,
93            });
94        }
95
96        Ok(Emotes { rows, })
97    }
98
99    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
100        let header = DbcHeader {
101            record_count: self.rows.len() as u32,
102            field_count: 7,
103            record_size: 28,
104            string_block_size: self.string_block_size(),
105        };
106
107        b.write_all(&header.write_header())?;
108
109        let mut string_index = 1;
110        for row in &self.rows {
111            // id: primary_key (Emotes) uint32
112            b.write_all(&row.id.id.to_le_bytes())?;
113
114            // emote_slash_command: string_ref
115            if !row.emote_slash_command.is_empty() {
116                b.write_all(&(string_index as u32).to_le_bytes())?;
117                string_index += row.emote_slash_command.len() + 1;
118            }
119            else {
120                b.write_all(&(0_u32).to_le_bytes())?;
121            }
122
123            // animation_data: foreign_key (AnimationData) uint32
124            b.write_all(&(row.animation_data.id as u32).to_le_bytes())?;
125
126            // flags: EmoteFlags
127            b.write_all(&(row.flags.as_int() as i32).to_le_bytes())?;
128
129            // spec_proc: EmoteSpecProc
130            b.write_all(&(row.spec_proc.as_int() as i32).to_le_bytes())?;
131
132            // emote_spec_proc_param: int32
133            b.write_all(&row.emote_spec_proc_param.to_le_bytes())?;
134
135            // event_sound_entry: foreign_key (SoundEntries) uint32
136            b.write_all(&(row.event_sound_entry.id as u32).to_le_bytes())?;
137
138        }
139
140        self.write_string_block(b)?;
141
142        Ok(())
143    }
144
145}
146
147impl Indexable for Emotes {
148    type PrimaryKey = EmotesKey;
149    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
150        let key = key.try_into().ok()?;
151        self.rows.iter().find(|a| a.id.id == key.id)
152    }
153
154    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
155        let key = key.try_into().ok()?;
156        self.rows.iter_mut().find(|a| a.id.id == key.id)
157    }
158}
159
160impl Emotes {
161    fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
162        b.write_all(&[0])?;
163
164        for row in &self.rows {
165            if !row.emote_slash_command.is_empty() { b.write_all(row.emote_slash_command.as_bytes())?; b.write_all(&[0])?; };
166        }
167
168        Ok(())
169    }
170
171    fn string_block_size(&self) -> u32 {
172        let mut sum = 1;
173        for row in &self.rows {
174            if !row.emote_slash_command.is_empty() { sum += row.emote_slash_command.len() + 1; };
175        }
176
177        sum as u32
178    }
179
180}
181
182#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
183pub struct EmotesKey {
184    pub id: u32
185}
186
187impl EmotesKey {
188    pub const fn new(id: u32) -> Self {
189        Self { id }
190    }
191
192}
193
194impl From<u8> for EmotesKey {
195    fn from(v: u8) -> Self {
196        Self::new(v.into())
197    }
198}
199
200impl From<u16> for EmotesKey {
201    fn from(v: u16) -> Self {
202        Self::new(v.into())
203    }
204}
205
206impl From<u32> for EmotesKey {
207    fn from(v: u32) -> Self {
208        Self::new(v)
209    }
210}
211
212impl TryFrom<u64> for EmotesKey {
213    type Error = u64;
214    fn try_from(v: u64) -> Result<Self, Self::Error> {
215        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
216    }
217}
218
219impl TryFrom<usize> for EmotesKey {
220    type Error = usize;
221    fn try_from(v: usize) -> Result<Self, Self::Error> {
222        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
223    }
224}
225
226impl TryFrom<i8> for EmotesKey {
227    type Error = i8;
228    fn try_from(v: i8) -> Result<Self, Self::Error> {
229        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
230    }
231}
232
233impl TryFrom<i16> for EmotesKey {
234    type Error = i16;
235    fn try_from(v: i16) -> Result<Self, Self::Error> {
236        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
237    }
238}
239
240impl TryFrom<i32> for EmotesKey {
241    type Error = i32;
242    fn try_from(v: i32) -> Result<Self, Self::Error> {
243        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
244    }
245}
246
247impl TryFrom<i64> for EmotesKey {
248    type Error = i64;
249    fn try_from(v: i64) -> Result<Self, Self::Error> {
250        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
251    }
252}
253
254impl TryFrom<isize> for EmotesKey {
255    type Error = isize;
256    fn try_from(v: isize) -> Result<Self, Self::Error> {
257        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
258    }
259}
260
261#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
262pub struct EmotesRow {
263    pub id: EmotesKey,
264    pub emote_slash_command: String,
265    pub animation_data: AnimationDataKey,
266    pub flags: EmoteFlags,
267    pub spec_proc: EmoteSpecProc,
268    pub emote_spec_proc_param: i32,
269    pub event_sound_entry: SoundEntriesKey,
270}
271