wow_dbc/wrath_tables/
loading_screen_taxi_splines.rs

1use crate::{
2    DbcTable, Indexable,
3};
4use crate::header::{
5    DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::wrath_tables::taxi_path::TaxiPathKey;
8use std::io::Write;
9
10#[derive(Debug, Clone, PartialEq, PartialOrd)]
11pub struct LoadingScreenTaxiSplines {
12    pub rows: Vec<LoadingScreenTaxiSplinesRow>,
13}
14
15impl DbcTable for LoadingScreenTaxiSplines {
16    type Row = LoadingScreenTaxiSplinesRow;
17
18    const FILENAME: &'static str = "LoadingScreenTaxiSplines.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 != 76 {
29            return Err(crate::DbcError::InvalidHeader(
30                crate::InvalidHeaderError::RecordSize {
31                    expected: 76,
32                    actual: header.record_size,
33                },
34            ));
35        }
36
37        if header.field_count != 19 {
38            return Err(crate::DbcError::InvalidHeader(
39                crate::InvalidHeaderError::FieldCount {
40                    expected: 19,
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
49        let mut rows = Vec::with_capacity(header.record_count as usize);
50
51        for mut chunk in r.chunks(header.record_size as usize) {
52            let chunk = &mut chunk;
53
54            // id: primary_key (LoadingScreenTaxiSplines) int32
55            let id = LoadingScreenTaxiSplinesKey::new(crate::util::read_i32_le(chunk)?);
56
57            // path_id: foreign_key (TaxiPath) int32
58            let path_id = TaxiPathKey::new(crate::util::read_i32_le(chunk)?.into());
59
60            // locx: float[8]
61            let locx = crate::util::read_array_f32::<8>(chunk)?;
62
63            // locy: float[8]
64            let locy = crate::util::read_array_f32::<8>(chunk)?;
65
66            // leg_index: int32
67            let leg_index = crate::util::read_i32_le(chunk)?;
68
69
70            rows.push(LoadingScreenTaxiSplinesRow {
71                id,
72                path_id,
73                locx,
74                locy,
75                leg_index,
76            });
77        }
78
79        Ok(LoadingScreenTaxiSplines { rows, })
80    }
81
82    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
83        let header = DbcHeader {
84            record_count: self.rows.len() as u32,
85            field_count: 19,
86            record_size: 76,
87            string_block_size: 1,
88        };
89
90        b.write_all(&header.write_header())?;
91
92        for row in &self.rows {
93            // id: primary_key (LoadingScreenTaxiSplines) int32
94            b.write_all(&row.id.id.to_le_bytes())?;
95
96            // path_id: foreign_key (TaxiPath) int32
97            b.write_all(&(row.path_id.id as i32).to_le_bytes())?;
98
99            // locx: float[8]
100            for i in row.locx {
101                b.write_all(&i.to_le_bytes())?;
102            }
103
104
105            // locy: float[8]
106            for i in row.locy {
107                b.write_all(&i.to_le_bytes())?;
108            }
109
110
111            // leg_index: int32
112            b.write_all(&row.leg_index.to_le_bytes())?;
113
114        }
115
116        b.write_all(&[0_u8])?;
117
118        Ok(())
119    }
120
121}
122
123impl Indexable for LoadingScreenTaxiSplines {
124    type PrimaryKey = LoadingScreenTaxiSplinesKey;
125    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
126        let key = key.try_into().ok()?;
127        self.rows.iter().find(|a| a.id.id == key.id)
128    }
129
130    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
131        let key = key.try_into().ok()?;
132        self.rows.iter_mut().find(|a| a.id.id == key.id)
133    }
134}
135
136#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
137pub struct LoadingScreenTaxiSplinesKey {
138    pub id: i32
139}
140
141impl LoadingScreenTaxiSplinesKey {
142    pub const fn new(id: i32) -> Self {
143        Self { id }
144    }
145
146}
147
148impl From<u8> for LoadingScreenTaxiSplinesKey {
149    fn from(v: u8) -> Self {
150        Self::new(v.into())
151    }
152}
153
154impl From<u16> for LoadingScreenTaxiSplinesKey {
155    fn from(v: u16) -> Self {
156        Self::new(v.into())
157    }
158}
159
160impl From<i8> for LoadingScreenTaxiSplinesKey {
161    fn from(v: i8) -> Self {
162        Self::new(v.into())
163    }
164}
165
166impl From<i16> for LoadingScreenTaxiSplinesKey {
167    fn from(v: i16) -> Self {
168        Self::new(v.into())
169    }
170}
171
172impl From<i32> for LoadingScreenTaxiSplinesKey {
173    fn from(v: i32) -> Self {
174        Self::new(v)
175    }
176}
177
178impl TryFrom<u32> for LoadingScreenTaxiSplinesKey {
179    type Error = u32;
180    fn try_from(v: u32) -> Result<Self, Self::Error> {
181        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
182    }
183}
184
185impl TryFrom<usize> for LoadingScreenTaxiSplinesKey {
186    type Error = usize;
187    fn try_from(v: usize) -> Result<Self, Self::Error> {
188        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
189    }
190}
191
192impl TryFrom<u64> for LoadingScreenTaxiSplinesKey {
193    type Error = u64;
194    fn try_from(v: u64) -> Result<Self, Self::Error> {
195        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
196    }
197}
198
199impl TryFrom<i64> for LoadingScreenTaxiSplinesKey {
200    type Error = i64;
201    fn try_from(v: i64) -> Result<Self, Self::Error> {
202        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
203    }
204}
205
206impl TryFrom<isize> for LoadingScreenTaxiSplinesKey {
207    type Error = isize;
208    fn try_from(v: isize) -> Result<Self, Self::Error> {
209        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
210    }
211}
212
213#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
214pub struct LoadingScreenTaxiSplinesRow {
215    pub id: LoadingScreenTaxiSplinesKey,
216    pub path_id: TaxiPathKey,
217    pub locx: [f32; 8],
218    pub locy: [f32; 8],
219    pub leg_index: i32,
220}
221