wow_dbc/vanilla_tables/
wow_error_strings.rs

1use crate::{
2    DbcTable, Indexable, LocalizedString,
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 != 44 {
29            return Err(crate::DbcError::InvalidHeader(
30                crate::InvalidHeaderError::RecordSize {
31                    expected: 44,
32                    actual: header.record_size,
33                },
34            ));
35        }
36
37        if header.field_count != 11 {
38            return Err(crate::DbcError::InvalidHeader(
39                crate::InvalidHeaderError::FieldCount {
40                    expected: 11,
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            // id: primary_key (WowError_Strings) uint32
57            let id = WowError_StringsKey::new(crate::util::read_u32_le(chunk)?);
58
59            // name: string_ref
60            let name = {
61                let s = crate::util::get_string_as_vec(chunk, &string_block)?;
62                String::from_utf8(s)?
63            };
64
65            // text: string_ref_loc
66            let text = crate::util::read_localized_string(chunk, &string_block)?;
67
68
69            rows.push(WowError_StringsRow {
70                id,
71                name,
72                text,
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: 11,
83            record_size: 44,
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            // id: primary_key (WowError_Strings) uint32
92            b.write_all(&row.id.id.to_le_bytes())?;
93
94            // name: string_ref
95            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            // text: string_ref_loc
104            b.write_all(&row.text.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.text.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.text.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: u32
156}
157
158impl WowError_StringsKey {
159    pub const fn new(id: u32) -> 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<u32> for WowError_StringsKey {
178    fn from(v: u32) -> Self {
179        Self::new(v)
180    }
181}
182
183impl TryFrom<u64> for WowError_StringsKey {
184    type Error = u64;
185    fn try_from(v: u64) -> Result<Self, Self::Error> {
186        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
187    }
188}
189
190impl TryFrom<usize> for WowError_StringsKey {
191    type Error = usize;
192    fn try_from(v: usize) -> Result<Self, Self::Error> {
193        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
194    }
195}
196
197impl TryFrom<i8> for WowError_StringsKey {
198    type Error = i8;
199    fn try_from(v: i8) -> Result<Self, Self::Error> {
200        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
201    }
202}
203
204impl TryFrom<i16> for WowError_StringsKey {
205    type Error = i16;
206    fn try_from(v: i16) -> Result<Self, Self::Error> {
207        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
208    }
209}
210
211impl TryFrom<i32> for WowError_StringsKey {
212    type Error = i32;
213    fn try_from(v: i32) -> Result<Self, Self::Error> {
214        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
215    }
216}
217
218impl TryFrom<i64> for WowError_StringsKey {
219    type Error = i64;
220    fn try_from(v: i64) -> Result<Self, Self::Error> {
221        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
222    }
223}
224
225impl TryFrom<isize> for WowError_StringsKey {
226    type Error = isize;
227    fn try_from(v: isize) -> Result<Self, Self::Error> {
228        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
229    }
230}
231
232#[allow(non_camel_case_types)]
233#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
234pub struct WowError_StringsRow {
235    pub id: WowError_StringsKey,
236    pub name: String,
237    pub text: LocalizedString,
238}
239