wow_dbc/tbc_tables/
transport_animation.rs

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