wow_dbc/vanilla_tables/
skill_line.rs1use crate::{
2 DbcTable, Indexable, LocalizedString,
3};
4use crate::header::{
5 DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::vanilla_tables::skill_costs_data::SkillCostsDataKey;
8use crate::vanilla_tables::skill_line_category::SkillLineCategoryKey;
9use crate::vanilla_tables::spell_icon::SpellIconKey;
10use std::io::Write;
11
12#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub struct SkillLine {
14 pub rows: Vec<SkillLineRow>,
15}
16
17impl DbcTable for SkillLine {
18 type Row = SkillLineRow;
19
20 const FILENAME: &'static str = "SkillLine.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 != 88 {
31 return Err(crate::DbcError::InvalidHeader(
32 crate::InvalidHeaderError::RecordSize {
33 expected: 88,
34 actual: header.record_size,
35 },
36 ));
37 }
38
39 if header.field_count != 22 {
40 return Err(crate::DbcError::InvalidHeader(
41 crate::InvalidHeaderError::FieldCount {
42 expected: 22,
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 let mut string_block = vec![0_u8; header.string_block_size as usize];
51 b.read_exact(&mut string_block)?;
52
53 let mut rows = Vec::with_capacity(header.record_count as usize);
54
55 for mut chunk in r.chunks(header.record_size as usize) {
56 let chunk = &mut chunk;
57
58 let id = SkillLineKey::new(crate::util::read_u32_le(chunk)?);
60
61 let category = SkillLineCategoryKey::new(crate::util::read_u32_le(chunk)?.into());
63
64 let skill_costs = SkillCostsDataKey::new(crate::util::read_u32_le(chunk)?.into());
66
67 let display_name = crate::util::read_localized_string(chunk, &string_block)?;
69
70 let description = crate::util::read_localized_string(chunk, &string_block)?;
72
73 let spell_icon = SpellIconKey::new(crate::util::read_u32_le(chunk)?.into());
75
76
77 rows.push(SkillLineRow {
78 id,
79 category,
80 skill_costs,
81 display_name,
82 description,
83 spell_icon,
84 });
85 }
86
87 Ok(SkillLine { rows, })
88 }
89
90 fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
91 let header = DbcHeader {
92 record_count: self.rows.len() as u32,
93 field_count: 22,
94 record_size: 88,
95 string_block_size: self.string_block_size(),
96 };
97
98 b.write_all(&header.write_header())?;
99
100 let mut string_index = 1;
101 for row in &self.rows {
102 b.write_all(&row.id.id.to_le_bytes())?;
104
105 b.write_all(&(row.category.id as u32).to_le_bytes())?;
107
108 b.write_all(&(row.skill_costs.id as u32).to_le_bytes())?;
110
111 b.write_all(&row.display_name.string_indices_as_array(&mut string_index))?;
113
114 b.write_all(&row.description.string_indices_as_array(&mut string_index))?;
116
117 b.write_all(&(row.spell_icon.id as u32).to_le_bytes())?;
119
120 }
121
122 self.write_string_block(b)?;
123
124 Ok(())
125 }
126
127}
128
129impl Indexable for SkillLine {
130 type PrimaryKey = SkillLineKey;
131 fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
132 let key = key.try_into().ok()?;
133 self.rows.iter().find(|a| a.id.id == key.id)
134 }
135
136 fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
137 let key = key.try_into().ok()?;
138 self.rows.iter_mut().find(|a| a.id.id == key.id)
139 }
140}
141
142impl SkillLine {
143 fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
144 b.write_all(&[0])?;
145
146 for row in &self.rows {
147 row.display_name.string_block_as_array(b)?;
148 row.description.string_block_as_array(b)?;
149 }
150
151 Ok(())
152 }
153
154 fn string_block_size(&self) -> u32 {
155 let mut sum = 1;
156 for row in &self.rows {
157 sum += row.display_name.string_block_size();
158 sum += row.description.string_block_size();
159 }
160
161 sum as u32
162 }
163
164}
165
166#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
167pub struct SkillLineKey {
168 pub id: u32
169}
170
171impl SkillLineKey {
172 pub const fn new(id: u32) -> Self {
173 Self { id }
174 }
175
176}
177
178impl From<u8> for SkillLineKey {
179 fn from(v: u8) -> Self {
180 Self::new(v.into())
181 }
182}
183
184impl From<u16> for SkillLineKey {
185 fn from(v: u16) -> Self {
186 Self::new(v.into())
187 }
188}
189
190impl From<u32> for SkillLineKey {
191 fn from(v: u32) -> Self {
192 Self::new(v)
193 }
194}
195
196impl TryFrom<u64> for SkillLineKey {
197 type Error = u64;
198 fn try_from(v: u64) -> Result<Self, Self::Error> {
199 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
200 }
201}
202
203impl TryFrom<usize> for SkillLineKey {
204 type Error = usize;
205 fn try_from(v: usize) -> Result<Self, Self::Error> {
206 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
207 }
208}
209
210impl TryFrom<i8> for SkillLineKey {
211 type Error = i8;
212 fn try_from(v: i8) -> Result<Self, Self::Error> {
213 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
214 }
215}
216
217impl TryFrom<i16> for SkillLineKey {
218 type Error = i16;
219 fn try_from(v: i16) -> Result<Self, Self::Error> {
220 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
221 }
222}
223
224impl TryFrom<i32> for SkillLineKey {
225 type Error = i32;
226 fn try_from(v: i32) -> Result<Self, Self::Error> {
227 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
228 }
229}
230
231impl TryFrom<i64> for SkillLineKey {
232 type Error = i64;
233 fn try_from(v: i64) -> Result<Self, Self::Error> {
234 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
235 }
236}
237
238impl TryFrom<isize> for SkillLineKey {
239 type Error = isize;
240 fn try_from(v: isize) -> Result<Self, Self::Error> {
241 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
242 }
243}
244
245#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
246pub struct SkillLineRow {
247 pub id: SkillLineKey,
248 pub category: SkillLineCategoryKey,
249 pub skill_costs: SkillCostsDataKey,
250 pub display_name: LocalizedString,
251 pub description: LocalizedString,
252 pub spell_icon: SpellIconKey,
253}
254