wow_dbc/wrath_tables/
scaling_stat_values.rs

1use crate::{
2    DbcTable, 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 ScalingStatValues {
11    pub rows: Vec<ScalingStatValuesRow>,
12}
13
14impl DbcTable for ScalingStatValues {
15    type Row = ScalingStatValuesRow;
16
17    const FILENAME: &'static str = "ScalingStatValues.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 != 96 {
28            return Err(crate::DbcError::InvalidHeader(
29                crate::InvalidHeaderError::RecordSize {
30                    expected: 96,
31                    actual: header.record_size,
32                },
33            ));
34        }
35
36        if header.field_count != 24 {
37            return Err(crate::DbcError::InvalidHeader(
38                crate::InvalidHeaderError::FieldCount {
39                    expected: 24,
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
48        let mut rows = Vec::with_capacity(header.record_count as usize);
49
50        for mut chunk in r.chunks(header.record_size as usize) {
51            let chunk = &mut chunk;
52
53            // id: primary_key (ScalingStatValues) int32
54            let id = ScalingStatValuesKey::new(crate::util::read_i32_le(chunk)?);
55
56            // charlevel: int32
57            let charlevel = crate::util::read_i32_le(chunk)?;
58
59            // shoulder_budget: int32
60            let shoulder_budget = crate::util::read_i32_le(chunk)?;
61
62            // trinket_budget: int32
63            let trinket_budget = crate::util::read_i32_le(chunk)?;
64
65            // weapon_budget1_h: int32
66            let weapon_budget1_h = crate::util::read_i32_le(chunk)?;
67
68            // ranged_budget: int32
69            let ranged_budget = crate::util::read_i32_le(chunk)?;
70
71            // cloth_shoulder_armor: int32
72            let cloth_shoulder_armor = crate::util::read_i32_le(chunk)?;
73
74            // leather_shoulder_armor: int32
75            let leather_shoulder_armor = crate::util::read_i32_le(chunk)?;
76
77            // mail_shoulder_armor: int32
78            let mail_shoulder_armor = crate::util::read_i32_le(chunk)?;
79
80            // plate_shoulder_armor: int32
81            let plate_shoulder_armor = crate::util::read_i32_le(chunk)?;
82
83            // weapon_d_p_s1_h: int32
84            let weapon_d_p_s1_h = crate::util::read_i32_le(chunk)?;
85
86            // weapon_d_p_s2_h: int32
87            let weapon_d_p_s2_h = crate::util::read_i32_le(chunk)?;
88
89            // spellcaster_d_p_s1_h: int32
90            let spellcaster_d_p_s1_h = crate::util::read_i32_le(chunk)?;
91
92            // spellcaster_d_p_s2_h: int32
93            let spellcaster_d_p_s2_h = crate::util::read_i32_le(chunk)?;
94
95            // ranged_d_p_s: int32
96            let ranged_d_p_s = crate::util::read_i32_le(chunk)?;
97
98            // wand_d_p_s: int32
99            let wand_d_p_s = crate::util::read_i32_le(chunk)?;
100
101            // spell_power: int32
102            let spell_power = crate::util::read_i32_le(chunk)?;
103
104            // primary_budget: int32
105            let primary_budget = crate::util::read_i32_le(chunk)?;
106
107            // tertiary_budget: int32
108            let tertiary_budget = crate::util::read_i32_le(chunk)?;
109
110            // cloth_cloak_armor: int32
111            let cloth_cloak_armor = crate::util::read_i32_le(chunk)?;
112
113            // cloth_chest_armor: int32
114            let cloth_chest_armor = crate::util::read_i32_le(chunk)?;
115
116            // leather_chest_armor: int32
117            let leather_chest_armor = crate::util::read_i32_le(chunk)?;
118
119            // mail_chest_armor: int32
120            let mail_chest_armor = crate::util::read_i32_le(chunk)?;
121
122            // plate_chest_armor: int32
123            let plate_chest_armor = crate::util::read_i32_le(chunk)?;
124
125
126            rows.push(ScalingStatValuesRow {
127                id,
128                charlevel,
129                shoulder_budget,
130                trinket_budget,
131                weapon_budget1_h,
132                ranged_budget,
133                cloth_shoulder_armor,
134                leather_shoulder_armor,
135                mail_shoulder_armor,
136                plate_shoulder_armor,
137                weapon_d_p_s1_h,
138                weapon_d_p_s2_h,
139                spellcaster_d_p_s1_h,
140                spellcaster_d_p_s2_h,
141                ranged_d_p_s,
142                wand_d_p_s,
143                spell_power,
144                primary_budget,
145                tertiary_budget,
146                cloth_cloak_armor,
147                cloth_chest_armor,
148                leather_chest_armor,
149                mail_chest_armor,
150                plate_chest_armor,
151            });
152        }
153
154        Ok(ScalingStatValues { rows, })
155    }
156
157    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
158        let header = DbcHeader {
159            record_count: self.rows.len() as u32,
160            field_count: 24,
161            record_size: 96,
162            string_block_size: 1,
163        };
164
165        b.write_all(&header.write_header())?;
166
167        for row in &self.rows {
168            // id: primary_key (ScalingStatValues) int32
169            b.write_all(&row.id.id.to_le_bytes())?;
170
171            // charlevel: int32
172            b.write_all(&row.charlevel.to_le_bytes())?;
173
174            // shoulder_budget: int32
175            b.write_all(&row.shoulder_budget.to_le_bytes())?;
176
177            // trinket_budget: int32
178            b.write_all(&row.trinket_budget.to_le_bytes())?;
179
180            // weapon_budget1_h: int32
181            b.write_all(&row.weapon_budget1_h.to_le_bytes())?;
182
183            // ranged_budget: int32
184            b.write_all(&row.ranged_budget.to_le_bytes())?;
185
186            // cloth_shoulder_armor: int32
187            b.write_all(&row.cloth_shoulder_armor.to_le_bytes())?;
188
189            // leather_shoulder_armor: int32
190            b.write_all(&row.leather_shoulder_armor.to_le_bytes())?;
191
192            // mail_shoulder_armor: int32
193            b.write_all(&row.mail_shoulder_armor.to_le_bytes())?;
194
195            // plate_shoulder_armor: int32
196            b.write_all(&row.plate_shoulder_armor.to_le_bytes())?;
197
198            // weapon_d_p_s1_h: int32
199            b.write_all(&row.weapon_d_p_s1_h.to_le_bytes())?;
200
201            // weapon_d_p_s2_h: int32
202            b.write_all(&row.weapon_d_p_s2_h.to_le_bytes())?;
203
204            // spellcaster_d_p_s1_h: int32
205            b.write_all(&row.spellcaster_d_p_s1_h.to_le_bytes())?;
206
207            // spellcaster_d_p_s2_h: int32
208            b.write_all(&row.spellcaster_d_p_s2_h.to_le_bytes())?;
209
210            // ranged_d_p_s: int32
211            b.write_all(&row.ranged_d_p_s.to_le_bytes())?;
212
213            // wand_d_p_s: int32
214            b.write_all(&row.wand_d_p_s.to_le_bytes())?;
215
216            // spell_power: int32
217            b.write_all(&row.spell_power.to_le_bytes())?;
218
219            // primary_budget: int32
220            b.write_all(&row.primary_budget.to_le_bytes())?;
221
222            // tertiary_budget: int32
223            b.write_all(&row.tertiary_budget.to_le_bytes())?;
224
225            // cloth_cloak_armor: int32
226            b.write_all(&row.cloth_cloak_armor.to_le_bytes())?;
227
228            // cloth_chest_armor: int32
229            b.write_all(&row.cloth_chest_armor.to_le_bytes())?;
230
231            // leather_chest_armor: int32
232            b.write_all(&row.leather_chest_armor.to_le_bytes())?;
233
234            // mail_chest_armor: int32
235            b.write_all(&row.mail_chest_armor.to_le_bytes())?;
236
237            // plate_chest_armor: int32
238            b.write_all(&row.plate_chest_armor.to_le_bytes())?;
239
240        }
241
242        b.write_all(&[0_u8])?;
243
244        Ok(())
245    }
246
247}
248
249impl Indexable for ScalingStatValues {
250    type PrimaryKey = ScalingStatValuesKey;
251    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
252        let key = key.try_into().ok()?;
253        self.rows.iter().find(|a| a.id.id == key.id)
254    }
255
256    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
257        let key = key.try_into().ok()?;
258        self.rows.iter_mut().find(|a| a.id.id == key.id)
259    }
260}
261
262#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
263pub struct ScalingStatValuesKey {
264    pub id: i32
265}
266
267impl ScalingStatValuesKey {
268    pub const fn new(id: i32) -> Self {
269        Self { id }
270    }
271
272}
273
274impl From<u8> for ScalingStatValuesKey {
275    fn from(v: u8) -> Self {
276        Self::new(v.into())
277    }
278}
279
280impl From<u16> for ScalingStatValuesKey {
281    fn from(v: u16) -> Self {
282        Self::new(v.into())
283    }
284}
285
286impl From<i8> for ScalingStatValuesKey {
287    fn from(v: i8) -> Self {
288        Self::new(v.into())
289    }
290}
291
292impl From<i16> for ScalingStatValuesKey {
293    fn from(v: i16) -> Self {
294        Self::new(v.into())
295    }
296}
297
298impl From<i32> for ScalingStatValuesKey {
299    fn from(v: i32) -> Self {
300        Self::new(v)
301    }
302}
303
304impl TryFrom<u32> for ScalingStatValuesKey {
305    type Error = u32;
306    fn try_from(v: u32) -> Result<Self, Self::Error> {
307        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
308    }
309}
310
311impl TryFrom<usize> for ScalingStatValuesKey {
312    type Error = usize;
313    fn try_from(v: usize) -> Result<Self, Self::Error> {
314        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
315    }
316}
317
318impl TryFrom<u64> for ScalingStatValuesKey {
319    type Error = u64;
320    fn try_from(v: u64) -> Result<Self, Self::Error> {
321        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
322    }
323}
324
325impl TryFrom<i64> for ScalingStatValuesKey {
326    type Error = i64;
327    fn try_from(v: i64) -> Result<Self, Self::Error> {
328        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
329    }
330}
331
332impl TryFrom<isize> for ScalingStatValuesKey {
333    type Error = isize;
334    fn try_from(v: isize) -> Result<Self, Self::Error> {
335        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
336    }
337}
338
339#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
340pub struct ScalingStatValuesRow {
341    pub id: ScalingStatValuesKey,
342    pub charlevel: i32,
343    pub shoulder_budget: i32,
344    pub trinket_budget: i32,
345    pub weapon_budget1_h: i32,
346    pub ranged_budget: i32,
347    pub cloth_shoulder_armor: i32,
348    pub leather_shoulder_armor: i32,
349    pub mail_shoulder_armor: i32,
350    pub plate_shoulder_armor: i32,
351    pub weapon_d_p_s1_h: i32,
352    pub weapon_d_p_s2_h: i32,
353    pub spellcaster_d_p_s1_h: i32,
354    pub spellcaster_d_p_s2_h: i32,
355    pub ranged_d_p_s: i32,
356    pub wand_d_p_s: i32,
357    pub spell_power: i32,
358    pub primary_budget: i32,
359    pub tertiary_budget: i32,
360    pub cloth_cloak_armor: i32,
361    pub cloth_chest_armor: i32,
362    pub leather_chest_armor: i32,
363    pub mail_chest_armor: i32,
364    pub plate_chest_armor: i32,
365}
366