wow_dbc/wrath_tables/
ground_effect_texture.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 GroundEffectTexture {
11    pub rows: Vec<GroundEffectTextureRow>,
12}
13
14impl DbcTable for GroundEffectTexture {
15    type Row = GroundEffectTextureRow;
16
17    const FILENAME: &'static str = "GroundEffectTexture.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 != 44 {
28            return Err(crate::DbcError::InvalidHeader(
29                crate::InvalidHeaderError::RecordSize {
30                    expected: 44,
31                    actual: header.record_size,
32                },
33            ));
34        }
35
36        if header.field_count != 11 {
37            return Err(crate::DbcError::InvalidHeader(
38                crate::InvalidHeaderError::FieldCount {
39                    expected: 11,
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 (GroundEffectTexture) int32
54            let id = GroundEffectTextureKey::new(crate::util::read_i32_le(chunk)?);
55
56            // doodad_id: int32[4]
57            let doodad_id = crate::util::read_array_i32::<4>(chunk)?;
58
59            // doodad_weight: int32[4]
60            let doodad_weight = crate::util::read_array_i32::<4>(chunk)?;
61
62            // density: int32
63            let density = crate::util::read_i32_le(chunk)?;
64
65            // sound: int32
66            let sound = crate::util::read_i32_le(chunk)?;
67
68
69            rows.push(GroundEffectTextureRow {
70                id,
71                doodad_id,
72                doodad_weight,
73                density,
74                sound,
75            });
76        }
77
78        Ok(GroundEffectTexture { 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: 11,
85            record_size: 44,
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 (GroundEffectTexture) int32
93            b.write_all(&row.id.id.to_le_bytes())?;
94
95            // doodad_id: int32[4]
96            for i in row.doodad_id {
97                b.write_all(&i.to_le_bytes())?;
98            }
99
100
101            // doodad_weight: int32[4]
102            for i in row.doodad_weight {
103                b.write_all(&i.to_le_bytes())?;
104            }
105
106
107            // density: int32
108            b.write_all(&row.density.to_le_bytes())?;
109
110            // sound: int32
111            b.write_all(&row.sound.to_le_bytes())?;
112
113        }
114
115        b.write_all(&[0_u8])?;
116
117        Ok(())
118    }
119
120}
121
122impl Indexable for GroundEffectTexture {
123    type PrimaryKey = GroundEffectTextureKey;
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 GroundEffectTextureKey {
137    pub id: i32
138}
139
140impl GroundEffectTextureKey {
141    pub const fn new(id: i32) -> Self {
142        Self { id }
143    }
144
145}
146
147impl From<u8> for GroundEffectTextureKey {
148    fn from(v: u8) -> Self {
149        Self::new(v.into())
150    }
151}
152
153impl From<u16> for GroundEffectTextureKey {
154    fn from(v: u16) -> Self {
155        Self::new(v.into())
156    }
157}
158
159impl From<i8> for GroundEffectTextureKey {
160    fn from(v: i8) -> Self {
161        Self::new(v.into())
162    }
163}
164
165impl From<i16> for GroundEffectTextureKey {
166    fn from(v: i16) -> Self {
167        Self::new(v.into())
168    }
169}
170
171impl From<i32> for GroundEffectTextureKey {
172    fn from(v: i32) -> Self {
173        Self::new(v)
174    }
175}
176
177impl TryFrom<u32> for GroundEffectTextureKey {
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 GroundEffectTextureKey {
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 GroundEffectTextureKey {
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 GroundEffectTextureKey {
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 GroundEffectTextureKey {
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 GroundEffectTextureRow {
214    pub id: GroundEffectTextureKey,
215    pub doodad_id: [i32; 4],
216    pub doodad_weight: [i32; 4],
217    pub density: i32,
218    pub sound: i32,
219}
220