wow_dbc/vanilla_tables/
taxi_path_node.rs

1use crate::{
2    DbcTable, Indexable,
3};
4use crate::header::{
5    DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::vanilla_tables::map::MapKey;
8use crate::vanilla_tables::taxi_path::TaxiPathKey;
9use std::io::Write;
10
11#[derive(Debug, Clone, PartialEq, PartialOrd)]
12pub struct TaxiPathNode {
13    pub rows: Vec<TaxiPathNodeRow>,
14}
15
16impl DbcTable for TaxiPathNode {
17    type Row = TaxiPathNodeRow;
18
19    const FILENAME: &'static str = "TaxiPathNode.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
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            // id: primary_key (TaxiPathNode) uint32
56            let id = TaxiPathNodeKey::new(crate::util::read_u32_le(chunk)?);
57
58            // taxi_path: foreign_key (TaxiPath) uint32
59            let taxi_path = TaxiPathKey::new(crate::util::read_u32_le(chunk)?.into());
60
61            // node_index: int32
62            let node_index = crate::util::read_i32_le(chunk)?;
63
64            // map: foreign_key (Map) uint32
65            let map = MapKey::new(crate::util::read_u32_le(chunk)?.into());
66
67            // location_x: float
68            let location_x = crate::util::read_f32_le(chunk)?;
69
70            // location_y: float
71            let location_y = crate::util::read_f32_le(chunk)?;
72
73            // location_z: float
74            let location_z = crate::util::read_f32_le(chunk)?;
75
76            // flags: int32
77            let flags = crate::util::read_i32_le(chunk)?;
78
79            // delay: int32
80            let delay = crate::util::read_i32_le(chunk)?;
81
82
83            rows.push(TaxiPathNodeRow {
84                id,
85                taxi_path,
86                node_index,
87                map,
88                location_x,
89                location_y,
90                location_z,
91                flags,
92                delay,
93            });
94        }
95
96        Ok(TaxiPathNode { rows, })
97    }
98
99    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
100        let header = DbcHeader {
101            record_count: self.rows.len() as u32,
102            field_count: 9,
103            record_size: 36,
104            string_block_size: 1,
105        };
106
107        b.write_all(&header.write_header())?;
108
109        for row in &self.rows {
110            // id: primary_key (TaxiPathNode) uint32
111            b.write_all(&row.id.id.to_le_bytes())?;
112
113            // taxi_path: foreign_key (TaxiPath) uint32
114            b.write_all(&(row.taxi_path.id as u32).to_le_bytes())?;
115
116            // node_index: int32
117            b.write_all(&row.node_index.to_le_bytes())?;
118
119            // map: foreign_key (Map) uint32
120            b.write_all(&(row.map.id as u32).to_le_bytes())?;
121
122            // location_x: float
123            b.write_all(&row.location_x.to_le_bytes())?;
124
125            // location_y: float
126            b.write_all(&row.location_y.to_le_bytes())?;
127
128            // location_z: float
129            b.write_all(&row.location_z.to_le_bytes())?;
130
131            // flags: int32
132            b.write_all(&row.flags.to_le_bytes())?;
133
134            // delay: int32
135            b.write_all(&row.delay.to_le_bytes())?;
136
137        }
138
139        b.write_all(&[0_u8])?;
140
141        Ok(())
142    }
143
144}
145
146impl Indexable for TaxiPathNode {
147    type PrimaryKey = TaxiPathNodeKey;
148    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
149        let key = key.try_into().ok()?;
150        self.rows.iter().find(|a| a.id.id == key.id)
151    }
152
153    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
154        let key = key.try_into().ok()?;
155        self.rows.iter_mut().find(|a| a.id.id == key.id)
156    }
157}
158
159#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
160pub struct TaxiPathNodeKey {
161    pub id: u32
162}
163
164impl TaxiPathNodeKey {
165    pub const fn new(id: u32) -> Self {
166        Self { id }
167    }
168
169}
170
171impl From<u8> for TaxiPathNodeKey {
172    fn from(v: u8) -> Self {
173        Self::new(v.into())
174    }
175}
176
177impl From<u16> for TaxiPathNodeKey {
178    fn from(v: u16) -> Self {
179        Self::new(v.into())
180    }
181}
182
183impl From<u32> for TaxiPathNodeKey {
184    fn from(v: u32) -> Self {
185        Self::new(v)
186    }
187}
188
189impl TryFrom<u64> for TaxiPathNodeKey {
190    type Error = u64;
191    fn try_from(v: u64) -> Result<Self, Self::Error> {
192        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
193    }
194}
195
196impl TryFrom<usize> for TaxiPathNodeKey {
197    type Error = usize;
198    fn try_from(v: usize) -> Result<Self, Self::Error> {
199        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
200    }
201}
202
203impl TryFrom<i8> for TaxiPathNodeKey {
204    type Error = i8;
205    fn try_from(v: i8) -> Result<Self, Self::Error> {
206        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
207    }
208}
209
210impl TryFrom<i16> for TaxiPathNodeKey {
211    type Error = i16;
212    fn try_from(v: i16) -> Result<Self, Self::Error> {
213        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
214    }
215}
216
217impl TryFrom<i32> for TaxiPathNodeKey {
218    type Error = i32;
219    fn try_from(v: i32) -> Result<Self, Self::Error> {
220        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
221    }
222}
223
224impl TryFrom<i64> for TaxiPathNodeKey {
225    type Error = i64;
226    fn try_from(v: i64) -> Result<Self, Self::Error> {
227        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
228    }
229}
230
231impl TryFrom<isize> for TaxiPathNodeKey {
232    type Error = isize;
233    fn try_from(v: isize) -> Result<Self, Self::Error> {
234        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
235    }
236}
237
238#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
239pub struct TaxiPathNodeRow {
240    pub id: TaxiPathNodeKey,
241    pub taxi_path: TaxiPathKey,
242    pub node_index: i32,
243    pub map: MapKey,
244    pub location_x: f32,
245    pub location_y: f32,
246    pub location_z: f32,
247    pub flags: i32,
248    pub delay: i32,
249}
250