wow_dbc/wrath_tables/
wmo_area_table.rs1use crate::{
2 DbcTable, ExtendedLocalizedString, Indexable,
3};
4use crate::header::{
5 DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::wrath_tables::area_table::AreaTableKey;
8use crate::wrath_tables::sound_ambience::SoundAmbienceKey;
9use crate::wrath_tables::sound_provider_preferences::SoundProviderPreferencesKey;
10use crate::wrath_tables::zone_intro_music_table::ZoneIntroMusicTableKey;
11use crate::wrath_tables::zone_music::ZoneMusicKey;
12use std::io::Write;
13
14#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
15pub struct WMOAreaTable {
16 pub rows: Vec<WMOAreaTableRow>,
17}
18
19impl DbcTable for WMOAreaTable {
20 type Row = WMOAreaTableRow;
21
22 const FILENAME: &'static str = "WMOAreaTable.dbc";
23
24 fn rows(&self) -> &[Self::Row] { &self.rows }
25 fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }
26
27 fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
28 let mut header = [0_u8; HEADER_SIZE];
29 b.read_exact(&mut header)?;
30 let header = parse_header(&header)?;
31
32 if header.record_size != 112 {
33 return Err(crate::DbcError::InvalidHeader(
34 crate::InvalidHeaderError::RecordSize {
35 expected: 112,
36 actual: header.record_size,
37 },
38 ));
39 }
40
41 if header.field_count != 28 {
42 return Err(crate::DbcError::InvalidHeader(
43 crate::InvalidHeaderError::FieldCount {
44 expected: 28,
45 actual: header.field_count,
46 },
47 ));
48 }
49
50 let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
51 b.read_exact(&mut r)?;
52 let mut string_block = vec![0_u8; header.string_block_size as usize];
53 b.read_exact(&mut string_block)?;
54
55 let mut rows = Vec::with_capacity(header.record_count as usize);
56
57 for mut chunk in r.chunks(header.record_size as usize) {
58 let chunk = &mut chunk;
59
60 let id = WMOAreaTableKey::new(crate::util::read_i32_le(chunk)?);
62
63 let w_m_o_id = crate::util::read_i32_le(chunk)?;
65
66 let name_set_id = crate::util::read_i32_le(chunk)?;
68
69 let w_m_o_group_id = crate::util::read_i32_le(chunk)?;
71
72 let sound_provider_pref = SoundProviderPreferencesKey::new(crate::util::read_i32_le(chunk)?.into());
74
75 let sound_provider_pref_underwater = SoundProviderPreferencesKey::new(crate::util::read_i32_le(chunk)?.into());
77
78 let ambience_id = SoundAmbienceKey::new(crate::util::read_i32_le(chunk)?.into());
80
81 let zone_music = ZoneMusicKey::new(crate::util::read_i32_le(chunk)?.into());
83
84 let intro_sound = ZoneIntroMusicTableKey::new(crate::util::read_i32_le(chunk)?.into());
86
87 let flags = crate::util::read_i32_le(chunk)?;
89
90 let area_table_id = AreaTableKey::new(crate::util::read_i32_le(chunk)?.into());
92
93 let area_name_lang = crate::util::read_extended_localized_string(chunk, &string_block)?;
95
96
97 rows.push(WMOAreaTableRow {
98 id,
99 w_m_o_id,
100 name_set_id,
101 w_m_o_group_id,
102 sound_provider_pref,
103 sound_provider_pref_underwater,
104 ambience_id,
105 zone_music,
106 intro_sound,
107 flags,
108 area_table_id,
109 area_name_lang,
110 });
111 }
112
113 Ok(WMOAreaTable { rows, })
114 }
115
116 fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
117 let header = DbcHeader {
118 record_count: self.rows.len() as u32,
119 field_count: 28,
120 record_size: 112,
121 string_block_size: self.string_block_size(),
122 };
123
124 b.write_all(&header.write_header())?;
125
126 let mut string_index = 1;
127 for row in &self.rows {
128 b.write_all(&row.id.id.to_le_bytes())?;
130
131 b.write_all(&row.w_m_o_id.to_le_bytes())?;
133
134 b.write_all(&row.name_set_id.to_le_bytes())?;
136
137 b.write_all(&row.w_m_o_group_id.to_le_bytes())?;
139
140 b.write_all(&(row.sound_provider_pref.id as i32).to_le_bytes())?;
142
143 b.write_all(&(row.sound_provider_pref_underwater.id as i32).to_le_bytes())?;
145
146 b.write_all(&(row.ambience_id.id as i32).to_le_bytes())?;
148
149 b.write_all(&(row.zone_music.id as i32).to_le_bytes())?;
151
152 b.write_all(&(row.intro_sound.id as i32).to_le_bytes())?;
154
155 b.write_all(&row.flags.to_le_bytes())?;
157
158 b.write_all(&(row.area_table_id.id as i32).to_le_bytes())?;
160
161 b.write_all(&row.area_name_lang.string_indices_as_array(&mut string_index))?;
163
164 }
165
166 self.write_string_block(b)?;
167
168 Ok(())
169 }
170
171}
172
173impl Indexable for WMOAreaTable {
174 type PrimaryKey = WMOAreaTableKey;
175 fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
176 let key = key.try_into().ok()?;
177 self.rows.iter().find(|a| a.id.id == key.id)
178 }
179
180 fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
181 let key = key.try_into().ok()?;
182 self.rows.iter_mut().find(|a| a.id.id == key.id)
183 }
184}
185
186impl WMOAreaTable {
187 fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
188 b.write_all(&[0])?;
189
190 for row in &self.rows {
191 row.area_name_lang.string_block_as_array(b)?;
192 }
193
194 Ok(())
195 }
196
197 fn string_block_size(&self) -> u32 {
198 let mut sum = 1;
199 for row in &self.rows {
200 sum += row.area_name_lang.string_block_size();
201 }
202
203 sum as u32
204 }
205
206}
207
208#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
209pub struct WMOAreaTableKey {
210 pub id: i32
211}
212
213impl WMOAreaTableKey {
214 pub const fn new(id: i32) -> Self {
215 Self { id }
216 }
217
218}
219
220impl From<u8> for WMOAreaTableKey {
221 fn from(v: u8) -> Self {
222 Self::new(v.into())
223 }
224}
225
226impl From<u16> for WMOAreaTableKey {
227 fn from(v: u16) -> Self {
228 Self::new(v.into())
229 }
230}
231
232impl From<i8> for WMOAreaTableKey {
233 fn from(v: i8) -> Self {
234 Self::new(v.into())
235 }
236}
237
238impl From<i16> for WMOAreaTableKey {
239 fn from(v: i16) -> Self {
240 Self::new(v.into())
241 }
242}
243
244impl From<i32> for WMOAreaTableKey {
245 fn from(v: i32) -> Self {
246 Self::new(v)
247 }
248}
249
250impl TryFrom<u32> for WMOAreaTableKey {
251 type Error = u32;
252 fn try_from(v: u32) -> Result<Self, Self::Error> {
253 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
254 }
255}
256
257impl TryFrom<usize> for WMOAreaTableKey {
258 type Error = usize;
259 fn try_from(v: usize) -> Result<Self, Self::Error> {
260 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
261 }
262}
263
264impl TryFrom<u64> for WMOAreaTableKey {
265 type Error = u64;
266 fn try_from(v: u64) -> Result<Self, Self::Error> {
267 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
268 }
269}
270
271impl TryFrom<i64> for WMOAreaTableKey {
272 type Error = i64;
273 fn try_from(v: i64) -> Result<Self, Self::Error> {
274 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
275 }
276}
277
278impl TryFrom<isize> for WMOAreaTableKey {
279 type Error = isize;
280 fn try_from(v: isize) -> Result<Self, Self::Error> {
281 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
282 }
283}
284
285#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
286pub struct WMOAreaTableRow {
287 pub id: WMOAreaTableKey,
288 pub w_m_o_id: i32,
289 pub name_set_id: i32,
290 pub w_m_o_group_id: i32,
291 pub sound_provider_pref: SoundProviderPreferencesKey,
292 pub sound_provider_pref_underwater: SoundProviderPreferencesKey,
293 pub ambience_id: SoundAmbienceKey,
294 pub zone_music: ZoneMusicKey,
295 pub intro_sound: ZoneIntroMusicTableKey,
296 pub flags: i32,
297 pub area_table_id: AreaTableKey,
298 pub area_name_lang: ExtendedLocalizedString,
299}
300