wow_dbc/vanilla_tables/
char_start_outfit.rs

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