wow_dbc/vanilla_tables/
item_class.rs1use crate::{
2 DbcTable, Indexable, LocalizedString,
3};
4use crate::header::{
5 DbcHeader, HEADER_SIZE, parse_header,
6};
7use std::io::Write;
8use wow_world_base::vanilla::ItemWeaponClass;
9
10#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct ItemClass {
12 pub rows: Vec<ItemClassRow>,
13}
14
15impl DbcTable for ItemClass {
16 type Row = ItemClassRow;
17
18 const FILENAME: &'static str = "ItemClass.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 != 48 {
29 return Err(crate::DbcError::InvalidHeader(
30 crate::InvalidHeaderError::RecordSize {
31 expected: 48,
32 actual: header.record_size,
33 },
34 ));
35 }
36
37 if header.field_count != 12 {
38 return Err(crate::DbcError::InvalidHeader(
39 crate::InvalidHeaderError::FieldCount {
40 expected: 12,
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 = ItemClassKey::new(crate::util::read_u32_le(chunk)?);
58
59 let subclass_map = crate::util::read_u32_le(chunk)?;
61
62 let item_class = crate::util::read_i32_le(chunk)?.try_into()?;
64
65 let class_name = crate::util::read_localized_string(chunk, &string_block)?;
67
68
69 rows.push(ItemClassRow {
70 id,
71 subclass_map,
72 item_class,
73 class_name,
74 });
75 }
76
77 Ok(ItemClass { rows, })
78 }
79
80 fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
81 let header = DbcHeader {
82 record_count: self.rows.len() as u32,
83 field_count: 12,
84 record_size: 48,
85 string_block_size: self.string_block_size(),
86 };
87
88 b.write_all(&header.write_header())?;
89
90 let mut string_index = 1;
91 for row in &self.rows {
92 b.write_all(&row.id.id.to_le_bytes())?;
94
95 b.write_all(&row.subclass_map.to_le_bytes())?;
97
98 b.write_all(&(row.item_class.as_int() as i32).to_le_bytes())?;
100
101 b.write_all(&row.class_name.string_indices_as_array(&mut string_index))?;
103
104 }
105
106 self.write_string_block(b)?;
107
108 Ok(())
109 }
110
111}
112
113impl Indexable for ItemClass {
114 type PrimaryKey = ItemClassKey;
115 fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
116 let key = key.try_into().ok()?;
117 self.rows.iter().find(|a| a.id.id == key.id)
118 }
119
120 fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
121 let key = key.try_into().ok()?;
122 self.rows.iter_mut().find(|a| a.id.id == key.id)
123 }
124}
125
126impl ItemClass {
127 fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
128 b.write_all(&[0])?;
129
130 for row in &self.rows {
131 row.class_name.string_block_as_array(b)?;
132 }
133
134 Ok(())
135 }
136
137 fn string_block_size(&self) -> u32 {
138 let mut sum = 1;
139 for row in &self.rows {
140 sum += row.class_name.string_block_size();
141 }
142
143 sum as u32
144 }
145
146}
147
148#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
149pub struct ItemClassKey {
150 pub id: u32
151}
152
153impl ItemClassKey {
154 pub const fn new(id: u32) -> Self {
155 Self { id }
156 }
157
158}
159
160impl From<u8> for ItemClassKey {
161 fn from(v: u8) -> Self {
162 Self::new(v.into())
163 }
164}
165
166impl From<u16> for ItemClassKey {
167 fn from(v: u16) -> Self {
168 Self::new(v.into())
169 }
170}
171
172impl From<u32> for ItemClassKey {
173 fn from(v: u32) -> Self {
174 Self::new(v)
175 }
176}
177
178impl TryFrom<u64> for ItemClassKey {
179 type Error = u64;
180 fn try_from(v: u64) -> Result<Self, Self::Error> {
181 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
182 }
183}
184
185impl TryFrom<usize> for ItemClassKey {
186 type Error = usize;
187 fn try_from(v: usize) -> Result<Self, Self::Error> {
188 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
189 }
190}
191
192impl TryFrom<i8> for ItemClassKey {
193 type Error = i8;
194 fn try_from(v: i8) -> Result<Self, Self::Error> {
195 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
196 }
197}
198
199impl TryFrom<i16> for ItemClassKey {
200 type Error = i16;
201 fn try_from(v: i16) -> Result<Self, Self::Error> {
202 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
203 }
204}
205
206impl TryFrom<i32> for ItemClassKey {
207 type Error = i32;
208 fn try_from(v: i32) -> Result<Self, Self::Error> {
209 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
210 }
211}
212
213impl TryFrom<i64> for ItemClassKey {
214 type Error = i64;
215 fn try_from(v: i64) -> Result<Self, Self::Error> {
216 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
217 }
218}
219
220impl TryFrom<isize> for ItemClassKey {
221 type Error = isize;
222 fn try_from(v: isize) -> Result<Self, Self::Error> {
223 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
224 }
225}
226
227#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
228pub struct ItemClassRow {
229 pub id: ItemClassKey,
230 pub subclass_map: u32,
231 pub item_class: ItemWeaponClass,
232 pub class_name: LocalizedString,
233}
234