wow_dbc/vanilla_tables/
spell_range.rs

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