wow_dbc/wrath_tables/
resistances.rs

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