wow_dbc/tbc_tables/
creature_display_info_extra.rs

1use crate::{
2    DbcTable, Indexable,
3};
4use crate::header::{
5    DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::tbc_tables::chr_races::ChrRacesKey;
8use std::io::Write;
9
10#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct CreatureDisplayInfoExtra {
12    pub rows: Vec<CreatureDisplayInfoExtraRow>,
13}
14
15impl DbcTable for CreatureDisplayInfoExtra {
16    type Row = CreatureDisplayInfoExtraRow;
17
18    const FILENAME: &'static str = "CreatureDisplayInfoExtra.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 (CreatureDisplayInfoExtra) int32
57            let id = CreatureDisplayInfoExtraKey::new(crate::util::read_i32_le(chunk)?);
58
59            // display_race_id: foreign_key (ChrRaces) int32
60            let display_race_id = ChrRacesKey::new(crate::util::read_i32_le(chunk)?.into());
61
62            // display_sex_id: int32
63            let display_sex_id = crate::util::read_i32_le(chunk)?;
64
65            // skin_id: int32
66            let skin_id = crate::util::read_i32_le(chunk)?;
67
68            // face_id: int32
69            let face_id = crate::util::read_i32_le(chunk)?;
70
71            // hair_style_id: int32
72            let hair_style_id = crate::util::read_i32_le(chunk)?;
73
74            // hair_color_id: int32
75            let hair_color_id = crate::util::read_i32_le(chunk)?;
76
77            // facial_hair_id: int32
78            let facial_hair_id = crate::util::read_i32_le(chunk)?;
79
80            // n_p_c_item_display: int32[11]
81            let n_p_c_item_display = crate::util::read_array_i32::<11>(chunk)?;
82
83            // flags: int32
84            let flags = crate::util::read_i32_le(chunk)?;
85
86            // bake_name: string_ref
87            let bake_name = {
88                let s = crate::util::get_string_as_vec(chunk, &string_block)?;
89                String::from_utf8(s)?
90            };
91
92
93            rows.push(CreatureDisplayInfoExtraRow {
94                id,
95                display_race_id,
96                display_sex_id,
97                skin_id,
98                face_id,
99                hair_style_id,
100                hair_color_id,
101                facial_hair_id,
102                n_p_c_item_display,
103                flags,
104                bake_name,
105            });
106        }
107
108        Ok(CreatureDisplayInfoExtra { rows, })
109    }
110
111    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
112        let header = DbcHeader {
113            record_count: self.rows.len() as u32,
114            field_count: 21,
115            record_size: 84,
116            string_block_size: self.string_block_size(),
117        };
118
119        b.write_all(&header.write_header())?;
120
121        let mut string_index = 1;
122        for row in &self.rows {
123            // id: primary_key (CreatureDisplayInfoExtra) int32
124            b.write_all(&row.id.id.to_le_bytes())?;
125
126            // display_race_id: foreign_key (ChrRaces) int32
127            b.write_all(&(row.display_race_id.id as i32).to_le_bytes())?;
128
129            // display_sex_id: int32
130            b.write_all(&row.display_sex_id.to_le_bytes())?;
131
132            // skin_id: int32
133            b.write_all(&row.skin_id.to_le_bytes())?;
134
135            // face_id: int32
136            b.write_all(&row.face_id.to_le_bytes())?;
137
138            // hair_style_id: int32
139            b.write_all(&row.hair_style_id.to_le_bytes())?;
140
141            // hair_color_id: int32
142            b.write_all(&row.hair_color_id.to_le_bytes())?;
143
144            // facial_hair_id: int32
145            b.write_all(&row.facial_hair_id.to_le_bytes())?;
146
147            // n_p_c_item_display: int32[11]
148            for i in row.n_p_c_item_display {
149                b.write_all(&i.to_le_bytes())?;
150            }
151
152
153            // flags: int32
154            b.write_all(&row.flags.to_le_bytes())?;
155
156            // bake_name: string_ref
157            if !row.bake_name.is_empty() {
158                b.write_all(&(string_index as u32).to_le_bytes())?;
159                string_index += row.bake_name.len() + 1;
160            }
161            else {
162                b.write_all(&(0_u32).to_le_bytes())?;
163            }
164
165        }
166
167        self.write_string_block(b)?;
168
169        Ok(())
170    }
171
172}
173
174impl Indexable for CreatureDisplayInfoExtra {
175    type PrimaryKey = CreatureDisplayInfoExtraKey;
176    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
177        let key = key.try_into().ok()?;
178        self.rows.iter().find(|a| a.id.id == key.id)
179    }
180
181    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
182        let key = key.try_into().ok()?;
183        self.rows.iter_mut().find(|a| a.id.id == key.id)
184    }
185}
186
187impl CreatureDisplayInfoExtra {
188    fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
189        b.write_all(&[0])?;
190
191        for row in &self.rows {
192            if !row.bake_name.is_empty() { b.write_all(row.bake_name.as_bytes())?; b.write_all(&[0])?; };
193        }
194
195        Ok(())
196    }
197
198    fn string_block_size(&self) -> u32 {
199        let mut sum = 1;
200        for row in &self.rows {
201            if !row.bake_name.is_empty() { sum += row.bake_name.len() + 1; };
202        }
203
204        sum as u32
205    }
206
207}
208
209#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
210pub struct CreatureDisplayInfoExtraKey {
211    pub id: i32
212}
213
214impl CreatureDisplayInfoExtraKey {
215    pub const fn new(id: i32) -> Self {
216        Self { id }
217    }
218
219}
220
221impl From<u8> for CreatureDisplayInfoExtraKey {
222    fn from(v: u8) -> Self {
223        Self::new(v.into())
224    }
225}
226
227impl From<u16> for CreatureDisplayInfoExtraKey {
228    fn from(v: u16) -> Self {
229        Self::new(v.into())
230    }
231}
232
233impl From<i8> for CreatureDisplayInfoExtraKey {
234    fn from(v: i8) -> Self {
235        Self::new(v.into())
236    }
237}
238
239impl From<i16> for CreatureDisplayInfoExtraKey {
240    fn from(v: i16) -> Self {
241        Self::new(v.into())
242    }
243}
244
245impl From<i32> for CreatureDisplayInfoExtraKey {
246    fn from(v: i32) -> Self {
247        Self::new(v)
248    }
249}
250
251impl TryFrom<u32> for CreatureDisplayInfoExtraKey {
252    type Error = u32;
253    fn try_from(v: u32) -> Result<Self, Self::Error> {
254        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
255    }
256}
257
258impl TryFrom<usize> for CreatureDisplayInfoExtraKey {
259    type Error = usize;
260    fn try_from(v: usize) -> Result<Self, Self::Error> {
261        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
262    }
263}
264
265impl TryFrom<u64> for CreatureDisplayInfoExtraKey {
266    type Error = u64;
267    fn try_from(v: u64) -> Result<Self, Self::Error> {
268        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
269    }
270}
271
272impl TryFrom<i64> for CreatureDisplayInfoExtraKey {
273    type Error = i64;
274    fn try_from(v: i64) -> Result<Self, Self::Error> {
275        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
276    }
277}
278
279impl TryFrom<isize> for CreatureDisplayInfoExtraKey {
280    type Error = isize;
281    fn try_from(v: isize) -> Result<Self, Self::Error> {
282        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
283    }
284}
285
286#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
287pub struct CreatureDisplayInfoExtraRow {
288    pub id: CreatureDisplayInfoExtraKey,
289    pub display_race_id: ChrRacesKey,
290    pub display_sex_id: i32,
291    pub skin_id: i32,
292    pub face_id: i32,
293    pub hair_style_id: i32,
294    pub hair_color_id: i32,
295    pub facial_hair_id: i32,
296    pub n_p_c_item_display: [i32; 11],
297    pub flags: i32,
298    pub bake_name: String,
299}
300