wow_dbc/tbc_tables/
taxi_path_node.rs

1use crate::{
2    DbcTable, Indexable,
3};
4use crate::header::{
5    DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::tbc_tables::map::MapKey;
8use crate::tbc_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 != 44 {
30            return Err(crate::DbcError::InvalidHeader(
31                crate::InvalidHeaderError::RecordSize {
32                    expected: 44,
33                    actual: header.record_size,
34                },
35            ));
36        }
37
38        if header.field_count != 11 {
39            return Err(crate::DbcError::InvalidHeader(
40                crate::InvalidHeaderError::FieldCount {
41                    expected: 11,
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) int32
56            let id = TaxiPathNodeKey::new(crate::util::read_i32_le(chunk)?);
57
58            // path_id: foreign_key (TaxiPath) int32
59            let path_id = TaxiPathKey::new(crate::util::read_i32_le(chunk)?.into());
60
61            // node_index: int32
62            let node_index = crate::util::read_i32_le(chunk)?;
63
64            // continent_id: foreign_key (Map) int32
65            let continent_id = MapKey::new(crate::util::read_i32_le(chunk)?.into());
66
67            // loc: float[3]
68            let loc = crate::util::read_array_f32::<3>(chunk)?;
69
70            // flags: int32
71            let flags = crate::util::read_i32_le(chunk)?;
72
73            // delay: int32
74            let delay = crate::util::read_i32_le(chunk)?;
75
76            // arrival_event_id: int32
77            let arrival_event_id = crate::util::read_i32_le(chunk)?;
78
79            // departure_event_id: int32
80            let departure_event_id = crate::util::read_i32_le(chunk)?;
81
82
83            rows.push(TaxiPathNodeRow {
84                id,
85                path_id,
86                node_index,
87                continent_id,
88                loc,
89                flags,
90                delay,
91                arrival_event_id,
92                departure_event_id,
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: 11,
103            record_size: 44,
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) int32
111            b.write_all(&row.id.id.to_le_bytes())?;
112
113            // path_id: foreign_key (TaxiPath) int32
114            b.write_all(&(row.path_id.id as i32).to_le_bytes())?;
115
116            // node_index: int32
117            b.write_all(&row.node_index.to_le_bytes())?;
118
119            // continent_id: foreign_key (Map) int32
120            b.write_all(&(row.continent_id.id as i32).to_le_bytes())?;
121
122            // loc: float[3]
123            for i in row.loc {
124                b.write_all(&i.to_le_bytes())?;
125            }
126
127
128            // flags: int32
129            b.write_all(&row.flags.to_le_bytes())?;
130
131            // delay: int32
132            b.write_all(&row.delay.to_le_bytes())?;
133
134            // arrival_event_id: int32
135            b.write_all(&row.arrival_event_id.to_le_bytes())?;
136
137            // departure_event_id: int32
138            b.write_all(&row.departure_event_id.to_le_bytes())?;
139
140        }
141
142        b.write_all(&[0_u8])?;
143
144        Ok(())
145    }
146
147}
148
149impl Indexable for TaxiPathNode {
150    type PrimaryKey = TaxiPathNodeKey;
151    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
152        let key = key.try_into().ok()?;
153        self.rows.iter().find(|a| a.id.id == key.id)
154    }
155
156    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
157        let key = key.try_into().ok()?;
158        self.rows.iter_mut().find(|a| a.id.id == key.id)
159    }
160}
161
162#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
163pub struct TaxiPathNodeKey {
164    pub id: i32
165}
166
167impl TaxiPathNodeKey {
168    pub const fn new(id: i32) -> Self {
169        Self { id }
170    }
171
172}
173
174impl From<u8> for TaxiPathNodeKey {
175    fn from(v: u8) -> Self {
176        Self::new(v.into())
177    }
178}
179
180impl From<u16> for TaxiPathNodeKey {
181    fn from(v: u16) -> Self {
182        Self::new(v.into())
183    }
184}
185
186impl From<i8> for TaxiPathNodeKey {
187    fn from(v: i8) -> Self {
188        Self::new(v.into())
189    }
190}
191
192impl From<i16> for TaxiPathNodeKey {
193    fn from(v: i16) -> Self {
194        Self::new(v.into())
195    }
196}
197
198impl From<i32> for TaxiPathNodeKey {
199    fn from(v: i32) -> Self {
200        Self::new(v)
201    }
202}
203
204impl TryFrom<u32> for TaxiPathNodeKey {
205    type Error = u32;
206    fn try_from(v: u32) -> Result<Self, Self::Error> {
207        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
208    }
209}
210
211impl TryFrom<usize> for TaxiPathNodeKey {
212    type Error = usize;
213    fn try_from(v: usize) -> Result<Self, Self::Error> {
214        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
215    }
216}
217
218impl TryFrom<u64> for TaxiPathNodeKey {
219    type Error = u64;
220    fn try_from(v: u64) -> Result<Self, Self::Error> {
221        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
222    }
223}
224
225impl TryFrom<i64> for TaxiPathNodeKey {
226    type Error = i64;
227    fn try_from(v: i64) -> Result<Self, Self::Error> {
228        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
229    }
230}
231
232impl TryFrom<isize> for TaxiPathNodeKey {
233    type Error = isize;
234    fn try_from(v: isize) -> Result<Self, Self::Error> {
235        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
236    }
237}
238
239#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
240pub struct TaxiPathNodeRow {
241    pub id: TaxiPathNodeKey,
242    pub path_id: TaxiPathKey,
243    pub node_index: i32,
244    pub continent_id: MapKey,
245    pub loc: [f32; 3],
246    pub flags: i32,
247    pub delay: i32,
248    pub arrival_event_id: i32,
249    pub departure_event_id: i32,
250}
251