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