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