wow_dbc/tbc_tables/
auction_house.rs

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