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