wow_dbc/wrath_tables/
chr_classes.rs1use crate::{
2 DbcTable, ExtendedLocalizedString, Indexable,
3};
4use crate::header::{
5 DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::wrath_tables::cinematic_sequences::CinematicSequencesKey;
8use std::io::Write;
9
10#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct ChrClasses {
12 pub rows: Vec<ChrClassesRow>,
13}
14
15impl DbcTable for ChrClasses {
16 type Row = ChrClassesRow;
17
18 const FILENAME: &'static str = "ChrClasses.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 != 240 {
29 return Err(crate::DbcError::InvalidHeader(
30 crate::InvalidHeaderError::RecordSize {
31 expected: 240,
32 actual: header.record_size,
33 },
34 ));
35 }
36
37 if header.field_count != 60 {
38 return Err(crate::DbcError::InvalidHeader(
39 crate::InvalidHeaderError::FieldCount {
40 expected: 60,
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 let id = ChrClassesKey::new(crate::util::read_i32_le(chunk)?);
58
59 let damage_bonus_stat = crate::util::read_i32_le(chunk)?;
61
62 let display_power = crate::util::read_i32_le(chunk)?;
64
65 let pet_name_token = {
67 let s = crate::util::get_string_as_vec(chunk, &string_block)?;
68 String::from_utf8(s)?
69 };
70
71 let name_lang = crate::util::read_extended_localized_string(chunk, &string_block)?;
73
74 let name_female_lang = crate::util::read_extended_localized_string(chunk, &string_block)?;
76
77 let name_male_lang = crate::util::read_extended_localized_string(chunk, &string_block)?;
79
80 let filename = {
82 let s = crate::util::get_string_as_vec(chunk, &string_block)?;
83 String::from_utf8(s)?
84 };
85
86 let spell_class_set = crate::util::read_i32_le(chunk)?;
88
89 let flags = crate::util::read_i32_le(chunk)?;
91
92 let cinematic_sequence_id = CinematicSequencesKey::new(crate::util::read_i32_le(chunk)?.into());
94
95 let required_expansion = crate::util::read_i32_le(chunk)?;
97
98
99 rows.push(ChrClassesRow {
100 id,
101 damage_bonus_stat,
102 display_power,
103 pet_name_token,
104 name_lang,
105 name_female_lang,
106 name_male_lang,
107 filename,
108 spell_class_set,
109 flags,
110 cinematic_sequence_id,
111 required_expansion,
112 });
113 }
114
115 Ok(ChrClasses { rows, })
116 }
117
118 fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
119 let header = DbcHeader {
120 record_count: self.rows.len() as u32,
121 field_count: 60,
122 record_size: 240,
123 string_block_size: self.string_block_size(),
124 };
125
126 b.write_all(&header.write_header())?;
127
128 let mut string_index = 1;
129 for row in &self.rows {
130 b.write_all(&row.id.id.to_le_bytes())?;
132
133 b.write_all(&row.damage_bonus_stat.to_le_bytes())?;
135
136 b.write_all(&row.display_power.to_le_bytes())?;
138
139 if !row.pet_name_token.is_empty() {
141 b.write_all(&(string_index as u32).to_le_bytes())?;
142 string_index += row.pet_name_token.len() + 1;
143 }
144 else {
145 b.write_all(&(0_u32).to_le_bytes())?;
146 }
147
148 b.write_all(&row.name_lang.string_indices_as_array(&mut string_index))?;
150
151 b.write_all(&row.name_female_lang.string_indices_as_array(&mut string_index))?;
153
154 b.write_all(&row.name_male_lang.string_indices_as_array(&mut string_index))?;
156
157 if !row.filename.is_empty() {
159 b.write_all(&(string_index as u32).to_le_bytes())?;
160 string_index += row.filename.len() + 1;
161 }
162 else {
163 b.write_all(&(0_u32).to_le_bytes())?;
164 }
165
166 b.write_all(&row.spell_class_set.to_le_bytes())?;
168
169 b.write_all(&row.flags.to_le_bytes())?;
171
172 b.write_all(&(row.cinematic_sequence_id.id as i32).to_le_bytes())?;
174
175 b.write_all(&row.required_expansion.to_le_bytes())?;
177
178 }
179
180 self.write_string_block(b)?;
181
182 Ok(())
183 }
184
185}
186
187impl Indexable for ChrClasses {
188 type PrimaryKey = ChrClassesKey;
189 fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
190 let key = key.try_into().ok()?;
191 self.rows.iter().find(|a| a.id.id == key.id)
192 }
193
194 fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
195 let key = key.try_into().ok()?;
196 self.rows.iter_mut().find(|a| a.id.id == key.id)
197 }
198}
199
200impl ChrClasses {
201 fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
202 b.write_all(&[0])?;
203
204 for row in &self.rows {
205 if !row.pet_name_token.is_empty() { b.write_all(row.pet_name_token.as_bytes())?; b.write_all(&[0])?; };
206 row.name_lang.string_block_as_array(b)?;
207 row.name_female_lang.string_block_as_array(b)?;
208 row.name_male_lang.string_block_as_array(b)?;
209 if !row.filename.is_empty() { b.write_all(row.filename.as_bytes())?; b.write_all(&[0])?; };
210 }
211
212 Ok(())
213 }
214
215 fn string_block_size(&self) -> u32 {
216 let mut sum = 1;
217 for row in &self.rows {
218 if !row.pet_name_token.is_empty() { sum += row.pet_name_token.len() + 1; };
219 sum += row.name_lang.string_block_size();
220 sum += row.name_female_lang.string_block_size();
221 sum += row.name_male_lang.string_block_size();
222 if !row.filename.is_empty() { sum += row.filename.len() + 1; };
223 }
224
225 sum as u32
226 }
227
228}
229
230#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
231pub struct ChrClassesKey {
232 pub id: i32
233}
234
235impl ChrClassesKey {
236 pub const fn new(id: i32) -> Self {
237 Self { id }
238 }
239
240}
241
242impl From<u8> for ChrClassesKey {
243 fn from(v: u8) -> Self {
244 Self::new(v.into())
245 }
246}
247
248impl From<u16> for ChrClassesKey {
249 fn from(v: u16) -> Self {
250 Self::new(v.into())
251 }
252}
253
254impl From<i8> for ChrClassesKey {
255 fn from(v: i8) -> Self {
256 Self::new(v.into())
257 }
258}
259
260impl From<i16> for ChrClassesKey {
261 fn from(v: i16) -> Self {
262 Self::new(v.into())
263 }
264}
265
266impl From<i32> for ChrClassesKey {
267 fn from(v: i32) -> Self {
268 Self::new(v)
269 }
270}
271
272impl TryFrom<u32> for ChrClassesKey {
273 type Error = u32;
274 fn try_from(v: u32) -> Result<Self, Self::Error> {
275 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
276 }
277}
278
279impl TryFrom<usize> for ChrClassesKey {
280 type Error = usize;
281 fn try_from(v: usize) -> Result<Self, Self::Error> {
282 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
283 }
284}
285
286impl TryFrom<u64> for ChrClassesKey {
287 type Error = u64;
288 fn try_from(v: u64) -> Result<Self, Self::Error> {
289 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
290 }
291}
292
293impl TryFrom<i64> for ChrClassesKey {
294 type Error = i64;
295 fn try_from(v: i64) -> Result<Self, Self::Error> {
296 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
297 }
298}
299
300impl TryFrom<isize> for ChrClassesKey {
301 type Error = isize;
302 fn try_from(v: isize) -> Result<Self, Self::Error> {
303 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
304 }
305}
306
307#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
308pub struct ChrClassesRow {
309 pub id: ChrClassesKey,
310 pub damage_bonus_stat: i32,
311 pub display_power: i32,
312 pub pet_name_token: String,
313 pub name_lang: ExtendedLocalizedString,
314 pub name_female_lang: ExtendedLocalizedString,
315 pub name_male_lang: ExtendedLocalizedString,
316 pub filename: String,
317 pub spell_class_set: i32,
318 pub flags: i32,
319 pub cinematic_sequence_id: CinematicSequencesKey,
320 pub required_expansion: i32,
321}
322