wow_dbc/vanilla_tables/
chat_profanity.rs

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