wow_dbc/tbc_tables/
char_start_outfit.rs

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