wow_dbc/wrath_tables/
world_chunk_sounds.rs

1use crate::{
2    DbcTable, Indexable,
3};
4use crate::header::{
5    DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::wrath_tables::sound_ambience::SoundAmbienceKey;
8use crate::wrath_tables::sound_provider_preferences::SoundProviderPreferencesKey;
9use crate::wrath_tables::zone_music::ZoneMusicKey;
10use std::io::Write;
11
12#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub struct WorldChunkSounds {
14    pub rows: Vec<WorldChunkSoundsRow>,
15}
16
17impl DbcTable for WorldChunkSounds {
18    type Row = WorldChunkSoundsRow;
19
20    const FILENAME: &'static str = "WorldChunkSounds.dbc";
21
22    fn rows(&self) -> &[Self::Row] { &self.rows }
23    fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }
24
25    fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
26        let mut header = [0_u8; HEADER_SIZE];
27        b.read_exact(&mut header)?;
28        let header = parse_header(&header)?;
29
30        if header.record_size != 36 {
31            return Err(crate::DbcError::InvalidHeader(
32                crate::InvalidHeaderError::RecordSize {
33                    expected: 36,
34                    actual: header.record_size,
35                },
36            ));
37        }
38
39        if header.field_count != 9 {
40            return Err(crate::DbcError::InvalidHeader(
41                crate::InvalidHeaderError::FieldCount {
42                    expected: 9,
43                    actual: header.field_count,
44                },
45            ));
46        }
47
48        let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
49        b.read_exact(&mut r)?;
50
51        let mut rows = Vec::with_capacity(header.record_count as usize);
52
53        for mut chunk in r.chunks(header.record_size as usize) {
54            let chunk = &mut chunk;
55
56            // id: primary_key (WorldChunkSounds) int32
57            let id = WorldChunkSoundsKey::new(crate::util::read_i32_le(chunk)?);
58
59            // chunk_x: int32
60            let chunk_x = crate::util::read_i32_le(chunk)?;
61
62            // chunk_y: int32
63            let chunk_y = crate::util::read_i32_le(chunk)?;
64
65            // subchunk_x: int32
66            let subchunk_x = crate::util::read_i32_le(chunk)?;
67
68            // subchunk_y: int32
69            let subchunk_y = crate::util::read_i32_le(chunk)?;
70
71            // zone_intro_music_id: int32
72            let zone_intro_music_id = crate::util::read_i32_le(chunk)?;
73
74            // zone_music_id: foreign_key (ZoneMusic) int32
75            let zone_music_id = ZoneMusicKey::new(crate::util::read_i32_le(chunk)?.into());
76
77            // sound_ambience_id: foreign_key (SoundAmbience) int32
78            let sound_ambience_id = SoundAmbienceKey::new(crate::util::read_i32_le(chunk)?.into());
79
80            // sound_provider_preferences_id: foreign_key (SoundProviderPreferences) int32
81            let sound_provider_preferences_id = SoundProviderPreferencesKey::new(crate::util::read_i32_le(chunk)?.into());
82
83
84            rows.push(WorldChunkSoundsRow {
85                id,
86                chunk_x,
87                chunk_y,
88                subchunk_x,
89                subchunk_y,
90                zone_intro_music_id,
91                zone_music_id,
92                sound_ambience_id,
93                sound_provider_preferences_id,
94            });
95        }
96
97        Ok(WorldChunkSounds { rows, })
98    }
99
100    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
101        let header = DbcHeader {
102            record_count: self.rows.len() as u32,
103            field_count: 9,
104            record_size: 36,
105            string_block_size: 1,
106        };
107
108        b.write_all(&header.write_header())?;
109
110        for row in &self.rows {
111            // id: primary_key (WorldChunkSounds) int32
112            b.write_all(&row.id.id.to_le_bytes())?;
113
114            // chunk_x: int32
115            b.write_all(&row.chunk_x.to_le_bytes())?;
116
117            // chunk_y: int32
118            b.write_all(&row.chunk_y.to_le_bytes())?;
119
120            // subchunk_x: int32
121            b.write_all(&row.subchunk_x.to_le_bytes())?;
122
123            // subchunk_y: int32
124            b.write_all(&row.subchunk_y.to_le_bytes())?;
125
126            // zone_intro_music_id: int32
127            b.write_all(&row.zone_intro_music_id.to_le_bytes())?;
128
129            // zone_music_id: foreign_key (ZoneMusic) int32
130            b.write_all(&(row.zone_music_id.id as i32).to_le_bytes())?;
131
132            // sound_ambience_id: foreign_key (SoundAmbience) int32
133            b.write_all(&(row.sound_ambience_id.id as i32).to_le_bytes())?;
134
135            // sound_provider_preferences_id: foreign_key (SoundProviderPreferences) int32
136            b.write_all(&(row.sound_provider_preferences_id.id as i32).to_le_bytes())?;
137
138        }
139
140        b.write_all(&[0_u8])?;
141
142        Ok(())
143    }
144
145}
146
147impl Indexable for WorldChunkSounds {
148    type PrimaryKey = WorldChunkSoundsKey;
149    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
150        let key = key.try_into().ok()?;
151        self.rows.iter().find(|a| a.id.id == key.id)
152    }
153
154    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
155        let key = key.try_into().ok()?;
156        self.rows.iter_mut().find(|a| a.id.id == key.id)
157    }
158}
159
160#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
161pub struct WorldChunkSoundsKey {
162    pub id: i32
163}
164
165impl WorldChunkSoundsKey {
166    pub const fn new(id: i32) -> Self {
167        Self { id }
168    }
169
170}
171
172impl From<u8> for WorldChunkSoundsKey {
173    fn from(v: u8) -> Self {
174        Self::new(v.into())
175    }
176}
177
178impl From<u16> for WorldChunkSoundsKey {
179    fn from(v: u16) -> Self {
180        Self::new(v.into())
181    }
182}
183
184impl From<i8> for WorldChunkSoundsKey {
185    fn from(v: i8) -> Self {
186        Self::new(v.into())
187    }
188}
189
190impl From<i16> for WorldChunkSoundsKey {
191    fn from(v: i16) -> Self {
192        Self::new(v.into())
193    }
194}
195
196impl From<i32> for WorldChunkSoundsKey {
197    fn from(v: i32) -> Self {
198        Self::new(v)
199    }
200}
201
202impl TryFrom<u32> for WorldChunkSoundsKey {
203    type Error = u32;
204    fn try_from(v: u32) -> Result<Self, Self::Error> {
205        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
206    }
207}
208
209impl TryFrom<usize> for WorldChunkSoundsKey {
210    type Error = usize;
211    fn try_from(v: usize) -> Result<Self, Self::Error> {
212        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
213    }
214}
215
216impl TryFrom<u64> for WorldChunkSoundsKey {
217    type Error = u64;
218    fn try_from(v: u64) -> Result<Self, Self::Error> {
219        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
220    }
221}
222
223impl TryFrom<i64> for WorldChunkSoundsKey {
224    type Error = i64;
225    fn try_from(v: i64) -> Result<Self, Self::Error> {
226        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
227    }
228}
229
230impl TryFrom<isize> for WorldChunkSoundsKey {
231    type Error = isize;
232    fn try_from(v: isize) -> Result<Self, Self::Error> {
233        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
234    }
235}
236
237#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
238pub struct WorldChunkSoundsRow {
239    pub id: WorldChunkSoundsKey,
240    pub chunk_x: i32,
241    pub chunk_y: i32,
242    pub subchunk_x: i32,
243    pub subchunk_y: i32,
244    pub zone_intro_music_id: i32,
245    pub zone_music_id: ZoneMusicKey,
246    pub sound_ambience_id: SoundAmbienceKey,
247    pub sound_provider_preferences_id: SoundProviderPreferencesKey,
248}
249