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