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