wow_dbc/wrath_tables/
spell_visual_effect_name.rs

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