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