wow_dbc/tbc_tables/
talent_tab.rs1use crate::{
2 DbcTable, ExtendedLocalizedString, Indexable,
3};
4use crate::header::{
5 DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::tbc_tables::spell_icon::SpellIconKey;
8use std::io::Write;
9
10#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct TalentTab {
12 pub rows: Vec<TalentTabRow>,
13}
14
15impl DbcTable for TalentTab {
16 type Row = TalentTabRow;
17
18 const FILENAME: &'static str = "TalentTab.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 != 92 {
29 return Err(crate::DbcError::InvalidHeader(
30 crate::InvalidHeaderError::RecordSize {
31 expected: 92,
32 actual: header.record_size,
33 },
34 ));
35 }
36
37 if header.field_count != 23 {
38 return Err(crate::DbcError::InvalidHeader(
39 crate::InvalidHeaderError::FieldCount {
40 expected: 23,
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 = TalentTabKey::new(crate::util::read_i32_le(chunk)?);
58
59 let name_lang = crate::util::read_extended_localized_string(chunk, &string_block)?;
61
62 let spell_icon_id = SpellIconKey::new(crate::util::read_i32_le(chunk)?.into());
64
65 let race_mask = crate::util::read_i32_le(chunk)?;
67
68 let class_mask = crate::util::read_i32_le(chunk)?;
70
71 let order_index = crate::util::read_i32_le(chunk)?;
73
74 let background_file = {
76 let s = crate::util::get_string_as_vec(chunk, &string_block)?;
77 String::from_utf8(s)?
78 };
79
80
81 rows.push(TalentTabRow {
82 id,
83 name_lang,
84 spell_icon_id,
85 race_mask,
86 class_mask,
87 order_index,
88 background_file,
89 });
90 }
91
92 Ok(TalentTab { 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: 23,
99 record_size: 92,
100 string_block_size: self.string_block_size(),
101 };
102
103 b.write_all(&header.write_header())?;
104
105 let mut string_index = 1;
106 for row in &self.rows {
107 b.write_all(&row.id.id.to_le_bytes())?;
109
110 b.write_all(&row.name_lang.string_indices_as_array(&mut string_index))?;
112
113 b.write_all(&(row.spell_icon_id.id as i32).to_le_bytes())?;
115
116 b.write_all(&row.race_mask.to_le_bytes())?;
118
119 b.write_all(&row.class_mask.to_le_bytes())?;
121
122 b.write_all(&row.order_index.to_le_bytes())?;
124
125 if !row.background_file.is_empty() {
127 b.write_all(&(string_index as u32).to_le_bytes())?;
128 string_index += row.background_file.len() + 1;
129 }
130 else {
131 b.write_all(&(0_u32).to_le_bytes())?;
132 }
133
134 }
135
136 self.write_string_block(b)?;
137
138 Ok(())
139 }
140
141}
142
143impl Indexable for TalentTab {
144 type PrimaryKey = TalentTabKey;
145 fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
146 let key = key.try_into().ok()?;
147 self.rows.iter().find(|a| a.id.id == key.id)
148 }
149
150 fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
151 let key = key.try_into().ok()?;
152 self.rows.iter_mut().find(|a| a.id.id == key.id)
153 }
154}
155
156impl TalentTab {
157 fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
158 b.write_all(&[0])?;
159
160 for row in &self.rows {
161 row.name_lang.string_block_as_array(b)?;
162 if !row.background_file.is_empty() { b.write_all(row.background_file.as_bytes())?; b.write_all(&[0])?; };
163 }
164
165 Ok(())
166 }
167
168 fn string_block_size(&self) -> u32 {
169 let mut sum = 1;
170 for row in &self.rows {
171 sum += row.name_lang.string_block_size();
172 if !row.background_file.is_empty() { sum += row.background_file.len() + 1; };
173 }
174
175 sum as u32
176 }
177
178}
179
180#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
181pub struct TalentTabKey {
182 pub id: i32
183}
184
185impl TalentTabKey {
186 pub const fn new(id: i32) -> Self {
187 Self { id }
188 }
189
190}
191
192impl From<u8> for TalentTabKey {
193 fn from(v: u8) -> Self {
194 Self::new(v.into())
195 }
196}
197
198impl From<u16> for TalentTabKey {
199 fn from(v: u16) -> Self {
200 Self::new(v.into())
201 }
202}
203
204impl From<i8> for TalentTabKey {
205 fn from(v: i8) -> Self {
206 Self::new(v.into())
207 }
208}
209
210impl From<i16> for TalentTabKey {
211 fn from(v: i16) -> Self {
212 Self::new(v.into())
213 }
214}
215
216impl From<i32> for TalentTabKey {
217 fn from(v: i32) -> Self {
218 Self::new(v)
219 }
220}
221
222impl TryFrom<u32> for TalentTabKey {
223 type Error = u32;
224 fn try_from(v: u32) -> Result<Self, Self::Error> {
225 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
226 }
227}
228
229impl TryFrom<usize> for TalentTabKey {
230 type Error = usize;
231 fn try_from(v: usize) -> Result<Self, Self::Error> {
232 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
233 }
234}
235
236impl TryFrom<u64> for TalentTabKey {
237 type Error = u64;
238 fn try_from(v: u64) -> Result<Self, Self::Error> {
239 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
240 }
241}
242
243impl TryFrom<i64> for TalentTabKey {
244 type Error = i64;
245 fn try_from(v: i64) -> Result<Self, Self::Error> {
246 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
247 }
248}
249
250impl TryFrom<isize> for TalentTabKey {
251 type Error = isize;
252 fn try_from(v: isize) -> Result<Self, Self::Error> {
253 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
254 }
255}
256
257#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
258pub struct TalentTabRow {
259 pub id: TalentTabKey,
260 pub name_lang: ExtendedLocalizedString,
261 pub spell_icon_id: SpellIconKey,
262 pub race_mask: i32,
263 pub class_mask: i32,
264 pub order_index: i32,
265 pub background_file: String,
266}
267