wow_dbc/tbc_tables/
item_set.rs

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