wow_dbc/wrath_tables/
spell_shapeshift_form.rs

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