wow_dbc/tbc_tables/
spell_item_enchantment.rs1use crate::{
2 DbcTable, ExtendedLocalizedString, Indexable,
3};
4use crate::header::{
5 DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::tbc_tables::item_visuals::ItemVisualsKey;
8use crate::tbc_tables::spell_item_enchantment_condition::SpellItemEnchantmentConditionKey;
9use std::io::Write;
10
11#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub struct SpellItemEnchantment {
13 pub rows: Vec<SpellItemEnchantmentRow>,
14}
15
16impl DbcTable for SpellItemEnchantment {
17 type Row = SpellItemEnchantmentRow;
18
19 const FILENAME: &'static str = "SpellItemEnchantment.dbc";
20
21 fn rows(&self) -> &[Self::Row] { &self.rows }
22 fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }
23
24 fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
25 let mut header = [0_u8; HEADER_SIZE];
26 b.read_exact(&mut header)?;
27 let header = parse_header(&header)?;
28
29 if header.record_size != 136 {
30 return Err(crate::DbcError::InvalidHeader(
31 crate::InvalidHeaderError::RecordSize {
32 expected: 136,
33 actual: header.record_size,
34 },
35 ));
36 }
37
38 if header.field_count != 34 {
39 return Err(crate::DbcError::InvalidHeader(
40 crate::InvalidHeaderError::FieldCount {
41 expected: 34,
42 actual: header.field_count,
43 },
44 ));
45 }
46
47 let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
48 b.read_exact(&mut r)?;
49 let mut string_block = vec![0_u8; header.string_block_size as usize];
50 b.read_exact(&mut string_block)?;
51
52 let mut rows = Vec::with_capacity(header.record_count as usize);
53
54 for mut chunk in r.chunks(header.record_size as usize) {
55 let chunk = &mut chunk;
56
57 let id = SpellItemEnchantmentKey::new(crate::util::read_i32_le(chunk)?);
59
60 let effect = crate::util::read_array_i32::<3>(chunk)?;
62
63 let effect_points_min = crate::util::read_array_i32::<3>(chunk)?;
65
66 let effect_points_max = crate::util::read_array_i32::<3>(chunk)?;
68
69 let effect_arg = crate::util::read_array_i32::<3>(chunk)?;
71
72 let name_lang = crate::util::read_extended_localized_string(chunk, &string_block)?;
74
75 let item_visual = ItemVisualsKey::new(crate::util::read_i32_le(chunk)?.into());
77
78 let flags = crate::util::read_i32_le(chunk)?;
80
81 let src_item_id = crate::util::read_i32_le(chunk)?;
83
84 let condition_id = SpellItemEnchantmentConditionKey::new(crate::util::read_i32_le(chunk)?.into());
86
87
88 rows.push(SpellItemEnchantmentRow {
89 id,
90 effect,
91 effect_points_min,
92 effect_points_max,
93 effect_arg,
94 name_lang,
95 item_visual,
96 flags,
97 src_item_id,
98 condition_id,
99 });
100 }
101
102 Ok(SpellItemEnchantment { rows, })
103 }
104
105 fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
106 let header = DbcHeader {
107 record_count: self.rows.len() as u32,
108 field_count: 34,
109 record_size: 136,
110 string_block_size: self.string_block_size(),
111 };
112
113 b.write_all(&header.write_header())?;
114
115 let mut string_index = 1;
116 for row in &self.rows {
117 b.write_all(&row.id.id.to_le_bytes())?;
119
120 for i in row.effect {
122 b.write_all(&i.to_le_bytes())?;
123 }
124
125
126 for i in row.effect_points_min {
128 b.write_all(&i.to_le_bytes())?;
129 }
130
131
132 for i in row.effect_points_max {
134 b.write_all(&i.to_le_bytes())?;
135 }
136
137
138 for i in row.effect_arg {
140 b.write_all(&i.to_le_bytes())?;
141 }
142
143
144 b.write_all(&row.name_lang.string_indices_as_array(&mut string_index))?;
146
147 b.write_all(&(row.item_visual.id as i32).to_le_bytes())?;
149
150 b.write_all(&row.flags.to_le_bytes())?;
152
153 b.write_all(&row.src_item_id.to_le_bytes())?;
155
156 b.write_all(&(row.condition_id.id as i32).to_le_bytes())?;
158
159 }
160
161 self.write_string_block(b)?;
162
163 Ok(())
164 }
165
166}
167
168impl Indexable for SpellItemEnchantment {
169 type PrimaryKey = SpellItemEnchantmentKey;
170 fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
171 let key = key.try_into().ok()?;
172 self.rows.iter().find(|a| a.id.id == key.id)
173 }
174
175 fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
176 let key = key.try_into().ok()?;
177 self.rows.iter_mut().find(|a| a.id.id == key.id)
178 }
179}
180
181impl SpellItemEnchantment {
182 fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
183 b.write_all(&[0])?;
184
185 for row in &self.rows {
186 row.name_lang.string_block_as_array(b)?;
187 }
188
189 Ok(())
190 }
191
192 fn string_block_size(&self) -> u32 {
193 let mut sum = 1;
194 for row in &self.rows {
195 sum += row.name_lang.string_block_size();
196 }
197
198 sum as u32
199 }
200
201}
202
203#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
204pub struct SpellItemEnchantmentKey {
205 pub id: i32
206}
207
208impl SpellItemEnchantmentKey {
209 pub const fn new(id: i32) -> Self {
210 Self { id }
211 }
212
213}
214
215impl From<u8> for SpellItemEnchantmentKey {
216 fn from(v: u8) -> Self {
217 Self::new(v.into())
218 }
219}
220
221impl From<u16> for SpellItemEnchantmentKey {
222 fn from(v: u16) -> Self {
223 Self::new(v.into())
224 }
225}
226
227impl From<i8> for SpellItemEnchantmentKey {
228 fn from(v: i8) -> Self {
229 Self::new(v.into())
230 }
231}
232
233impl From<i16> for SpellItemEnchantmentKey {
234 fn from(v: i16) -> Self {
235 Self::new(v.into())
236 }
237}
238
239impl From<i32> for SpellItemEnchantmentKey {
240 fn from(v: i32) -> Self {
241 Self::new(v)
242 }
243}
244
245impl TryFrom<u32> for SpellItemEnchantmentKey {
246 type Error = u32;
247 fn try_from(v: u32) -> Result<Self, Self::Error> {
248 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
249 }
250}
251
252impl TryFrom<usize> for SpellItemEnchantmentKey {
253 type Error = usize;
254 fn try_from(v: usize) -> Result<Self, Self::Error> {
255 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
256 }
257}
258
259impl TryFrom<u64> for SpellItemEnchantmentKey {
260 type Error = u64;
261 fn try_from(v: u64) -> Result<Self, Self::Error> {
262 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
263 }
264}
265
266impl TryFrom<i64> for SpellItemEnchantmentKey {
267 type Error = i64;
268 fn try_from(v: i64) -> Result<Self, Self::Error> {
269 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
270 }
271}
272
273impl TryFrom<isize> for SpellItemEnchantmentKey {
274 type Error = isize;
275 fn try_from(v: isize) -> Result<Self, Self::Error> {
276 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
277 }
278}
279
280#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
281pub struct SpellItemEnchantmentRow {
282 pub id: SpellItemEnchantmentKey,
283 pub effect: [i32; 3],
284 pub effect_points_min: [i32; 3],
285 pub effect_points_max: [i32; 3],
286 pub effect_arg: [i32; 3],
287 pub name_lang: ExtendedLocalizedString,
288 pub item_visual: ItemVisualsKey,
289 pub flags: i32,
290 pub src_item_id: i32,
291 pub condition_id: SpellItemEnchantmentConditionKey,
292}
293