wow_dbc/tbc_tables/
world_map_area.rs

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