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