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