wow_dbc/wrath_tables/
creature_family.rs

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