wow_dbc/vanilla_tables/
transport_animation.rs

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