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