wow_dbc/tbc_tables/
sheathe_sound_lookups.rs

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