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