wow_dbc/wrath_tables/
declined_word_cases.rs

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