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