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