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