wow_dbc/wrath_tables/
cinematic_sequences.rs

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