wow_dbc/vanilla_tables/
spell_shapeshift_form.rs

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