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