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