wow_dbc/wrath_tables/
package.rs1use crate::{
2 DbcTable, ExtendedLocalizedString, Indexable,
3};
4use crate::header::{
5 DbcHeader, HEADER_SIZE, parse_header,
6};
7use std::io::Write;
8
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct Package {
11 pub rows: Vec<PackageRow>,
12}
13
14impl DbcTable for Package {
15 type Row = PackageRow;
16
17 const FILENAME: &'static str = "Package.dbc";
18
19 fn rows(&self) -> &[Self::Row] { &self.rows }
20 fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }
21
22 fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
23 let mut header = [0_u8; HEADER_SIZE];
24 b.read_exact(&mut header)?;
25 let header = parse_header(&header)?;
26
27 if header.record_size != 80 {
28 return Err(crate::DbcError::InvalidHeader(
29 crate::InvalidHeaderError::RecordSize {
30 expected: 80,
31 actual: header.record_size,
32 },
33 ));
34 }
35
36 if header.field_count != 20 {
37 return Err(crate::DbcError::InvalidHeader(
38 crate::InvalidHeaderError::FieldCount {
39 expected: 20,
40 actual: header.field_count,
41 },
42 ));
43 }
44
45 let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
46 b.read_exact(&mut r)?;
47 let mut string_block = vec![0_u8; header.string_block_size as usize];
48 b.read_exact(&mut string_block)?;
49
50 let mut rows = Vec::with_capacity(header.record_count as usize);
51
52 for mut chunk in r.chunks(header.record_size as usize) {
53 let chunk = &mut chunk;
54
55 let id = PackageKey::new(crate::util::read_i32_le(chunk)?);
57
58 let icon = {
60 let s = crate::util::get_string_as_vec(chunk, &string_block)?;
61 String::from_utf8(s)?
62 };
63
64 let cost = crate::util::read_i32_le(chunk)?;
66
67 let name_lang = crate::util::read_extended_localized_string(chunk, &string_block)?;
69
70
71 rows.push(PackageRow {
72 id,
73 icon,
74 cost,
75 name_lang,
76 });
77 }
78
79 Ok(Package { rows, })
80 }
81
82 fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
83 let header = DbcHeader {
84 record_count: self.rows.len() as u32,
85 field_count: 20,
86 record_size: 80,
87 string_block_size: self.string_block_size(),
88 };
89
90 b.write_all(&header.write_header())?;
91
92 let mut string_index = 1;
93 for row in &self.rows {
94 b.write_all(&row.id.id.to_le_bytes())?;
96
97 if !row.icon.is_empty() {
99 b.write_all(&(string_index as u32).to_le_bytes())?;
100 string_index += row.icon.len() + 1;
101 }
102 else {
103 b.write_all(&(0_u32).to_le_bytes())?;
104 }
105
106 b.write_all(&row.cost.to_le_bytes())?;
108
109 b.write_all(&row.name_lang.string_indices_as_array(&mut string_index))?;
111
112 }
113
114 self.write_string_block(b)?;
115
116 Ok(())
117 }
118
119}
120
121impl Indexable for Package {
122 type PrimaryKey = PackageKey;
123 fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
124 let key = key.try_into().ok()?;
125 self.rows.iter().find(|a| a.id.id == key.id)
126 }
127
128 fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
129 let key = key.try_into().ok()?;
130 self.rows.iter_mut().find(|a| a.id.id == key.id)
131 }
132}
133
134impl Package {
135 fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
136 b.write_all(&[0])?;
137
138 for row in &self.rows {
139 if !row.icon.is_empty() { b.write_all(row.icon.as_bytes())?; b.write_all(&[0])?; };
140 row.name_lang.string_block_as_array(b)?;
141 }
142
143 Ok(())
144 }
145
146 fn string_block_size(&self) -> u32 {
147 let mut sum = 1;
148 for row in &self.rows {
149 if !row.icon.is_empty() { sum += row.icon.len() + 1; };
150 sum += row.name_lang.string_block_size();
151 }
152
153 sum as u32
154 }
155
156}
157
158#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
159pub struct PackageKey {
160 pub id: i32
161}
162
163impl PackageKey {
164 pub const fn new(id: i32) -> Self {
165 Self { id }
166 }
167
168}
169
170impl From<u8> for PackageKey {
171 fn from(v: u8) -> Self {
172 Self::new(v.into())
173 }
174}
175
176impl From<u16> for PackageKey {
177 fn from(v: u16) -> Self {
178 Self::new(v.into())
179 }
180}
181
182impl From<i8> for PackageKey {
183 fn from(v: i8) -> Self {
184 Self::new(v.into())
185 }
186}
187
188impl From<i16> for PackageKey {
189 fn from(v: i16) -> Self {
190 Self::new(v.into())
191 }
192}
193
194impl From<i32> for PackageKey {
195 fn from(v: i32) -> Self {
196 Self::new(v)
197 }
198}
199
200impl TryFrom<u32> for PackageKey {
201 type Error = u32;
202 fn try_from(v: u32) -> Result<Self, Self::Error> {
203 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
204 }
205}
206
207impl TryFrom<usize> for PackageKey {
208 type Error = usize;
209 fn try_from(v: usize) -> Result<Self, Self::Error> {
210 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
211 }
212}
213
214impl TryFrom<u64> for PackageKey {
215 type Error = u64;
216 fn try_from(v: u64) -> Result<Self, Self::Error> {
217 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
218 }
219}
220
221impl TryFrom<i64> for PackageKey {
222 type Error = i64;
223 fn try_from(v: i64) -> Result<Self, Self::Error> {
224 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
225 }
226}
227
228impl TryFrom<isize> for PackageKey {
229 type Error = isize;
230 fn try_from(v: isize) -> Result<Self, Self::Error> {
231 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
232 }
233}
234
235#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
236pub struct PackageRow {
237 pub id: PackageKey,
238 pub icon: String,
239 pub cost: i32,
240 pub name_lang: ExtendedLocalizedString,
241}
242