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