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