wow_dbc/vanilla_tables/
cinematic_camera.rs

1use crate::{
2    DbcTable, Indexable,
3};
4use crate::header::{
5    DbcHeader, HEADER_SIZE, parse_header,
6};
7use crate::vanilla_tables::sound_entries::SoundEntriesKey;
8use std::io::Write;
9
10#[derive(Debug, Clone, PartialEq, PartialOrd)]
11pub struct CinematicCamera {
12    pub rows: Vec<CinematicCameraRow>,
13}
14
15impl DbcTable for CinematicCamera {
16    type Row = CinematicCameraRow;
17
18    const FILENAME: &'static str = "CinematicCamera.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        let mut string_block = vec![0_u8; header.string_block_size as usize];
49        b.read_exact(&mut string_block)?;
50
51        let mut rows = Vec::with_capacity(header.record_count as usize);
52
53        for mut chunk in r.chunks(header.record_size as usize) {
54            let chunk = &mut chunk;
55
56            // id: primary_key (CinematicCamera) uint32
57            let id = CinematicCameraKey::new(crate::util::read_u32_le(chunk)?);
58
59            // model: string_ref
60            let model = {
61                let s = crate::util::get_string_as_vec(chunk, &string_block)?;
62                String::from_utf8(s)?
63            };
64
65            // sound_entry: foreign_key (SoundEntries) uint32
66            let sound_entry = SoundEntriesKey::new(crate::util::read_u32_le(chunk)?.into());
67
68            // location_x: float
69            let location_x = crate::util::read_f32_le(chunk)?;
70
71            // location_y: float
72            let location_y = crate::util::read_f32_le(chunk)?;
73
74            // location_z: float
75            let location_z = crate::util::read_f32_le(chunk)?;
76
77            // rotation: float
78            let rotation = crate::util::read_f32_le(chunk)?;
79
80
81            rows.push(CinematicCameraRow {
82                id,
83                model,
84                sound_entry,
85                location_x,
86                location_y,
87                location_z,
88                rotation,
89            });
90        }
91
92        Ok(CinematicCamera { rows, })
93    }
94
95    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
96        let header = DbcHeader {
97            record_count: self.rows.len() as u32,
98            field_count: 7,
99            record_size: 28,
100            string_block_size: self.string_block_size(),
101        };
102
103        b.write_all(&header.write_header())?;
104
105        let mut string_index = 1;
106        for row in &self.rows {
107            // id: primary_key (CinematicCamera) uint32
108            b.write_all(&row.id.id.to_le_bytes())?;
109
110            // model: string_ref
111            if !row.model.is_empty() {
112                b.write_all(&(string_index as u32).to_le_bytes())?;
113                string_index += row.model.len() + 1;
114            }
115            else {
116                b.write_all(&(0_u32).to_le_bytes())?;
117            }
118
119            // sound_entry: foreign_key (SoundEntries) uint32
120            b.write_all(&(row.sound_entry.id as u32).to_le_bytes())?;
121
122            // location_x: float
123            b.write_all(&row.location_x.to_le_bytes())?;
124
125            // location_y: float
126            b.write_all(&row.location_y.to_le_bytes())?;
127
128            // location_z: float
129            b.write_all(&row.location_z.to_le_bytes())?;
130
131            // rotation: float
132            b.write_all(&row.rotation.to_le_bytes())?;
133
134        }
135
136        self.write_string_block(b)?;
137
138        Ok(())
139    }
140
141}
142
143impl Indexable for CinematicCamera {
144    type PrimaryKey = CinematicCameraKey;
145    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
146        let key = key.try_into().ok()?;
147        self.rows.iter().find(|a| a.id.id == key.id)
148    }
149
150    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
151        let key = key.try_into().ok()?;
152        self.rows.iter_mut().find(|a| a.id.id == key.id)
153    }
154}
155
156impl CinematicCamera {
157    fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
158        b.write_all(&[0])?;
159
160        for row in &self.rows {
161            if !row.model.is_empty() { b.write_all(row.model.as_bytes())?; b.write_all(&[0])?; };
162        }
163
164        Ok(())
165    }
166
167    fn string_block_size(&self) -> u32 {
168        let mut sum = 1;
169        for row in &self.rows {
170            if !row.model.is_empty() { sum += row.model.len() + 1; };
171        }
172
173        sum as u32
174    }
175
176}
177
178#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
179pub struct CinematicCameraKey {
180    pub id: u32
181}
182
183impl CinematicCameraKey {
184    pub const fn new(id: u32) -> Self {
185        Self { id }
186    }
187
188}
189
190impl From<u8> for CinematicCameraKey {
191    fn from(v: u8) -> Self {
192        Self::new(v.into())
193    }
194}
195
196impl From<u16> for CinematicCameraKey {
197    fn from(v: u16) -> Self {
198        Self::new(v.into())
199    }
200}
201
202impl From<u32> for CinematicCameraKey {
203    fn from(v: u32) -> Self {
204        Self::new(v)
205    }
206}
207
208impl TryFrom<u64> for CinematicCameraKey {
209    type Error = u64;
210    fn try_from(v: u64) -> Result<Self, Self::Error> {
211        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
212    }
213}
214
215impl TryFrom<usize> for CinematicCameraKey {
216    type Error = usize;
217    fn try_from(v: usize) -> Result<Self, Self::Error> {
218        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
219    }
220}
221
222impl TryFrom<i8> for CinematicCameraKey {
223    type Error = i8;
224    fn try_from(v: i8) -> Result<Self, Self::Error> {
225        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
226    }
227}
228
229impl TryFrom<i16> for CinematicCameraKey {
230    type Error = i16;
231    fn try_from(v: i16) -> Result<Self, Self::Error> {
232        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
233    }
234}
235
236impl TryFrom<i32> for CinematicCameraKey {
237    type Error = i32;
238    fn try_from(v: i32) -> Result<Self, Self::Error> {
239        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
240    }
241}
242
243impl TryFrom<i64> for CinematicCameraKey {
244    type Error = i64;
245    fn try_from(v: i64) -> Result<Self, Self::Error> {
246        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
247    }
248}
249
250impl TryFrom<isize> for CinematicCameraKey {
251    type Error = isize;
252    fn try_from(v: isize) -> Result<Self, Self::Error> {
253        Ok(TryInto::<u32>::try_into(v).ok().ok_or(v)?.into())
254    }
255}
256
257#[derive(Debug, Clone, PartialEq, PartialOrd)]
258pub struct CinematicCameraRow {
259    pub id: CinematicCameraKey,
260    pub model: String,
261    pub sound_entry: SoundEntriesKey,
262    pub location_x: f32,
263    pub location_y: f32,
264    pub location_z: f32,
265    pub rotation: f32,
266}
267