wow_dbc/wrath_tables/
world_map_overlay.rs1use crate::{
2 DbcTable, Indexable,
3};
4use crate::header::{
5 DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::wrath_tables::world_map_area::WorldMapAreaKey;
8use std::io::Write;
9
10#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct WorldMapOverlay {
12 pub rows: Vec<WorldMapOverlayRow>,
13}
14
15impl DbcTable for WorldMapOverlay {
16 type Row = WorldMapOverlayRow;
17
18 const FILENAME: &'static str = "WorldMapOverlay.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 != 68 {
29 return Err(crate::DbcError::InvalidHeader(
30 crate::InvalidHeaderError::RecordSize {
31 expected: 68,
32 actual: header.record_size,
33 },
34 ));
35 }
36
37 if header.field_count != 17 {
38 return Err(crate::DbcError::InvalidHeader(
39 crate::InvalidHeaderError::FieldCount {
40 expected: 17,
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 = WorldMapOverlayKey::new(crate::util::read_i32_le(chunk)?);
58
59 let map_area_id = WorldMapAreaKey::new(crate::util::read_i32_le(chunk)?.into());
61
62 let area_id = crate::util::read_array_i32::<4>(chunk)?;
64
65 let map_point_x = crate::util::read_i32_le(chunk)?;
67
68 let map_point_y = crate::util::read_i32_le(chunk)?;
70
71 let texture_name = {
73 let s = crate::util::get_string_as_vec(chunk, &string_block)?;
74 String::from_utf8(s)?
75 };
76
77 let texture_width = crate::util::read_i32_le(chunk)?;
79
80 let texture_height = crate::util::read_i32_le(chunk)?;
82
83 let offset_x = crate::util::read_i32_le(chunk)?;
85
86 let offset_y = crate::util::read_i32_le(chunk)?;
88
89 let hit_rect_top = crate::util::read_i32_le(chunk)?;
91
92 let hit_rect_left = crate::util::read_i32_le(chunk)?;
94
95 let hit_rect_bottom = crate::util::read_i32_le(chunk)?;
97
98 let hit_rect_right = crate::util::read_i32_le(chunk)?;
100
101
102 rows.push(WorldMapOverlayRow {
103 id,
104 map_area_id,
105 area_id,
106 map_point_x,
107 map_point_y,
108 texture_name,
109 texture_width,
110 texture_height,
111 offset_x,
112 offset_y,
113 hit_rect_top,
114 hit_rect_left,
115 hit_rect_bottom,
116 hit_rect_right,
117 });
118 }
119
120 Ok(WorldMapOverlay { rows, })
121 }
122
123 fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
124 let header = DbcHeader {
125 record_count: self.rows.len() as u32,
126 field_count: 17,
127 record_size: 68,
128 string_block_size: self.string_block_size(),
129 };
130
131 b.write_all(&header.write_header())?;
132
133 let mut string_index = 1;
134 for row in &self.rows {
135 b.write_all(&row.id.id.to_le_bytes())?;
137
138 b.write_all(&(row.map_area_id.id as i32).to_le_bytes())?;
140
141 for i in row.area_id {
143 b.write_all(&i.to_le_bytes())?;
144 }
145
146
147 b.write_all(&row.map_point_x.to_le_bytes())?;
149
150 b.write_all(&row.map_point_y.to_le_bytes())?;
152
153 if !row.texture_name.is_empty() {
155 b.write_all(&(string_index as u32).to_le_bytes())?;
156 string_index += row.texture_name.len() + 1;
157 }
158 else {
159 b.write_all(&(0_u32).to_le_bytes())?;
160 }
161
162 b.write_all(&row.texture_width.to_le_bytes())?;
164
165 b.write_all(&row.texture_height.to_le_bytes())?;
167
168 b.write_all(&row.offset_x.to_le_bytes())?;
170
171 b.write_all(&row.offset_y.to_le_bytes())?;
173
174 b.write_all(&row.hit_rect_top.to_le_bytes())?;
176
177 b.write_all(&row.hit_rect_left.to_le_bytes())?;
179
180 b.write_all(&row.hit_rect_bottom.to_le_bytes())?;
182
183 b.write_all(&row.hit_rect_right.to_le_bytes())?;
185
186 }
187
188 self.write_string_block(b)?;
189
190 Ok(())
191 }
192
193}
194
195impl Indexable for WorldMapOverlay {
196 type PrimaryKey = WorldMapOverlayKey;
197 fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
198 let key = key.try_into().ok()?;
199 self.rows.iter().find(|a| a.id.id == key.id)
200 }
201
202 fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
203 let key = key.try_into().ok()?;
204 self.rows.iter_mut().find(|a| a.id.id == key.id)
205 }
206}
207
208impl WorldMapOverlay {
209 fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
210 b.write_all(&[0])?;
211
212 for row in &self.rows {
213 if !row.texture_name.is_empty() { b.write_all(row.texture_name.as_bytes())?; b.write_all(&[0])?; };
214 }
215
216 Ok(())
217 }
218
219 fn string_block_size(&self) -> u32 {
220 let mut sum = 1;
221 for row in &self.rows {
222 if !row.texture_name.is_empty() { sum += row.texture_name.len() + 1; };
223 }
224
225 sum as u32
226 }
227
228}
229
230#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
231pub struct WorldMapOverlayKey {
232 pub id: i32
233}
234
235impl WorldMapOverlayKey {
236 pub const fn new(id: i32) -> Self {
237 Self { id }
238 }
239
240}
241
242impl From<u8> for WorldMapOverlayKey {
243 fn from(v: u8) -> Self {
244 Self::new(v.into())
245 }
246}
247
248impl From<u16> for WorldMapOverlayKey {
249 fn from(v: u16) -> Self {
250 Self::new(v.into())
251 }
252}
253
254impl From<i8> for WorldMapOverlayKey {
255 fn from(v: i8) -> Self {
256 Self::new(v.into())
257 }
258}
259
260impl From<i16> for WorldMapOverlayKey {
261 fn from(v: i16) -> Self {
262 Self::new(v.into())
263 }
264}
265
266impl From<i32> for WorldMapOverlayKey {
267 fn from(v: i32) -> Self {
268 Self::new(v)
269 }
270}
271
272impl TryFrom<u32> for WorldMapOverlayKey {
273 type Error = u32;
274 fn try_from(v: u32) -> Result<Self, Self::Error> {
275 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
276 }
277}
278
279impl TryFrom<usize> for WorldMapOverlayKey {
280 type Error = usize;
281 fn try_from(v: usize) -> Result<Self, Self::Error> {
282 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
283 }
284}
285
286impl TryFrom<u64> for WorldMapOverlayKey {
287 type Error = u64;
288 fn try_from(v: u64) -> Result<Self, Self::Error> {
289 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
290 }
291}
292
293impl TryFrom<i64> for WorldMapOverlayKey {
294 type Error = i64;
295 fn try_from(v: i64) -> Result<Self, Self::Error> {
296 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
297 }
298}
299
300impl TryFrom<isize> for WorldMapOverlayKey {
301 type Error = isize;
302 fn try_from(v: isize) -> Result<Self, Self::Error> {
303 Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
304 }
305}
306
307#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
308pub struct WorldMapOverlayRow {
309 pub id: WorldMapOverlayKey,
310 pub map_area_id: WorldMapAreaKey,
311 pub area_id: [i32; 4],
312 pub map_point_x: i32,
313 pub map_point_y: i32,
314 pub texture_name: String,
315 pub texture_width: i32,
316 pub texture_height: i32,
317 pub offset_x: i32,
318 pub offset_y: i32,
319 pub hit_rect_top: i32,
320 pub hit_rect_left: i32,
321 pub hit_rect_bottom: i32,
322 pub hit_rect_right: i32,
323}
324