wow_dbc/vanilla_tables/
wmo_area_table.rs1use crate::{
2 DbcTable, Indexable, LocalizedString,
3};
4use crate::header::{
5 DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::vanilla_tables::area_table::AreaTableKey;
8use crate::vanilla_tables::sound_ambience::SoundAmbienceKey;
9use crate::vanilla_tables::sound_provider_preferences::SoundProviderPreferencesKey;
10use crate::vanilla_tables::zone_intro_music_table::ZoneIntroMusicTableKey;
11use crate::vanilla_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 != 80 {
33 return Err(crate::DbcError::InvalidHeader(
34 crate::InvalidHeaderError::RecordSize {
35 expected: 80,
36 actual: header.record_size,
37 },
38 ));
39 }
40
41 if header.field_count != 20 {
42 return Err(crate::DbcError::InvalidHeader(
43 crate::InvalidHeaderError::FieldCount {
44 expected: 20,
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_u32_le(chunk)?);
62
63 let wmo_id = crate::util::read_u32_le(chunk)?;
65
66 let name_set_id = crate::util::read_u32_le(chunk)?;
68
69 let wmo_group_id = crate::util::read_i32_le(chunk)?;
71
72 let sound_provider_preferences = SoundProviderPreferencesKey::new(crate::util::read_u32_le(chunk)?.into());
74
75 let sound_provider_preferences_underwater = SoundProviderPreferencesKey::new(crate::util::read_u32_le(chunk)?.into());
77
78 let sound_ambience = SoundAmbienceKey::new(crate::util::read_u32_le(chunk)?.into());
80
81 let zone_music = ZoneMusicKey::new(crate::util::read_u32_le(chunk)?.into());
83
84 let zone_intro_music = ZoneIntroMusicTableKey::new(crate::util::read_u32_le(chunk)?.into());
86
87 let flags = crate::util::read_u32_le(chunk)?;
89
90 let area_table = AreaTableKey::new(crate::util::read_u32_le(chunk)?.into());
92
93 let name = crate::util::read_localized_string(chunk, &string_block)?;
95
96
97 rows.push(WMOAreaTableRow {
98 id,
99 wmo_id,
100 name_set_id,
101 wmo_group_id,
102 sound_provider_preferences,
103 sound_provider_preferences_underwater,
104 sound_ambience,
105 zone_music,
106 zone_intro_music,
107 flags,
108 area_table,
109 name,
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: 20,
120 record_size: 80,
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.wmo_id.to_le_bytes())?;
133
134 b.write_all(&row.name_set_id.to_le_bytes())?;
136
137 b.write_all(&row.wmo_group_id.to_le_bytes())?;
139
140 b.write_all(&(row.sound_provider_preferences.id as u32).to_le_bytes())?;
142
143 b.write_all(&(row.sound_provider_preferences_underwater.id as u32).to_le_bytes())?;
145
146 b.write_all(&(row.sound_ambience.id as u32).to_le_bytes())?;
148
149 b.write_all(&(row.zone_music.id as u32).to_le_bytes())?;
151
152 b.write_all(&(row.zone_intro_music.id as u32).to_le_bytes())?;
154
155 b.write_all(&row.flags.to_le_bytes())?;
157
158 b.write_all(&(row.area_table.id as u32).to_le_bytes())?;
160
161 b.write_all(&row.name.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.name.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.name.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: u32
211}
212
213impl WMOAreaTableKey {
214 pub const fn new(id: u32) -> 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<u32> for WMOAreaTableKey {
233 fn from(v: u32) -> Self {
234 Self::new(v)
235 }
236}
237
238impl TryFrom<u64> for WMOAreaTableKey {
239 type Error = u64;
240 fn try_from(v: u64) -> Result<Self, Self::Error> {
241 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
242 }
243}
244
245impl TryFrom<usize> for WMOAreaTableKey {
246 type Error = usize;
247 fn try_from(v: usize) -> Result<Self, Self::Error> {
248 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
249 }
250}
251
252impl TryFrom<i8> for WMOAreaTableKey {
253 type Error = i8;
254 fn try_from(v: i8) -> Result<Self, Self::Error> {
255 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
256 }
257}
258
259impl TryFrom<i16> for WMOAreaTableKey {
260 type Error = i16;
261 fn try_from(v: i16) -> Result<Self, Self::Error> {
262 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
263 }
264}
265
266impl TryFrom<i32> for WMOAreaTableKey {
267 type Error = i32;
268 fn try_from(v: i32) -> Result<Self, Self::Error> {
269 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
270 }
271}
272
273impl TryFrom<i64> for WMOAreaTableKey {
274 type Error = i64;
275 fn try_from(v: i64) -> Result<Self, Self::Error> {
276 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
277 }
278}
279
280impl TryFrom<isize> for WMOAreaTableKey {
281 type Error = isize;
282 fn try_from(v: isize) -> Result<Self, Self::Error> {
283 Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
284 }
285}
286
287#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
288pub struct WMOAreaTableRow {
289 pub id: WMOAreaTableKey,
290 pub wmo_id: u32,
291 pub name_set_id: u32,
292 pub wmo_group_id: i32,
293 pub sound_provider_preferences: SoundProviderPreferencesKey,
294 pub sound_provider_preferences_underwater: SoundProviderPreferencesKey,
295 pub sound_ambience: SoundAmbienceKey,
296 pub zone_music: ZoneMusicKey,
297 pub zone_intro_music: ZoneIntroMusicTableKey,
298 pub flags: u32,
299 pub area_table: AreaTableKey,
300 pub name: LocalizedString,
301}
302