wow_dbc/wrath_tables/
spell_range.rs

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