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